Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub(crate) enum InstallOpts {
///
/// At the current time, the only output key is `root-fs-type` which is a string-valued
/// filesystem name suitable for passing to `mkfs.$type`.
PrintConfiguration,
PrintConfiguration(crate::install::InstallPrintConfigurationOpts),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the change itself is correct, it would be beneficial to add a unit test to verify the parsing of the new --all flag for the install print-configuration subcommand. This ensures future changes don't accidentally break this functionality.

You could add a new test function in the tests module of this file, similar to test_parse_install_args. For example:

#[test]
fn test_parse_print_configuration() {
    // Default, no --all
    let o = Opt::try_parse_from(["bootc", "install", "print-configuration"]).unwrap();
    let o = match o {
        Opt::Install(InstallOpts::PrintConfiguration(opts)) => opts,
        o => panic!("Expected print-configuration opts, not {o:?}"),
    };
    assert!(!o.all);

    // With --all
    let o = Opt::try_parse_from(["bootc", "install", "print-configuration", "--all"]).unwrap();
    let o = match o {
        Opt::Install(InstallOpts::PrintConfiguration(opts)) => opts,
        o => panic!("Expected print-configuration opts, not {o:?}"),
    };
    assert!(o.all);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are good, but we can't really do this in unit tests because it operates on ambient data.

We can do it in tests-integration/src/container.rs though I think.

}

/// Subcommands which can be executed as part of a container build.
Expand Down Expand Up @@ -1483,7 +1483,7 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
crate::install::install_to_existing_root(opts).await
}
InstallOpts::Reset(opts) => crate::install::install_reset(opts).await,
InstallOpts::PrintConfiguration => crate::install::print_configuration(),
InstallOpts::PrintConfiguration(opts) => crate::install::print_configuration(opts),
InstallOpts::EnsureCompletion {} => {
let rootfs = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
crate::install::completion::run_from_anaconda(rootfs).await
Expand Down
15 changes: 13 additions & 2 deletions crates/lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ pub(crate) struct InstallResetOpts {
karg: Option<Vec<CmdlineOwned>>,
}

#[derive(Debug, clap::Parser, PartialEq, Eq)]
pub(crate) struct InstallPrintConfigurationOpts {
/// Print all configuration.
///
/// Print configuration that is usually handled internally, like kargs.
#[clap(long)]
pub(crate) all: bool,
}

/// Global state captured from the container.
#[derive(Debug, Clone)]
pub(crate) struct SourceInfo {
Expand Down Expand Up @@ -722,9 +731,11 @@ impl SourceInfo {
}
}

pub(crate) fn print_configuration() -> Result<()> {
pub(crate) fn print_configuration(opts: InstallPrintConfigurationOpts) -> Result<()> {
let mut install_config = config::load_config()?.unwrap_or_default();
install_config.filter_to_external();
if !opts.all {
install_config.filter_to_external();
}
let stdout = std::io::stdout().lock();
anyhow::Ok(install_config.to_canon_json_writer(stdout)?)
}
Expand Down
1 change: 1 addition & 0 deletions crates/tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bootc-kernel-cmdline = { path = "../kernel_cmdline", version = "0.0.0" }
libtest-mimic = "0.8.0"
oci-spec = "0.8.0"
rexpect = "0.6"
scopeguard = "1.2.0"

[lints]
workspace = true
35 changes: 33 additions & 2 deletions crates/tests-integration/src/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use indoc::indoc;
use scopeguard::defer;
use serde::Deserialize;
use std::fs;
use std::process::Command;

use anyhow::{Context, Result};
Expand Down Expand Up @@ -36,8 +40,34 @@ pub(crate) fn test_bootc_install_config() -> Result<()> {
let config = cmd!(sh, "bootc install print-configuration").read()?;
let config: serde_json::Value =
serde_json::from_str(&config).context("Parsing install config")?;
// Just verify we parsed the config, if any
drop(config);
// check that it parses okay, but also ensure kargs is not available here (only via --all)
assert!(config.get("kargs").is_none());
Ok(())
}

pub(crate) fn test_bootc_install_config_all() -> Result<()> {
#[derive(Deserialize)]
struct TestInstallConfig {
kargs: Vec<String>,
}

let config_d = std::path::Path::new("/run/bootc/install/");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, though I guess probably what we really need here is a workflow that combines container build + test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it would be nice to have a combined build+test here. I can remove this part of the code again, its a bit unnecessary because the default container already has some kargs I can test for. I wanted to make this is a test-as-example thing so wanted to include the code that writes the toml but maybe I was overthinking it :) Either way is fine with me!

let test_toml_path = config_d.join("10-test.toml");
std::fs::create_dir_all(&config_d)?;
let content = indoc! {r#"
[install]
kargs = ["karg1=1", "karg2=2"]
"#};
std::fs::write(&test_toml_path, content)?;
defer! {
fs::remove_file(test_toml_path).expect("cannot remove tempfile");
}

let sh = &xshell::Shell::new()?;
let config = cmd!(sh, "bootc install print-configuration --all").read()?;
let config: TestInstallConfig =
serde_json::from_str(&config).context("Parsing install config")?;
assert_eq! {config.kargs, vec!["karg1=1".to_string(), "karg2=2".to_string(), "localtestkarg=somevalue".to_string(), "otherlocalkarg=42".to_string()]};
Ok(())
}

Expand Down Expand Up @@ -88,6 +118,7 @@ pub(crate) fn run(testargs: libtest_mimic::Arguments) -> Result<()> {
new_test("variant-base-crosscheck", test_variant_base_crosscheck),
new_test("bootc upgrade", test_bootc_upgrade),
new_test("install config", test_bootc_install_config),
new_test("printconfig --all", test_bootc_install_config_all),
new_test("status", test_bootc_status),
new_test("system-reinstall --help", test_system_reinstall_help),
];
Expand Down
Loading