Skip to content

Commit 0b6e27b

Browse files
committed
Fix install environment bug
1 parent 38f5829 commit 0b6e27b

File tree

3 files changed

+73
-47
lines changed

3 files changed

+73
-47
lines changed

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

+1-23
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub async fn install_app(
8686
replace_openvpn_cert(rpc).await?;
8787

8888
// Override env vars
89-
ensure_daemon_environment(rpc).await?;
89+
rpc.set_daemon_environment(get_app_env().await?).await?;
9090

9191
// Wait for the relay list to be updated
9292
let mut mullvad_client = rpc_provider.new_client().await;
@@ -96,28 +96,6 @@ pub async fn install_app(
9696
Ok(mullvad_client)
9797
}
9898

99-
/// Conditionally restart the running daemon
100-
///
101-
/// If the daemon was started with non-standard environment variables, subsequent tests may break
102-
/// due to assuming a default configuration. In that case, reset the environment variables and
103-
/// restart.
104-
pub async fn ensure_daemon_environment(rpc: &ServiceClient) -> Result<(), anyhow::Error> {
105-
let current_env = rpc
106-
.get_daemon_environment()
107-
.await
108-
.context("Failed to get daemon env variables")?;
109-
let default_env = get_app_env()
110-
.await
111-
.context("Failed to get daemon default env variables")?;
112-
if current_env != default_env {
113-
log::debug!("Restarting daemon due changed environment variables. Values since last test {current_env:?}");
114-
rpc.set_daemon_environment(default_env)
115-
.await
116-
.context("Failed to restart daemon")?;
117-
};
118-
Ok(())
119-
}
120-
12199
pub async fn replace_openvpn_cert(rpc: &ServiceClient) -> Result<(), Error> {
122100
const SOURCE_CERT_FILENAME: &str = "openvpn.ca.crt";
123101
const DEST_CERT_FILENAME: &str = "ca.crt";

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use mullvad_types::{constraints::Constraint, relay_constraints};
66
use test_macro::test_function;
77
use test_rpc::{mullvad_daemon::ServiceStatus, ServiceClient};
88

9+
use crate::tests::helpers;
10+
911
use super::{
1012
config::TEST_CONFIG,
1113
helpers::{
@@ -86,10 +88,18 @@ pub async fn test_upgrade_app(
8688
let pinger = Pinger::start(rpc).await;
8789

8890
// install new package
89-
let mut mullvad_client = install_app(rpc, &TEST_CONFIG.app_package_filename, &ctx.rpc_provider)
90-
.await
91-
.context("Failed to install new app version")?;
9291

92+
log::debug!("Installing new app");
93+
rpc.install_app(helpers::get_package_desc(&TEST_CONFIG.app_package_filename))
94+
.await?;
95+
96+
// Give it some time to start
97+
tokio::time::sleep(Duration::from_secs(3)).await;
98+
99+
// verify that daemon is running
100+
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
101+
bail!(Error::DaemonNotRunning);
102+
}
93103
// Check if any traffic was observed
94104
//
95105
let guest_ip = pinger.guest_ip;
@@ -99,6 +109,10 @@ pub async fn test_upgrade_app(
99109
"observed unexpected packets from {guest_ip}"
100110
);
101111

112+
// NOTE: Need to create a new `mullvad_client` here after the restart
113+
// otherwise we *probably* can't communicate with the daemon.
114+
let mut mullvad_client = ctx.rpc_provider.new_client().await;
115+
102116
// check if settings were (partially) preserved
103117
log::info!("Sanity checking settings");
104118

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

+55-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
};
2121
use anyhow::Context;
2222
use config::TEST_CONFIG;
23-
use helpers::install_app;
23+
use helpers::{get_app_env, install_app};
2424
pub use install::test_upgrade_app;
2525
use mullvad_management_interface::MullvadProxyClient;
2626
pub use test_metadata::TestMetadata;
@@ -78,13 +78,13 @@ pub fn get_tests() -> Vec<&'static TestMetadata> {
7878
tests
7979
}
8080

81-
/// Make sure the daemon is installed and logged in. and restore settings to the defaults.
81+
/// Make sure the daemon is installed and logged in and restore settings to the defaults.
8282
pub async fn prepare_daemon(
8383
rpc: &ServiceClient,
8484
rpc_provider: &RpcClientProvider,
8585
) -> anyhow::Result<()> {
8686
// Check if daemon should be restarted
87-
let mut mullvad_client = restart_daemon(rpc, rpc_provider)
87+
let mut mullvad_client = ensure_daemon_version(rpc, rpc_provider)
8888
.await
8989
.context("Failed to restart daemon")?;
9090

@@ -105,35 +105,69 @@ pub async fn prepare_daemon(
105105
/// Reset the daemons environment.
106106
///
107107
/// Will and restart or reinstall it if necessary.
108-
async fn restart_daemon(
108+
async fn ensure_daemon_version(
109109
rpc: &ServiceClient,
110110
rpc_provider: &RpcClientProvider,
111111
) -> anyhow::Result<MullvadProxyClient> {
112112
let mut mullvad_client = rpc_provider.new_client().await;
113113
let app_package_filename = &TEST_CONFIG.app_package_filename;
114114

115+
let mullvad_client = if correct_daemon_version_is_running(&mut mullvad_client).await {
116+
ensure_daemon_environment(rpc)
117+
.await
118+
.context("Failed to reset daemon environment")?;
119+
rpc_provider.new_client().await
120+
} else {
121+
// NOTE: Reinstalling the app resets the daemon environment
122+
install_app(rpc, app_package_filename, rpc_provider)
123+
.await
124+
.with_context(|| format!("Failed to install app '{app_package_filename}'"))?
125+
};
126+
Ok(mullvad_client)
127+
}
128+
129+
/// Conditionally restart the running daemon
130+
///
131+
/// If the daemon was started with non-standard environment variables, subsequent tests may break
132+
/// due to assuming a default configuration. In that case, reset the environment variables and
133+
/// restart.
134+
pub async fn ensure_daemon_environment(rpc: &ServiceClient) -> Result<(), anyhow::Error> {
135+
let current_env = rpc
136+
.get_daemon_environment()
137+
.await
138+
.context("Failed to get daemon env variables")?;
139+
let default_env = get_app_env()
140+
.await
141+
.context("Failed to get daemon default env variables")?;
142+
if current_env != default_env {
143+
log::debug!("Restarting daemon due changed environment variables. Values since last test {current_env:?}");
144+
rpc.set_daemon_environment(default_env)
145+
.await
146+
.context("Failed to restart daemon")?;
147+
};
148+
Ok(())
149+
}
150+
151+
/// Reset the daemons environment.
152+
///
153+
/// Will and restart or reinstall it if necessary.
154+
async fn correct_daemon_version_is_running(mullvad_client: &mut MullvadProxyClient) -> bool {
155+
let app_package_filename = &TEST_CONFIG.app_package_filename;
156+
let expected_version = get_version_from_path(std::path::Path::new(app_package_filename))
157+
.unwrap_or_else(|_| panic!("Invalid app version: {app_package_filename}"));
158+
115159
use mullvad_management_interface::Error::*;
116160
match mullvad_client.get_current_version().await {
117161
// Failing to reach the daemon is a sign that it is not installed
118162
Err(Rpc(..)) => {
119-
log::info!("Could not reach active daemon before test, (re)installing app");
120-
// NOTE: Reinstalling the app resets the daemon environment
121-
mullvad_client = install_app(rpc, app_package_filename, rpc_provider)
122-
.await
123-
.with_context(|| format!("Failed to install app '{app_package_filename}'"))?;
163+
log::info!("Could not reach active daemon before test");
164+
false
124165
}
125-
Err(e) => return Err(anyhow::anyhow!(e).context("Failed to get app version")),
126-
Ok(version) => {
127-
if version != get_version_from_path(std::path::Path::new(app_package_filename))? {
128-
log::info!("Daemon version mismatch, re-installing app");
129-
mullvad_client = install_app(rpc, app_package_filename, rpc_provider)
130-
.await
131-
.context("Failed to install app")?;
132-
}
133-
helpers::ensure_daemon_environment(rpc)
134-
.await
135-
.context("Failed to reset daemon environment")?;
166+
Err(e) => panic!("Failed to get app version: {e}"),
167+
Ok(version) if version == expected_version => true,
168+
_ => {
169+
log::info!("Daemon version mismatch");
170+
false
136171
}
137172
}
138-
Ok(mullvad_client)
139173
}

0 commit comments

Comments
 (0)