Skip to content

tree-wide: Move everything else under #[cfg(test)] #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 85 additions & 79 deletions lib/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,44 +817,6 @@ pub(crate) fn switch_origin_inplace(root: &Dir, imgref: &ImageReference) -> Resu
Ok(newest_deployment)
}

#[test]
fn test_switch_inplace() -> Result<()> {
use cap_std::fs::DirBuilderExt;

let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
let mut builder = cap_std::fs::DirBuilder::new();
let builder = builder.recursive(true).mode(0o755);
let deploydir = "sysroot/ostree/deploy/default/deploy";
let target_deployment = "af36eb0086bb55ac601600478c6168f834288013d60f8870b7851f44bf86c3c5.0";
td.ensure_dir_with(
format!("sysroot/ostree/deploy/default/deploy/{target_deployment}"),
builder,
)?;
let deploydir = &td.open_dir(deploydir)?;
let orig_imgref = ImageReference {
image: "quay.io/exampleos/original:sometag".into(),
transport: "registry".into(),
signature: None,
};
{
let origin = origin_from_imageref(&orig_imgref)?;
deploydir.atomic_write(
format!("{target_deployment}.origin"),
origin.to_data().as_bytes(),
)?;
}

let target_imgref = ImageReference {
image: "quay.io/someother/otherimage:latest".into(),
transport: "registry".into(),
signature: None,
};

let replaced = switch_origin_inplace(&td, &target_imgref).unwrap();
assert_eq!(replaced, target_deployment);
Ok(())
}

/// A workaround for https://github.com/ostreedev/ostree/issues/3193
/// as generated by anaconda.
#[context("Updating /etc/fstab for anaconda+composefs")]
Expand Down Expand Up @@ -935,56 +897,100 @@ pub(crate) fn fixup_etc_fstab(root: &Dir) -> Result<()> {
Ok(())
}

#[test]
fn test_fixup_etc_fstab_default() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n";
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_switch_inplace() -> Result<()> {
use cap_std::fs::DirBuilderExt;

let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
let mut builder = cap_std::fs::DirBuilder::new();
let builder = builder.recursive(true).mode(0o755);
let deploydir = "sysroot/ostree/deploy/default/deploy";
let target_deployment =
"af36eb0086bb55ac601600478c6168f834288013d60f8870b7851f44bf86c3c5.0";
td.ensure_dir_with(
format!("sysroot/ostree/deploy/default/deploy/{target_deployment}"),
builder,
)?;
let deploydir = &td.open_dir(deploydir)?;
let orig_imgref = ImageReference {
image: "quay.io/exampleos/original:sometag".into(),
transport: "registry".into(),
signature: None,
};
{
let origin = origin_from_imageref(&orig_imgref)?;
deploydir.atomic_write(
format!("{target_deployment}.origin"),
origin.to_data().as_bytes(),
)?;
}

let target_imgref = ImageReference {
image: "quay.io/someother/otherimage:latest".into(),
transport: "registry".into(),
signature: None,
};

#[test]
fn test_fixup_etc_fstab_multi() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
let replaced = switch_origin_inplace(&td, &target_imgref).unwrap();
assert_eq!(replaced, target_deployment);
Ok(())
}

#[test]
fn test_fixup_etc_fstab_default() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n";
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}

#[test]
fn test_fixup_etc_fstab_multi() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n";
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}

#[test]
fn test_fixup_etc_fstab_ro() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
#[test]
fn test_fixup_etc_fstab_ro() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs ro 0 0\n\
UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n";
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, default);
Ok(())
}

#[test]
fn test_fixup_etc_fstab_rw() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
// This case uses `defaults`
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
#[test]
fn test_fixup_etc_fstab_rw() -> Result<()> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
// This case uses `defaults`
let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs defaults 0 0\n\
UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n";
let modified = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
let modified = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\
# Updated by bootc-fstab-edit.service\n\
UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs defaults,ro 0 0\n\
UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n";
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, modified);
Ok(())
tempdir.create_dir_all("etc")?;
tempdir.atomic_write("etc/fstab", default)?;
fixup_etc_fstab(&tempdir).unwrap();
assert_eq!(tempdir.read_to_string("etc/fstab")?, modified);
Ok(())
}
}
100 changes: 52 additions & 48 deletions lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,54 +1882,58 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) ->

install_to_filesystem(opts, true).await
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn install_opts_serializable() {
let c: InstallToDiskOpts = serde_json::from_value(serde_json::json!({
"device": "/dev/vda"
}))
.unwrap();
assert_eq!(c.block_opts.device, "/dev/vda");
}

#[test]
fn install_opts_serializable() {
let c: InstallToDiskOpts = serde_json::from_value(serde_json::json!({
"device": "/dev/vda"
}))
.unwrap();
assert_eq!(c.block_opts.device, "/dev/vda");
}

