Skip to content

Commit

Permalink
fix: use the test context teardown fn to delete fixtures
Browse files Browse the repository at this point in the history
Almost fixes #587 in that it will handle panics within the test
bodies, but it will not handle a panic as a result of the
ntest::timeout macro.

Signed-off-by: Jim Crossley <jim@crossleys.org>
  • Loading branch information
jcrossley3 authored and Ulf Lilleengen committed Sep 28, 2023
1 parent 90d82d3 commit 180e9eb
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 92 deletions.
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ clap = { version = "4", features = ["derive"] }

prometheus = "0.13.3"
uuid = { version = "1", features = ["v4"] }
urlencoding = "2.1.2"

[dev-dependencies]
env_logger = "0.10"
urlencoding = "2.1.2"
spog-model = { path = "../spog/model" }

[features]
Expand Down
55 changes: 33 additions & 22 deletions integration-tests/src/bom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ impl AsyncTestContext for BombasticContext {
let config = Config::new().await;
start_bombastic(&config).await
}
async fn teardown(self) {
for id in &self.fixtures {
self.delete_sbom(id).await;
}
}
}

impl Urlifier for BombasticContext {
Expand All @@ -23,6 +28,7 @@ pub struct BombasticContext {
pub provider: ProviderContext,
pub events: EventBusConfig,
_runner: Option<Runner>,
fixtures: Vec<String>,
}

pub async fn start_bombastic(config: &Config) -> BombasticContext {
Expand All @@ -34,6 +40,7 @@ pub async fn start_bombastic(config: &Config) -> BombasticContext {
provider: config.provider().await,
events: config.events(),
_runner: None,
fixtures: Vec::new(),
};
}

Expand Down Expand Up @@ -80,6 +87,7 @@ pub async fn start_bombastic(config: &Config) -> BombasticContext {
provider: config.provider().await,
events,
_runner: Some(runner),
fixtures: Vec::new(),
};

// ensure it's initialized
Expand All @@ -104,29 +112,32 @@ pub async fn start_bombastic(config: &Config) -> BombasticContext {
}
}

