Skip to content

Commit

Permalink
add test for layer_count
Browse files Browse the repository at this point in the history
Signed-off-by: Vaughn Dice <[email protected]>
  • Loading branch information
vdice committed Oct 4, 2023
1 parent ebfbbfa commit 8617a83
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/oci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ tokio-util = "0.7.9"
tracing = { workspace = true }
walkdir = "2.3"

[dev-dependencies]
spin-testing = { path = "../testing" }
56 changes: 55 additions & 1 deletion crates/oci/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ fn digest_from_url(manifest_url: &str) -> Option<String> {
}

// TODO: good use case for DeployableApp addition in cloud-plugin? (Well, would want to move into spin to avoid circular deps)
// TODO: add test
async fn layer_count(locked: LockedApp) -> Result<usize> {
let mut layer_count = 0;
for c in locked.components {
Expand Down Expand Up @@ -539,4 +538,59 @@ mod test {
digest
);
}

#[tokio::test]
async fn can_get_layer_count() {
use spin_app::locked::LockedComponent;

let working_dir = tempfile::tempdir().unwrap();
let source_dir = working_dir.path().join("foo");
let _ = tokio::fs::create_dir(source_dir.as_path()).await;
let file_path = source_dir.join("bar");
let _ = tokio::fs::File::create(file_path.as_path()).await;

let tests: Vec<(Vec<LockedComponent>, usize)> = [
(
spin_testing::from_json!([{
"id": "test-component",
"source": {
"content_type": "application/wasm",
"digest": "test-source",
},
}]),
1,
),
(
spin_testing::from_json!([{
"id": "test-component",
"source": {
"content_type": "application/wasm",
"digest": "test-source",
},
"files": [
{
"source": format!("file://{}", file_path.to_str().unwrap()),
"path": ""
}
]
}]),
2,
),
]
.to_vec();

for (components, expected) in tests {
let triggers = Default::default();
let metadata = Default::default();
let variables = Default::default();
let locked = LockedApp {
spin_lock_version: spin_app::locked::FixedVersion,
components,
triggers,
metadata,
variables,
};
assert_eq!(expected, layer_count(locked).await.unwrap());
}
}
}
1 change: 1 addition & 0 deletions crates/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn init_tracing() {
}

// Convenience wrapper for deserializing from literal JSON
#[macro_export]
macro_rules! from_json {
($($json:tt)+) => {
serde_json::from_value(serde_json::json!($($json)+)).expect("valid json")
Expand Down

0 comments on commit 8617a83

Please sign in to comment.