From 5b1459e623b0e0246a0bffade2945da8196bccf3 Mon Sep 17 00:00:00 2001 From: Archana Choudhary Date: Thu, 22 May 2025 14:03:54 +0000 Subject: [PATCH] genpolicy: test framework: enable config map usage This patch improves the test framework for the genpolicy tool by enabling the use of config maps. Signed-off-by: Archana Choudhary --- src/tools/genpolicy/tests/policy/main.rs | 30 ++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/tools/genpolicy/tests/policy/main.rs b/src/tools/genpolicy/tests/policy/main.rs index 0f61c9f30..e6c730c9c 100644 --- a/src/tools/genpolicy/tests/policy/main.rs +++ b/src/tools/genpolicy/tests/policy/main.rs @@ -63,14 +63,36 @@ mod tests { /// a JSON list of [TestCase] instances. Each instance will be of type enum TestRequest, /// with the tag `type` listing the exact type of request. async fn runtests(test_case_dir: &str) { - // Prepare temp dir for running genpolicy. - let (workdir, testdata_dir) = prepare_workdir(test_case_dir, &["pod.yaml"]); + // Check if config_map.yaml exists. + // If it does, we need to copy it to the workdir. + let is_config_map_file_present = path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/policy/testdata") + .join(test_case_dir) + .join("config_map.yaml") + .exists(); - // Run the command and return the generated policy. + let files_to_copy = if is_config_map_file_present { + vec!["pod.yaml", "config_map.yaml"] + } else { + vec!["pod.yaml"] + }; + + // Prepare temp dir for running genpolicy. + let (workdir, testdata_dir) = prepare_workdir(test_case_dir, &files_to_copy); + + let config_files = if is_config_map_file_present { + Some(vec![workdir + .join("config_map.yaml") + .to_str() + .unwrap() + .to_string()]) + } else { + None + }; let config = genpolicy::utils::Config { base64_out: false, - config_files: None, + config_files, containerd_socket_path: None, // Some(String::from("/var/run/containerd/containerd.sock")), insecure_registries: Vec::new(), layers_cache: genpolicy::layers_cache::ImageLayersCache::new(&None),