Skip to content

Commit b6188ae

Browse files
WIP Use anyhow in test_automatic_wireguard_rotation
1 parent db67d44 commit b6188ae

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

test/test-manager/src/tests/account.rs

+19-34
Original file line numberDiff line numberDiff line change
@@ -277,78 +277,63 @@ pub async fn retry_if_throttled<
277277

278278
#[test_function]
279279
pub async fn test_automatic_wireguard_rotation(
280-
ctx: TestContext,
280+
_: TestContext,
281281
rpc: ServiceClient,
282282
mut mullvad_client: MullvadProxyClient,
283-
) -> Result<(), Error> {
283+
) -> anyhow::Result<()> {
284284
// Make note of current WG key
285285
let old_key = mullvad_client
286286
.get_device()
287-
.await
288-
.unwrap()
287+
.await?
289288
.into_device()
290-
.expect("Could not get device")
289+
.context("Could not get device")?
291290
.device
292291
.pubkey;
293292

294293
log::info!("Old wireguard key: {old_key}");
295294

296-
log::info!("Stopping daemon");
297-
rpc.stop_mullvad_daemon()
298-
.await
299-
.expect("Could not stop system service");
295+
rpc.stop_mullvad_daemon().await?;
300296

301297
log::info!("Changing created field of `device.json` to more than 7 days ago");
302298
rpc.make_device_json_old()
303299
.await
304-
.expect("Could not change device.json to have an old created timestamp");
305-
306-
log::info!("Starting daemon");
307-
rpc.start_mullvad_daemon()
308-
.await
309-
.expect("Could not start system service");
300+
.context("Could not change device.json to have an old created timestamp")?;
310301

311-
// NOTE: Need to create a new `mullvad_client` here after the restart otherwise we can't
312-
// communicate with the daemon
313-
log::info!("Reconnecting to daemon");
314-
drop(mullvad_client);
315-
let mut mullvad_client = ctx.rpc_provider.new_client().await;
316-
317-
log::info!("Verifying that wireguard key has change");
302+
rpc.start_mullvad_daemon().await?;
318303

319304
// Check if the key rotation has already occurred when connected to the daemon, otherwise
320305
// listen for device daemon events until we observe the change. We have to register the event
321306
// listener before polling the current key to be sure we don't miss the change.
307+
log::info!("Verifying that wireguard key has change");
322308
let event_listener = mullvad_client.events_listen().await.unwrap();
323309
let new_key = mullvad_client
324310
.get_device()
325-
.await
326-
.unwrap()
311+
.await?
327312
.into_device()
328-
.expect("Could not get device")
313+
.context("Could not get device")?
329314
.device
330315
.pubkey;
331316

332317
// If key has not yet been updated, listen for changes to it
333318
if new_key == old_key {
334-
log::info!("Listening for device daemon event");
335319
// Verify rotation has happened within 100 seconds - if the key hasn't been rotated after
336320
// that, the rotation probably won't happen anytime soon.
321+
log::info!("Listening for device daemon event");
322+
let device_event = |daemon_event| match daemon_event {
323+
DaemonEvent::Device(device_event) => Some(device_event),
324+
_ => None,
325+
};
337326
let device_event = tokio::task::spawn(tokio::time::timeout(
338327
Duration::from_secs(100),
339-
helpers::find_daemon_event(event_listener, |daemon_event| match daemon_event {
340-
DaemonEvent::Device(device_event) => Some(device_event),
341-
_ => None,
342-
}),
328+
helpers::find_daemon_event(event_listener, device_event),
343329
))
344-
.await
345-
.unwrap()
346-
.map_err(|_error| Error::Daemon(String::from("Tunnel event listener timed out")))??;
330+
.await?
331+
.context("Tunnel event listener timed out")??;
347332

348333
let new_key = device_event
349334
.new_state
350335
.into_device()
351-
.expect("Could not get device")
336+
.context("Could not get device")?
352337
.device
353338
.pubkey;
354339

0 commit comments

Comments
 (0)