]>
Commit | Line | Data |
---|---|---|
c4b85794 AL |
1 | use std::path::PathBuf; |
2 | ||
3 | use serde_json::Value; | |
4 | use std::fs; | |
5 | ||
6 | use proxmox_auto_installer::answer; | |
7 | use proxmox_auto_installer::answer::Answer; | |
8 | use proxmox_auto_installer::udevinfo::UdevInfo; | |
9 | use proxmox_auto_installer::utils::parse_answer; | |
10 | ||
11 | use proxmox_installer_common::setup::{read_json, LocaleInfo, RuntimeInfo, SetupInfo}; | |
12 | ||
13 | fn get_test_resource_path() -> Result<PathBuf, String> { | |
14 | Ok(std::env::current_dir() | |
15 | .expect("current dir failed") | |
16 | .join("tests/resources")) | |
17 | } | |
18 | fn get_answer(path: PathBuf) -> Result<Answer, String> { | |
810c860d | 19 | let answer_raw = std::fs::read_to_string(path).unwrap(); |
c4b85794 AL |
20 | let answer: answer::Answer = toml::from_str(&answer_raw) |
21 | .map_err(|err| format!("error parsing answer.toml: {err}")) | |
22 | .unwrap(); | |
23 | ||
24 | Ok(answer) | |
25 | } | |
26 | ||
27 | fn setup_test_basic(path: &PathBuf) -> (SetupInfo, LocaleInfo, RuntimeInfo, UdevInfo) { | |
28 | let installer_info: SetupInfo = { | |
29 | let mut path = path.clone(); | |
30 | path.push("iso-info.json"); | |
31 | ||
32 | read_json(&path) | |
33 | .map_err(|err| format!("Failed to retrieve setup info: {err}")) | |
34 | .unwrap() | |
35 | }; | |
36 | ||
37 | let locale_info = { | |
38 | let mut path = path.clone(); | |
39 | path.push("locales.json"); | |
40 | ||
41 | read_json(&path) | |
42 | .map_err(|err| format!("Failed to retrieve locale info: {err}")) | |
43 | .unwrap() | |
44 | }; | |
45 | ||
46 | let mut runtime_info: RuntimeInfo = { | |
47 | let mut path = path.clone(); | |
48 | path.push("run-env-info.json"); | |
49 | ||
50 | read_json(&path) | |
51 | .map_err(|err| format!("Failed to retrieve runtime environment info: {err}")) | |
52 | .unwrap() | |
53 | }; | |
54 | ||
55 | let udev_info: UdevInfo = { | |
56 | let mut path = path.clone(); | |
57 | path.push("run-env-udev.json"); | |
58 | ||
59 | read_json(&path) | |
60 | .map_err(|err| format!("Failed to retrieve udev info details: {err}")) | |
61 | .unwrap() | |
62 | }; | |
63 | runtime_info.disks.sort(); | |
64 | if runtime_info.disks.is_empty() { | |
65 | panic!("disk list is empty!"); | |
66 | } | |
67 | (installer_info, locale_info, runtime_info, udev_info) | |
68 | } | |
69 | ||
70 | #[test] | |
71 | fn test_parse_answers() { | |
72 | let path = get_test_resource_path().unwrap(); | |
73 | let (setup_info, locales, runtime_info, udev_info) = setup_test_basic(&path); | |
74 | let mut tests_path = path.clone(); | |
75 | tests_path.push("parse_answer"); | |
76 | let test_dir = fs::read_dir(tests_path.clone()).unwrap(); | |
77 | ||
78 | for file_entry in test_dir { | |
79 | let file = file_entry.unwrap(); | |
80 | if !file.file_type().unwrap().is_file() || file.file_name() == "readme" { | |
81 | continue; | |
82 | } | |
83 | let p = file.path(); | |
84 | let name = p.file_stem().unwrap().to_str().unwrap(); | |
85 | let extension = p.extension().unwrap().to_str().unwrap(); | |
86 | if extension == "toml" { | |
87 | println!("Test: {name}"); | |
88 | let answer = get_answer(p.clone()).unwrap(); | |
89 | let config = | |
90 | &parse_answer(&answer, &udev_info, &runtime_info, &locales, &setup_info).unwrap(); | |
91 | println!("Selected disks: {:#?}", &config.disk_selection); | |
92 | let config_json = serde_json::to_string(config); | |
93 | let config: Value = serde_json::from_str(config_json.unwrap().as_str()).unwrap(); | |
94 | let mut path = tests_path.clone(); | |
95 | path.push(format!("{name}.json")); | |
96 | let compare_raw = std::fs::read_to_string(&path).unwrap(); | |
97 | let compare: Value = serde_json::from_str(&compare_raw).unwrap(); | |
98 | if config != compare { | |
99 | panic!( | |
100 | "Test {} failed:\nleft: {:#?}\nright: {:#?}\n", | |
101 | name, config, compare | |
102 | ); | |
103 | } | |
104 | } | |
105 | } | |
106 | } |