use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use tempfile::TempDir;
use super::find_archive;
use crate::chart::Chart;
use crate::command::{ChartPathOptions, ChartRef, Pull, Pulled};
use crate::error::Error;
use crate::helm::Helm;
use crate::mock::Mock;
fn signed() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/commands/fetch/signed")
}
fn local() -> Chart {
Chart::load(signed().join("demo-0.1.0.tgz")).expect("fixture loads")
}
fn puller(name: &'static str, prov: bool, fail: bool) -> (Helm<Mock>, Arc<Mutex<Vec<PathBuf>>>) {
let seen = Arc::new(Mutex::new(Vec::new()));
let record = Arc::clone(&seen);
let mock = Mock::new().on::<Pull>(move |_, pull| {
let destination = pull.destination.clone().expect("destination");
fs::copy(signed().join("demo-0.1.0.tgz"), destination.join(name)).expect("archive");
if prov {
fs::write(destination.join(format!("{name}.prov")), "signature").expect("prov");
}
record.lock().expect("lock").push(destination);
if fail {
return Err(Error::InvalidCommand("pull failed".to_owned()));
}
Ok(Pulled::default())
});
(Helm::new(mock), seen)
}
fn remote() -> ChartRef {
ChartRef::Repo("repo/demo".to_owned())
}
#[tokio::test]
async fn a_path_loads_natively_without_running_helm() {
let helm = Helm::new(Mock::new());
let options = ChartPathOptions::default();
let archive = ChartRef::Path(signed().join("demo-0.1.0.tgz"));
let missing = ChartRef::Path(signed().join("missing"));
let fetched = Chart::fetch(&helm, &archive, &options).await;
assert_eq!(fetched.expect("archive loads"), local());
assert!(matches!(
Chart::fetch(&helm, &missing, &options).await,
Err(Error::Chart(_))
));
assert_eq!(helm.backend().calls::<Pull>().len(), 0);
}
#[tokio::test]
async fn a_remote_chart_is_pulled_into_a_private_directory_that_is_removed() {
let options = ChartPathOptions::builder().version("0.1.0").build();
for (name, prov) in [("demo-0.1.0.tgz", false), ("renamed.tgz", true)] {
let (helm, seen) = puller(name, prov, false);
let fetched = Chart::fetch(&helm, &remote(), &options).await;
let pulls = helm.backend().calls::<Pull>();
let destination = seen.lock().expect("lock").remove(0);
assert_eq!(fetched.expect("fetches"), local(), "{name}");
assert_eq!(pulls.len(), 1);
assert_eq!(pulls[0].chart, remote());
assert_eq!(pulls[0].chart_path, options);
assert!(!destination.exists(), "{name}: temp dir left behind");
}
}
#[tokio::test]
async fn a_failed_pull_leaves_no_directory_behind() {
let (helm, seen) = puller("demo-0.1.0.tgz", false, true);
let fetched = Chart::fetch(&helm, &remote(), &ChartPathOptions::default()).await;
let destination = seen.lock().expect("lock").remove(0);
assert!(matches!(fetched, Err(Error::InvalidCommand(_))));
assert!(!destination.exists());
}
#[test]
fn the_archive_is_the_one_file_that_is_not_the_provenance() {
let dir = TempDir::new().expect("temp dir");
let write = |name: &str| fs::write(dir.path().join(name), "x").expect("write");
let found = |dir: &Path| find_archive(dir).map(|path| path.file_name().map(PathBuf::from));
assert!(found(dir.path()).is_err(), "empty directory");
write("demo-0.1.0.tgz.prov");
assert!(found(dir.path()).is_err(), "provenance only");
write("demo-0.1.0.tgz");
assert_eq!(
found(dir.path()).expect("archive"),
Some(PathBuf::from("demo-0.1.0.tgz"))
);
write("other.tgz");
assert!(found(dir.path()).is_err(), "two archives");
}