From 72aa9a15e5404febe7ad71307f5131c51b815d5e Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 21 Jun 2024 09:28:40 -0400 Subject: [PATCH] tests: Deduplicate fixture code Signed-off-by: Colin Walters --- lib/src/fixture.rs | 21 +++++++++++++++++++++ lib/src/integrationtest.rs | 4 ++-- lib/tests/it/main.rs | 30 ++---------------------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/lib/src/fixture.rs b/lib/src/fixture.rs index b21bec1e..769e8bf8 100644 --- a/lib/src/fixture.rs +++ b/lib/src/fixture.rs @@ -19,6 +19,7 @@ use fn_error_context::context; use gvariant::aligned_bytes::TryAsAligned; use gvariant::{Marker, Structure}; use io_lifetimes::AsFd; +use ocidir::cap_std::fs::{DirBuilder, DirBuilderExt as _}; use once_cell::sync::Lazy; use regex::Regex; use std::borrow::Cow; @@ -28,6 +29,7 @@ use std::ops::Add; use std::process::{Command, Stdio}; use std::rc::Rc; use std::sync::Arc; +use tempfile::TempDir; const OSTREE_GPG_HOME: &[u8] = include_bytes!("fixtures/ostree-gpg-test-home.tar.gz"); const TEST_GPG_KEYID_1: &str = "7FCA23D8472CDAFA"; @@ -841,4 +843,23 @@ impl Fixture { .context("exporting")?; Ok((imgref, digest)) } + + // Generate a directory with some test contents + #[context("Generating temp content")] + pub fn generate_test_derived_oci( + &self, + derived_path: impl AsRef, + tag: Option<&str>, + ) -> Result<()> { + let temproot = TempDir::new_in(&self.path)?; + let temprootd = Dir::open_ambient_dir(&temproot, cap_std::ambient_authority())?; + let mut db = DirBuilder::new(); + db.mode(0o755); + db.recursive(true); + temprootd.create_dir_with("usr/bin", &db)?; + temprootd.write("usr/bin/newderivedfile", "newderivedfile v0")?; + temprootd.write("usr/bin/newderivedfile3", "newderivedfile3 v0")?; + crate::integrationtest::generate_derived_oci(derived_path, temproot, tag)?; + Ok(()) + } } diff --git a/lib/src/integrationtest.rs b/lib/src/integrationtest.rs index ec57c9f2..e7397dd4 100644 --- a/lib/src/integrationtest.rs +++ b/lib/src/integrationtest.rs @@ -33,7 +33,7 @@ pub(crate) fn detectenv() -> Result<&'static str> { #[context("Generating derived oci")] pub fn generate_derived_oci( src: impl AsRef, - dir: impl AsRef, + dir: impl AsRef, tag: Option<&str>, ) -> Result<()> { generate_derived_oci_from_tar( @@ -41,7 +41,7 @@ pub fn generate_derived_oci( move |w| { let dir = dir.as_ref(); let mut layer_tar = tar::Builder::new(w); - layer_tar.append_dir_all("./", dir.as_std_path())?; + layer_tar.append_dir_all("./", dir)?; layer_tar.finish()?; Ok(()) }, diff --git a/lib/tests/it/main.rs b/lib/tests/it/main.rs index 70efc1f6..f7999b8c 100644 --- a/lib/tests/it/main.rs +++ b/lib/tests/it/main.rs @@ -644,20 +644,7 @@ async fn test_export_as_container_derived() -> Result<()> { let derived_tag = "derived"; // Build a derived image let srcpath = src_imgref.name.as_str(); - let temproot = &fixture.path.join("temproot"); - || -> Result<_> { - std::fs::create_dir(temproot)?; - let temprootd = Dir::open_ambient_dir(temproot, cap_std::ambient_authority())?; - let mut db = DirBuilder::new(); - db.mode(0o755); - db.recursive(true); - temprootd.create_dir_with("usr/bin", &db)?; - temprootd.write("usr/bin/newderivedfile", "newderivedfile v0")?; - temprootd.write("usr/bin/newderivedfile3", "newderivedfile3 v0")?; - Ok(()) - }() - .context("generating temp content")?; - ostree_ext::integrationtest::generate_derived_oci(srcpath, temproot, Some(derived_tag))?; + fixture.generate_test_derived_oci(srcpath, Some(&derived_tag))?; let derived_imgref = ImageReference { transport: src_imgref.transport.clone(), name: format!("{}:{derived_tag}", src_imgref.name.as_str()), @@ -917,21 +904,8 @@ r usr/bin/bash bash-v0 // Build a derived image let srcpath = imgref.imgref.name.as_str(); - let temproot = &fixture.path.join("temproot"); - || -> Result<_> { - std::fs::create_dir(temproot)?; - let temprootd = Dir::open_ambient_dir(temproot, cap_std::ambient_authority())?; - let mut db = DirBuilder::new(); - db.mode(0o755); - db.recursive(true); - temprootd.create_dir_with("usr/bin", &db)?; - temprootd.write("usr/bin/newderivedfile", "newderivedfile v0")?; - temprootd.write("usr/bin/newderivedfile3", "newderivedfile3 v0")?; - Ok(()) - }() - .context("generating temp content")?; let derived_tag = "derived"; - ostree_ext::integrationtest::generate_derived_oci(srcpath, temproot, Some(derived_tag))?; + fixture.generate_test_derived_oci(srcpath, Some(&derived_tag))?; let derived_imgref = OstreeImageReference { sigverify: SignatureSource::ContainerPolicyAllowInsecure,