-
Notifications
You must be signed in to change notification settings - Fork 70
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-integration: Add basic local tmt flow #593
Merged
+201
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This tmt test just demonstrates local tmt usage. | ||
# We'll hopefully expand it to do more interesting things in the | ||
# future and unify with the other test plans. | ||
provision: | ||
how: virtual | ||
# Generated by `cargo xtask ` | ||
image: file://./target/testbootc-cloud.qcow2 | ||
summary: Basic smoke test | ||
execute: | ||
how: tmt | ||
script: bootc status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
use anyhow::{Context, Result}; | ||
use camino::{Utf8Path, Utf8PathBuf}; | ||
use clap::Subcommand; | ||
use fn_error_context::context; | ||
use xshell::{cmd, Shell}; | ||
|
||
const BUILDER_ANNOTATION: &str = "bootc.diskimage-builder"; | ||
const TEST_IMAGE: &str = "localhost/bootc"; | ||
const TESTVMDIR: &str = "testvm"; | ||
const DISK_CACHE: &str = "disk.qcow2"; | ||
const IMAGEID_XATTR: &str = "user.bootc.container-image-digest"; | ||
|
||
#[derive(Debug, Subcommand)] | ||
#[clap(rename_all = "kebab-case")] | ||
pub(crate) enum Opt { | ||
PrepareTmt { | ||
#[clap(long)] | ||
/// The container image to spawn, otherwise one will be built | ||
testimage: Option<String>, | ||
}, | ||
CreateQcow2 { | ||
/// Input container image | ||
container: String, | ||
/// Write disk to this path | ||
disk: Utf8PathBuf, | ||
}, | ||
} | ||
|
||
struct TestContext { | ||
sh: xshell::Shell, | ||
targetdir: Utf8PathBuf, | ||
} | ||
|
||
fn image_digest(sh: &Shell, cimage: &str) -> Result<String> { | ||
let key = "{{ .Digest }}"; | ||
let r = cmd!(sh, "podman inspect --type image --format {key} {cimage}").read()?; | ||
Ok(r) | ||
} | ||
|
||
fn builder_from_image(sh: &Shell, cimage: &str) -> Result<String> { | ||
let mut inspect: serde_json::Value = | ||
serde_json::from_str(&cmd!(sh, "podman inspect --type image {cimage}").read()?)?; | ||
let inspect = inspect | ||
.as_array_mut() | ||
.and_then(|v| v.pop()) | ||
.ok_or_else(|| anyhow::anyhow!("Failed to parse inspect output"))?; | ||
let config = inspect | ||
.get("Config") | ||
.ok_or_else(|| anyhow::anyhow!("Missing config"))?; | ||
let config: oci_spec::image::Config = | ||
serde_json::from_value(config.clone()).context("Parsing config")?; | ||
let builder = config | ||
.labels() | ||
.as_ref() | ||
.and_then(|l| l.get(BUILDER_ANNOTATION)) | ||
.ok_or_else(|| anyhow::anyhow!("Missing {BUILDER_ANNOTATION}"))?; | ||
Ok(builder.to_owned()) | ||
} | ||
|
||
#[context("Running bootc-image-builder")] | ||
fn run_bib(sh: &Shell, cimage: &str, tmpdir: &Utf8Path, diskpath: &Utf8Path) -> Result<()> { | ||
let diskpath: Utf8PathBuf = sh.current_dir().join(diskpath).try_into()?; | ||
let digest = image_digest(sh, cimage)?; | ||
println!("{cimage} digest={digest}"); | ||
if diskpath.try_exists()? { | ||
let mut buf = [0u8; 2048]; | ||
if rustix::fs::getxattr(diskpath.as_std_path(), IMAGEID_XATTR, &mut buf) | ||
.context("Reading xattr") | ||
.is_ok() | ||
{ | ||
let buf = String::from_utf8_lossy(&buf); | ||
if &*buf == digest.as_str() { | ||
println!("Existing disk {diskpath} matches container digest {digest}"); | ||
return Ok(()); | ||
} else { | ||
println!("Cache miss; previous digest={buf}"); | ||
} | ||
} | ||
} | ||
let builder = if let Ok(b) = std::env::var("BOOTC_BUILDER") { | ||
b | ||
} else { | ||
builder_from_image(sh, cimage)? | ||
}; | ||
let _g = sh.push_dir(tmpdir); | ||
let bibwork = "bib-work"; | ||
sh.remove_path(bibwork)?; | ||
sh.create_dir(bibwork)?; | ||
let _g = sh.push_dir(bibwork); | ||
let pwd = sh.current_dir(); | ||
cmd!(sh, "podman run --rm --privileged -v /var/lib/containers/storage:/var/lib/containers/storage --security-opt label=type:unconfined_t -v {pwd}:/output {builder} --type qcow2 --local {cimage}").run()?; | ||
let tmp_disk: Utf8PathBuf = sh | ||
.current_dir() | ||
.join("qcow2/disk.qcow2") | ||
.try_into() | ||
.unwrap(); | ||
rustix::fs::setxattr( | ||
tmp_disk.as_std_path(), | ||
IMAGEID_XATTR, | ||
digest.as_bytes(), | ||
rustix::fs::XattrFlags::empty(), | ||
) | ||
.context("Setting xattr")?; | ||
cmd!(sh, "mv -Tf {tmp_disk} {diskpath}").run()?; | ||
cmd!(sh, "rm -rf {bibwork}").run()?; | ||
Ok(()) | ||
} | ||
|
||
/// Given the input container image reference, create a disk | ||
/// image in the target directory. | ||
#[context("Creating disk")] | ||
fn create_disk(ctx: &TestContext, cimage: &str) -> Result<Utf8PathBuf> { | ||
let sh = &ctx.sh; | ||
let targetdir = ctx.targetdir.as_path(); | ||
let _targetdir_guard = sh.push_dir(targetdir); | ||
sh.create_dir(TESTVMDIR)?; | ||
let output_disk: Utf8PathBuf = sh | ||
.current_dir() | ||
.join(TESTVMDIR) | ||
.join(DISK_CACHE) | ||
.try_into() | ||
.unwrap(); | ||
|
||
let bibwork = "bib-work"; | ||
sh.remove_path(bibwork)?; | ||
sh.create_dir(bibwork)?; | ||
|
||
run_bib(sh, cimage, bibwork.into(), &output_disk)?; | ||
|
||
Ok(output_disk) | ||
} | ||
|
||
pub(crate) fn run(opt: Opt) -> Result<()> { | ||
let ctx = &{ | ||
let sh = xshell::Shell::new()?; | ||
let mut targetdir: Utf8PathBuf = cmd!(sh, "git rev-parse --show-toplevel").read()?.into(); | ||
targetdir.push("target"); | ||
TestContext { targetdir, sh } | ||
}; | ||
match opt { | ||
Opt::PrepareTmt { mut testimage } => { | ||
let testimage = if let Some(i) = testimage.take() { | ||
i | ||
} else { | ||
cmd!( | ||
&ctx.sh, | ||
"podman build --build-arg=variant=tmt -t {TEST_IMAGE} -f hack/Containerfile ." | ||
) | ||
.run()?; | ||
TEST_IMAGE.to_string() | ||
}; | ||
|
||
let disk = create_disk(ctx, &testimage)?; | ||
println!("Created: {disk}"); | ||
Ok(()) | ||
} | ||
Opt::CreateQcow2 { container, disk } => { | ||
let g = ctx.sh.push_dir(&ctx.targetdir); | ||
ctx.sh.remove_path("tmp")?; | ||
ctx.sh.create_dir("tmp")?; | ||
drop(g); | ||
run_bib(&ctx.sh, &container, "tmp".into(), &disk) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
bootc install to-disk
to generate disk image?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but I think down the line we'll migrate bib to be more of a SDK (cc https://gitlab.com/fedora/bootc/tracker/-/issues/2 ) and so we might as well start pulling a container here.