public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 2/4] auto-installer: tests: create separate unit test for each answer file
Date: Tue,  3 Dec 2024 13:23:26 +0100	[thread overview]
Message-ID: <20241203122331.383398-3-c.heiss@proxmox.com> (raw)
In-Reply-To: <20241203122331.383398-1-c.heiss@proxmox.com>

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 <c.heiss@proxmox.com>
---
 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<PathBuf, String> {
 }
 
 fn get_answer(path: impl AsRef<Path>) -> Result<Answer, String> {
-    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<Path>) -> Result<Answer, String> {
     Ok(answer)
 }
 
-pub fn setup_test_basic(path: impl AsRef<Path>) -> (SetupInfo, LocaleInfo, RuntimeInfo, UdevInfo) {
+fn setup_test_basic(path: impl AsRef<Path>) -> (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<Path>) -> (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


  parent reply	other threads:[~2024-12-03 12:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-03 12:23 [pve-devel] [PATCH installer 0/4] auto, tui: improve rust unit test setup Christoph Heiss
2024-12-03 12:23 ` [pve-devel] [PATCH installer 1/4] auto-installer: tests: add tests for raid level case-insensitivity Christoph Heiss
2024-12-03 12:23 ` Christoph Heiss [this message]
2024-12-03 12:23 ` [pve-devel] [PATCH installer 3/4] auto-installer: tests: add diff'ed assertions for answer parsing tests Christoph Heiss
2024-12-03 12:23 ` [pve-devel] [PATCH installer 4/4] tui: use pretty_assertions for object equal asserts Christoph Heiss
2024-12-03 17:20 ` [pve-devel] applied-series: [PATCH installer 0/4] auto, tui: improve rust unit test setup Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241203122331.383398-3-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal