@@ -20,7 +20,7 @@ use crate::{
20
20
} ;
21
21
use anyhow:: Context ;
22
22
use config:: TEST_CONFIG ;
23
- use helpers:: install_app;
23
+ use helpers:: { get_app_env , install_app} ;
24
24
pub use install:: test_upgrade_app;
25
25
use mullvad_management_interface:: MullvadProxyClient ;
26
26
pub use test_metadata:: TestMetadata ;
@@ -78,13 +78,13 @@ pub fn get_tests() -> Vec<&'static TestMetadata> {
78
78
tests
79
79
}
80
80
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.
82
82
pub async fn prepare_daemon (
83
83
rpc : & ServiceClient ,
84
84
rpc_provider : & RpcClientProvider ,
85
85
) -> anyhow:: Result < ( ) > {
86
86
// 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)
88
88
. await
89
89
. context ( "Failed to restart daemon" ) ?;
90
90
@@ -105,35 +105,69 @@ pub async fn prepare_daemon(
105
105
/// Reset the daemons environment.
106
106
///
107
107
/// Will and restart or reinstall it if necessary.
108
- async fn restart_daemon (
108
+ async fn ensure_daemon_version (
109
109
rpc : & ServiceClient ,
110
110
rpc_provider : & RpcClientProvider ,
111
111
) -> anyhow:: Result < MullvadProxyClient > {
112
112
let mut mullvad_client = rpc_provider. new_client ( ) . await ;
113
113
let app_package_filename = & TEST_CONFIG . app_package_filename ;
114
114
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
+
115
159
use mullvad_management_interface:: Error :: * ;
116
160
match mullvad_client. get_current_version ( ) . await {
117
161
// Failing to reach the daemon is a sign that it is not installed
118
162
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
124
165
}
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
136
171
}
137
172
}
138
- Ok ( mullvad_client)
139
173
}
0 commit comments