Skip to content

Commit 1ec0c96

Browse files
Differentiate Service error variant
1 parent c065a5f commit 1ec0c96

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

test/test-rpc/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ pub enum Error {
4747
Ping(String),
4848
#[error("Failed to get or set registry value: {0}")]
4949
Registry(String),
50+
#[error("Failed to start the service: {0}")]
51+
ServiceStart(String),
52+
#[error("Failed to stop the service: {0}")]
53+
ServiceStop(String),
5054
#[error("Failed to change the service: {0}")]
51-
Service(String),
55+
ServiceChange(String),
56+
#[error("Failed to find the service: {0}")]
57+
ServiceNotFound(String),
5258
#[error("Could not read from or write to the file system: {0}")]
5359
FileSystem(String),
5460
#[error("Could not serialize or deserialize file: {0}")]

test/test-runner/src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub async fn version() -> Result<String, Error> {
99
.arg("--version")
1010
.output()
1111
.await
12-
.map_err(|e| Error::Service(e.to_string()))?;
12+
.map_err(|e| Error::ServiceNotFound(e.to_string()))?;
1313
let version = String::from_utf8(version.stdout).map_err(|err| Error::Other(err.to_string()))?;
1414
// HACK: The output from `mullvad --version` includes the `mullvad-cli` binary name followed by
1515
// the version string. Simply remove the leading noise and get at the version string.

test/test-runner/src/sys.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ ExecStart=/usr/bin/mullvad-daemon --disable-stdout-timestamps {verbosity}"#
180180
if let Some(parent) = override_path.parent() {
181181
tokio::fs::create_dir_all(parent)
182182
.await
183-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
183+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
184184
}
185185

186186
let mut file = tokio::fs::OpenOptions::new()
@@ -189,17 +189,17 @@ ExecStart=/usr/bin/mullvad-daemon --disable-stdout-timestamps {verbosity}"#
189189
.write(true)
190190
.open(override_path)
191191
.await
192-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
192+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
193193

194194
file.write_all(systemd_service_file_content.as_bytes())
195195
.await
196-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
196+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
197197

198198
tokio::process::Command::new("systemctl")
199199
.args(["daemon-reload"])
200200
.status()
201201
.await
202-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
202+
.map_err(|e| test_rpc::Error::ServiceStart(e.to_string()))?;
203203

204204
restart_app().await?;
205205
Ok(())
@@ -214,7 +214,7 @@ pub async fn restart_app() -> Result<(), test_rpc::Error> {
214214
.args(["restart", "mullvad-daemon"])
215215
.status()
216216
.await
217-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
217+
.map_err(|e| test_rpc::Error::ServiceStart(e.to_string()))?;
218218
wait_for_service_state(ServiceState::Running).await?;
219219
Ok(())
220220
}
@@ -228,7 +228,7 @@ pub async fn stop_app() -> Result<(), test_rpc::Error> {
228228
.args(["stop", "mullvad-daemon"])
229229
.status()
230230
.await
231-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
231+
.map_err(|e| test_rpc::Error::ServiceStop(e.to_string()))?;
232232
wait_for_service_state(ServiceState::Inactive).await?;
233233

234234
Ok(())
@@ -243,7 +243,7 @@ pub async fn start_app() -> Result<(), test_rpc::Error> {
243243
.args(["start", "mullvad-daemon"])
244244
.status()
245245
.await
246-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
246+
.map_err(|e| test_rpc::Error::ServiceStart(e.to_string()))?;
247247
wait_for_service_state(ServiceState::Running).await?;
248248
Ok(())
249249
}
@@ -267,7 +267,7 @@ pub async fn stop_app() -> Result<(), test_rpc::Error> {
267267
.args(["stop", "mullvadvpn"])
268268
.status()
269269
.await
270-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
270+
.map_err(|e| test_rpc::Error::ServiceStop(e.to_string()))?;
271271
Ok(())
272272
}
273273

@@ -280,7 +280,7 @@ pub async fn start_app() -> Result<(), test_rpc::Error> {
280280
.args(["start", "mullvadvpn"])
281281
.status()
282282
.await
283-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
283+
.map_err(|e| test_rpc::Error::ServiceStart(e.to_string()))?;
284284
Ok(())
285285
}
286286

@@ -325,7 +325,7 @@ pub async fn set_daemon_log_level(verbosity_level: Verbosity) -> Result<(), test
325325
};
326326

327327
let manager = ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CONNECT)
328-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
328+
.map_err(|e| test_rpc::Error::ServiceNotFound(e.to_string()))?;
329329
let service = manager
330330
.open_service(
331331
"mullvadvpn",
@@ -334,23 +334,23 @@ pub async fn set_daemon_log_level(verbosity_level: Verbosity) -> Result<(), test
334334
| ServiceAccess::START
335335
| ServiceAccess::STOP,
336336
)
337-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
337+
.map_err(|e| test_rpc::Error::ServiceNotFound(e.to_string()))?;
338338

339339
// Stop the service
340340
// TODO: Extract to separate function.
341341
service
342342
.stop()
343-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
343+
.map_err(|e| test_rpc::Error::ServiceStop(e.to_string()))?;
344344
tokio::process::Command::new("net")
345345
.args(["stop", "mullvadvpn"])
346346
.status()
347347
.await
348-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
348+
.map_err(|e| test_rpc::Error::ServiceStop(e.to_string()))?;
349349

350350
// Get the current service configuration
351351
let config = service
352352
.query_config()
353-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
353+
.map_err(|e| test_rpc::Error::ServiceNotFound(e.to_string()))?;
354354

355355
let executable_path = "C:\\Program Files\\Mullvad VPN\\resources\\mullvad-daemon.exe";
356356
let launch_arguments = vec![
@@ -375,13 +375,13 @@ pub async fn set_daemon_log_level(verbosity_level: Verbosity) -> Result<(), test
375375
// Apply the updated configuration
376376
service
377377
.change_config(&updated_config)
378-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
378+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
379379

380380
// Start the service
381381
// TODO: Extract to separate function.
382382
service
383383
.start::<String>(&[])
384-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
384+
.map_err(|e| test_rpc::Error::ServiceNotFound(e.to_string()))?;
385385

386386
Ok(())
387387
}
@@ -445,19 +445,19 @@ pub async fn set_daemon_environment(env: HashMap<String, String>) -> Result<(),
445445
.map(|env_var| env_var.to_systemd_string())
446446
{
447447
writeln!(&mut override_content, "{env_var}")
448-
.map_err(|err| test_rpc::Error::Service(err.to_string()))?;
448+
.map_err(|err| test_rpc::Error::ServiceChange(err.to_string()))?;
449449
}
450450

451451
let override_path = std::path::Path::new(SYSTEMD_OVERRIDE_FILE);
452452
if let Some(parent) = override_path.parent() {
453453
tokio::fs::create_dir_all(parent)
454454
.await
455-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
455+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
456456
}
457457

458458
tokio::fs::write(override_path, override_content)
459459
.await
460-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?;
460+
.map_err(|e| test_rpc::Error::ServiceChange(e.to_string()))?;
461461

462462
if tokio::process::Command::new("systemctl")
463463
.args(["daemon-reload"])
@@ -467,7 +467,7 @@ pub async fn set_daemon_environment(env: HashMap<String, String>) -> Result<(),
467467
.success()
468468
.not()
469469
{
470-
return Err(test_rpc::Error::Service(
470+
return Err(test_rpc::Error::ServiceChange(
471471
"Daemon service could not be reloaded".to_owned(),
472472
));
473473
};
@@ -480,7 +480,7 @@ pub async fn set_daemon_environment(env: HashMap<String, String>) -> Result<(),
480480
.success()
481481
.not()
482482
{
483-
return Err(test_rpc::Error::Service(
483+
return Err(test_rpc::Error::ServiceStart(
484484
"Daemon service could not be restarted".to_owned(),
485485
));
486486
};
@@ -707,7 +707,7 @@ async fn wait_for_service_state(awaited_state: ServiceState) -> Result<(), test_
707707
loop {
708708
attempt += 1;
709709
if attempt > RETRY_ATTEMPTS {
710-
return Err(test_rpc::Error::Service(String::from(
710+
return Err(test_rpc::Error::ServiceStart(String::from(
711711
"Awaiting new service state timed out",
712712
)));
713713
}
@@ -716,7 +716,7 @@ async fn wait_for_service_state(awaited_state: ServiceState) -> Result<(), test_
716716
.args(["status", "mullvad-daemon"])
717717
.output()
718718
.await
719-
.map_err(|e| test_rpc::Error::Service(e.to_string()))?
719+
.map_err(|e| test_rpc::Error::ServiceNotFound(e.to_string()))?
720720
.stdout;
721721
let output = String::from_utf8_lossy(&output);
722722

0 commit comments

Comments
 (0)