From 731e40ad8e01c80149e3019e82ea520a865a3b68 Mon Sep 17 00:00:00 2001 From: Helio Frota <00hf11@gmail.com> Date: Wed, 21 Feb 2024 20:06:06 -0300 Subject: [PATCH] test: some tests added for the cli part Also using workspace=true --- Cargo.toml | 23 ++++++ cli/Cargo.toml | 18 ++--- cli/tests/cli.rs | 186 +++++++++++++++++++++++++++++++++++++++++++++++ lib/Cargo.toml | 32 ++++---- 4 files changed, 234 insertions(+), 25 deletions(-) create mode 100644 cli/tests/cli.rs diff --git a/Cargo.toml b/Cargo.toml index 6c74af5..641291a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,26 @@ members = [ "cli", ] resolver = "2" + +[workspace.dependencies] +anyhow = "1.0.39" +async-nats = "0.33.0" +async-trait = "0.1" +chrono = "0.4.23" +clap = "4.5.0" +colored_json = "4.1.0" +env_logger = "0.11.1" +exporter = { git = "https://github.com/trustification/trustification.git", tag = "v0.1.0-nightly.9382a428" } +graphql_client = "0.13.0" +humantime = "2.1.0" +packageurl = "0.3.0" +prost = "0.12.3" +reqwest = "0.11" +serde = "1.0.114" +serde_json = "1.0.56" +strum = "0.26.1" +strum_macros = "0.26.1" +thiserror = "1" +tokio = "1.36.0" +tonic = "0.11.0" +tonic-build = "0.11.0" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 3205960..1e65cb2 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -9,15 +9,15 @@ description = "A command-line interface for working with [Guac](https://guac.sh) [dependencies] guac = { path = "../lib" } -anyhow = "1.0.39" -tokio = { version = "1", features = ["macros", "rt-multi-thread"] } -serde_json = "1.0.56" -colored_json = "4.1.0" -clap = { version = "4.5.0", features = ["derive"] } -exporter = { git = "https://github.com/trustification/trustification.git", tag="v0.1.0-nightly.9382a428"} -env_logger = "0.11.1" -humantime = "2.1.0" -packageurl = "0.3.0" +anyhow = { workspace = true } +tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } +serde_json = { workspace = true } +colored_json = { workspace = true } +clap = { workspace = true, features = ["derive"] } +exporter = { workspace = true } +env_logger = { workspace = true } +humantime = { workspace = true } +packageurl = { workspace = true } [[bin]] name = "guac" diff --git a/cli/tests/cli.rs b/cli/tests/cli.rs new file mode 100644 index 0000000..58581cd --- /dev/null +++ b/cli/tests/cli.rs @@ -0,0 +1,186 @@ +use std::{env, fs}; +use tokio::process::Command; + +const GUAC_URL: &str = "http://localhost:8085/query"; + +#[tokio::test] +async fn test_cli_should_fail() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")).output().await.unwrap(); + // [expected], [output] + assert_eq!(Some(2), output.status.code()); +} + +#[tokio::test] +async fn test_query_dependencies() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("dependencies") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-pkg-A@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let out = String::from_utf8(output.stdout).unwrap(); + let expected = "[]\n"; + assert_eq!(expected, out); +} + +#[tokio::test] +async fn test_query_dependents() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("dependents") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-pkg-A@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let out = String::from_utf8(output.stdout).unwrap(); + assert!(out.contains("trustification-pkg-B")); +} + +#[tokio::test] +async fn test_query_bad() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("bad") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-certify-bad@0.3.0") + .output() + .await + .unwrap(); + + let out = String::from_utf8(output.stdout).unwrap(); + assert!(output.status.success()); + assert!(out.contains("test-justification")); +} + +#[tokio::test] +async fn test_query_good() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("good") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-certify-good@0.3.0") + .output() + .await + .unwrap(); + + let out = String::from_utf8(output.stdout).unwrap(); + assert!(output.status.success()); + assert!(out.contains("test-justification")); +} + +#[tokio::test] +async fn test_query_packages() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("packages") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-test") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let out = String::from_utf8(output.stdout).unwrap(); + assert!(out.contains("0.3.0")); + assert!(out.contains("0.3.1")); +} + +#[tokio::test] +async fn test_query_vulnerabilities() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("vulnerabilities") + .arg(format!("-g {}", &GUAC_URL)) + .arg("pkg:rpm/trustification-certify-good@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let expected = ""; + let out = String::from_utf8(output.stdout).unwrap(); + assert_eq!(expected, out); +} + +#[tokio::test] +async fn test_query_vulnerabilities_vex_enabled() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("query") + .arg("vulnerabilities") + .arg(format!("-g {}", &GUAC_URL)) + .arg("-v") + .arg("pkg:rpm/trustification-certify-good@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let expected = ""; + let out = String::from_utf8(output.stdout).unwrap(); + assert_eq!(expected, out); +} + +#[tokio::test] +async fn test_certify_good() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("certify") + .arg("good") + .arg(format!("-g {}", &GUAC_URL)) + .arg("--justification") + .arg("lgtm") + .arg("pkg:rpm/trustification-certify-good@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let expected = ""; + let out = String::from_utf8(output.stdout).unwrap(); + assert_eq!(expected, out); +} + +#[tokio::test] +async fn test_certify_bad() { + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("certify") + .arg("bad") + .arg(format!("-g {}", &GUAC_URL)) + .arg("--justification") + .arg("not good") + .arg("pkg:rpm/trustification-certify-good@0.3.0") + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let expected = ""; + let out = String::from_utf8(output.stdout).unwrap(); + assert_eq!(expected, out); +} + +#[tokio::test] +async fn test_collect_file() { + let path = fs::canonicalize("../example/seedwing-java-example.bom") + .unwrap() + .to_string_lossy() + .into_owned(); + let output = Command::new(env!("CARGO_BIN_EXE_guac")) + .arg("collect") + .arg("file") + .arg(path.clone()) + .output() + .await + .unwrap(); + + assert!(output.status.success()); + let expected = format!("Collecting file \"{}\"\n", path); + let out = String::from_utf8(output.stdout).unwrap(); + assert_eq!(expected, out); +} diff --git a/lib/Cargo.toml b/lib/Cargo.toml index ab4002b..e18dfa4 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -8,23 +8,23 @@ description = "A library that provides toolkit for working with [Guac](https://g # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -graphql_client = {version = "0.13.0", features = ["reqwest"]} -reqwest = { version = "^0.11", features = ["json"] } -anyhow = "1.0.39" -serde = "1.0.114" -chrono = { version = "0.4.23", features = ["serde"] } -packageurl = { version = "0.3.0", features = ["serde"] } -async-trait = "0.1" -async-nats = "0.33.0" -serde_json = "1.0.56" -thiserror = "1" -tonic = "0.11.0" -prost = "0.12.3" -strum = "0.26.1" -strum_macros = "0.26.1" +graphql_client = { workspace = true, features = ["reqwest"] } +reqwest = { workspace = true, features = ["json"] } +anyhow = { workspace = true } +serde = { workspace = true } +chrono = { workspace = true, features = ["serde"] } +packageurl = { workspace = true, features = ["serde"] } +async-trait = { workspace = true } +async-nats = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +tonic = { workspace = true } +prost = { workspace = true } +strum = { workspace = true } +strum_macros = { workspace = true } [dev-dependencies] -tokio = "1.36.0" +tokio = { workspace = true } [build-dependencies] -tonic-build = "0.11.0" \ No newline at end of file +tonic-build = { workspace = true }