This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathServiceDeployManagerTest.cpp
More file actions
201 lines (174 loc) · 9.42 KB
/
Copy pathServiceDeployManagerTest.cpp
File metadata and controls
201 lines (174 loc) · 9.42 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2022 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdint.h>
#include <QMetaType>
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
#include <tuple>
#include <variant>
#include <vector>
#include "OrbitBase/Result.h"
#include "OrbitBase/StopSource.h"
#include "OrbitSsh/Context.h"
#include "OrbitSshQt/ScopedConnection.h"
#include "OrbitSshQt/Session.h"
#include "OrbitSshQt/Task.h"
#include "QtTestUtils/WaitForWithTimeout.h"
#include "SessionSetup/DeploymentConfigurations.h"
#include "SessionSetup/ServiceDeployManager.h"
#include "SshQtTestUtils/KillProcessListeningOnTcpPort.h"
#include "SshQtTestUtils/ParsePortNumberFromSocatOutput.h"
#include "SshQtTestUtils/SshSessionTest.h"
#include "Test/Path.h"
#include "TestUtils/TemporaryDirectory.h"
#include "TestUtils/TestUtils.h"
namespace orbit_session_setup {
using orbit_qt_test_utils::WaitForWithTimeout;
using orbit_qt_test_utils::YieldsResult;
using orbit_ssh_qt_test_utils::KillProcessListeningOnTcpPort;
using orbit_test_utils::HasNoError;
using ServiceDeployManagerTest = orbit_ssh_qt_test_utils::SshSessionTest;
struct ServiceDeployManagerSigningTest : orbit_ssh_qt_test_utils::SshSessionTest {
constexpr static std::string_view kSigningSshServerEnvironmentVariableName =
"ORBIT_TESTING_SSH_SERVER_SIGNING_ADDRESS";
ServiceDeployManagerSigningTest()
: orbit_ssh_qt_test_utils::SshSessionTest{
std::string{kSigningSshServerEnvironmentVariableName}} {}
};
TEST_F(ServiceDeployManagerTest, NoDeployment) {
auto context = orbit_ssh::Context::Create();
ASSERT_THAT(context, orbit_test_utils::HasValue());
orbit_ssh_qt::Session helper_session{&context.value()};
EXPECT_THAT(WaitForWithTimeout(helper_session.ConnectToServer(GetCredentials())),
YieldsResult(HasNoError()));
// This is emulating OrbitService - It's important that we don't hard code any ports because all
// tests usually share the same network namespace and they might run concurrently.
orbit_ssh_qt::Task orbit_service_task{&helper_session,
"socat -dd TCP-LISTEN:0,fork exec:'/bin/cat'"};
std::string socat_output{};
orbit_ssh_qt::ScopedConnection output_connection{
QObject::connect(&orbit_service_task, &orbit_ssh_qt::Task::readyReadStdErr,
&orbit_service_task, [&orbit_service_task, &socat_output]() {
socat_output.append(orbit_service_task.ReadStdErr());
})};
EXPECT_THAT(WaitForWithTimeout(orbit_service_task.Start()), YieldsResult(HasNoError()));
std::optional<ErrorMessageOr<int>> port_or_error{};
std::ignore = QTest::qWaitFor([&]() {
port_or_error = orbit_ssh_qt_test_utils::ParsePortNumberFromSocatOutput(socat_output);
return port_or_error.has_value();
});
ASSERT_TRUE(port_or_error.has_value()) << "The socat output was: " << socat_output;
ASSERT_THAT(port_or_error.value(), HasNoError());
const ServiceDeployManager::GrpcPort grpc_port{static_cast<uint16_t>(port_or_error->value())};
// We need the signal spy have longer lifetime than the ServiceDeployManager because we wanna
// ensure that the ServiceDeployManager's destructor does not emit any signals.
qRegisterMetaType<std::error_code>("std::error_code");
std::optional<QSignalSpy> socket_error_signal{};
{
DeploymentConfiguration deployment_config{NoDeployment{}};
ServiceDeployManager sdm{&deployment_config, &context.value(), GetCredentials(), grpc_port};
socket_error_signal.emplace(&sdm, &ServiceDeployManager::socketErrorOccurred);
EXPECT_THAT(sdm.Exec(), orbit_test_utils::HasNoError());
}
EXPECT_THAT(*socket_error_signal, testing::IsEmpty());
EXPECT_THAT(KillProcessListeningOnTcpPort(&helper_session, grpc_port.grpc_port), HasNoError());
EXPECT_THAT(WaitForWithTimeout(orbit_service_task.Stop()), YieldsResult(HasNoError()));
EXPECT_THAT(WaitForWithTimeout(helper_session.Disconnect()), YieldsResult(HasNoError()));
}
TEST_F(ServiceDeployManagerTest, BareExecutableAndRootPassword) {
auto context = orbit_ssh::Context::Create();
ASSERT_THAT(context, orbit_test_utils::HasValue());
orbit_ssh_qt::Session helper_session{&context.value()};
EXPECT_THAT(WaitForWithTimeout(helper_session.ConnectToServer(GetCredentials())),
YieldsResult(HasNoError()));
// We need the signal spy have longer lifetime than the ServiceDeployManager because we wanna
// ensure that the ServiceDeployManager's destructor does not emit any signals.
qRegisterMetaType<std::error_code>("std::error_code");
std::optional<QSignalSpy> socket_error_signal{};
{
DeploymentConfiguration deployment_config{BareExecutableAndRootPasswordDeployment{
orbit_test::GetTestdataDir() / "deployments" / "BareExecutableAndRootPassword" / "bin" /
"emulate_orbit_service.sh",
"loginpassword"}};
ServiceDeployManager sdm{&deployment_config, &context.value(), GetCredentials(),
ServiceDeployManager::GrpcPort{44765}};
socket_error_signal.emplace(&sdm, &ServiceDeployManager::socketErrorOccurred);
EXPECT_THAT(sdm.Exec(), orbit_test_utils::HasNoError());
}
EXPECT_THAT(*socket_error_signal, testing::IsEmpty());
EXPECT_THAT(WaitForWithTimeout(helper_session.Disconnect()), YieldsResult(HasNoError()));
}
TEST_F(ServiceDeployManagerSigningTest, SignedDebianPackageDeployment) {
auto context = orbit_ssh::Context::Create();
ASSERT_THAT(context, orbit_test_utils::HasValue());
orbit_ssh_qt::Session helper_session{&context.value()};
EXPECT_THAT(WaitForWithTimeout(helper_session.ConnectToServer(GetCredentials())),
YieldsResult(HasNoError()));
// We need the signal spy have longer lifetime than the ServiceDeployManager because we wanna
// ensure that the ServiceDeployManager's destructor does not emit any signals.
qRegisterMetaType<std::error_code>("std::error_code");
std::optional<QSignalSpy> socket_error_signal{};
{
DeploymentConfiguration deployment_config{SignedDebianPackageDeployment{
orbit_test::GetTestdataDir() / "deployments" / "SignedDebianPackage" / "OrbitService",
orbit_test::GetTestdataDir() / "deployments" / "SignedDebianPackage" / "OrbitService.asc"}};
ServiceDeployManager sdm{&deployment_config, &context.value(), GetCredentials(),
ServiceDeployManager::GrpcPort{44765}};
socket_error_signal.emplace(&sdm, &ServiceDeployManager::socketErrorOccurred);
EXPECT_THAT(sdm.Exec(), orbit_test_utils::HasNoError());
}
EXPECT_THAT(*socket_error_signal, testing::IsEmpty());
EXPECT_THAT(WaitForWithTimeout(helper_session.Disconnect()), YieldsResult(HasNoError()));
}
TEST_F(ServiceDeployManagerTest, CopyFileToLocal) {
auto context = orbit_ssh::Context::Create();
ASSERT_THAT(context, orbit_test_utils::HasValue());
orbit_ssh_qt::Session helper_session{&context.value()};
EXPECT_THAT(WaitForWithTimeout(helper_session.ConnectToServer(GetCredentials())),
YieldsResult(HasNoError()));
// This is emulating OrbitService - It's important that we don't hard code any ports because all
// tests usually share the same network namespace and they might run concurrently.
orbit_ssh_qt::Task orbit_service_task{&helper_session,
"socat -dd TCP-LISTEN:0,fork exec:'/bin/cat'"};
std::string socat_output{};
orbit_ssh_qt::ScopedConnection output_connection{
QObject::connect(&orbit_service_task, &orbit_ssh_qt::Task::readyReadStdErr,
&orbit_service_task, [&orbit_service_task, &socat_output]() {
socat_output.append(orbit_service_task.ReadStdErr());
})};
EXPECT_THAT(WaitForWithTimeout(orbit_service_task.Start()), YieldsResult(HasNoError()));
std::optional<ErrorMessageOr<int>> port_or_error{};
std::ignore = QTest::qWaitFor([&]() {
port_or_error = orbit_ssh_qt_test_utils::ParsePortNumberFromSocatOutput(socat_output);
return port_or_error.has_value();
});
ASSERT_TRUE(port_or_error.has_value()) << "The socat output was: " << socat_output;
ASSERT_THAT(port_or_error.value(), HasNoError());
const ServiceDeployManager::GrpcPort grpc_port{static_cast<uint16_t>(port_or_error->value())};
{
DeploymentConfiguration deployment_config{NoDeployment{}};
ServiceDeployManager sdm{&deployment_config, &context.value(), GetCredentials(), grpc_port};
EXPECT_THAT(sdm.Exec(), orbit_test_utils::HasNoError());
auto temp_dir = orbit_test_utils::TemporaryDirectory::Create();
ASSERT_THAT(temp_dir, orbit_test_utils::HasValue());
orbit_base::StopSource stop_source{};
auto future = sdm.CopyFileToLocal("/home/loginuser/plain.txt",
temp_dir.value().GetDirectoryPath() / "plain.txt",
stop_source.GetStopToken());
auto result = WaitForWithTimeout(future);
ASSERT_THAT(result, YieldsResult(HasValue(orbit_test_utils::HasNotBeenCanceled())));
}
EXPECT_THAT(KillProcessListeningOnTcpPort(&helper_session, grpc_port.grpc_port), HasNoError());
EXPECT_THAT(WaitForWithTimeout(orbit_service_task.Stop()), YieldsResult(HasNoError()));
EXPECT_THAT(WaitForWithTimeout(helper_session.Disconnect()), YieldsResult(HasNoError()));
}
} // namespace orbit_session_setup