pub async fn upload_sbom(context: &BombasticContext, key: &str, input: &serde_json::Value) {
let response = reqwest::Client::new()
.post(context.urlify(format!("/api/v1/sbom?id={key}")))
.json(input)
.inject_token(&context.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
}
impl BombasticContext {
pub async fn upload_sbom(&mut self, key: &str, input: &serde_json::Value) {
let response = reqwest::Client::new()
.post(self.urlify(format!("/api/v1/sbom?id={key}")))
.json(input)
.inject_token(&self.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
self.fixtures.push(key.to_string());
}

pub async fn delete_sbom(context: &BombasticContext, key: &str) {
let response = reqwest::Client::new()
.delete(context.urlify(format!("/api/v1/sbom?id={key}")))
.inject_token(&context.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
pub async fn delete_sbom(&self, key: &str) {
let response = reqwest::Client::new()
.delete(self.urlify(format!("/api/v1/sbom?id={key}")))
.inject_token(&self.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
}
}

pub async fn wait_for_search_result<F: Fn(serde_json::Value) -> bool>(
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/src/spog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ impl AsyncTestContext for SpogContext {
let config = Config::new().await;
start_spog(&config).await
}
async fn teardown(self) {
self.bombastic.teardown().await;
self.vexination.teardown().await;
}
}

impl Urlifier for SpogContext {
Expand Down
58 changes: 35 additions & 23 deletions integration-tests/src/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ impl AsyncTestContext for VexinationContext {
let config = Config::new().await;
start_vexination(&config).await
}
async fn teardown(self) {
for id in &self.fixtures {
self.delete_vex(id).await;
}
}
}

impl Urlifier for VexinationContext {
Expand All @@ -22,6 +27,7 @@ pub struct VexinationContext {
pub provider: ProviderContext,
pub events: EventBusConfig,
_runner: Option<Runner>,
fixtures: Vec<String>,
}

pub async fn start_vexination(config: &Config) -> VexinationContext {
Expand All @@ -33,6 +39,7 @@ pub async fn start_vexination(config: &Config) -> VexinationContext {
provider: config.provider().await,
events: config.events(),
_runner: None,
fixtures: Vec::new(),
};
}
#[cfg(not(feature = "with-services"))]
Expand Down Expand Up @@ -80,6 +87,7 @@ pub async fn start_vexination(config: &Config) -> VexinationContext {
provider,
events,
_runner: Some(runner),
fixtures: Vec::new(),
};

// ensure it's initialized
Expand All @@ -104,31 +112,35 @@ pub async fn start_vexination(config: &Config) -> VexinationContext {
}
}

pub async fn upload_vex(context: &VexinationContext, input: &serde_json::Value) {
let response = reqwest::Client::new()
.post(context.urlify("/api/v1/vex"))
.json(input)
.inject_token(&context.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
}
impl VexinationContext {
pub async fn upload_vex(&mut self, input: &serde_json::Value) {
let response = reqwest::Client::new()
.post(self.urlify("/api/v1/vex"))
.json(input)
.inject_token(&self.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
let id = input["document"]["tracking"]["id"].as_str().unwrap().to_string();
self.fixtures.push(id);
}

pub async fn delete_vex(context: &VexinationContext, key: &str) {
let response = reqwest::Client::new()
.delete(context.urlify(format!("/api/v1/vex?advisory={key}")))
.inject_token(&context.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
pub async fn delete_vex(&self, key: &str) {
let id = urlencoding::encode(key);
let response = reqwest::Client::new()
.delete(self.urlify(format!("/api/v1/vex?advisory={id}")))
.inject_token(&self.provider.provider_manager)
.await
.unwrap()
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
}
}

// Configuration for the vexination indexer
#[cfg(feature = "with-services")]
fn vexination_indexer() -> vexination_indexer::Run {
Expand Down
39 changes: 15 additions & 24 deletions integration-tests/tests/bombastic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use integration_tests::{
delete_sbom, get_response, id, upload_sbom, wait_for_event, wait_for_search_result, BombasticContext, Urlifier,
};
use integration_tests::{get_response, id, wait_for_event, wait_for_search_result, BombasticContext, Urlifier};
use reqwest::StatusCode;
use serde_json::{json, Value};
use test_context::test_context;
Expand All @@ -15,7 +13,7 @@ use urlencoding::encode;
async fn upload_happy_sbom(context: &mut BombasticContext) {
let input = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-upload";
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let response = reqwest::Client::new()
.get(context.urlify(format!("/api/v1/sbom?id={id}")))
.inject_token(&context.provider.provider_manager)
Expand All @@ -27,7 +25,6 @@ async fn upload_happy_sbom(context: &mut BombasticContext) {
assert_eq!(response.status(), StatusCode::OK);
let output: Value = response.json().await.unwrap();
assert_eq!(input, output);
delete_sbom(context, id).await;
}

#[test_context(BombasticContext)]
Expand All @@ -36,7 +33,7 @@ async fn upload_happy_sbom(context: &mut BombasticContext) {
async fn delete_happy_sbom(context: &mut BombasticContext) {
let input = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-delete";
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let url = context.urlify(format!("/api/v1/sbom?id={id}"));
let client = reqwest::Client::new();
let response = client
Expand Down Expand Up @@ -152,7 +149,7 @@ async fn bombastic_search(context: &mut BombasticContext) {
// we generate a unique id and use it as the SBOM's version for searching
let key = id("test-search");
input["packages"][617]["versionInfo"] = json!(key);
upload_sbom(context, &key, &input).await;
context.upload_sbom(&key, &input).await;

wait_for_search_result(context, &[("q", &encode(&key))], |response| {
if response["total"].as_u64().unwrap() >= 1 {
Expand All @@ -163,7 +160,6 @@ async fn bombastic_search(context: &mut BombasticContext) {
}
})
.await;
delete_sbom(context, &key).await;
}

#[test_context(BombasticContext)]
Expand Down Expand Up @@ -196,7 +192,7 @@ async fn bombastic_reindexing(context: &mut BombasticContext) {
// we generate a unique id and use it as the SBOM's version for searching
let key = id("test-reindexing");
input["packages"][617]["versionInfo"] = json!(key);
upload_sbom(context, &key, &input).await;
context.upload_sbom(&key, &input).await;

wait_for_search_result(context, &[("q", &encode(&key))], |response| {
if response["total"].as_u64().unwrap() >= 1 {
Expand All @@ -211,7 +207,7 @@ async fn bombastic_reindexing(context: &mut BombasticContext) {
let now = OffsetDateTime::now_utc();

// Push update and check reindex
upload_sbom(context, &key, &input).await;
context.upload_sbom(&key, &input).await;

wait_for_search_result(context, &[("q", &encode(&key)), ("metadata", "true")], |response| {
assert!(response["total"].as_u64().unwrap() >= 1);
Expand All @@ -226,7 +222,6 @@ async fn bombastic_reindexing(context: &mut BombasticContext) {
ts > now
})
.await;
delete_sbom(context, &key).await;
}

#[test_context(BombasticContext)]
Expand All @@ -236,7 +231,7 @@ async fn bombastic_deletion(context: &mut BombasticContext) {
let mut input: Value = serde_json::from_str(include_str!("../../bombastic/testdata/ubi9-sbom.json")).unwrap();
let key = id("test-index-deletion");
input["packages"][617]["versionInfo"] = json!(key);
upload_sbom(context, &key, &input).await;
context.upload_sbom(&key, &input).await;

wait_for_search_result(context, &[("q", &encode(&key))], |response| {
if response["total"].as_u64().unwrap() >= 1 {
Expand All @@ -248,7 +243,7 @@ async fn bombastic_deletion(context: &mut BombasticContext) {
})
.await;

delete_sbom(context, &key).await;
context.delete_sbom(&key).await;

wait_for_search_result(context, &[("q", &encode(&key))], |response| {
response["total"].as_u64().unwrap() == 1
Expand Down Expand Up @@ -300,20 +295,19 @@ async fn upload_sbom_existing_without_change(context: &mut BombasticContext) {
let input = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-upload-without-change";
let api_end_point = context.urlify(format!("api/v1/sbom?id={id}"));
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let response: Value = get_response(&api_end_point, StatusCode::OK, &context.provider)
.await
.into();
assert_eq!(input, response, "Content mismatch between request and response");
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let response: Value = get_response(&api_end_point, StatusCode::OK, &context.provider)
.await
.into();
assert_eq!(
input, response,
"Content mismatch between request and response after update"
);
delete_sbom(context, id).await;
}

#[test_context(BombasticContext)]
Expand All @@ -323,21 +317,20 @@ async fn upload_sbom_existing_with_change(context: &mut BombasticContext) {
let mut input1 = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-upload-with-change";
let api_end_point = context.urlify(format!("api/v1/sbom?id={id}"));
upload_sbom(context, id, &input1).await;
context.upload_sbom(id, &input1).await;
let response1: Value = get_response(&api_end_point, StatusCode::OK, &context.provider)
.await
.into();
assert_eq!(input1, response1, "Content mismatch between request and response");
input1["metadata"]["component"]["name"] = Value::String(String::from("update-sbom-name"));
upload_sbom(context, id, &input1).await;
context.upload_sbom(id, &input1).await;
let response2: Value = get_response(&api_end_point, StatusCode::OK, &context.provider)
.await
.into();
assert_eq!(
input1, response2,
"Content mismatch between request and response after update"
);
delete_sbom(context, id).await;
}

#[ignore = "until API can support failures so that event bus is not needed to check"]
Expand All @@ -362,7 +355,7 @@ async fn sbom_upload_empty_json(context: &mut BombasticContext) {
assert_eq!(response.status(), StatusCode::CREATED);
})
.await;
delete_sbom(context, id).await;
context.delete_sbom(id).await;
}

#[ignore = "until API can support failures so that event bus is not needed to check"]
Expand Down Expand Up @@ -423,7 +416,7 @@ async fn sbom_upload_unauthorized(context: &mut BombasticContext) {
async fn sbom_delete_user_not_allowed(context: &mut BombasticContext) {
let input: serde_json::Value = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-delete-user-not-allowed";
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let response = reqwest::Client::new()
.delete(context.urlify(format!("api/v1/sbom?id={id}")))
.json(&input)
Expand All @@ -434,7 +427,6 @@ async fn sbom_delete_user_not_allowed(context: &mut BombasticContext) {
.await
.unwrap();
assert_eq!(response.status(), StatusCode::FORBIDDEN);
delete_sbom(context, id).await;
}

#[test_context(BombasticContext)]
Expand All @@ -443,15 +435,14 @@ async fn sbom_delete_user_not_allowed(context: &mut BombasticContext) {
async fn sbom_delete_unauthorized(context: &mut BombasticContext) {
let input: serde_json::Value = serde_json::from_str(include_str!("../../bombastic/testdata/my-sbom.json")).unwrap();
let id = "test-delete-unauthorized";
upload_sbom(context, id, &input).await;
context.upload_sbom(id, &input).await;
let response = reqwest::Client::new()
.delete(context.urlify(format!("api/v1/sbom?id={id}")))
.json(&input)
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
delete_sbom(context, id).await;
}

#[test_context(BombasticContext)]
Expand Down

0 comments on commit 180e9eb

Please sign in to comment.