Skip to content

Commit

Permalink
test: some tests added for the cli part
Browse files Browse the repository at this point in the history
Also using workspace=true
  • Loading branch information
helio-frota authored and dejanb committed Apr 23, 2024
1 parent 855afd9 commit 731e40a
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 25 deletions.
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 9 additions & 9 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
186 changes: 186 additions & 0 deletions cli/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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/[email protected]")
.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);
}
32 changes: 16 additions & 16 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
tonic-build = { workspace = true }

0 comments on commit 731e40a

Please sign in to comment.