[pve-devel] [PATCH installer 5/5] tui: add some tests for `NetworkInfo` -> `NetworkOptions` conversion

Christoph Heiss c.heiss at proxmox.com
Fri Oct 20 11:46:48 CEST 2023


Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 proxmox-tui-installer/src/options.rs | 110 ++++++++++++++++++++++++++-
 proxmox-tui-installer/src/utils.rs   |   2 +-
 2 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/proxmox-tui-installer/src/options.rs b/proxmox-tui-installer/src/options.rs
index 9e54da7..d4614aa 100644
--- a/proxmox-tui-installer/src/options.rs
+++ b/proxmox-tui-installer/src/options.rs
@@ -328,7 +328,7 @@ impl Default for PasswordOptions {
     }
 }
 
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
 pub struct NetworkOptions {
     pub ifname: String,
     pub fqdn: Fqdn,
@@ -450,3 +450,111 @@ impl InstallerOptions {
         ]
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::setup::{
+        Dns, Gateway, Interface, IsoInfo, IsoLocations, NetworkInfo, ProductConfig, ProxmoxProduct,
+        Routes, SetupInfo,
+    };
+    use std::{collections::HashMap, path::PathBuf};
+
+    fn fill_setup_info() {
+        crate::init_setup_info(SetupInfo {
+            config: ProductConfig {
+                fullname: "Proxmox VE".to_owned(),
+                product: ProxmoxProduct::PVE,
+                enable_btrfs: true,
+            },
+            iso_info: IsoInfo {
+                release: String::new(),
+                isorelease: String::new(),
+            },
+            locations: IsoLocations {
+                iso: PathBuf::new(),
+            },
+        });
+    }
+
+    #[test]
+    fn network_options_from_setup_network_info() {
+        fill_setup_info();
+
+        let mut interfaces = HashMap::new();
+        interfaces.insert(
+            "eth0".to_owned(),
+            Interface {
+                name: "eth0".to_owned(),
+                index: 0,
+                mac: "01:23:45:67:89:ab".to_owned(),
+                addresses: Some(vec![
+                    CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap()
+                ]),
+            },
+        );
+
+        let mut info = NetworkInfo {
+            dns: Dns {
+                domain: Some("bar.com".to_owned()),
+                dns: Vec::new(),
+            },
+            routes: Some(Routes {
+                gateway4: Some(Gateway {
+                    dev: "eth0".to_owned(),
+                    gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
+                }),
+                gateway6: None,
+            }),
+            interfaces,
+            hostname: Some("foo".to_owned()),
+        };
+
+        assert_eq!(
+            NetworkOptions::from(&info),
+            NetworkOptions {
+                ifname: "eth0".to_owned(),
+                fqdn: Fqdn::from("foo.bar.com").unwrap(),
+                address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
+                gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
+                dns_server: Ipv4Addr::UNSPECIFIED.into(),
+            }
+        );
+
+        info.hostname = None;
+        assert_eq!(
+            NetworkOptions::from(&info),
+            NetworkOptions {
+                ifname: "eth0".to_owned(),
+                fqdn: Fqdn::from("pve.bar.com").unwrap(),
+                address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
+                gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
+                dns_server: Ipv4Addr::UNSPECIFIED.into(),
+            }
+        );
+
+        info.dns.domain = None;
+        assert_eq!(
+            NetworkOptions::from(&info),
+            NetworkOptions {
+                ifname: "eth0".to_owned(),
+                fqdn: Fqdn::from("pve.example.invalid").unwrap(),
+                address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
+                gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
+                dns_server: Ipv4Addr::UNSPECIFIED.into(),
+            }
+        );
+
+        info.hostname = Some("foo".to_owned());
+        assert_eq!(
+            NetworkOptions::from(&info),
+            NetworkOptions {
+                ifname: "eth0".to_owned(),
+                fqdn: Fqdn::from("foo.example.invalid").unwrap(),
+                address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
+                gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
+                dns_server: Ipv4Addr::UNSPECIFIED.into(),
+            }
+        );
+    }
+}
diff --git a/proxmox-tui-installer/src/utils.rs b/proxmox-tui-installer/src/utils.rs
index 516f9c6..89349ed 100644
--- a/proxmox-tui-installer/src/utils.rs
+++ b/proxmox-tui-installer/src/utils.rs
@@ -33,7 +33,7 @@ pub enum CidrAddressParseError {
 /// assert_eq!(ipv4.to_string(), "192.168.0.1/24");
 /// assert_eq!(ipv6.to_string(), "2001:db8::c0a8:1/32");
 /// ```
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
 pub struct CidrAddress {
     addr: IpAddr,
     mask: usize,
-- 
2.42.0






More information about the pve-devel mailing list