From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 94A341FF170 for ; Tue, 3 Dec 2024 13:24:06 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DD8AA321C; Tue, 3 Dec 2024 13:24:08 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Tue, 3 Dec 2024 13:23:26 +0100 Message-ID: <20241203122331.383398-3-c.heiss@proxmox.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241203122331.383398-1-c.heiss@proxmox.com> References: <20241203122331.383398-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.027 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [parse-answer.rs] Subject: [pve-devel] [PATCH installer 2/4] auto-installer: tests: create separate unit test for each answer file X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Defines all auto-installer "parse answer" tests in a declaratively manner, such that each test actually represent a single rust unit test. This improves the overall developer experience by having a proper, separate test function for each answer parsing test - making better use of the built-in facilities of `cargo test` at the same time. Also means that a failing test won't cancel all other tests. Signed-off-by: Christoph Heiss --- proxmox-auto-installer/tests/parse-answer.rs | 83 +++++++++++-------- .../{first-boot.json => first_boot.json} | 0 .../{first-boot.toml => first_boot.toml} | 0 3 files changed, 47 insertions(+), 36 deletions(-) rename proxmox-auto-installer/tests/resources/parse_answer/{first-boot.json => first_boot.json} (100%) rename proxmox-auto-installer/tests/resources/parse_answer/{first-boot.toml => first_boot.toml} (100%) diff --git a/proxmox-auto-installer/tests/parse-answer.rs b/proxmox-auto-installer/tests/parse-answer.rs index 65f8c1e..f6450cf 100644 --- a/proxmox-auto-installer/tests/parse-answer.rs +++ b/proxmox-auto-installer/tests/parse-answer.rs @@ -19,7 +19,7 @@ fn get_test_resource_path() -> Result { } fn get_answer(path: impl AsRef) -> Result { - let answer_raw = std::fs::read_to_string(path).unwrap(); + let answer_raw = fs::read_to_string(path).unwrap(); let answer: answer::Answer = toml::from_str(&answer_raw) .map_err(|err| format!("error parsing answer.toml: {err}")) .unwrap(); @@ -27,7 +27,7 @@ fn get_answer(path: impl AsRef) -> Result { Ok(answer) } -pub fn setup_test_basic(path: impl AsRef) -> (SetupInfo, LocaleInfo, RuntimeInfo, UdevInfo) { +fn setup_test_basic(path: impl AsRef) -> (SetupInfo, LocaleInfo, RuntimeInfo, UdevInfo) { let (installer_info, locale_info, mut runtime_info) = load_installer_setup_files(&path).unwrap(); @@ -46,40 +46,51 @@ pub fn setup_test_basic(path: impl AsRef) -> (SetupInfo, LocaleInfo, Runti (installer_info, locale_info, runtime_info, udev_info) } -#[test] -fn test_parse_answers() { - let path = get_test_resource_path().unwrap(); - let (setup_info, locales, runtime_info, udev_info) = setup_test_basic(&path); - let mut tests_path = path; - tests_path.push("parse_answer"); - let test_dir = fs::read_dir(tests_path.clone()).unwrap(); - - for file_entry in test_dir { - let file = file_entry.unwrap(); - if !file.file_type().unwrap().is_file() || file.file_name() == "readme" { - continue; - } - let p = file.path(); - let name = p.file_stem().unwrap().to_str().unwrap(); - let extension = p.extension().unwrap().to_str().unwrap(); - if extension == "toml" { - println!("Test: {name}"); - let answer = get_answer(p.clone()).unwrap(); - let config = - &parse_answer(&answer, &udev_info, &runtime_info, &locales, &setup_info).unwrap(); - println!("Selected disks: {:#?}", &config.disk_selection); - let config_json = serde_json::to_string(config); - let config: Value = serde_json::from_str(config_json.unwrap().as_str()).unwrap(); - let mut path = tests_path.clone(); - path.push(format!("{name}.json")); - let compare_raw = std::fs::read_to_string(&path).unwrap(); - let compare: Value = serde_json::from_str(&compare_raw).unwrap(); - if config != compare { - panic!( - "Test {} failed:\nleft: {:#?}\nright: {:#?}\n", - name, config, compare - ); - } +fn run_named_test(name: &str) { + let resource_path = get_test_resource_path().unwrap(); + let (setup_info, locales, runtime_info, udev_info) = setup_test_basic(&resource_path); + + let answer_path = resource_path.join(format!("parse_answer/{name}.toml")); + + let answer = get_answer(&answer_path).unwrap(); + let config = &parse_answer(&answer, &udev_info, &runtime_info, &locales, &setup_info).unwrap(); + + let config_json = serde_json::to_string(config); + let config: Value = serde_json::from_str(config_json.unwrap().as_str()).unwrap(); + + let json_path = resource_path.join(format!("parse_answer/{name}.json")); + let compare_raw = fs::read_to_string(&json_path).unwrap(); + let compare: Value = serde_json::from_str(&compare_raw).unwrap(); + assert_eq!(config, compare); +} + +mod tests { + mod parse_answer { + use super::super::run_named_test; + + macro_rules! declare_named_tests { + ($name:ident, $( $rest:ident ),* $(,)?) => { declare_named_tests!($name); declare_named_tests!($( $rest ),+); }; + ($name:ident) => { + #[test] + fn $name() { + run_named_test(&stringify!($name)); + } + }; } + + declare_named_tests!( + btrfs, + btrfs_raid_level_uppercase, + disk_match, + disk_match_all, + disk_match_any, + first_boot, + hashed_root_password, + minimal, + nic_matching, + specific_nic, + zfs, + zfs_raid_level_uppercase, + ); } } diff --git a/proxmox-auto-installer/tests/resources/parse_answer/first-boot.json b/proxmox-auto-installer/tests/resources/parse_answer/first_boot.json similarity index 100% rename from proxmox-auto-installer/tests/resources/parse_answer/first-boot.json rename to proxmox-auto-installer/tests/resources/parse_answer/first_boot.json diff --git a/proxmox-auto-installer/tests/resources/parse_answer/first-boot.toml b/proxmox-auto-installer/tests/resources/parse_answer/first_boot.toml similarity index 100% rename from proxmox-auto-installer/tests/resources/parse_answer/first-boot.toml rename to proxmox-auto-installer/tests/resources/parse_answer/first_boot.toml -- 2.47.0 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel