Skip to content

Commit

Permalink
test: add Store::list integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito committed Aug 29, 2023
1 parent 41e940f commit edd3bb2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use std::fmt::Display;
use std::path::PathBuf;

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct Policy {
pub uri: String,
pub local_path: PathBuf,
Expand Down
60 changes: 60 additions & 0 deletions tests/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::path::Path;

use anyhow::Result;
use policy_fetcher::policy::Policy;
use policy_fetcher::store::{path, Store};
use tempfile::tempdir;

#[test]
fn test_list() -> Result<()> {
let store_root = tempdir()?;

let mut expected_policies = vec![
Policy {
uri: "https://internal.host.company/some/path/to/1.0.0/wasm-module.wasm".to_owned(),
local_path: store_root.path().join(path::encode_path(
"https/internal.host.company/some/path/to/1.0.0/wasm-module.wasm",
)),
},
Policy {
uri: "registry://ghcr.io/some/path/to/wasm-module.wasm:1.0.0".to_owned(),
local_path: store_root.path().join(path::encode_path(
"registry/ghcr.io/some/path/to/wasm-module.wasm:1.0.0",
)),
},
Policy {
uri: "registry://internal.host.company:5000/some/path/to/wasm-module.wasm:1.0.0"
.to_owned(),
local_path: store_root.path().join(path::encode_path(
"registry/internal.host.company:5000/some/path/to/wasm-module.wasm:1.0.0",
)),
},
];

setup_store(&expected_policies)?;

let store = Store::new(store_root.path());
let mut list = store.list()?;

expected_policies.sort_by_key(|p| p.uri.clone());
list.sort_by_key(|p| p.uri.clone());

assert_eq!(expected_policies, list);

Ok(())
}

/// Setup a temporary store with the given policies.
/// By using tempdir, the store will be cleaned up automatically when the TempDir struct is dropped.
fn setup_store(policies: &[Policy]) -> Result<()> {
for policy in policies {
std::fs::create_dir_all(policy.local_path.parent().unwrap())?;

std::fs::copy(
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/test_data/simple.wasm"),
&policy.local_path,
)?;
}

Ok(())
}

0 comments on commit edd3bb2

Please sign in to comment.