Skip to content
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

tests: Deduplicate fixture code #643

Merged
merged 1 commit into from
Jun 21, 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
21 changes: 21 additions & 0 deletions lib/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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<Utf8Path>,
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(())
}
}
4 changes: 2 additions & 2 deletions lib/src/integrationtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ pub(crate) fn detectenv() -> Result<&'static str> {
#[context("Generating derived oci")]
pub fn generate_derived_oci(
src: impl AsRef<Utf8Path>,
dir: impl AsRef<Utf8Path>,
dir: impl AsRef<Path>,
tag: Option<&str>,
) -> Result<()> {
generate_derived_oci_from_tar(
src,
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(())
},
Expand Down
30 changes: 2 additions & 28 deletions lib/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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,
Expand Down
Loading