Skip to content

Commit 2ad0bee

Browse files
committed
test: add docker-gated integration tests for hm cache save/restore
1 parent 65ef6e6 commit 2ad0bee

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

crates/hm/src/commands/cache/manifest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn tag_from_tar_name(filename: &str) -> Option<String> {
5454
}
5555

5656
#[cfg(test)]
57+
#[allow(clippy::unwrap_used, reason = "unit tests")]
5758
mod tests {
5859
use super::*;
5960

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//! Docker-gated integration test for `hm cache save` / `hm cache restore`.
2+
//!
3+
//! Run: `cargo test -p harmont-cli --features docker-integration -- --ignored cache`
4+
5+
#![cfg(feature = "docker-integration")]
6+
#![allow(
7+
clippy::unwrap_used,
8+
reason = "integration tests panic on unexpected failures"
9+
)]
10+
#![allow(
11+
clippy::expect_used,
12+
reason = "integration tests panic on unexpected failures"
13+
)]
14+
#![allow(
15+
clippy::ignore_without_reason,
16+
reason = "reason is in the test name and doc comment above"
17+
)]
18+
19+
use assert_cmd::Command;
20+
use predicates::str::contains;
21+
use tempfile::TempDir;
22+
23+
#[test]
24+
#[ignore]
25+
fn cache_save_creates_manifest() {
26+
let cache_dir = TempDir::new().unwrap();
27+
let cache_path = cache_dir.path();
28+
29+
let out = Command::cargo_bin("hm")
30+
.unwrap()
31+
.args(["cache", "save", cache_path.to_str().unwrap()])
32+
.assert()
33+
.success();
34+
35+
// manifest.json must exist
36+
let manifest_path = cache_path.join("manifest.json");
37+
assert!(manifest_path.exists(), "manifest.json should exist");
38+
39+
let content = std::fs::read_to_string(&manifest_path).unwrap();
40+
let manifest: serde_json::Value = serde_json::from_str(&content).unwrap();
41+
assert_eq!(manifest["version"], 1);
42+
43+
// stdout has the 16-char hex content hash
44+
let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
45+
let hash = stdout.trim();
46+
assert_eq!(hash.len(), 16, "content hash should be 16 hex chars");
47+
assert!(hash.chars().all(|c| c.is_ascii_hexdigit()), "should be hex");
48+
}
49+
50+
#[test]
51+
#[ignore]
52+
fn cache_save_is_deterministic() {
53+
let cache_dir = TempDir::new().unwrap();
54+
let path = cache_dir.path().to_str().unwrap();
55+
56+
let out1 = Command::cargo_bin("hm")
57+
.unwrap()
58+
.args(["cache", "save", path])
59+
.assert()
60+
.success();
61+
let out2 = Command::cargo_bin("hm")
62+
.unwrap()
63+
.args(["cache", "save", path])
64+
.assert()
65+
.success();
66+
67+
let h1 = String::from_utf8(out1.get_output().stdout.clone()).unwrap();
68+
let h2 = String::from_utf8(out2.get_output().stdout.clone()).unwrap();
69+
assert_eq!(h1.trim(), h2.trim(), "content hash should be deterministic");
70+
}
71+
72+
#[test]
73+
#[ignore]
74+
fn cache_restore_after_save() {
75+
let cache_dir = TempDir::new().unwrap();
76+
let path = cache_dir.path().to_str().unwrap();
77+
78+
// Save first
79+
Command::cargo_bin("hm")
80+
.unwrap()
81+
.args(["cache", "save", path])
82+
.assert()
83+
.success();
84+
85+
// Restore — all images already present
86+
Command::cargo_bin("hm")
87+
.unwrap()
88+
.args(["cache", "restore", path])
89+
.assert()
90+
.success()
91+
.stderr(contains("already present"));
92+
}
93+
94+
#[test]
95+
#[ignore]
96+
fn cache_restore_missing_dir() {
97+
Command::cargo_bin("hm")
98+
.unwrap()
99+
.args(["cache", "restore", "/tmp/harmont-nonexistent-cache-dir-test"])
100+
.assert()
101+
.success()
102+
.stderr(contains("0/0"));
103+
}

0 commit comments

Comments
 (0)