summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock231
-rw-r--r--sm/plugins/tedge_apt_plugin/Cargo.toml8
-rw-r--r--sm/plugins/tedge_apt_plugin/tests/main.rs163
3 files changed, 400 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9ed3963a..0e7fa29b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -470,6 +470,22 @@ dependencies = [
]
[[package]]
+name = "core-foundation"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
+
+[[package]]
name = "cpufeatures"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -603,7 +619,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4cccf60bb98c0fca115a581f894aed0e43fa55bf289fdac5599bec440bb4fd6"
dependencies = [
"nom",
- "num-bigint",
+ "num-bigint 0.4.2",
"num-traits",
"syn 1.0.76",
]
@@ -616,7 +632,7 @@ checksum = "2d7ededb7525bb4114bc209685ce7894edc2965f4914312a1ea578a645a237f0"
dependencies = [
"der-oid-macro",
"nom",
- "num-bigint",
+ "num-bigint 0.4.2",
"num-traits",
"rusticata-macros",
]
@@ -783,6 +799,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
+name = "foreign-types"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
+dependencies = [
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
+
+[[package]]
name = "form_urlencoded"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -974,6 +1005,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3"
[[package]]
+name = "hamcrest2"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49f837c62de05dc9cc71ff6486cd85de8856a330395ae338a04bfcefe5e91075"
+dependencies = [
+ "num",
+ "regex",
+]
+
+[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1111,6 +1152,19 @@ dependencies = [
]
[[package]]
+name = "hyper-tls"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
+dependencies = [
+ "bytes",
+ "hyper",
+ "native-tls",
+ "tokio",
+ "tokio-native-tls",
+]
+
+[[package]]
name = "idna"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1473,6 +1527,24 @@ dependencies = [
]
[[package]]
+name = "native-tls"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d"
+dependencies = [
+ "lazy_static",
+ "libc",
+ "log",
+ "openssl",
+ "openssl-probe",
+ "openssl-sys",
+ "schannel",
+ "security-framework",
+ "security-framework-sys",
+ "tempfile",
+]
+
+[[package]]
name = "nix"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1514,6 +1586,31 @@ dependencies = [
]
[[package]]
+name = "num"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"
+dependencies = [
+ "num-bigint 0.2.6",
+ "num-complex",
+ "num-integer",
+ "num-iter",
+ "num-rational",
+ "num-traits",
+]
+
+[[package]]
+name = "num-bigint"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
name = "num-bigint"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1525,6 +1622,16 @@ dependencies = [
]
[[package]]
+name = "num-complex"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1535,6 +1642,29 @@ dependencies = [
]
[[package]]
+name = "num-iter"
+version = "0.1.42"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-rational"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
+dependencies = [
+ "autocfg",
+ "num-bigint 0.2.6",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1590,6 +1720,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
+name = "openssl"
+version = "0.10.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8d9facdb76fec0b73c406f125d44d86fdad818d66fef0531eec9233ca425ff4a"
+dependencies = [
+ "bitflags",
+ "cfg-if 1.0.0",
+ "foreign-types",
+ "libc",
+ "once_cell",
+ "openssl-sys",
+]
+
+[[package]]
+name = "openssl-probe"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.67"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69df2d8dfc6ce3aaf44b40dec6f487d5a886516cf6879c49e98e0710f310a058"
+dependencies = [
+ "autocfg",
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
name = "output_vt100"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1673,6 +1836,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
+name = "pkg-config"
+version = "0.3.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7c9b1041b4387893b91ee6746cddfc28516aff326a3519fb2adf820932c5e6cb"
+
+[[package]]
name = "plotters"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2104,11 +2273,13 @@ dependencies = [
"http-body",
"hyper",
"hyper-rustls",
+ "hyper-tls",
"ipnet",
"js-sys",
"lazy_static",
"log",
"mime",
+ "native-tls",
"percent-encoding",
"pin-project-lite",
"rustls",
@@ -2116,6 +2287,7 @@ dependencies = [
"serde_json",
"serde_urlencoded",
"tokio",
+ "tokio-native-tls",
"tokio-rustls",
"url",
"wasm-bindgen",
@@ -2285,6 +2457,16 @@ dependencies = [
]
[[package]]
+name = "schannel"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75"
+dependencies = [
+ "lazy_static",
+ "winapi",
+]
+
+[[package]]
name = "scoped-tls"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2307,6 +2489,29 @@ dependencies = [
]
[[package]]
+name = "security-framework"
+version = "2.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467"
+dependencies = [
+ "bitflags",
+ "core-foundation",
+ "core-foundation-sys",
+ "libc",
+ "security-framework-sys",
+]
+
+[[package]]
+name = "security-framework-sys"
+version = "2.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a9dd14d83160b528b7bfd66439110573efcfbe281b17fc2ca9f39f550d619c7e"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
name = "segments"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2593,7 +2798,13 @@ dependencies = [
name = "tedge_apt_plugin"
version = "0.3.2"
dependencies = [
+ "anyhow",
+ "hamcrest2",
+ "reqwest",
+ "serial_test",
"structopt",
+ "tedge_utils",
+ "test-case",
"thiserror",
]
@@ -2839,6 +3050,16 @@ dependencies = [
]
[[package]]
+name = "tokio-native-tls"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
+dependencies = [
+ "native-tls",
+ "tokio",
+]
+
+[[package]]
name = "tokio-rustls"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3130,6 +3351,12 @@ dependencies = [
]
[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
+[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/sm/plugins/tedge_apt_plugin/Cargo.toml b/sm/plugins/tedge_apt_plugin/Cargo.toml
index 6d5bbef1..595d908a 100644
--- a/sm/plugins/tedge_apt_plugin/Cargo.toml
+++ b/sm/plugins/tedge_apt_plugin/Cargo.toml
@@ -16,3 +16,11 @@ assets = [
[dependencies]
structopt = "0.3"
thiserror = "1.0"
+
+[dev-dependencies]
+hamcrest2 = "0.3.0"
+serial_test = "0.5.1"
+test-case = "1.2.0"
+reqwest = { version = "0.11.4", features = [ "blocking" ] }
+tedge_utils = { path = "../../../common/tedge_utils" }
+anyhow = "1.0.44"
diff --git a/sm/plugins/tedge_apt_plugin/tests/main.rs b/sm/plugins/tedge_apt_plugin/tests/main.rs
new file mode 100644
index 00000000..c1fcb5b5
--- /dev/null
+++ b/sm/plugins/tedge_apt_plugin/tests/main.rs
@@ -0,0 +1,163 @@
+use hamcrest2::prelude::*;
+use serial_test::serial;
+use std::process::{Command, Stdio};
+use std::sync::Once;
+use tedge_utils::fs::atomically_write_file_sync;
+use test_case::test_case;
+
+const TEDGE_APT_COMMAND: &str = "/etc/tedge/sm-plugins/apt";
+const APT_COMMAND: &str = "/usr/bin/apt-get";
+const PACKAGE_NAME: &str = "rolldice";
+const PACKAGE_VERSION: &str = "1.16-1+b3";
+
+#[cfg(target_arch = "x86_64")]
+const PACKAGE_URL: &str =
+ "http://ftp.br.debian.org/debian/pool/main/r/rolldice/rolldice_1.16-1+b3_amd64.deb";
+
+#[cfg(target_arch = "x86_64")]
+const PACKAGE_FILE_PATH: &str = "/tmp/rolldice_1.16-1+b3_amd64.deb";
+
+#[cfg(target_arch = "aarch64")]
+const PACKAGE_URL: &str =
+ "http://ftp.br.debian.org/debian/pool/main/r/rolldice/rolldice_1.16-1+b3_arm64.deb";
+
+#[cfg(target_arch = "aarch64")]
+const PACKAGE_FILE_PATH: &str = "/tmp/rolldice_1.16-1+b3_arm64.deb";
+
+#[cfg(target_arch = "arm")]
+const PACKAGE_URL: &str =
+ "http://ftp.br.debian.org/debian/pool/main/r/rolldice/rolldice_1.16-1+b3_armhf.deb";
+
+#[cfg(target_arch = "arm")]
+const PACKAGE_FILE_PATH: &str = "/tmp/rolldice_1.16-1+b3_armhf.deb";
+
+static DOWNLOAD_PACKAGE_BINARY: Once = Once::new();
+
+pub fn download_package_binary_once() {
+ DOWNLOAD_PACKAGE_BINARY.call_once_force(|_state| {
+ simple_download(PACKAGE_URL);
+ });
+}
+
+fn simple_download(url: &str) {
+ let response = reqwest::blocking::get(url).unwrap();
+ let content = response.bytes().unwrap();
+
+ atomically_write_file_sync("/tmp/rolldice.deb", PACKAGE_FILE_PATH, content.as_ref()).unwrap();
+}
+
+/// executes a `cmd` with `args`
+/// returns the stdout, stderr and exit code
+fn run_cmd(cmd: &str, args: &str) -> anyhow::Result<(String, String, i32)> {
+ let args = args.split_whitespace().collect::<Vec<&str>>();
+ let output = Command::new(cmd)
+ .args(args)
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .output()?;
+
+ let stdout = String::from_utf8(output.stdout).unwrap();
+ let stderr = String::from_utf8(output.stderr).unwrap();
+ let status_code = output.status.code().unwrap();
+ Ok((stdout, stderr, status_code))
+}
+
+// Parameters:
+//
+// input command
+//
+// expected stderr
+//
+// expected exit code
+//
+// description about the test
+#[test_case(
+ &format!("install {} --file {}", PACKAGE_NAME, "wrong_path"),
+ "ERROR: Parsing Debian package failed",
+ 5
+ ; "wrong path"
+)]
+#[test_case(
+ &format!("install {} --file {} --module-version {}", PACKAGE_NAME, "not/a/package/path", PACKAGE_VERSION),
+ "ERROR: Parsing Debian package failed",
+ 5
+ ; "wrong path with right version"
+)]
+#[test_case(
+ &format!("install {} --file {} --module-version {}", PACKAGE_NAME, PACKAGE_FILE_PATH, "some_version"),
+ "ERROR: Parsing Debian package failed",
+ 5
+ ; "right path with wrong version"
+)]
+#[test_case(
+ &format!("install {} --file {} --module-version {}", PACKAGE_NAME, "not/a/package/path", "some_version"),
+ "ERROR: Parsing Debian package failed",
+ 5
+ ; "wrong path with wrong version"
+)]
+fn install_from_local_file_fail(
+ input_command: &str,
+ expected_stderr: &str,
+ expected_exit_code: i32,
+) -> anyhow::Result<()> {
+ // no setup needed, wrong arguments are provided to tedge apt plugin
+ let (stdout, stderr, exit_code) = run_cmd(TEDGE_APT_COMMAND, input_command)?;
+
+ // asserting command failed
+ assert_that!(stdout.is_empty(), true);
+ assert_that!(stderr, match_regex(expected_stderr));
+ assert_that!(exit_code, eq(expected_exit_code));
+ Ok(())
+}
+
+// Parameters:
+//
+// input command
+//
+// expected stderr
+//
+// expected exit code
+//
+// description about the test
+#[test_case(
+ &format!("install {} --file {}", PACKAGE_NAME, PACKAGE_FILE_PATH),
+ &format!("The following NEW packages will be installed\n {}", PACKAGE_NAME),
+ 0
+ ; "path"
+)]
+#[serial]
+#[test_case(
+ &format!("install {} --file {} --module-version {}", PACKAGE_NAME, PACKAGE_FILE_PATH, PACKAGE_VERSION),
+ &format!("The following NEW packages will be installed\n {}", PACKAGE_NAME),
+ 0
+ ; "path with version"
+)]
+#[serial]
+#[test_case(
+ &format!("install {} --module-version {} --file {}", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_FILE_PATH),
+ &format!("The following NEW packages will be installed\n {}", PACKAGE_NAME),
+ 0
+ ; "version with path"
+)]
+#[serial]
+fn install_from_local_file_success(
+ input_command: &str,
+ expected_stdout: &str,
+ expected_exit_code: i32,
+) -> anyhow::Result<()> {
+ // fetching the debian package & removing rolldice in case it is already installed.
+ // only executed once.
+ download_package_binary_once();
+ let _ = run_cmd(APT_COMMAND, &format!("remove {} -y", PACKAGE_NAME))?;
+
+ // execute command to install from local file
+ let (stdout, stderr, exit_code) = run_cmd(TEDGE_APT_COMMAND, input_command)?;
+
+ // asserting success
+ assert_that!(stdout, match_regex(expected_stdout));
+ assert_that!(stderr.is_empty(), true);
+ assert_that!(exit_code, eq(expected_exit_code));
+
+ Ok(())
+}