use assert_cmd::prelude::*; use predicates::prelude::*; use std::fs::File; use std::io::prelude::*; use std::process::Command; mod common; #[test] fn set_value() -> Result<(), Box> { // Create a temporary project and service let root_dir = common::create_project("set-value")?; let folder = root_dir.path(); common::create_service(folder, "hello-world")?; // Store the value let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "hello-world") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("set") .arg("my_param") .arg("my-param-value"); cmd.assert().success(); // Check the store file let mut file = File::open(&folder.join(".store").join("store.json"))?; let mut data = String::new(); file.read_to_string(&mut data)?; assert!(predicate::str::contains("{\"hello-world:my_param\":\"my-param-value\"}").eval(&data)); Ok(()) } #[test] fn set_file() -> Result<(), Box> { // Create a temporary project and service let root_dir = common::create_project("set-value")?; let folder = root_dir.path(); common::create_service(folder, "hello-world")?; // Store the file let param_filename = &folder.join("test.txt"); let mut param_file = File::create(¶m_filename)?; let param_data = "Hello, world!"; param_file.write_all(param_data.as_bytes())?; // Store the file let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "hello-world") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("set-file") .arg("my_param") .arg(¶m_filename); cmd.assert().success(); // Check the parameter store let mut file = File::open(&folder.join(".store").join("hello-world:my_param.store"))?; let mut data = String::new(); file.read_to_string(&mut data)?; assert_eq!(data, param_data); Ok(()) } #[test] fn set_get_value() -> Result<(), Box> { // Create a temporary project and services let root_dir = common::create_project("set-value")?; let folder = root_dir.path(); common::create_service(folder, "service-a")?; common::create_service(folder, "service-b")?; // Override the project file let mut cfg_file = File::create(folder.join("orcs.toml"))?; let cfg_data = "[stages.my_stage]"; cfg_file.write_all(cfg_data.as_bytes())?; // Override service B file // We need to mark service-a as a dependency of service-b, otherwise // we'll get a permission denied error. let mut cfg_file = File::create(folder.join("srv").join("service-b").join("orcs.toml"))?; let cfg_data = "stages.my_stage.depends_on = [\"service-a\"]"; cfg_file.write_all(cfg_data.as_bytes())?; // Store the value let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "service-a") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("set") .arg("my_param") .arg("my-param-value"); cmd.assert().success(); // Retrieve the value let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "service-b") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("get") .arg("service-a:my_param"); cmd.assert() .success() .stdout(predicate::str::contains("my-param-value")); Ok(()) } // TODO: set_get_file #[test] fn set_get_file() -> Result<(), Box> { // Create a temporary project and services let root_dir = common::create_project("set-value")?; let folder = root_dir.path(); common::create_service(folder, "service-a")?; common::create_service(folder, "service-b")?; // Override the project file let mut cfg_file = File::create(folder.join("orcs.toml"))?; let cfg_data = "[stages.my_stage]"; cfg_file.write_all(cfg_data.as_bytes())?; // Override service B file // We need to mark service-a as a dependency of service-b, otherwise // we'll get a permission denied error. let mut cfg_file = File::create(folder.join("srv").join("service-b").join("orcs.toml"))?; let cfg_data = "stages.my_stage.depends_on = [\"service-a\"]"; cfg_file.write_all(cfg_data.as_bytes())?; // Store the file let param_filename = &folder.join("test.txt"); let mut param_file = File::create(¶m_filename)?; let param_data = "Hello, world!"; param_file.write_all(param_data.as_bytes())?; let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "service-a") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("set-file") .arg("my_param") .arg(¶m_filename); cmd.assert().success(); // Retrieve the file let param2_filename = &folder.join("test2.txt"); let mut cmd = Command::cargo_bin("orcs")?; cmd.env("ORCS_SERVICE", "service-b") .env("ORCS_STAGE", "my_stage") .arg("-p") .arg(&folder) .arg("get-file") .arg("service-a:my_param") .arg(¶m2_filename); cmd.assert().success(); // Check the parameter file let mut param2_file = File::open(param2_filename)?; let mut param2_data = String::new(); param2_file.read_to_string(&mut param2_data)?; assert_eq!(param_data, param2_data); Ok(()) }