From 25b53eef7631274a076d38b6af6db49bd057259b Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 11 Oct 2021 09:52:23 +0200 Subject: [PATCH 1/3] cleanup of unused code and cleanup of cleanup procedure --- .../smoke/test_router_dhcphosts.py | 103 +++++++----------- 1 file changed, 41 insertions(+), 62 deletions(-) diff --git a/test/integration/smoke/test_router_dhcphosts.py b/test/integration/smoke/test_router_dhcphosts.py index aff50bc93c2c..dc39b4ffffbe 100644 --- a/test/integration/smoke/test_router_dhcphosts.py +++ b/test/integration/smoke/test_router_dhcphosts.py @@ -30,6 +30,7 @@ NATRule, NetworkACL, FireWallRule, + EgressFireWallRule, PublicIPAddress, NetworkOffering, Network, @@ -63,6 +64,7 @@ def setUpClass(cls): cls.testClient = super(TestRouterDHCPHosts, cls).getClsTestClient() cls.api_client = cls.testClient.getApiClient() + cls._cleanup = [] cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates @@ -86,12 +88,14 @@ def setUpClass(cls): admin=True, domainid=cls.domain.id ) + cls._cleanup.append(cls.account) cls.logger.debug("Creating Service Offering on zone %s" % (cls.zone.id)) cls.service_offering = ServiceOffering.create( cls.api_client, cls.services["service_offering"] ) + cls._cleanup.append(cls.service_offering) cls.services["isolated_network_offering"]["egress_policy"] = "true" @@ -99,6 +103,7 @@ def setUpClass(cls): cls.network_offering = NetworkOffering.create(cls.api_client, cls.services["isolated_network_offering"], conservemode=True) + cls._cleanup.append(cls.network_offering) cls.network_offering.update(cls.api_client, state='Enabled') @@ -109,6 +114,7 @@ def setUpClass(cls): domainid=cls.account.domainid, networkofferingid=cls.network_offering.id, zoneid=cls.zone.id) + cls._cleanup.append(cls.network) cls.logger.debug("Creating VM1 for Account %s using offering %s with IP 10.1.1.50" % (cls.account.name, cls.service_offering.id)) cls.vm_1 = VirtualMachine.create(cls.api_client, @@ -119,6 +125,7 @@ def setUpClass(cls): serviceofferingid=cls.service_offering.id, networkids=[str(cls.network.id)], ipaddress="10.1.1.50") + cls._cleanup.append(cls.vm_1) cls.logger.debug("Creating VM2 for Account %s using offering %s with IP 10.1.1.51" % (cls.account.name, cls.service_offering.id)) cls.vm_2 = VirtualMachine.create(cls.api_client, @@ -129,6 +136,7 @@ def setUpClass(cls): serviceofferingid=cls.service_offering.id, networkids=[str(cls.network.id)], ipaddress="10.1.1.51") + cls._cleanup.append(cls.vm_2) cls.services["natrule1"] = { "privateport": 22, @@ -150,23 +158,11 @@ def setUpClass(cls): "forward": "FORWARD" } - cls._cleanup = [ - cls.vm_2, - cls.network, - cls.network_offering, - cls.service_offering, - cls.account - ] - return @classmethod def tearDownClass(cls): - try: - cleanup_resources(cls.api_client, cls._cleanup) - except Exception as e: - raise Exception("Warning: Exception during cleanup : %s" % e) - return + super(TestRouterDHCPHosts, cls).tearDownClass() def setUp(self): self.apiclient = self.testClient.getApiClient() @@ -175,17 +171,21 @@ def setUp(self): return def tearDown(self): - try: - cleanup_resources(self.apiclient, self.cleanup) - except Exception as e: - raise Exception("Warning: Exception during cleanup : %s" % e) - return + super(TestRouterDHCPHosts, self).tearDown() def test_ssh_command(self, vm, nat_rule, rule_label): result = 'failed' try: ssh_command = "ping -c 3 8.8.8.8" self.logger.debug("SSH into VM with IP: %s" % nat_rule.ipaddress) + fwr = EgressFireWallRule.create(protocol="ICMP", + cidrlist="0.0.0.0/0", + destcidrlist="8.8.8.8/32", + icmpcode=-1, + icmptype=-1, + networkid=self.network.id + ) + self.cleanup.append(fwr) ssh = vm.get_ssh_client(ipaddress=nat_rule.ipaddress, port=self.services[rule_label]["publicport"], retries=5) result = str(ssh.execute(ssh_command)) @@ -294,7 +294,7 @@ def test_router_dhcphosts(self): public_ip = public_ips[0] self.logger.debug("Creating Firewall rule for VM ID: %s" % self.vm_1.id) - FireWallRule.create( + fwr1 = FireWallRule.create( self.apiclient, ipaddressid=public_ip.id, protocol=self.services["natrule1"]["protocol"], @@ -302,6 +302,7 @@ def test_router_dhcphosts(self): startport=self.services["natrule1"]["publicport"], endport=self.services["natrule1"]["publicport"] ) + self.cleanup.append(fwr1) self.logger.debug("Creating NAT rule for VM ID: %s" % self.vm_1.id) # Create NAT rule @@ -311,9 +312,10 @@ def test_router_dhcphosts(self): self.services["natrule1"], public_ip.id ) + self.cleanup.append(nat_rule1) self.logger.debug("Creating Firewall rule for VM ID: %s" % self.vm_2.id) - FireWallRule.create( + fwr2 = FireWallRule.create( self.apiclient, ipaddressid=public_ip.id, protocol=self.services["natrule2"]["protocol"], @@ -321,6 +323,7 @@ def test_router_dhcphosts(self): startport=self.services["natrule2"]["publicport"], endport=self.services["natrule2"]["publicport"] ) + self.cleanup.append(fwr2) self.logger.debug("Creating NAT rule for VM ID: %s" % self.vm_2.id) # Create NAT rule @@ -330,6 +333,7 @@ def test_router_dhcphosts(self): self.services["natrule2"], public_ip.id ) + self.cleanup.append(nat_rule2) nat_rules = list_nat_rules( self.apiclient, @@ -371,6 +375,7 @@ def test_router_dhcphosts(self): self.logger.debug("Deleting and Expunging VM %s with ip %s" % (self.vm_1.id, self.vm_1.nic[0].ipaddress)) self.vm_1.delete(self.apiclient) + self._cleanup.remove(self.vm_1) self.logger.debug("Creating new VM using the same IP as the one which was deleted => IP 10.1.1.50") self.vm_1 = VirtualMachine.create(self.apiclient, @@ -381,7 +386,6 @@ def test_router_dhcphosts(self): serviceofferingid=self.service_offering.id, networkids=[str(self.network.id)], ipaddress="10.1.1.50") - self.cleanup.append(self.vm_1) self.logger.debug("Testing DHCP hosts for VMs %s and %s" % (self.vm_1.id, self.vm_2.id)) @@ -403,6 +407,7 @@ def setUpClass(cls): cls.testClient = super(TestRouterDHCPOpts, cls).getClsTestClient() cls.api_client = cls.testClient.getApiClient() + cls._cleanup = [] cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates @@ -426,12 +431,14 @@ def setUpClass(cls): admin=True, domainid=cls.domain.id ) + cls._cleanup.append(cls.account) cls.logger.debug("Creating Service Offering on zone %s" % (cls.zone.id)) cls.service_offering = ServiceOffering.create( cls.api_client, cls.services["service_offering"] ) + cls._cleanup.append(cls.service_offering) cls.services["isolated_network_offering"]["egress_policy"] = "true" @@ -439,6 +446,7 @@ def setUpClass(cls): cls.network_offering = NetworkOffering.create(cls.api_client, cls.services["isolated_network_offering"], conservemode=True) + cls._cleanup.append(cls.network_offering) cls.network_offering.update(cls.api_client, state='Enabled') @@ -452,6 +460,7 @@ def setUpClass(cls): domainid=cls.account.domainid, networkofferingid=cls.network_offering.id, zoneid=cls.zone.id) + cls._cleanup.append(cls.network1) cls.services["network"]["name"] = "Test Network 2" cls.services["network"]["gateway"] = "10.1.2.1" cls.services["network"]["netmask"] = "255.255.255.0" @@ -461,6 +470,7 @@ def setUpClass(cls): domainid=cls.account.domainid, networkofferingid=cls.network_offering.id, zoneid=cls.zone.id) + cls._cleanup.append(cls.network2) cls.logger.debug("Creating VM1 for Account %s using offering %s with IP 10.1.1.50" % (cls.account.name, cls.service_offering.id)) cls.vm_1 = VirtualMachine.create(cls.api_client, cls.services["virtual_machine"], @@ -469,6 +479,7 @@ def setUpClass(cls): domainid=cls.domain.id, serviceofferingid=cls.service_offering.id, networkids=[str(cls.network1.id),str(cls.network2.id)]) + cls._cleanup.append(cls.vm_1) cls.logger.debug("Creating VM2 for Account %s using offering %s with IP 10.1.1.51" % (cls.account.name, cls.service_offering.id)) cls.vm_2 = VirtualMachine.create(cls.api_client, @@ -478,6 +489,7 @@ def setUpClass(cls): domainid=cls.domain.id, serviceofferingid=cls.service_offering.id, networkids=[str(cls.network2.id),str(cls.network1.id)]) + cls._cleanup.append(cls.vm_2) cls.services["natrule1"] = { "privateport": 22, @@ -499,25 +511,11 @@ def setUpClass(cls): "forward": "FORWARD" } - cls._cleanup = [ - cls.vm_1, - cls.vm_2, - cls.network1, - cls.network2, - cls.network_offering, - cls.service_offering, - cls.account - ] - return @classmethod def tearDownClass(cls): - try: - cleanup_resources(cls.api_client, cls._cleanup) - except Exception as e: - raise Exception("Warning: Exception during cleanup : %s" % e) - return + super(TestRouterDHCPOpts, cls).tearDownClass() def setUp(self): self.apiclient = self.testClient.getApiClient() @@ -526,30 +524,7 @@ def setUp(self): return def tearDown(self): - try: - cleanup_resources(self.apiclient, self.cleanup) - except Exception as e: - raise Exception("Warning: Exception during cleanup : %s" % e) - return - - def test_ssh_command(self, vm, nat_rule, rule_label): - result = 'failed' - try: - ssh_command = "ping -c 3 8.8.8.8" - self.logger.debug("SSH into VM with IP: %s" % nat_rule.ipaddress) - - ssh = vm.get_ssh_client(ipaddress=nat_rule.ipaddress, port=self.services[rule_label]["publicport"], retries=5) - result = str(ssh.execute(ssh_command)) - - self.logger.debug("SSH result: %s; COUNT is ==> %s" % (result, result.count(" 0% packet loss"))) - except: - self.fail("Failed to SSH into VM - %s" % (nat_rule.ipaddress)) - - self.assertEqual( - result.count(" 0% packet loss"), - 1, - "Ping to outside world from VM should be successful" - ) + super(TestRouterDHCPOpts, self).tearDown() def test_dhcphopts(self, ipaddress, router): hosts = list_hosts( @@ -684,7 +659,7 @@ def test_router_dhcp_opts(self): network2_public_ip = public_ips[0] self.logger.debug("Creating Firewall rule for VM ID: %s" % self.vm_1.id) - FireWallRule.create( + fwr1 = FireWallRule.create( self.apiclient, ipaddressid=network1_public_ip.id, protocol=self.services["natrule1"]["protocol"], @@ -692,6 +667,7 @@ def test_router_dhcp_opts(self): startport=self.services["natrule1"]["publicport"], endport=self.services["natrule1"]["publicport"] ) + self.cleanup.append(fwr1) self.logger.debug("Creating NAT rule for VM ID: %s" % self.vm_1.id) # Create NAT rule @@ -701,9 +677,10 @@ def test_router_dhcp_opts(self): self.services["natrule1"], network1_public_ip.id ) + self.cleanup.append(nat_rule1) self.logger.debug("Creating Firewall rule for VM ID: %s" % self.vm_2.id) - FireWallRule.create( + fwr2 = FireWallRule.create( self.apiclient, ipaddressid=network2_public_ip.id, protocol=self.services["natrule2"]["protocol"], @@ -711,6 +688,7 @@ def test_router_dhcp_opts(self): startport=self.services["natrule2"]["publicport"], endport=self.services["natrule2"]["publicport"] ) + self.cleanup.append(fwr2) self.logger.debug("Creating NAT rule for VM ID: %s" % self.vm_2.id) # Create NAT rule @@ -720,6 +698,7 @@ def test_router_dhcp_opts(self): self.services["natrule2"], network2_public_ip.id ) + self.cleanup.append(nat_rule2) nat_rules = list_nat_rules( self.apiclient, From d8350e91197976d6d998f0bea721de5bbd427fdf Mon Sep 17 00:00:00 2001 From: dahn Date: Tue, 12 Oct 2021 22:04:38 +0200 Subject: [PATCH 2/3] remove unnecessary egress rule --- test/integration/smoke/test_router_dhcphosts.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/integration/smoke/test_router_dhcphosts.py b/test/integration/smoke/test_router_dhcphosts.py index dc39b4ffffbe..9ac58a1779a7 100644 --- a/test/integration/smoke/test_router_dhcphosts.py +++ b/test/integration/smoke/test_router_dhcphosts.py @@ -178,15 +178,6 @@ def test_ssh_command(self, vm, nat_rule, rule_label): try: ssh_command = "ping -c 3 8.8.8.8" self.logger.debug("SSH into VM with IP: %s" % nat_rule.ipaddress) - fwr = EgressFireWallRule.create(protocol="ICMP", - cidrlist="0.0.0.0/0", - destcidrlist="8.8.8.8/32", - icmpcode=-1, - icmptype=-1, - networkid=self.network.id - ) - self.cleanup.append(fwr) - ssh = vm.get_ssh_client(ipaddress=nat_rule.ipaddress, port=self.services[rule_label]["publicport"], retries=5) result = str(ssh.execute(ssh_command)) From 5200a9fad96c405764ad5d59cea5b592644128e1 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 18 Oct 2021 16:40:38 +0200 Subject: [PATCH 3/3] remove rule and add rule removal --- test/integration/smoke/test_router_dhcphosts.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/integration/smoke/test_router_dhcphosts.py b/test/integration/smoke/test_router_dhcphosts.py index 9ac58a1779a7..fbf130041fe2 100644 --- a/test/integration/smoke/test_router_dhcphosts.py +++ b/test/integration/smoke/test_router_dhcphosts.py @@ -30,7 +30,6 @@ NATRule, NetworkACL, FireWallRule, - EgressFireWallRule, PublicIPAddress, NetworkOffering, Network, @@ -178,6 +177,7 @@ def test_ssh_command(self, vm, nat_rule, rule_label): try: ssh_command = "ping -c 3 8.8.8.8" self.logger.debug("SSH into VM with IP: %s" % nat_rule.ipaddress) + ssh = vm.get_ssh_client(ipaddress=nat_rule.ipaddress, port=self.services[rule_label]["publicport"], retries=5) result = str(ssh.execute(ssh_command)) @@ -364,9 +364,11 @@ def test_router_dhcphosts(self): self.test_dhcphosts(self.vm_1, router) self.test_dhcphosts(self.vm_2, router) + #TODO vm1 should not be appended/created at class level self.logger.debug("Deleting and Expunging VM %s with ip %s" % (self.vm_1.id, self.vm_1.nic[0].ipaddress)) self.vm_1.delete(self.apiclient) self._cleanup.remove(self.vm_1) + self.cleanup.remove(nat_rule1) self.logger.debug("Creating new VM using the same IP as the one which was deleted => IP 10.1.1.50") self.vm_1 = VirtualMachine.create(self.apiclient,