From f3bf63a76b2a8c2d5f5734e99f88ed78a333fdfe Mon Sep 17 00:00:00 2001 From: Robert Detjens Date: Sat, 21 Sep 2024 14:03:30 -0700 Subject: [PATCH] basic registry access integration tests Signed-off-by: Robert Detjens --- Cargo.lock | 177 +++++++++++++++++++++++----- Cargo.toml | 1 + tests/check_access/main.rs | 1 + tests/check_access/test_registry.rs | 137 +++++++++++++++++++++ tests/common.rs | 40 +++++++ tests/config_parsing.rs | 14 +++ 6 files changed, 340 insertions(+), 30 deletions(-) create mode 100644 tests/check_access/main.rs create mode 100644 tests/check_access/test_registry.rs create mode 100644 tests/common.rs create mode 100644 tests/config_parsing.rs diff --git a/Cargo.lock b/Cargo.lock index de9c19c..e439157 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -212,6 +212,7 @@ dependencies = [ "bollard", "clap", "clap-verbosity-flag", + "cmd_lib", "fully_pub", "futures-util", "itertools", @@ -334,7 +335,7 @@ dependencies = [ "iana-time-zone", "num-traits", "serde", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -411,6 +412,32 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +[[package]] +name = "cmd_lib" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718f77610af91e4d648fe9da0150ae58698cbcfb93bf63e764907fbefd56ffe8" +dependencies = [ + "cmd_lib_macros", + "env_logger", + "faccess", + "lazy_static", + "log", + "os_pipe", +] + +[[package]] +name = "cmd_lib_macros" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a80fac05ed12fe97a70b5dfdd910c9b90b53f4de69002c3179e29ac2d066abc" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "colorchoice" version = "1.0.1" @@ -590,6 +617,19 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -617,6 +657,17 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "faccess" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ae66425802d6a903e268ae1a08b8c38ba143520f227a205edf4e9c7e3e26d5" +dependencies = [ + "bitflags 1.3.2", + "libc", + "winapi", +] + [[package]] name = "fnv" version = "1.0.7" @@ -798,6 +849,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "hex" version = "0.4.3" @@ -862,6 +919,12 @@ dependencies = [ "libm", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.3.1" @@ -1049,6 +1112,17 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi 0.4.0", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -1323,7 +1397,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] @@ -1366,6 +1440,16 @@ dependencies = [ "num-traits", ] +[[package]] +name = "os_pipe" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffd2b0a5634335b135d5728d84c5e0fd726954b87111f7506a61c502280d982" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "paris" version = "1.5.15" @@ -1398,7 +1482,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -1553,6 +1637,30 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.82" @@ -2537,7 +2645,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -2555,7 +2663,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -2575,18 +2692,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -2597,9 +2714,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -2609,9 +2726,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -2621,15 +2738,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -2639,9 +2756,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -2651,9 +2768,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -2663,9 +2780,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -2675,9 +2792,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "zerocopy" diff --git a/Cargo.toml b/Cargo.toml index 31f0c45..5cf4655 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ k8s-openapi = { version = "0.22.0", features = ["latest"] } tokio = { version = "1.38.0", features = ["rt", "macros"] } bollard = "0.16.1" futures-util = "0.3.30" +cmd_lib = "1.9.4" diff --git a/tests/check_access/main.rs b/tests/check_access/main.rs new file mode 100644 index 0000000..faedbd8 --- /dev/null +++ b/tests/check_access/main.rs @@ -0,0 +1 @@ +mod test_registry; diff --git a/tests/check_access/test_registry.rs b/tests/check_access/test_registry.rs new file mode 100644 index 0000000..1a5d952 --- /dev/null +++ b/tests/check_access/test_registry.rs @@ -0,0 +1,137 @@ +use anyhow::{Error, Result}; +use beavercds_ng::{self, configparser::config::*}; +use cmd_lib::run_cmd; +use common::empty_config; + +// crate:: or super:: doesnt see this from in a subfolder +#[path = "../common.rs"] +mod common; + +// +// Integration tests with real registry +// + +/// Set up local registry container to test against, return "domain:port" +fn registry_setup(creds: &UserPass) { + // shell out to docker to create registry + // overwrite entrypoint to set up credentials first + let (user, pass) = (&creds.user, &creds.pass); + run_cmd!( + docker run + -d + --publish="5000:5000" + --name="beavercds_test_registry" + --entrypoint="" + --env="REGISTRY_AUTH=htpasswd" + --env="REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" + --env="REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" + registry:2 + sh -c "htpasswd -Bbs $user $pass > /auth/htpasswd && registry serve" + ) + .unwrap(); +} + +/// clean up registry container after tests (if running) +fn registry_teardown() { + // run_cmd!(docker rm -f beavercds_test_registry).unwrap(); + run_cmd!( + info "waiting..."; + sleep 5; + docker rm -f beavercds_test_registry) + .unwrap(); +} + +/// Config is OK and registry behaves as normal +#[test] +fn no_problems() { + let creds = UserPass { + user: "user".to_string(), + pass: "pass".to_string(), + }; + + registry_setup(&creds); + + // generate fake config with credentials + beavercds_ng::configparser::CONFIG + .set(RcdsConfig { + registry: Registry { + domain: "localhost:5000".to_string(), + build: creds.clone(), + cluster: creds.clone(), + }, + ..empty_config() + }) + .unwrap(); + + // run check-access --registry against test registry + let check_result = beavercds_ng::access_handlers::docker::check("default"); + + registry_teardown(); + + assert!(check_result.is_ok()) +} + +/// Invalid credentials +#[test] +fn bad_credentials() { + let creds = UserPass { + user: "user".to_string(), + pass: "pass".to_string(), + }; + + let bad_creds = UserPass { + user: "bogus".to_string(), + pass: "hunter2".to_string(), + }; + + registry_setup(&creds); + + // generate fake config with credentials + beavercds_ng::configparser::CONFIG + .set(RcdsConfig { + registry: Registry { + domain: "localhost:5000".to_string(), + build: bad_creds.clone(), + cluster: bad_creds.clone(), + }, + ..empty_config() + }) + .unwrap(); + + // run check-access --registry against test registry with bad creds + let check_result = beavercds_ng::access_handlers::docker::check("default"); + + registry_teardown(); + + assert!(check_result.is_err()) +} + +/// Registry is unreachable +#[test] +fn bad_registry() { + let creds = UserPass { + user: "user".to_string(), + pass: "pass".to_string(), + }; + + // *don't* set up the registry this time + + // generate fake config with credentials + beavercds_ng::configparser::CONFIG + .set(RcdsConfig { + registry: Registry { + domain: "localhost:5000".to_string(), + build: creds.clone(), + cluster: creds.clone(), + }, + ..empty_config() + }) + .unwrap(); + + // run check-access --registry against non-existent registry + let check_result = beavercds_ng::access_handlers::docker::check("default"); + + registry_teardown(); + + assert!(check_result.is_err()) +} diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..e128de3 --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,40 @@ +// Shared common helper functions, mock structs, etc + +use beavercds_ng::configparser::config::*; +use std::collections::BTreeMap; + +/// Build empty rcds.yaml config +pub fn empty_config() -> RcdsConfig { + RcdsConfig { + flag_regex: "".to_string(), + registry: Registry { + domain: "".to_string(), + build: UserPass { + user: "".to_string(), + pass: "".to_string(), + }, + cluster: UserPass { + user: "".to_string(), + pass: "".to_string(), + }, + }, + points: vec![], + defaults: Defaults { + difficulty: 0, + resources: Resource { + cpu: 0, + memory: "0".to_string(), + }, + }, + profiles: BTreeMap::from([( + "default".to_string(), + ProfileConfig { + frontend_url: "".to_string(), + frontend_token: None, + challenges_domain: "".to_string(), + kubeconfig: None, + kubecontext: "".to_string(), + }, + )]), + } +} diff --git a/tests/config_parsing.rs b/tests/config_parsing.rs new file mode 100644 index 0000000..ec94165 --- /dev/null +++ b/tests/config_parsing.rs @@ -0,0 +1,14 @@ +// Integration tests to test a whole repo's configs at once + +// TODO: is this worth integration tests? should these be all unit tests? +// #[test] +// fn good() {} + +// #[test] +// fn missing_registry() {} + +// #[test] +// fn no_challenges() {} + +// #[test] +// fn challenge_missing_fields() {}