#[test]
fn test_mountspec() {
let mut ms = MountSpec::new("/dev/vda4", "/boot");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto defaults 0 0");
ms.push_option("ro");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro 0 0");
ms.push_option("relatime");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro,relatime 0 0");
}
#[test]
fn test_mountspec() {
let mut ms = MountSpec::new("/dev/vda4", "/boot");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto defaults 0 0");
ms.push_option("ro");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro 0 0");
ms.push_option("relatime");
assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro,relatime 0 0");
}

#[test]
fn test_gather_root_args() {
// A basic filesystem using a UUID
let inspect = Filesystem {
source: "/dev/vda4".into(),
target: "/".into(),
fstype: "xfs".into(),
maj_min: "252:4".into(),
options: "rw".into(),
uuid: Some("965eb3c7-5a3f-470d-aaa2-1bcf04334bc6".into()),
children: None,
};
let r = find_root_args_to_inherit(&[], &inspect).unwrap();
assert_eq!(r.mount_spec, "UUID=965eb3c7-5a3f-470d-aaa2-1bcf04334bc6");

// In this case we take the root= from the kernel cmdline
let r = find_root_args_to_inherit(
&[
"root=/dev/mapper/root",
"rw",
"someother=karg",
"rd.lvm.lv=root",
"systemd.debug=1",
],
&inspect,
)
.unwrap();
assert_eq!(r.mount_spec, "/dev/mapper/root");
assert_eq!(r.kargs.len(), 1);
assert_eq!(r.kargs[0], "rd.lvm.lv=root");
#[test]
fn test_gather_root_args() {
// A basic filesystem using a UUID
let inspect = Filesystem {
source: "/dev/vda4".into(),
target: "/".into(),
fstype: "xfs".into(),
maj_min: "252:4".into(),
options: "rw".into(),
uuid: Some("965eb3c7-5a3f-470d-aaa2-1bcf04334bc6".into()),
children: None,
};
let r = find_root_args_to_inherit(&[], &inspect).unwrap();
assert_eq!(r.mount_spec, "UUID=965eb3c7-5a3f-470d-aaa2-1bcf04334bc6");

// In this case we take the root= from the kernel cmdline
let r = find_root_args_to_inherit(
&[
"root=/dev/mapper/root",
"rw",
"someother=karg",
"rd.lvm.lv=root",
"systemd.debug=1",
],
&inspect,
)
.unwrap();
assert_eq!(r.mount_spec, "/dev/mapper/root");
assert_eq!(r.kargs.len(), 1);
assert_eq!(r.kargs[0], "rd.lvm.lv=root");
}
}
47 changes: 26 additions & 21 deletions lib/src/install/osconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,42 @@ pub(crate) fn inject_root_ssh_authorized_keys(
Ok(())
}

#[test]
fn test_inject_root_ssh_symlinked() -> Result<()> {
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
#[cfg(test)]
mod tests {
use super::*;

// The code expects this to exist, reasonably so
root.create_dir("etc")?;
// Test with a symlink
root.symlink("var/roothome", "root")?;
inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap();
#[test]
fn test_inject_root_ssh_symlinked() -> Result<()> {
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;

let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?;
assert_eq!(
// The code expects this to exist, reasonably so
root.create_dir("etc")?;
// Test with a symlink
root.symlink("var/roothome", "root")?;
inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap();

let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?;
assert_eq!(
content,
"f~ /var/roothome/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n"
);

Ok(())
}
Ok(())
}

#[test]
fn test_inject_root_ssh_dir() -> Result<()> {
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
#[test]
fn test_inject_root_ssh_dir() -> Result<()> {
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;

root.create_dir("etc")?;
root.create_dir("root")?;
inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap();
root.create_dir("etc")?;
root.create_dir("root")?;
inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap();

let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?;
assert_eq!(
let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?;
assert_eq!(
content,
"f~ /root/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n"
);
Ok(())
Ok(())
}
}
27 changes: 16 additions & 11 deletions ostree-ext/src/container/encapsulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,20 @@ pub async fn encapsulate<S: AsRef<str>>(
build_impl(repo, ostree_ref.as_ref(), config, opts, dest).await
}

#[test]
fn test_parse_ocipath() {
let default = "/foo/bar";
let untagged = "/foo/bar:baz";
let tagged = "/foo/bar:baz:latest";
assert_eq!(parse_oci_path_and_tag(default), ("/foo/bar", None));
assert_eq!(
parse_oci_path_and_tag(tagged),
("/foo/bar", Some("baz:latest"))
);
assert_eq!(parse_oci_path_and_tag(untagged), ("/foo/bar", Some("baz")));
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_ocipath() {
let default = "/foo/bar";
let untagged = "/foo/bar:baz";
let tagged = "/foo/bar:baz:latest";
assert_eq!(parse_oci_path_and_tag(default), ("/foo/bar", None));
assert_eq!(
parse_oci_path_and_tag(tagged),
("/foo/bar", Some("baz:latest"))
);
assert_eq!(parse_oci_path_and_tag(untagged), ("/foo/bar", Some("baz")));
}
}
Loading
Loading