Skip to content

Commit 21486d9

Browse files
committed
Refactor run_tests.rs
1 parent 077286c commit 21486d9

File tree

1 file changed

+119
-136
lines changed

1 file changed

+119
-136
lines changed

test/test-manager/src/run_tests.rs

+119-136
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
logging::{Logger, Panic, TestOutput, TestResult},
3-
mullvad_daemon::{self, MullvadClientArgument},
3+
mullvad_daemon::{self, MullvadClientArgument, RpcClientProvider},
44
summary::SummaryLogger,
55
tests::{self, config::TEST_CONFIG, get_tests, TestContext},
66
vm,
@@ -17,13 +17,96 @@ use test_rpc::{logging::Output, ServiceClient};
1717
/// Keep this constant in sync with `test-runner/src/main.rs`
1818
const BAUD: u32 = if cfg!(target_os = "macos") { 0 } else { 115200 };
1919

20+
struct TestHandler<'a> {
21+
rpc_provider: &'a RpcClientProvider,
22+
test_runner_client: &'a ServiceClient,
23+
failed_tests: Vec<&'static str>,
24+
successful_tests: Vec<&'static str>,
25+
summary_logger: Option<SummaryLogger>,
26+
print_failed_tests_only: bool,
27+
logger: Logger,
28+
}
29+
30+
impl TestHandler<'_> {
31+
/// Run `tests::test_upgrade_app` and register the result
32+
async fn run_test<R, F>(
33+
&mut self,
34+
test: &F,
35+
test_name: &'static str,
36+
mullvad_client: MullvadClientArgument,
37+
) -> Result<(), anyhow::Error>
38+
where
39+
F: Fn(super::tests::TestContext, ServiceClient, MullvadClientArgument) -> R,
40+
R: Future<Output = anyhow::Result<()>>,
41+
{
42+
log::info!("Running test_upgrade_app");
43+
44+
if self.print_failed_tests_only {
45+
// Stop live record
46+
self.logger.store_records(true);
47+
}
48+
49+
let test_output = run_test_function(
50+
self.test_runner_client.clone(),
51+
mullvad_client,
52+
&test,
53+
test_name,
54+
TestContext {
55+
rpc_provider: self.rpc_provider.clone(),
56+
},
57+
)
58+
.await;
59+
60+
if self.print_failed_tests_only {
61+
// Print results of failed test
62+
if test_output.result.failure() {
63+
self.logger.print_stored_records();
64+
} else {
65+
self.logger.flush_records();
66+
}
67+
self.logger.store_records(false);
68+
}
69+
70+
test_output.print();
71+
72+
register_test_result(
73+
test_output.result,
74+
&mut self.failed_tests,
75+
test_name,
76+
&mut self.successful_tests,
77+
self.summary_logger.as_mut(),
78+
)
79+
.await?;
80+
81+
Ok(())
82+
}
83+
84+
fn gather_results(self) -> TestResult {
85+
log::info!("TESTS THAT SUCCEEDED:");
86+
for test in self.successful_tests {
87+
log::info!("{test}");
88+
}
89+
90+
log::info!("TESTS THAT FAILED:");
91+
for test in &self.failed_tests {
92+
log::info!("{test}");
93+
}
94+
95+
if self.failed_tests.is_empty() {
96+
TestResult::Pass
97+
} else {
98+
TestResult::Fail(anyhow::anyhow!("Some tests failed"))
99+
}
100+
}
101+
}
102+
20103
pub async fn run(
21104
config: tests::config::TestConfig,
22105
instance: &dyn vm::VmInstance,
23106
test_filters: &[String],
24107
skip_wait: bool,
25108
print_failed_tests_only: bool,
26-
mut summary_logger: Option<SummaryLogger>,
109+
summary_logger: Option<SummaryLogger>,
27110
) -> Result<TestResult> {
28111
log::trace!("Setting test constants");
29112
TEST_CONFIG.init(config);
@@ -44,11 +127,20 @@ pub async fn run(
44127
log::info!("Running client");
45128

46129
let test_runner_client = ServiceClient::new(connection_handle.clone(), runner_transport);
47-
let mullvad_client_provider =
48-
mullvad_daemon::new_rpc_client(connection_handle, mullvad_daemon_transport);
130+
let rpc_provider = mullvad_daemon::new_rpc_client(connection_handle, mullvad_daemon_transport);
49131

50132
print_os_version(&test_runner_client).await;
51133

134+
let mut test_handler = TestHandler {
135+
rpc_provider: &rpc_provider,
136+
test_runner_client: &test_runner_client,
137+
failed_tests: vec![],
138+
successful_tests: vec![],
139+
summary_logger,
140+
print_failed_tests_only,
141+
logger: Logger::get_or_init(),
142+
};
143+
52144
let mut tests = get_tests();
53145

54146
tests.retain(|test| test.should_run_on_os(TEST_CONFIG.os));
@@ -68,154 +160,45 @@ pub async fn run(
68160
});
69161
}
70162

