Skip to content

Commit

Permalink
[antlir2] Add exclude option to the image_diff_test
Browse files Browse the repository at this point in the history
Summary: Add exclude option to the image_diff_test. Exclude list is a list of relative path prefixes.

Test Plan: Used in the image_diff_test for fbpkg installed in Chef solo.

Reviewed By: vmagro

Differential Revision: D47637931

fbshipit-source-id: 247f4c45cd972f9024f0a793e4018ae377b5c5cf
  • Loading branch information
Serge Dubrouski authored and facebook-github-bot committed Jul 20, 2023
1 parent bc90d8b commit 24103f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions antlir/antlir2/testing/image_diff_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def _impl(ctx: "context") -> ["provider"]:
ctx.attrs.image_diff_test[RunInfo],
cmd_args(ctx.attrs.layer[LayerInfo].parent.subvol_symlink, format = "--parent={}"),
cmd_args(ctx.attrs.layer[LayerInfo].subvol_symlink, format = "--layer={}"),
cmd_args(ctx.attrs.exclude, format = "--exclude={}"),
)
test_cmd = cmd_args(
base_cmd,
Expand All @@ -36,6 +37,7 @@ _image_diff_test = rule(
impl = _impl,
attrs = {
"diff": attrs.source(doc = "expected diff between the two"),
"exclude": attrs.list(attrs.string(), default = []),
"image_diff_test": attrs.default_only(attrs.exec_dep(default = "//antlir/antlir2/testing/image_diff_test:image-diff-test")),
"labels": attrs.list(attrs.string(), default = []),
"layer": attrs.dep(providers = [LayerInfo]),
Expand Down
30 changes: 30 additions & 0 deletions antlir/antlir2/testing/image_diff_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::io::Error;
use std::path::Path;
use std::path::PathBuf;

use antlir2_users::group::EtcGroup;
Expand Down Expand Up @@ -38,6 +39,9 @@ struct Args {
layer: PathBuf,
#[clap(subcommand)]
subcommand: Subcommand,
/// Exclude entries that start with given prefixes
#[clap(long)]
exclude: Vec<String>,
}

#[derive(Parser)]
Expand All @@ -50,8 +54,23 @@ enum Subcommand {
},
}

const ALWAYS_EXCLUDE: [&str; 2] = ["var/lib/rpm", "var/lib/dnf"];

fn exclude_entry(entry: &Path, exclude_list: &[String]) -> bool {
exclude_list
.iter()
.any(|e| entry.to_string_lossy().starts_with(e))
}

fn main() -> Result<()> {
let args = Args::parse();
let exclude_list: Vec<String> = args
.exclude
.iter()
.map(|e| e.to_owned())
.chain(ALWAYS_EXCLUDE.iter().map(|e| e.to_string()))
.collect();

let mut entries = BTreeMap::new();
let mut paths_that_exist_in_layer = HashSet::new();

Expand All @@ -77,6 +96,11 @@ fn main() -> Result<()> {
.path()
.strip_prefix(&args.layer)
.expect("this must be relative");

if exclude_entry(relpath, &exclude_list) {
continue;
}

let parent_path = args.parent.join(relpath);
let entry = Entry::new(fs_entry.path(), &layer_userdb, &layer_groupdb)
.with_context(|| format!("while building Entry for '{}", relpath.display()))?;
Expand Down Expand Up @@ -105,6 +129,7 @@ fn main() -> Result<()> {
}
}
}

for fs_entry in WalkDir::new(&args.parent) {
let fs_entry = fs_entry?;
if fs_entry.path() == args.parent {
Expand All @@ -114,6 +139,11 @@ fn main() -> Result<()> {
.path()
.strip_prefix(&args.parent)
.expect("this must be relative");

if exclude_entry(relpath, &exclude_list) {
continue;
}

if !paths_that_exist_in_layer.contains(relpath) {
let entry = Entry::new(fs_entry.path(), &parent_userdb, &parent_groupdb)
.with_context(|| format!("while building Entry for '{}'", relpath.display()))?;
Expand Down

0 comments on commit 24103f7

Please sign in to comment.