From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id ECB169166F for ; Thu, 4 Apr 2024 11:53:01 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CE64E36211 for ; Thu, 4 Apr 2024 11:52:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Thu, 4 Apr 2024 11:52:28 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2459B45248 for ; Thu, 4 Apr 2024 11:52:28 +0200 (CEST) From: Stefan Lendl To: s.lendl@proxmox.com, pbs-devel@lists.proxmox.com Date: Thu, 4 Apr 2024 11:51:45 +0200 Message-ID: <20240404095151.184141-4-s.lendl@proxmox.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404095151.184141-1-s.lendl@proxmox.com> References: <20240404095151.184141-1-s.lendl@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.023 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 SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup v3 2/9] tests: move network tests to parser.rs X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Apr 2024 09:53:02 -0000 All current tests in network/mod.rs only test parser functionality and should therefore live in the parser module. Signed-off-by: Stefan Lendl Tested-by: Lukas Wagner Reviewed-by: Lukas Wagner --- pbs-config/src/network/mod.rs | 147 ------------------------------- pbs-config/src/network/parser.rs | 147 +++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 147 deletions(-) diff --git a/pbs-config/src/network/mod.rs b/pbs-config/src/network/mod.rs index e2008b7c..21187ec2 100644 --- a/pbs-config/src/network/mod.rs +++ b/pbs-config/src/network/mod.rs @@ -503,150 +503,3 @@ pub fn complete_port_list(arg: &str, _param: &HashMap) -> Vec Result<(), Error> { - let input = ""; - - let mut parser = NetworkParser::new(input.as_bytes()); - - let config = parser.parse_interfaces(None)?; - - let output = String::try_from(config)?; - - let expected = "auto lo\niface lo inet loopback\n\n"; - assert_eq!(output, expected); - - // run again using output as input - let mut parser = NetworkParser::new(output.as_bytes()); - - let config = parser.parse_interfaces(None)?; - - let output = String::try_from(config)?; - - assert_eq!(output, expected); - - Ok(()) - } - - #[test] - fn test_network_config_create_lo_2() -> Result<(), Error> { - let input = "#c1\n\n#c2\n\niface test inet manual\n"; - - let mut parser = NetworkParser::new(input.as_bytes()); - - let config = parser.parse_interfaces(None)?; - - let output = String::try_from(config)?; - - // Note: loopback should be added in front of other interfaces - let expected = "#c1\n#c2\n\nauto lo\niface lo inet loopback\n\niface test inet manual\n\n"; - assert_eq!(output, expected); - - Ok(()) - } - - #[test] - fn test_network_config_parser_no_blank_1() -> Result<(), Error> { - let input = "auto lo\n\ - iface lo inet loopback\n\ - iface lo inet6 loopback\n\ - auto ens18\n\ - iface ens18 inet static\n\ - \taddress 192.168.20.144/20\n\ - \tgateway 192.168.16.1\n\ - # comment\n\ - iface ens20 inet static\n\ - \taddress 192.168.20.145/20\n\ - iface ens21 inet manual\n\ - iface ens22 inet manual\n"; - - let mut parser = NetworkParser::new(input.as_bytes()); - - let config = parser.parse_interfaces(None)?; - - let output = String::try_from(config)?; - - let expected = "auto lo\n\ - iface lo inet loopback\n\ - \n\ - iface lo inet6 loopback\n\ - \n\ - auto ens18\n\ - iface ens18 inet static\n\ - \taddress 192.168.20.144/20\n\ - \tgateway 192.168.16.1\n\ - #comment\n\ - \n\ - iface ens20 inet static\n\ - \taddress 192.168.20.145/20\n\ - \n\ - iface ens21 inet manual\n\ - \n\ - iface ens22 inet manual\n\ - \n"; - assert_eq!(output, expected); - - Ok(()) - } - - #[test] - fn test_network_config_parser_no_blank_2() -> Result<(), Error> { - // Adapted from bug 2926 - let input = "### Hetzner Online GmbH installimage\n\ - \n\ - source /etc/network/interfaces.d/*\n\ - \n\ - auto lo\n\ - iface lo inet loopback\n\ - iface lo inet6 loopback\n\ - \n\ - auto enp4s0\n\ - iface enp4s0 inet static\n\ - \taddress 10.10.10.10/24\n\ - \tgateway 10.10.10.1\n\ - \t# route 10.10.20.10/24 via 10.10.20.1\n\ - \tup route add -net 10.10.20.10 netmask 255.255.255.0 gw 10.10.20.1 dev enp4s0\n\ - \n\ - iface enp4s0 inet6 static\n\ - \taddress fe80::5496:35ff:fe99:5a6a/64\n\ - \tgateway fe80::1\n"; - - let mut parser = NetworkParser::new(input.as_bytes()); - - let config = parser.parse_interfaces(None)?; - - let output = String::try_from(config)?; - - let expected = "### Hetzner Online GmbH installimage\n\ - \n\ - source /etc/network/interfaces.d/*\n\ - \n\ - auto lo\n\ - iface lo inet loopback\n\ - \n\ - iface lo inet6 loopback\n\ - \n\ - auto enp4s0\n\ - iface enp4s0 inet static\n\ - \taddress 10.10.10.10/24\n\ - \tgateway 10.10.10.1\n\ - \t# route 10.10.20.10/24 via 10.10.20.1\n\ - \tup route add -net 10.10.20.10 netmask 255.255.255.0 gw 10.10.20.1 dev enp4s0\n\ - \n\ - iface enp4s0 inet6 static\n\ - \taddress fe80::5496:35ff:fe99:5a6a/64\n\ - \tgateway fe80::1\n\ - \n"; - assert_eq!(output, expected); - - Ok(()) - } -} diff --git a/pbs-config/src/network/parser.rs b/pbs-config/src/network/parser.rs index ec2c64eb..1b55569a 100644 --- a/pbs-config/src/network/parser.rs +++ b/pbs-config/src/network/parser.rs @@ -602,3 +602,150 @@ impl NetworkParser { Ok(config) } } + +#[cfg(test)] +mod test { + + use anyhow::Error; + + use super::*; + + #[test] + fn test_network_config_create_lo_1() -> Result<(), Error> { + let input = ""; + + let mut parser = NetworkParser::new(input.as_bytes()); + + let config = parser.parse_interfaces(None)?; + + let output = String::try_from(config)?; + + let expected = "auto lo\niface lo inet loopback\n\n"; + assert_eq!(output, expected); + + // run again using output as input + let mut parser = NetworkParser::new(output.as_bytes()); + + let config = parser.parse_interfaces(None)?; + + let output = String::try_from(config)?; + + assert_eq!(output, expected); + + Ok(()) + } + + #[test] + fn test_network_config_create_lo_2() -> Result<(), Error> { + let input = "#c1\n\n#c2\n\niface test inet manual\n"; + + let mut parser = NetworkParser::new(input.as_bytes()); + + let config = parser.parse_interfaces(None)?; + + let output = String::try_from(config)?; + + // Note: loopback should be added in front of other interfaces + let expected = "#c1\n#c2\n\nauto lo\niface lo inet loopback\n\niface test inet manual\n\n"; + assert_eq!(output, expected); + + Ok(()) + } + + #[test] + fn test_network_config_parser_no_blank_1() -> Result<(), Error> { + let input = "auto lo\n\ + iface lo inet loopback\n\ + iface lo inet6 loopback\n\ + auto ens18\n\ + iface ens18 inet static\n\ + \taddress 192.168.20.144/20\n\ + \tgateway 192.168.16.1\n\ + # comment\n\ + iface ens20 inet static\n\ + \taddress 192.168.20.145/20\n\ + iface ens21 inet manual\n\ + iface ens22 inet manual\n"; + + let mut parser = NetworkParser::new(input.as_bytes()); + + let config = parser.parse_interfaces(None)?; + + let output = String::try_from(config)?; + + let expected = "auto lo\n\ + iface lo inet loopback\n\ + \n\ + iface lo inet6 loopback\n\ + \n\ + auto ens18\n\ + iface ens18 inet static\n\ + \taddress 192.168.20.144/20\n\ + \tgateway 192.168.16.1\n\ + #comment\n\ + \n\ + iface ens20 inet static\n\ + \taddress 192.168.20.145/20\n\ + \n\ + iface ens21 inet manual\n\ + \n\ + iface ens22 inet manual\n\ + \n"; + assert_eq!(output, expected); + + Ok(()) + } + + #[test] + fn test_network_config_parser_no_blank_2() -> Result<(), Error> { + // Adapted from bug 2926 + let input = "### Hetzner Online GmbH installimage\n\ + \n\ + source /etc/network/interfaces.d/*\n\ + \n\ + auto lo\n\ + iface lo inet loopback\n\ + iface lo inet6 loopback\n\ + \n\ + auto enp4s0\n\ + iface enp4s0 inet static\n\ + \taddress 10.10.10.10/24\n\ + \tgateway 10.10.10.1\n\ + \t# route 10.10.20.10/24 via 10.10.20.1\n\ + \tup route add -net 10.10.20.10 netmask 255.255.255.0 gw 10.10.20.1 dev enp4s0\n\ + \n\ + iface enp4s0 inet6 static\n\ + \taddress fe80::5496:35ff:fe99:5a6a/64\n\ + \tgateway fe80::1\n"; + + let mut parser = NetworkParser::new(input.as_bytes()); + + let config = parser.parse_interfaces(None)?; + + let output = String::try_from(config)?; + + let expected = "### Hetzner Online GmbH installimage\n\ + \n\ + source /etc/network/interfaces.d/*\n\ + \n\ + auto lo\n\ + iface lo inet loopback\n\ + \n\ + iface lo inet6 loopback\n\ + \n\ + auto enp4s0\n\ + iface enp4s0 inet static\n\ + \taddress 10.10.10.10/24\n\ + \tgateway 10.10.10.1\n\ + \t# route 10.10.20.10/24 via 10.10.20.1\n\ + \tup route add -net 10.10.20.10 netmask 255.255.255.0 gw 10.10.20.1 dev enp4s0\n\ + \n\ + iface enp4s0 inet6 static\n\ + \taddress fe80::5496:35ff:fe99:5a6a/64\n\ + \tgateway fe80::1\n\ + \n"; + assert_eq!(output, expected); + + Ok(()) + } +} -- 2.44.0