Skip to content

Commit

Permalink
ci: add lint tests
Browse files Browse the repository at this point in the history
Add lint tests to CI which runs linting against test image,
and then cp's kernel to create a negative test.

Co-authored-by: Joseph Marrero <[email protected]>
Co-authored-by: Huijing Hei <[email protected]>
Co-authored-by: Yasmin de Souza <[email protected]>

Signed-off-by: Steven Presti <[email protected]>
  • Loading branch information
prestist committed May 7, 2024
1 parent af127db commit cb6ac66
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ jobs:
run: sudo tar -C / -xvf bootc.tar.zst
- name: Integration tests
run: bootc internal-tests run-container-integration
- name: lint tests
run: |
set -xeuo pipefail
bootc build-lint
if ["$1" -eq 0]; then
echo "No errors found"
else
echo "Linting failed"
exit 1
fi
cd /usr/lib/modules
kernel=$(ls | tail -n 1)
sudo cp -r $kernel "$kernel-to-delete"
bootc build-lint
if ["$1" -eq 0]; then
echo "No errors found"
exit 1
else
echo "Linting found both kernels"
fi

#clean-up
sudo rm -rf "$kernel-to-delete"


privtest-alongside:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'control/skip-ci') }}
name: "Test install-alongside"
Expand Down
26 changes: 24 additions & 2 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//!
//! Command line tool to manage bootable ostree-based containers.
use anyhow::Ok;
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
use cap_std_ext::cap_std;
Expand Down Expand Up @@ -176,6 +175,10 @@ pub(crate) enum TestingOpts {
image: String,
blockdev: Utf8PathBuf,
},
// Test set of lints on ostree container
TestBuildLint {
image: String,
},
#[clap(name = "verify-selinux")]
VerifySELinux {
root: String,
Expand Down Expand Up @@ -624,7 +627,8 @@ fn lint() -> Result<()> {
}

let root = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
ostree_ext::bootabletree::find_kernel_dir_fs(&root)?;
let result = ostree_ext::bootabletree::find_kernel_dir_fs(&root)?;
tracing::debug!("Found kernel: {:?}", result);
return Ok(());
}

Expand Down Expand Up @@ -749,3 +753,21 @@ fn test_parse_generator() {
Opt::Internals(InternalsOpts::SystemdGenerator { .. })
));
}

#[test]
fn test_linting() {
// linting should only occur in side of a container.
match ostree_ext::container_utils::is_ostree_container() {
Ok(result) => {
if !result {
let expected_error_message = "Not in a ostree container, this command only verifies ostree containers.";

let result = lint();
assert_eq!(result.err().unwrap().to_string(), expected_error_message, "Error message mismatch");
}

},
Err(_) =>{
}
}
}
36 changes: 35 additions & 1 deletion lib/src/privtests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use cap_std_ext::cap_std::fs::Dir;
use fn_error_context::context;
use rustix::fd::AsFd;
use xshell::{cmd, Shell};

use crate::blockdev::LoopbackDevice;
use crate::install::config::InstallConfiguration;

Expand Down Expand Up @@ -196,6 +195,37 @@ fn verify_selinux_recurse(root: &Dir, path: &mut PathBuf, warn: bool) -> Result<
Ok(())
}

#[context("Container tests")]
fn test_build_lint(image: &str) -> Result<()> {

let sh = Shell::new()?;

// Smoke test of build_lint
let _test_1_result = cmd!(sh, "podman run --rm --privileged --pid=host --env=RUST_LOG -v /usr/bin/bootc:/usr/bin/bootc {image} bootc build-lint").run();

// Setup for multiple kernels lint test
cmd!(sh, "podman run -dt --name test --privileged --pid=host --env=RUST_LOG -v /usr/bin/bootc:/usr/bin/bootc {image} bash").run()?;
let kernel_name = cmd!(sh, "podman exec test bash -c 'ls /usr/lib/modules | tail -n -1'" ).read()?;
Command::new("podman")
.arg("exec")
.arg("test")
.arg("bash")
.arg("-c")
.arg(format!("sudo cp -r /usr/lib/modules/{} /usr/lib/modules/delete-me", kernel_name))
.output()?;
let more_then_one_kernel_result = cmd!(sh, "podman exec test bash -c 'bootc build-lint'").read_stderr();
// Container Cleanup
cmd!(sh, "podman rm -f test").run()?;

_test_1_result?;
if let Err(e) = more_then_one_kernel_result {
assert!(e.to_string().contains("bootc build-lint"));
} else {
assert!(false, "Expected error, got none");
}
Ok(())
}

pub(crate) async fn run(opts: TestingOpts) -> Result<()> {
match opts {
TestingOpts::RunPrivilegedIntegration {} => {
Expand All @@ -221,5 +251,9 @@ pub(crate) async fn run(opts: TestingOpts) -> Result<()> {
tokio::task::spawn_blocking(move || verify_selinux_recurse(&rootfs, &mut path, warn))
.await?
}
TestingOpts::TestBuildLint { image } => {
tokio::task::spawn_blocking(move || test_build_lint(&image)).await?
}

}
}

0 comments on commit cb6ac66

Please sign in to comment.