]> git.proxmox.com Git - pve-installer.git/commitdiff
common: add mocked variants for setup and ISO related info structs
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 19 Apr 2024 12:17:52 +0000 (14:17 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 22 Apr 2024 12:31:37 +0000 (14:31 +0200)
and add necessary derives for debug and serialize so that we can use
this for the auto-installer HTTP payload that gets send to the client
to be able to determine a dynamic answer file.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
proxmox-installer-common/src/setup.rs

index 773aa8d7a5fb197d4ec3251f4664eb78b8326c8c..7232f41f9ecda4f1894d9b2689d653827ee32eda 100644 (file)
@@ -20,7 +20,7 @@ use crate::{
 };
 
 #[allow(clippy::upper_case_acronyms)]
-#[derive(Clone, Copy, Deserialize, PartialEq)]
+#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Serialize)]
 #[serde(rename_all = "lowercase")]
 pub enum ProxmoxProduct {
     PVE,
@@ -48,7 +48,7 @@ impl fmt::Display for ProxmoxProduct {
     }
 }
 
-#[derive(Clone, Deserialize)]
+#[derive(Debug, Clone, Deserialize, Serialize)]
 pub struct ProductConfig {
     pub fullname: String,
     pub product: ProxmoxProduct,
@@ -56,18 +56,48 @@ pub struct ProductConfig {
     pub enable_btrfs: bool,
 }
 
-#[derive(Clone, Deserialize)]
+impl ProductConfig {
+    /// A mocked ProductConfig simulating a Proxmox VE environment.
+    pub fn mocked() -> Self {
+        Self {
+            fullname: String::from("Proxmox VE (mocked)"),
+            product: ProxmoxProduct::PVE,
+            enable_btrfs: true,
+        }
+    }
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
 pub struct IsoInfo {
     pub release: String,
     pub isorelease: String,
 }
 
+impl IsoInfo {
+    /// A mocked IsoInfo with some edge case to convey that this is not necessarily purely numeric.
+    pub fn mocked() -> Self {
+        Self {
+            release: String::from("42.1"),
+            isorelease: String::from("mocked-1"),
+        }
+    }
+}
+
 /// Paths in the ISO environment containing installer data.
 #[derive(Clone, Deserialize)]
 pub struct IsoLocations {
     pub iso: PathBuf,
 }
 
+impl IsoLocations {
+    /// A mocked location, uses the current working directory by default
+    pub fn mocked() -> Self {
+        Self {
+            iso: std::env::current_dir().unwrap_or("/dev/null".into()),
+        }
+    }
+}
+
 #[derive(Clone, Deserialize)]
 pub struct SetupInfo {
     #[serde(rename = "product-cfg")]
@@ -77,6 +107,18 @@ pub struct SetupInfo {
     pub locations: IsoLocations,
 }
 
+impl SetupInfo {
+    /// Return a mocked SetupInfo that is very similar to how our actual ones look like and should
+    /// be good enough for testing.
+    pub fn mocked() -> Self {
+        Self {
+            config: ProductConfig::mocked(),
+            iso_info: IsoInfo::mocked(),
+            locations: IsoLocations::mocked(),
+        }
+    }
+}
+
 #[derive(Clone, Deserialize)]
 pub struct CountryInfo {
     pub name: String,