71-
let test_context = TestContext {
72-
rpc_provider: mullvad_client_provider,
163+
if TEST_CONFIG.app_package_to_upgrade_from_filename.is_some() {
164+
test_handler
165+
.run_test(
166+
&tests::test_upgrade_app,
167+
"test_upgrade_app",
168+
MullvadClientArgument::None,
169+
)
170+
.await?;
171+
} else {
172+
log::warn!("No previous app to upgrade from, skipping upgrade test");
73173
};
74174

75-
let mut successful_tests = vec![];
76-
let mut failed_tests = vec![];
77-
78-
let logger = super::logging::Logger::get_or_init();
79-
80-
test_upgrade_app(
81-
&test_context,
82-
&test_runner_client,
83-
&mut failed_tests,
84-
&mut successful_tests,
85-
&mut summary_logger,
86-
print_failed_tests_only,
87-
&logger,
88-
)
89-
.await?;
90-
91175
for test in tests {
92-
crate::tests::prepare_daemon(&test_runner_client, &test_context.rpc_provider)
176+
tests::prepare_daemon(&test_runner_client, &rpc_provider)
93177
.await
94178
.context("Failed to reset daemon before test")?;
95179

96-
let mullvad_client = test_context
97-
.rpc_provider
180+
let mullvad_client = rpc_provider
98181
.mullvad_client(test.mullvad_client_version)
99182
.await;
100-
101-
log::info!("Running {}", test.name);
102-
103-
if print_failed_tests_only {
104-
// Stop live record
105-
logger.store_records(true);
106-
}
107-
108-
// TODO: Log how long each test took to run.
109-
let test_output = run_test(
110-
test_runner_client.clone(),
111-
mullvad_client,
112-
&test.func,
113-
test.name,
114-
test_context.clone(),
115-
)
116-
.await;
117-
118-
if print_failed_tests_only {
119-
// Print results of failed test
120-
if test_output.result.failure() {
121-
logger.print_stored_records();
122-
} else {
123-
logger.flush_records();
124-
}
125-
logger.store_records(false);
126-
}
127-
128-
test_output.print();
129-
130-
register_test_result(
131-
test_output.result,
132-
&mut failed_tests,
133-
test.name,
134-
&mut successful_tests,
135-
summary_logger.as_mut(),
136-
)
137-
.await?;
183+
test_handler
184+
.run_test(&test.func, test.name, mullvad_client)
185+
.await?;
138186
}
139187

140-
log::info!("TESTS THAT SUCCEEDED:");
141-
for test in successful_tests {
142-
log::info!("{test}");
143-
}
144-
145-
log::info!("TESTS THAT FAILED:");
146-
for test in &failed_tests {
147-
log::info!("{test}");
148-
}
188+
let result = test_handler.gather_results();
149189

150190
// wait for cleanup
151-
drop(test_context);
191+
drop(rpc_provider);
152192
let _ = tokio::time::timeout(Duration::from_secs(5), completion_handle).await;
153193

154-
if failed_tests.is_empty() {
155-
Ok(TestResult::Pass)
156-
} else {
157-
Ok(TestResult::Fail(anyhow::anyhow!("Some tests failed")))
158-
}
159-
}
160-
161-
/// Run `tests::test_upgrade_app` and register the result
162-
async fn test_upgrade_app<'a>(
163-
test_context: &TestContext,
164-
test_runner_client: &ServiceClient,
165-
failed_tests: &mut Vec<&'a str>,
166-
successful_tests: &mut Vec<&'a str>,
167-
summary_logger: &mut Option<SummaryLogger>,
168-
print_failed_tests_only: bool,
169-
logger: &Logger,
170-
) -> Result<(), anyhow::Error> {
171-
if TEST_CONFIG.app_package_to_upgrade_from_filename.is_some() {
172-
log::info!("Running test_upgrade_app");
173-
174-
if print_failed_tests_only {
175-
// Stop live record
176-
logger.store_records(true);
177-
}
178-
179-
let test_output = run_test(
180-
test_runner_client.clone(),
181-
MullvadClientArgument::None,
182-
&tests::test_upgrade_app,
183-
"test_upgrade_app",
184-
test_context.clone(),
185-
)
186-
.await;
187-
188-
if print_failed_tests_only {
189-
// Print results of failed test
190-
if test_output.result.failure() {
191-
logger.print_stored_records();
192-
} else {
193-
logger.flush_records();
194-
}
195-
logger.store_records(false);
196-
}
197-
198-
test_output.print();
199-
200-
register_test_result(
201-
test_output.result,
202-
failed_tests,
203-
"test_upgrade_app",
204-
successful_tests,
205-
summary_logger.as_mut(),
206-
)
207-
.await?;
208-
} else {
209-
log::warn!("No previous app to upgrade from, skipping upgrade test");
210-
};
211-
Ok(())
194+
Ok(result)
212195
}
213196

214-
async fn register_test_result<'a>(
197+
async fn register_test_result(
215198
test_result: TestResult,
216-
failed_tests: &mut Vec<&'a str>,
217-
test_name: &'a str,
218-
successful_tests: &mut Vec<&'a str>,
199+
failed_tests: &mut Vec<&str>,
200+
test_name: &'static str,
201+
successful_tests: &mut Vec<&str>,
219202
summary_logger: Option<&mut SummaryLogger>,
220203
) -> anyhow::Result<()> {
221204
if let Some(logger) = summary_logger {
@@ -234,7 +217,7 @@ async fn register_test_result<'a>(
234217
Ok(())
235218
}
236219

237-
pub async fn run_test<F, R>(
220+
pub async fn run_test_function<F, R>(
238221
runner_rpc: ServiceClient,
239222
mullvad_rpc: MullvadClientArgument,
240223
test: &F,

0 commit comments

Comments
 (0)