use super::*;
pub(super) fn repo_scripts() -> [(&'static VendorRepo, RootScript); 7] {
[
(&HASHICORP, terraform_script()),
(&HASHICORP, vagrant_script()),
(&HASHICORP, vmware_utility_script()),
(&ORACLE, virtualbox_script()),
(&DOCKER, docker_script(None)),
(&MICROSOFT, azurecli_script()),
(&GOOGLE, gcloud_script()),
]
}
#[test]
fn repo_scripts_rerun_without_a_terminal_and_parse() {
for (_, s) in repo_scripts() {
assert!(s.contains("gpg --batch --yes --dearmor"), "{s}");
assert!(crate::testing::sh_parses(&s), "not valid sh: {s}");
}
assert!(virtualbox_script().contains("download.virtualbox.org/virtualbox/debian"));
let utility = vmware_utility_script();
assert!(
utility.contains(HASHICORP.fingerprint) && utility.contains("apt-get install -y --reinstall vagrant-vmware-utility"),
"{utility}"
);
assert!(
utility.contains("developer.hashicorp.com/vagrant/install/vmware") && !utility.contains("terraform/install"),
"{utility}"
);
}
#[test]
fn every_vendor_repository_is_set_up_the_same_way() {
for (repo, s) in repo_scripts() {
let name = repo.name;
assert!(s.contains(&format!("-o /etc/apt/keyrings/{name}.gpg")), "{s}");
let source = format!(
"signed-by=/etc/apt/keyrings/{name}.gpg] $repo {} {}\" > /etc/apt/sources.list.d/{name}.list",
repo.suite, repo.component
);
assert!(s.contains(&format!("deb [arch=$(dpkg --print-architecture) {source}")), "{s}");
assert!(!s.contains("/usr/share/keyrings"), "{s}");
assert_eq!(
s.contains(&format!(r#"release_served "$repo" "{}""#, repo.suite)),
repo.by_hand.is_some(),
"{s}"
);
}
}
pub(super) fn root_scripts() -> [RootScript; 8] {
[
awscli_script(),
docker_script(Some("alice")),
terraform_script(),
vagrant_script(),
virtualbox_script(),
azurecli_script(),
gcloud_script(),
vmware_utility_script(),
]
}
#[test]
fn every_root_script_that_downloads_installs_curl_first() {
for s in root_scripts() {
if let Some(first) = s.find("curl -") {
let installed = s.find(NEED_CURL).unwrap_or_else(|| panic!("curl used, never installed: {s}"));
assert!(installed < first, "curl used before it is installed: {s}");
}
}
}
#[cfg(unix)]
fn with_release_answer(http: &str, script: &str) -> (String, String) {
let bin = crate::testing::fresh_dir("curl");
std::fs::create_dir_all(&bin).unwrap();
let fake = bin.join("curl");
crate::testing::write_executable(&fake, format!("#!/bin/sh\nprintf '%s' '{http}'\n"));
let out = crate::testing::sh(
&format!("set -e; {RELEASE_SERVED}; {script}"),
&[("PATH", &format!("{}:/usr/bin:/bin", bin.display()))],
);
std::fs::remove_dir_all(&bin).unwrap();
(
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
#[cfg(unix)]
fn kernel_build(running: &str, packages: &[&str]) -> Result<(String, String), String> {
let bin = crate::testing::fresh_dir("kernel");
std::fs::create_dir_all(&bin).unwrap();
let fake = |name: &str, body: String| {
crate::testing::write_executable(bin.join(name), body);
};
fake("uname", format!("#!/bin/sh\necho '{running}'\n"));
let known = packages.iter().map(|p| format!("'{p}'")).collect::<Vec<_>>().join(" ");
fake(
"apt-cache",
format!(
"#!/bin/sh\nlabel='Candidat :'; [ \"$LC_ALL\" = C ] && label='Candidate:'\nfor p in {known}; do [ \"$2\" = \"$p\" ] && {{ echo \"$p:\"; echo \" $label 1.0-1\"; exit 0; }}; done\necho \"$2:\"; echo \" $label (none)\"\n"
),
);
let out = crate::testing::sh(
&format!("{}; {KERNEL_BUILD}; echo \"$build|$reboot\"", crate::install::root::PRELUDE),
&[("PATH", &format!("{}:/usr/bin:/bin", bin.display())), ("LANG", "fr_FR.UTF-8")],
);
std::fs::remove_dir_all(&bin).unwrap();
let said = String::from_utf8_lossy(&out.stdout).trim().to_owned();
if !out.status.success() {
return Err(String::from_utf8_lossy(&out.stderr).into_owned());
}
let (build, reboot) = said.split_once('|').unwrap();
Ok((build.to_owned(), reboot.to_owned()))
}
#[cfg(unix)]
#[test]
fn virtualbox_builds_its_driver_with_the_running_kernels_headers_or_waits_for_the_newer_kernel() {
let debian = "6.12.86+deb13-amd64";
let headers = format!("linux-headers-{debian}");
assert_eq!(
kernel_build(debian, &[&headers, "linux-headers-amd64"]).unwrap(),
("gcc make perl kmod linux-headers-6.12.86+deb13-amd64 linux-headers-amd64".into(), String::new())
);
assert_eq!(
kernel_build(debian, &["linux-headers-amd64"]).unwrap(),
("gcc make perl kmod linux-image-amd64 linux-headers-amd64".into(), "1".into())
);
assert_eq!(
kernel_build("6.12.86+deb13-cloud-amd64", &["linux-headers-cloud-amd64", "linux-headers-amd64"])
.unwrap()
.0,
"gcc make perl kmod linux-image-cloud-amd64 linux-headers-cloud-amd64"
);
assert_eq!(
kernel_build("6.8.0-45-generic", &["linux-headers-6.8.0-45-generic", "linux-headers-generic"])
.unwrap()
.0,
"gcc make perl kmod linux-headers-6.8.0-45-generic linux-headers-generic"
);
assert_eq!(
kernel_build("6.12.25-amd64", &["linux-headers-amd64"]).unwrap(),
("gcc make perl kmod linux-image-amd64 linux-headers-amd64".into(), "1".into())
);
let none = kernel_build(debian, &[]).unwrap_err();
assert!(
none.starts_with("VirtualBox builds its kernel driver with the running kernel's headers (linux-headers-6.12.86+deb13-amd64)"),
"{none}"
);
}
#[test]
fn apt_cache_policy_is_read_in_english_whatever_the_language() {
let s = virtualbox_script();
let prelude = crate::install::root::PRELUDE;
assert!(prelude.ends_with("export LC_ALL=C") && s.starts_with(prelude), "{s}");
assert_eq!(s.matches("apt-cache policy").count(), 3, "{s}");
}
#[test]
fn virtualbox_installs_without_its_recommends_with_what_builds_its_driver() {
let s = virtualbox_script();
assert!(s.contains("apt-get install -y --no-install-recommends virtualbox $build;"), "{s}");
assert!(s.contains(r#"apt-get install -y --no-install-recommends "$pkg" $build;"#), "{s}");
assert!(
!s.contains("apt-get install -y virtualbox") && !s.contains(r#"apt-get install -y "$pkg""#),
"{s}"
);
assert!(s.find(KERNEL_BUILD).unwrap() < s.find("--no-install-recommends").unwrap(), "{s}");
assert!(s.ends_with("restart the computer to start it, and VirtualBox's driver is built then.\""), "{s}");
assert!(crate::testing::sh_parses(&s), "{s}");
}
#[cfg(unix)]
#[test]
fn a_repository_check_says_no_packages_only_when_the_release_has_none() {
let s = apt_repo_script(&HASHICORP);
let snippet = s
.get(s.find("; release_served \"$repo\"").unwrap() + 2..s.find("; command -v gpg").unwrap())
.unwrap();
let check = |http: &str| with_release_answer(http, &format!("repo=https://apt.example; codename=trixie; {snippet}; echo went-on"));
assert_eq!(check("200").0.trim(), "went-on");
let missing = check("404").1;
assert!(missing.starts_with("HashiCorp has no packages for trixie yet"), "{missing}");
let unreachable = check("000").1;
assert!(
unreachable.starts_with("Couldn't reach https://apt.example (HTTP 000)") && !unreachable.contains("no packages"),
"{unreachable}"
);
}
#[cfg(unix)]
#[test]
fn a_debian_derivative_the_vendor_doesnt_serve_takes_its_debian_bases_packages() {
let s = apt_repo_script(&HASHICORP);
let snippet = s
.get(s.find("; release_served \"$repo\"").unwrap() + 2..s.find("; command -v gpg").unwrap())
.unwrap();
let bin = crate::testing::fresh_dir("curl");
std::fs::create_dir_all(&bin).unwrap();
let fake = bin.join("curl");
crate::testing::write_executable(&fake, "#!/bin/sh\ncase \"$*\" in */dists/trixie/*) printf 200 ;; *) printf 404 ;; esac\n");
let run = |vars: &str| {
let out = crate::testing::sh(
&format!("set -e; {RELEASE_SERVED}; repo=https://apt.example; {vars}; {snippet}; echo \"went-on $codename\""),
&[("PATH", &format!("{}:/usr/bin:/bin", bin.display()))],
);
(
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
};
let (out, _) = run("codename=kali-rolling; debian_base='trixie bookworm'");
assert!(
out.contains("has no packages for kali-rolling: installing its trixie ones") && out.trim_end().ends_with("went-on trixie"),
"{out}"
);
let (rolling, rolling_err) = run("codename=kali-rolling; debian_base=''");
assert!(
!rolling.contains("went-on") && rolling_err.starts_with("HashiCorp has no packages for kali-rolling yet"),
"{rolling} / {rolling_err}"
);
let (out_noble, err_noble) = run("codename=noble; debian_base='trixie bookworm'; unset debian_base");
assert!(
!out_noble.contains("went-on") && err_noble.starts_with("HashiCorp has no packages for noble yet"),
"{out_noble} / {err_noble}"
);
std::fs::remove_dir_all(&bin).unwrap();
}
#[cfg(unix)]
#[test]
fn dockers_repository_check_gives_the_distributions_docker_only_on_a_404() {
let dir = crate::testing::fresh_dir("docker-probe");
std::fs::create_dir_all(&dir).unwrap();
let release = dir.join("os-release");
std::fs::write(&release, "ID=debian\nVERSION_CODENAME=trixie\n").unwrap();
let probe = DOCKER_DIST
.replace("/etc/os-release", &release.display().to_string())
.replace("command -v apt-get >/dev/null", "true");
let check = |http: &str| with_release_answer(http, &format!("{probe}; echo \"$dist/$codename/$own\""));
assert_eq!(check("200").0.trim(), "debian/trixie/");
assert_eq!(check("404").0.trim(), "debian/trixie/1");
for unreachable in ["000", "407", "503"] {
let (out, err) = check(unreachable);
assert!(out.is_empty(), "went on after HTTP {unreachable}: {out}");
assert!(
err.starts_with(&format!("Couldn't reach https://download.docker.com/linux/debian (HTTP {unreachable})")),
"{err}"
);
}
let s = docker_script(None);
assert_eq!(s.matches(NEED_CURL).count(), 1, "{s}");
assert_eq!(s.matches("release_served()").count(), 1, "{s}");
assert!(s.find("release_served()").unwrap() < s.find("! release_served").unwrap());
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn vagrant_names_its_own_install_page() {
assert!(
vagrant_script().contains("https://developer.hashicorp.com/vagrant/install"),
"not Terraform's page"
);
}
#[test]
fn root_scripts_never_use_fixed_temp_paths_or_pipe_downloads_into_a_shell() {
const STATUS_PROBE: &str = "s -o /dev/null -w '%{http_code}'";
assert!(RELEASE_SERVED.contains(&format!("curl -{STATUS_PROBE}")));
for s in root_scripts() {
assert!(!s.contains("/tmp"), "fixed temp path: {s}");
assert!(!s.contains("| sh") && !s.contains("| bash"), "download piped into a shell: {s}");
for call in s.split("curl -").skip(1) {
assert!(
call.starts_with('f') || call.starts_with(STATUS_PROBE),
"a failed download must fail: curl -{call}"
);
}
assert!(s.starts_with("set -e"), "{s}");
assert!(crate::testing::sh_parses(&s), "not valid sh: {s}");
}
for s in root_scripts() {
assert!(s.contains(r#"d="$(mktemp -d)""#) && s.contains("trap 'rm -rf \"$d\"' EXIT"), "{s}");
}
assert!(awscli_script().contains(r#""$d/aws/install" --update"#) && awscli_script().contains("unzip -tq"));
}