Skip to content

Commit

Permalink
Reset daemon environment when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Apr 11, 2024
1 parent cae56cc commit e86ac2e
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 26 deletions.
4 changes: 2 additions & 2 deletions test/test-manager/src/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub async fn run(
// Try to reset the daemon state if the test failed OR if the test doesn't explicitly
// disabled cleanup.
if test.cleanup || matches!(test_result.result, Err(_) | Ok(Err(_))) {
let mut client = test_context.rpc_provider.new_client().await;
crate::tests::cleanup_after_test(&mut client).await?;
crate::tests::cleanup_after_test(client.clone(), &test_context.rpc_provider)
.await?;
}
}

Expand Down
34 changes: 30 additions & 4 deletions test/test-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ mod tunnel;
mod tunnel_state;
mod ui;

use crate::mullvad_daemon::{MullvadClientArgument, RpcClientProvider};
use crate::{
mullvad_daemon::{MullvadClientArgument, RpcClientProvider},
tests::helpers::get_app_env,
};
use anyhow::Context;
use mullvad_management_interface::MullvadProxyClient;
pub use test_metadata::TestMetadata;
use test_rpc::ServiceClient;

use futures::future::BoxFuture;

use mullvad_management_interface::MullvadProxyClient;
use std::time::Duration;

const WAIT_FOR_TUNNEL_STATE_TIMEOUT: Duration = Duration::from_secs(40);
Expand Down Expand Up @@ -62,10 +65,15 @@ pub enum Error {
}

/// Restore settings to the defaults.
pub async fn cleanup_after_test(mullvad_client: &mut MullvadProxyClient) -> anyhow::Result<()> {
pub async fn cleanup_after_test(
rpc: ServiceClient,
rpc_provider: &RpcClientProvider,
) -> anyhow::Result<()> {
log::debug!("Cleaning up daemon in test cleanup");
// Check if daemon should be restarted
let mut mullvad_client = restart_daemon(rpc, rpc_provider).await?;

helpers::disconnect_and_wait(mullvad_client).await?;
helpers::disconnect_and_wait(&mut mullvad_client).await?;

let default_settings = mullvad_types::settings::Settings::default();

Expand Down Expand Up @@ -122,3 +130,21 @@ pub async fn cleanup_after_test(mullvad_client: &mut MullvadProxyClient) -> anyh

Ok(())
}

/// Conditonally restart the running daemon
///
/// If the daemon was started with non-standard environment variables, subsequent tests may break
/// due to assuming a default configuration. In that case, reset the environment variables and
/// restart.
async fn restart_daemon(
rpc: ServiceClient,
rpc_provider: &RpcClientProvider,
) -> anyhow::Result<MullvadProxyClient> {
let current_env = rpc.get_daemon_environment().await?;
let default_env = get_app_env();
if current_env != default_env {
rpc.set_daemon_environment(default_env).await?;
}
let mullvad_client = rpc_provider.new_client().await;
Ok(mullvad_client)
}
14 changes: 14 additions & 0 deletions test/test-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,20 @@ impl ServiceClient {
Ok(())
}

/// Get the current daemon's environment variables.
///
/// # Returns
/// - `Result::Ok(env)` if the current environment variables could be read.
/// - `Result::Err(Error)` if communication with the daemon failed or the environment values
/// could not be parsed.
pub async fn get_daemon_environment(&self) -> Result<HashMap<String, String>, Error> {
let mut ctx = tarpc::context::current();
ctx.deadline = SystemTime::now().checked_add(LOG_LEVEL_TIMEOUT).unwrap();
let env = self.client.get_daemon_environment(ctx).await??;

Ok(env)
}

pub async fn copy_file(&self, src: String, dest: String) -> Result<(), Error> {
log::debug!("Copying \"{src}\" to \"{dest}\"");
self.client
Expand Down
3 changes: 3 additions & 0 deletions test/test-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ mod service {
/// service.
async fn set_daemon_environment(env: HashMap<String, String>) -> Result<(), Error>;

/// Get the environment variables for the running daemon service.
async fn get_daemon_environment() -> Result<HashMap<String, String>, Error>;

/// Copy a file from `src` to `dest` on the test runner.
async fn copy_file(src: String, dest: String) -> Result<(), Error>;

Expand Down
7 changes: 7 additions & 0 deletions test/test-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ impl Service for TestServer {
sys::set_daemon_environment(env).await
}

async fn get_daemon_environment(
self,
_: context::Context,
) -> Result<HashMap<String, String>, test_rpc::Error> {
sys::get_daemon_environment().await
}

async fn copy_file(
self,
_: context::Context,
Expand Down
Loading

0 comments on commit e86ac2e

Please sign in to comment.