forked from canonical/multipass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ssh_process.cpp
More file actions
156 lines (132 loc) · 4.88 KB
/
Copy pathtest_ssh_process.cpp
File metadata and controls
156 lines (132 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (C) 2018 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "mock_ssh.h"
#include <multipass/ssh/ssh_session.h>
#include <gmock/gmock.h>
#include <algorithm>
#include <thread>
namespace mp = multipass;
using namespace testing;
namespace
{
struct SSHProcess : public Test
{
SSHProcess()
{
connect.returnValue(SSH_OK);
is_connected.returnValue(true);
open_session.returnValue(SSH_OK);
request_exec.returnValue(SSH_OK);
channel_is_closed.returnValue(0);
}
decltype(MOCK(ssh_connect)) connect{MOCK(ssh_connect)};
decltype(MOCK(ssh_is_connected)) is_connected{MOCK(ssh_is_connected)};
decltype(MOCK(ssh_channel_open_session)) open_session{MOCK(ssh_channel_open_session)};
decltype(MOCK(ssh_channel_request_exec)) request_exec{MOCK(ssh_channel_request_exec)};
decltype(MOCK(ssh_channel_is_closed)) channel_is_closed{MOCK(ssh_channel_is_closed)};
mp::SSHSession session{"theanswertoeverything", 42};
};
} // namespace
TEST_F(SSHProcess, can_retrieve_exit_status)
{
ssh_channel_callbacks callbacks{nullptr};
auto add_channel_cbs = [&callbacks](ssh_channel, ssh_channel_callbacks cb) {
callbacks = cb;
return SSH_OK;
};
REPLACE(ssh_add_channel_callbacks, add_channel_cbs);
int expected_status{42};
auto event_dopoll = [&callbacks, &expected_status](auto...) {
if (!callbacks)
return SSH_ERROR;
callbacks->channel_exit_status_function(nullptr, nullptr, expected_status, callbacks->userdata);
return SSH_OK;
};
REPLACE(ssh_event_dopoll, event_dopoll);
auto proc = session.exec("something");
EXPECT_THAT(proc.exit_code(), Eq(expected_status));
}
TEST_F(SSHProcess, exit_code_times_out)
{
REPLACE(ssh_event_dopoll, [](ssh_event, int timeout) {
std::this_thread::sleep_for(std::chrono::milliseconds(timeout + 1));
return SSH_OK;
});
auto proc = session.exec("something");
EXPECT_THROW(proc.exit_code(std::chrono::milliseconds(1)), std::runtime_error);
}
TEST_F(SSHProcess, specifies_stderr_correctly)
{
int expected_is_stderr = 0;
auto channel_read = [&expected_is_stderr](ssh_channel, void*, uint32_t, int is_stderr, int) {
EXPECT_THAT(expected_is_stderr, Eq(is_stderr));
return 0;
};
REPLACE(ssh_channel_read_timeout, channel_read);
auto proc = session.exec("something");
proc.read_std_output();
expected_is_stderr = 1;
proc.read_std_error();
}
TEST_F(SSHProcess, reading_output_returns_empty_if_channel_closed)
{
REPLACE(ssh_channel_is_closed, [](auto...) { return 1; });
auto proc = session.exec("something");
auto output = proc.read_std_output();
EXPECT_TRUE(output.empty());
}
TEST_F(SSHProcess, reading_failure_returns_empty_if_channel_closed)
{
int channel_closed{0};
REPLACE(ssh_channel_read_timeout, [&channel_closed](auto...) {
channel_closed = 1;
return -1;
});
REPLACE(ssh_channel_is_closed, [&channel_closed](auto...) { return channel_closed; });
auto proc = session.exec("something");
auto output = proc.read_std_output();
EXPECT_TRUE(output.empty());
}
TEST_F(SSHProcess, throws_on_read_errors)
{
REPLACE(ssh_channel_read_timeout, [](auto...) { return -1; });
auto proc = session.exec("something");
EXPECT_THROW(proc.read_std_output(), std::runtime_error);
}
TEST_F(SSHProcess, read_std_output_returns_empty_string_on_eof)
{
REPLACE(ssh_channel_read_timeout, [](auto...) { return 0; });
auto proc = session.exec("something");
auto output = proc.read_std_output();
EXPECT_TRUE(output.empty());
}
TEST_F(SSHProcess, can_read_output)
{
std::string expected_output{"some content here"};
auto remaining = expected_output.size();
auto channel_read = [&expected_output, &remaining](ssh_channel, void* dest, uint32_t count, int is_stderr, int) {
const auto num_to_copy = std::min(count, static_cast<uint32_t>(remaining));
const auto begin = expected_output.begin() + expected_output.size() - remaining;
std::copy_n(begin, num_to_copy, reinterpret_cast<char*>(dest));
remaining -= num_to_copy;
return num_to_copy;
};
REPLACE(ssh_channel_read_timeout, channel_read);
auto proc = session.exec("something");
auto output = proc.read_std_output();
EXPECT_THAT(output, StrEq(expected_output));
}