summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-05-12 11:05:59 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-06-20 08:08:31 -0300
commite8bcc37bdacf656df37b69e369b89391de0f1248 (patch)
tree0c53e699107f34467a07d329c18eeeb9ff1c49f5
parent2632bf5a6b2cbae1d486cb27ddc7bf59dee8cca2 (diff)
downloadmullvadvpn-e8bcc37bdacf656df37b69e369b89391de0f1248.tar.xz
mullvadvpn-e8bcc37bdacf656df37b69e369b89391de0f1248.zip
Use a fake resource directory for tests
-rw-r--r--Cargo.lock1
-rw-r--r--mullvad-daemon/Cargo.toml1
-rw-r--r--mullvad-daemon/tests/common/mod.rs55
3 files changed, 44 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 46cd320f69..96396c2e4b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -779,6 +779,7 @@ dependencies = [
"talpid-core 0.1.0",
"talpid-ipc 0.1.0",
"talpid-types 0.1.0",
+ "tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-timer 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml
index 0bcc98b573..26961a6265 100644
--- a/mullvad-daemon/Cargo.toml
+++ b/mullvad-daemon/Cargo.toml
@@ -48,3 +48,4 @@ assert_matches = "1.0"
duct = "0.10"
notify = "4.0"
os_pipe = "0.6"
+tempfile = "3.0"
diff --git a/mullvad-daemon/tests/common/mod.rs b/mullvad-daemon/tests/common/mod.rs
index 31107955bb..a93d1b1528 100644
--- a/mullvad-daemon/tests/common/mod.rs
+++ b/mullvad-daemon/tests/common/mod.rs
@@ -6,10 +6,11 @@ extern crate mullvad_ipc_client;
extern crate mullvad_paths;
extern crate notify;
extern crate os_pipe;
+extern crate tempfile;
-use std::fs::{self, File};
-use std::io::{BufRead, BufReader, Write};
-use std::path::Path;
+use std::fs;
+use std::io::{BufRead, BufReader};
+use std::path::{Path, PathBuf};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
@@ -19,6 +20,7 @@ use duct;
use self::mullvad_ipc_client::DaemonRpcClient;
use self::notify::{op, RawEvent, RecursiveMode, Watcher};
use self::os_pipe::{pipe, PipeReader};
+use self::tempfile::TempDir;
#[cfg(unix)]
pub static DAEMON_EXECUTABLE_PATH: &str = "../target/debug/mullvad-daemon";
@@ -67,31 +69,57 @@ pub fn wait_for_file_write_finish<P: AsRef<Path>>(file_path: P, timeout: Duratio
}
}
-fn prepare_relay_list<T: AsRef<Path>>(path: T) {
- let path = path.as_ref();
+fn prepare_test_dirs() -> (TempDir, PathBuf) {
+ let temp_dir = TempDir::new().expect("Failed to create temporary daemon directory");
+ let resource_dir = temp_dir.path().join("resource-dir");
- if !path.exists() {
- File::create(path)
- .expect("Failed to create relay list file")
- .write_all(b"{ \"countries\": [] }")
- .expect("Failed to write relay list");
- }
+ fs::create_dir(&resource_dir).expect("Failed to create resource directory");
+
+ prepare_relay_list(resource_dir.join("relays.json"));
+
+ (temp_dir, resource_dir)
+}
+
+fn prepare_relay_list<T: AsRef<Path>>(path: T) {
+ fs::write(
+ path,
+ r#"{
+ "countries": [{
+ "name": "Mockland",
+ "code": "fake",
+ "latitude": -91,
+ "longitude": 0,
+ "relays": [{
+ "hostname": "fake-mockland",
+ "ipv4_addr_in": "192.168.0.100",
+ "ipv4_addr_exit": "192.168.0.101",
+ "include_in_country": true,
+ "weight": 100,
+ "tunnels": {
+ "openvpn": [ { "port": 10000, "protocol": "udp" } ],
+ "wireguard": [],
+ },
+ }],
+ }]
+ }"#,
+ ).expect("Failed to create mock relay list file");
}
pub struct DaemonRunner {
process: Option<duct::Handle>,
output: Arc<Mutex<BufReader<PipeReader>>>,
+ _temp_dir: TempDir,
}
impl DaemonRunner {
pub fn spawn() -> Self {
- prepare_relay_list("../dist-assets/relays.json");
+ let (temp_dir, resource_dir) = prepare_test_dirs();
let (reader, writer) = pipe().expect("Failed to open pipe to connect to daemon");
let process = cmd!(DAEMON_EXECUTABLE_PATH, "-v", "--disable-log-to-file")
.dir("..")
.env("MULLVAD_CACHE_DIR", "./")
- .env("MULLVAD_RESOURCE_DIR", "./dist-assets")
+ .env("MULLVAD_RESOURCE_DIR", resource_dir)
.stderr_to_stdout()
.stdout_handle(writer)
.start()
@@ -100,6 +128,7 @@ impl DaemonRunner {
DaemonRunner {
process: Some(process),
output: Arc::new(Mutex::new(BufReader::new(reader))),
+ _temp_dir: temp_dir,
}
}