Skip to content

Commit c36de74

Browse files
committed
ci: add lint tests
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]>
1 parent af127db commit c36de74

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ jobs:
127127
run: sudo tar -C / -xvf bootc.tar.zst
128128
- name: Integration tests
129129
run: bootc internal-tests run-container-integration
130+
131+
130132
privtest-alongside:
131133
if: ${{ !contains(github.event.pull_request.labels.*.name, 'control/skip-ci') }}
132134
name: "Test install-alongside"

lib/src/cli.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//!
33
//! Command line tool to manage bootable ostree-based containers.
44
5-
use anyhow::Ok;
65
use anyhow::{Context, Result};
76
use camino::Utf8PathBuf;
87
use cap_std_ext::cap_std;
@@ -176,6 +175,10 @@ pub(crate) enum TestingOpts {
176175
image: String,
177176
blockdev: Utf8PathBuf,
178177
},
178+
// Test set of lints on ostree container
179+
TestBuildLint {
180+
image: String,
181+
},
179182
#[clap(name = "verify-selinux")]
180183
VerifySELinux {
181184
root: String,
@@ -624,7 +627,8 @@ fn lint() -> Result<()> {
624627
}
625628

626629
let root = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
627-
ostree_ext::bootabletree::find_kernel_dir_fs(&root)?;
630+
let result = ostree_ext::bootabletree::find_kernel_dir_fs(&root)?;
631+
tracing::debug!("Found kernel: {:?}", result);
628632
return Ok(());
629633
}
630634

@@ -749,3 +753,21 @@ fn test_parse_generator() {
749753
Opt::Internals(InternalsOpts::SystemdGenerator { .. })
750754
));
751755
}
756+
757+
#[test]
758+
fn test_linting() {
759+
// linting should only occur in side of a container.
760+
match ostree_ext::container_utils::is_ostree_container() {
761+
Ok(result) => {
762+
if !result {
763+
let expected_error_message = "Not in a ostree container, this command only verifies ostree containers.";
764+
765+
let result = lint();
766+
assert_eq!(result.err().unwrap().to_string(), expected_error_message, "Error message mismatch");
767+
}
768+
769+
},
770+
Err(_) =>{
771+
}
772+
}
773+
}

lib/src/privtests.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use cap_std_ext::cap_std::fs::Dir;
99
use fn_error_context::context;
1010
use rustix::fd::AsFd;
1111
use xshell::{cmd, Shell};
12-
1312
use crate::blockdev::LoopbackDevice;
1413
use crate::install::config::InstallConfiguration;
1514

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

198+
#[context("Container tests")]
199+
fn test_build_lint(image: &str) -> Result<()> {
200+
201+
let sh = Shell::new()?;
202+
203+
// Smoke test of build_lint
204+
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();
205+
206+
// Setup for multiple kernels lint test
207+
cmd!(sh, "podman run -dt --name test --privileged --pid=host --env=RUST_LOG -v /usr/bin/bootc:/usr/bin/bootc {image} bash").run()?;
208+
let kernel_name = cmd!(sh, "podman exec test bash -c 'ls /usr/lib/modules | tail -n -1'" ).read()?;
209+
Command::new("podman")
210+
.arg("exec")
211+
.arg("test")
212+
.arg("bash")
213+
.arg("-c")
214+
.arg(format!("sudo cp -r /usr/lib/modules/{} /usr/lib/modules/delete-me", kernel_name))
215+
.output()?;
216+
let more_then_one_kernel_result = cmd!(sh, "podman exec test bash -c 'bootc build-lint'").read_stderr();
217+
// Container Cleanup
218+
cmd!(sh, "podman rm -f test").run()?;
219+
220+
_test_1_result?;
221+
if let Err(e) = more_then_one_kernel_result {
222+
assert!(e.to_string().contains("bootc build-lint"));
223+
} else {
224+
assert!(false, "Expected error, got none");
225+
}
226+
Ok(())
227+
}
228+
199229
pub(crate) async fn run(opts: TestingOpts) -> Result<()> {
200230
match opts {
201231
TestingOpts::RunPrivilegedIntegration {} => {
@@ -221,5 +251,9 @@ pub(crate) async fn run(opts: TestingOpts) -> Result<()> {
221251
tokio::task::spawn_blocking(move || verify_selinux_recurse(&rootfs, &mut path, warn))
222252
.await?
223253
}
254+
TestingOpts::TestBuildLint { image } => {
255+
tokio::task::spawn_blocking(move || test_build_lint(&image)).await?
256+
}
257+
224258
}
225259
}

0 commit comments

Comments
 (0)