-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathmain.rs
426 lines (373 loc) · 13.9 KB
/
main.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
mod config;
mod container;
mod logging;
mod mullvad_daemon;
mod network_monitor;
mod package;
mod run_tests;
mod summary;
mod tests;
mod vm;
#[cfg(target_os = "macos")]
use std::net::IpAddr;
use std::{net::SocketAddr, path::PathBuf};
use anyhow::{Context, Ok, Result};
use clap::{builder::PossibleValuesParser, Parser};
use config::ConfigFile;
use package::TargetInfo;
use tests::{config::TEST_CONFIG, get_filtered_tests};
use vm::provision;
use crate::tests::config::OpenVPNCertificate;
/// Test manager for Mullvad VPN app
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[clap(subcommand)]
cmd: Commands,
}
#[derive(clap::Subcommand, Debug)]
enum ConfigArg {
Get,
Set,
GetPath,
}
#[derive(clap::Subcommand, Debug)]
enum Commands {
/// Manage configuration for tests and VMs
#[clap(subcommand)]
Config(ConfigArg),
/// Create or edit a VM config
Set {
/// Name of the VM config
vm: String,
/// VM config
#[clap(flatten)]
config: config::VmConfig,
},
/// Remove specified VM config
Remove {
/// Name of the VM config, run `test-manager list` to see available configs
vm: String,
},
/// List available VM configurations
List,
/// Spawn a runner instance without running any tests
RunVm {
/// Name of the VM config, run `test-manager list` to see available configs
vm: String,
/// Run VNC server on a specified port
#[arg(long)]
vnc: Option<u16>,
/// Make permanent changes to image
#[arg(long)]
keep_changes: bool,
},
/// List all tests and their priority.
ListTests,
/// Spawn a runner instance and run tests
RunTests {
/// Name of the VM config, run `test-manager list` to see available configs
#[arg(long)]
vm: String,
/// Show display of guest
#[arg(long, group = "display_args")]
display: bool,
/// API and conncheck environment to use. The domain name will be prefixed with "api." and
/// "ipv4.am.i.".
#[arg(long, value_parser = PossibleValuesParser::new(&["mullvad.net", "stagemole.eu", "devmole.eu"]))]
mullvad_host: Option<String>,
/// Run VNC server on a specified port
#[arg(long, group = "display_args")]
vnc: Option<u16>,
/// Account number to use for testing
#[arg(long, short)]
account: String,
/// App package to test. Can be a path to the package, just the package file name, git hash
/// or tag. If the direct path is not given, the package is assumed to be in the directory
/// specified by the `--package-dir` argument.
///
/// # Note
///
/// The gRPC interface must be compatible with the version specified for
/// `mullvad-management-interface` in Cargo.toml.
#[arg(long)]
app_package: String,
/// Given this argument, the `test_upgrade_app` test will run, which installs the previous
/// version then upgrades to the version specified in by `--app-package`. If left empty,
/// the test will be skipped. Parsed the same way as `--app-package`.
///
/// # Note
///
/// The CLI interface must be compatible with the upgrade test.
#[arg(long)]
app_package_to_upgrade_from: Option<String>,
/// Package used for GUI tests. Parsed the same way as `--app-package`.
/// If not specified, will look for a package matching the version of the app package. If
/// no such package is found, the GUI tests will fail.
#[arg(long)]
gui_package: Option<String>,
/// Folder to search for packages. Defaults to current directory.
#[arg(long, value_name = "DIR")]
package_dir: Option<PathBuf>,
/// OpenVPN CA certificate to use with the app under test. The expected argument is a path
/// (absolut or relative) to the desired CA certificate. The default certificate is
/// `assets/openvpn.ca.crt`.
#[arg(long)]
openvpn_certificate: Option<PathBuf>,
/// Names of tests to run. The order given will be respected. If not set, all tests will be
/// run.
test_filters: Vec<String>,
/// Print results live
#[arg(long, short)]
verbose: bool,
/// Path to output test results in a structured format
#[arg(long, value_name = "PATH")]
test_report: Option<PathBuf>,
/// Path to the directory containing the test runner
#[arg(long, value_name = "DIR")]
runner_dir: Option<PathBuf>,
},
/// Output an HTML-formatted summary of one or more reports
FormatTestReports {
/// One or more test reports output by 'test-manager run-tests --test-report'
reports: Vec<PathBuf>,
},
/// Update the system image
///
/// Note that in order for the updates to take place, the VM's config need
/// to have `provisioner` set to `ssh`, `ssh_user` & `ssh_password` set and
/// the `ssh_user` should be able to execute commands with sudo/ as root.
Update {
/// Name of the VM config
name: String,
},
}
#[cfg(target_os = "linux")]
impl Args {
fn get_vnc_port(&self) -> Option<u16> {
match self.cmd {
Commands::RunTests { vnc, .. } | Commands::RunVm { vnc, .. } => vnc,
_ => None,
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
logging::Logger::get_or_init();
let args = Args::parse();
#[cfg(target_os = "linux")]
container::relaunch_with_rootlesskit(args.get_vnc_port()).await;
let mut config = config::ConfigFile::load_or_default()
.await
.context("Failed to load config")?;
match args.cmd {
Commands::Config(config_subcommand) => match config_subcommand {
ConfigArg::Get => {
println!("{:#?}", *config);
Ok(())
}
ConfigArg::Set => todo!(),
ConfigArg::GetPath => {
println!(
"{}",
ConfigFile::get_config_path()
.expect("Get config path")
.display()
);
Ok(())
}
},
Commands::Set {
vm,
config: vm_config,
} => vm::set_config(&mut config, &vm, vm_config)
.await
.context("Failed to edit or create VM config"),
Commands::Remove { vm } => {
if config.get_vm(&vm).is_none() {
println!("No such configuration");
return Ok(());
}
config
.edit(|config| {
config.vms.remove_entry(&vm);
})
.await
.context("Failed to remove config entry")?;
println!("Removed configuration \"{vm}\"");
Ok(())
}
Commands::List => {
println!("Available configurations:");
for (vm, config) in config.vms.iter() {
println!("{vm}: {config:#?}");
}
Ok(())
}
Commands::RunVm {
vm,
vnc,
keep_changes,
} => {
let mut config = config.clone();
config.runtime_opts.keep_changes = keep_changes;
config.runtime_opts.display = if vnc.is_some() {
config::Display::Vnc
} else {
config::Display::Local
};
let mut instance = vm::run(&config, &vm).await.context("Failed to start VM")?;
instance.wait().await;
Ok(())
}
Commands::ListTests => {
println!("priority\tname");
for test in tests::get_test_descriptions() {
println!(
"{priority:8}\t{name}",
name = test.name,
priority = test.priority.unwrap_or(0),
);
}
Ok(())
}
Commands::RunTests {
vm,
display,
mullvad_host,
vnc,
account,
app_package,
app_package_to_upgrade_from,
gui_package,
package_dir,
openvpn_certificate,
test_filters,
verbose,
test_report,
runner_dir,
} => {
let mut config = config.clone();
config.runtime_opts.display = match (display, vnc.is_some()) {
(false, false) => config::Display::None,
(true, false) => config::Display::Local,
(false, true) => config::Display::Vnc,
(true, true) => unreachable!("invalid combination"),
};
if let Some(mullvad_host) = mullvad_host {
if let Some(old_host) = config.mullvad_host.replace(mullvad_host) {
log::info!(
"Overriding Mullvad host from {old_host} to {}",
config.mullvad_host.as_ref().unwrap()
);
} else {
log::info!(
"Setting Mullvad host to {}",
config.mullvad_host.as_ref().unwrap()
);
}
}
let mullvad_host = config.get_host();
log::debug!("Mullvad host: {mullvad_host}");
let vm_config = vm::get_vm_config(&config, &vm).context("Cannot get VM config")?;
let runner_target = TargetInfo::try_from(vm_config)?;
let manifest = package::get_app_manifest(
runner_target,
app_package,
app_package_to_upgrade_from,
gui_package,
package_dir,
)
.context("Could not find the specified app packages")?;
// Load a new OpenVPN CA certificate if the user provided a path.
let openvpn_certificate = openvpn_certificate
.map(OpenVPNCertificate::from_file)
.transpose()
.context("Could not find OpenVPN CA certificate")?
.unwrap_or_default();
let mut instance = vm::run(&config, &vm).await.context("Failed to start VM")?;
let runner_dir = runner_dir.unwrap_or_else(|| vm_config.get_default_runner_dir());
let artifacts_dir = provision::provision(vm_config, &*instance, &manifest, runner_dir)
.await
.context("Failed to run provisioning for VM")?;
#[cfg(target_os = "macos")]
let IpAddr::V4(guest_ip) = instance.get_ip().to_owned() else {
panic!("Expected bridge IP to be version 4, but was version 6.")
};
let (bridge_name, bridge_ip) = vm::network::bridge(
#[cfg(target_os = "macos")]
&guest_ip,
)?;
TEST_CONFIG.init(tests::config::TestConfig::new(
account,
artifacts_dir,
manifest
.app_package_path
.file_name()
.unwrap()
.to_string_lossy()
.into_owned(),
manifest
.app_package_to_upgrade_from_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned()),
manifest
.gui_package_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned()),
mullvad_host,
bridge_name,
bridge_ip,
test_rpc::meta::Os::from(vm_config.os_type),
openvpn_certificate,
));
let mut tests = get_filtered_tests(&test_filters)?;
for test in tests.iter_mut() {
test.location = config.test_locations.lookup(test.name).cloned();
}
// For convenience, spawn a SOCKS5 server that is reachable for tests that need it
let socks = socks_server::spawn(SocketAddr::new(
TEST_CONFIG.host_bridge_ip.into(),
crate::vm::network::SOCKS5_PORT,
))
.await?;
let skip_wait = vm_config.provisioner != config::Provisioner::Noop;
let summary_logger = match test_report {
Some(path) => Some(
summary::SummaryLogger::new(
&vm,
test_rpc::meta::Os::from(vm_config.os_type),
&path,
)
.await
.context("Failed to create summary logger")?,
),
None => None,
};
let result = run_tests::run(&*instance, tests, skip_wait, !verbose, summary_logger)
.await
.context("Tests failed");
if display {
instance.wait().await;
}
socks.close();
// Propagate any error from the test run if applicable
result?.anyhow()
}
Commands::FormatTestReports { reports } => {
summary::print_summary_table(&reports).await;
Ok(())
}
Commands::Update { name } => {
let vm_config = vm::get_vm_config(&config, &name).context("Cannot get VM config")?;
let instance = vm::run(&config, &name)
.await
.context("Failed to start VM")?;
let update_output = vm::update_packages(vm_config.clone(), &*instance)
.await
.context("Failed to update packages to the VM image")?;
log::info!("Update command finished with output: {}", &update_output);
// TODO: If the update was successful, commit the changes to the VM image.
log::info!("Note: updates have not been persisted to the image");
Ok(())
}
}
}