From 99318caa67f25932b652ea53c1742b6c1e5b0927 Mon Sep 17 00:00:00 2001 From: Wilder Rodrigues Date: Tue, 27 Oct 2015 19:45:34 +0100 Subject: [PATCH 1/3] CLOUDSTACK-8991 - Make sure the public IP is removed form the router before checking - Adding proper logger to the tests - Changing message that was copied/pasted from another assert --- test/integration/smoke/test_network.py | 56 +++++++++++++++----------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/test/integration/smoke/test_network.py b/test/integration/smoke/test_network.py index 05dd67aeb12..a89055c853d 100644 --- a/test/integration/smoke/test_network.py +++ b/test/integration/smoke/test_network.py @@ -50,9 +50,14 @@ from nose.plugins.attrib import attr from ddt import ddt, data # Import System modules import time +import logging _multiprocess_shared_ = True +logger = logging.getLogger('TestNetworkOps') +stream_handler = logging.StreamHandler() +logger.setLevel(logging.DEBUG) +logger.addHandler(stream_handler) class TestPublicIP(cloudstackTestCase): @@ -390,7 +395,7 @@ class TestPortForwarding(cloudstackTestCase): ) # SSH virtual machine to test port forwarding try: - self.debug("SSHing into VM with IP address %s with NAT IP %s" % + logger.debug("SSHing into VM with IP address %s with NAT IP %s" % ( self.virtual_machine.ipaddress, src_nat_ip_addr.ipaddress @@ -424,7 +429,7 @@ class TestPortForwarding(cloudstackTestCase): # Check if the Public SSH port is inaccessible with self.assertRaises(Exception): - self.debug( + logger.debug( "SSHing into VM with IP address %s after NAT rule deletion" % self.virtual_machine.ipaddress) @@ -518,7 +523,7 @@ class TestPortForwarding(cloudstackTestCase): ) try: - self.debug("SSHing into VM with IP address %s with NAT IP %s" % + logger.debug("SSHing into VM with IP address %s with NAT IP %s" % ( self.virtual_machine.ipaddress, ip_address.ipaddress.ipaddress @@ -538,11 +543,11 @@ class TestPortForwarding(cloudstackTestCase): id=nat_rule.id ) except CloudstackAPIException: - self.debug("Nat Rule is deleted") + logger.debug("Nat Rule is deleted") # Check if the Public SSH port is inaccessible with self.assertRaises(Exception): - self.debug( + logger.debug( "SSHing into VM with IP address %s after NAT rule deletion" % self.virtual_machine.ipaddress) @@ -673,8 +678,8 @@ class TestRebootRouter(cloudstackTestCase): # Retrieve router for the user account - self.debug("Public IP: %s" % self.vm_1.ssh_ip) - self.debug("Public IP: %s" % self.public_ip.ipaddress.ipaddress) + logger.debug("Public IP: %s" % self.vm_1.ssh_ip) + logger.debug("Public IP: %s" % self.public_ip.ipaddress.ipaddress) routers = list_routers( self.apiclient, account=self.account.name, @@ -688,7 +693,7 @@ class TestRebootRouter(cloudstackTestCase): router = routers[0] - self.debug("Rebooting the router (ID: %s)" % router.id) + logger.debug("Rebooting the router (ID: %s)" % router.id) cmd = rebootRouter.rebootRouterCmd() cmd.id = router.id @@ -710,7 +715,7 @@ class TestRebootRouter(cloudstackTestCase): vm = list_vm_response[0] if vm.state == 'Running': - self.debug("VM state: %s" % vm.state) + logger.debug("VM state: %s" % vm.state) break if timeout == 0: @@ -722,7 +727,7 @@ class TestRebootRouter(cloudstackTestCase): # we should be able to SSH after successful reboot try: - self.debug("SSH into VM (ID : %s ) after reboot" % self.vm_1.id) + logger.debug("SSH into VM (ID : %s ) after reboot" % self.vm_1.id) SshClient( self.public_ip.ipaddress.ipaddress, @@ -825,7 +830,7 @@ class TestReleaseIP(cloudstackTestCase): def test_releaseIP(self): """Test for release public IP address""" - self.debug("Deleting Public IP : %s" % self.ip_addr.id) + logger.debug("Deleting Public IP : %s" % self.ip_addr.id) self.ip_address.delete(self.apiclient) @@ -854,9 +859,9 @@ class TestReleaseIP(cloudstackTestCase): self.apiclient, id=self.nat_rule.id ) - self.debug("List NAT Rule response" + str(list_nat_rule)) + logger.debug("List NAT Rule response" + str(list_nat_rule)) except CloudstackAPIException: - self.debug("Port Forwarding Rule is deleted") + logger.debug("Port Forwarding Rule is deleted") # listLoadBalancerRules should not list # associated rules with Public IP address @@ -865,9 +870,9 @@ class TestReleaseIP(cloudstackTestCase): self.apiclient, id=self.lb_rule.id ) - self.debug("List LB Rule response" + str(list_lb_rule)) + logger.debug("List LB Rule response" + str(list_lb_rule)) except CloudstackAPIException: - self.debug("Port Forwarding Rule is deleted") + logger.debug("Port Forwarding Rule is deleted") # SSH Attempt though public IP should fail with self.assertRaises(Exception): @@ -982,7 +987,7 @@ class TestDeleteAccount(cloudstackTestCase): domainid=self.account.domainid ) except CloudstackAPIException: - self.debug("Port Forwarding Rule is deleted") + logger.debug("Port Forwarding Rule is deleted") # ListPortForwardingRules should not # list associated rules with deleted account @@ -993,7 +998,7 @@ class TestDeleteAccount(cloudstackTestCase): domainid=self.account.domainid ) except CloudstackAPIException: - self.debug("NATRule is deleted") + logger.debug("NATRule is deleted") # Retrieve router for the user account try: @@ -1008,7 +1013,7 @@ class TestDeleteAccount(cloudstackTestCase): "Check routers are properly deleted." ) except CloudstackAPIException: - self.debug("Router is deleted") + logger.debug("Router is deleted") except Exception as e: raise Exception( @@ -1187,6 +1192,9 @@ class TestRouterRules(cloudstackTestCase): self.lb_rule.delete(self.apiclient) else: self.nat_rule.delete(self.apiclient) + + ipaddressobj.delete(self.apiclient) + return @data(STATIC_NAT_RULE, NAT_RULE, LB_RULE) @@ -1216,13 +1224,13 @@ class TestRouterRules(cloudstackTestCase): listall=True)[0] response = self.getCommandResultFromRouter(router, "ip addr") - self.debug(response) + logger.debug(response) stringToMatch = "inet %s" % self.ipaddress.ipaddress.ipaddress self.assertTrue(stringToMatch in str(response), "IP address is\ - not removed from VR even after disabling statin NAT") + not added to the VR!") try: - self.debug("SSHing into VM with IP address %s with NAT IP %s" % + logger.debug("SSHing into VM with IP address %s with NAT IP %s" % ( self.virtual_machine.ipaddress, self.ipaddress.ipaddress.ipaddress @@ -1242,14 +1250,14 @@ class TestRouterRules(cloudstackTestCase): self.removeNetworkRules(rule=value, ipaddressobj=self.ipaddress) response = self.getCommandResultFromRouter(router, "ip addr") - self.debug(response) + logger.debug(response) stringToMatch = "inet %s" % self.ipaddress.ipaddress.ipaddress self.assertFalse(stringToMatch in str(response), "IP address is\ - not removed from VR even after disabling statin NAT") + not removed from VR even after disabling stat in NAT") # Check if the Public SSH port is inaccessible with self.assertRaises(Exception): - self.debug( + logger.debug( "SSHing into VM with IP address %s after NAT rule deletion" % self.virtual_machine.ipaddress) From 7197cf2e24a02dbc5cd4c1964e7c8baadc112340 Mon Sep 17 00:00:00 2001 From: Wilder Rodrigues Date: Tue, 27 Oct 2015 19:46:13 +0100 Subject: [PATCH 2/3] CLOUDSTACK-8991 - Remove public IP form interface in case add = false --- .../config/opt/cloud/bin/cs/CsAddress.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py index b80187ab739..6d26de63a7e 100755 --- a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py @@ -103,6 +103,7 @@ class CsAddress(CsDataBag): for address in self.dbag[dev]: ip.setAddress(address) + logging.info("Address found in DataBag ==> %s" % address) if ip.configured(): logging.info( @@ -263,10 +264,17 @@ class CsIP: return self.address def configure(self, address): - logging.info( - "Configuring address %s on device %s", self.ip(), self.dev) - cmd = "ip addr add dev %s %s brd +" % (self.dev, self.ip()) - subprocess.call(cmd, shell=True) + # When "add" is false, it means that the IP has to be removed. + if address["add"]: + try: + logging.info("Configuring address %s on device %s", self.ip(), self.dev) + cmd = "ip addr add dev %s %s brd +" % (self.dev, self.ip()) + subprocess.call(cmd, shell=True) + except Exception as e: + logging.info("Exception occurred ==> %s" % e) + + else: + self.delete(self.ip()) self.post_configure(address) def post_configure(self, address): @@ -602,9 +610,8 @@ class CsIP: if self.dev in bag.keys(): for address in bag[self.dev]: self.setAddress(address) - if self.hasIP(ip): - found = True - if self.is_guest_gateway(address, ip): + if (self.hasIP(ip) or self.is_guest_gateway(address, ip)) and addess["add"]: + logging.debig("The IP address in '%s' will be configured" % address) found = True if not found: self.delete(ip) @@ -620,7 +627,7 @@ class CsIP: gw = interface.get_gateway() logging.info("Interface has the following gateway ==> %s", gw) - + if bag['nw_type'] == "guest" and rip == gw: return True return False From 7d40814ba1fdef8f7260a120b48358c8585a4570 Mon Sep 17 00:00:00 2001 From: Wilder Rodrigues Date: Tue, 27 Oct 2015 19:46:48 +0100 Subject: [PATCH 3/3] CLOUDSTACK-8991 - Process the IPs that have been removed --- .../config/opt/cloud/bin/cs/CsAddress.py | 4 +-- .../debian/config/opt/cloud/bin/cs_ip.py | 27 ++++++++++--------- test/integration/smoke/test_network.py | 10 +++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py index 6d26de63a7e..91ee87348af 100755 --- a/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs/CsAddress.py @@ -610,8 +610,8 @@ class CsIP: if self.dev in bag.keys(): for address in bag[self.dev]: self.setAddress(address) - if (self.hasIP(ip) or self.is_guest_gateway(address, ip)) and addess["add"]: - logging.debig("The IP address in '%s' will be configured" % address) + if (self.hasIP(ip) or self.is_guest_gateway(address, ip)) and address["add"]: + logging.debug("The IP address in '%s' will be configured" % address) found = True if not found: self.delete(ip) diff --git a/systemvm/patches/debian/config/opt/cloud/bin/cs_ip.py b/systemvm/patches/debian/config/opt/cloud/bin/cs_ip.py index 14003d9f4c4..efcf311296f 100755 --- a/systemvm/patches/debian/config/opt/cloud/bin/cs_ip.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/cs_ip.py @@ -27,17 +27,18 @@ def merge(dbag, ip): for address in dbag[dev]: if address['public_ip'] == ip['public_ip']: dbag[dev].remove(address) - if ip['add']: - ipo = IPNetwork(ip['public_ip'] + '/' + ip['netmask']) - ip['device'] = 'eth' + str(ip['nic_dev_id']) - ip['broadcast'] = str(ipo.broadcast) - ip['cidr'] = str(ipo.ip) + '/' + str(ipo.prefixlen) - ip['size'] = str(ipo.prefixlen) - ip['network'] = str(ipo.network) + '/' + str(ipo.prefixlen) - if 'nw_type' not in ip.keys(): - ip['nw_type'] = 'public' - if ip['nw_type'] == 'control': - dbag['eth' + str(ip['nic_dev_id'])] = [ip] - else: - dbag.setdefault('eth' + str(ip['nic_dev_id']), []).append(ip) + + ipo = IPNetwork(ip['public_ip'] + '/' + ip['netmask']) + ip['device'] = 'eth' + str(ip['nic_dev_id']) + ip['broadcast'] = str(ipo.broadcast) + ip['cidr'] = str(ipo.ip) + '/' + str(ipo.prefixlen) + ip['size'] = str(ipo.prefixlen) + ip['network'] = str(ipo.network) + '/' + str(ipo.prefixlen) + if 'nw_type' not in ip.keys(): + ip['nw_type'] = 'public' + if ip['nw_type'] == 'control': + dbag['eth' + str(ip['nic_dev_id'])] = [ip] + else: + dbag.setdefault('eth' + str(ip['nic_dev_id']), []).append(ip) + return dbag diff --git a/test/integration/smoke/test_network.py b/test/integration/smoke/test_network.py index a89055c853d..719d78afa88 100644 --- a/test/integration/smoke/test_network.py +++ b/test/integration/smoke/test_network.py @@ -1177,7 +1177,7 @@ class TestRouterRules(cloudstackTestCase): ) return - def removeNetworkRules(self, rule, ipaddressobj): + def removeNetworkRules(self, rule): """ Remove specified rule on acquired public IP and default network of virtual machine """ @@ -1186,14 +1186,15 @@ class TestRouterRules(cloudstackTestCase): if rule == STATIC_NAT_RULE: StaticNATRule.disable( self.apiclient, - ipaddressobj.ipaddress.id) + self.ipaddress.ipaddress.id) elif rule == LB_RULE: self.lb_rule.delete(self.apiclient) else: self.nat_rule.delete(self.apiclient) - ipaddressobj.delete(self.apiclient) + logger.debug("Releasing IP %s from account %s" % (self.ipaddress.ipaddress.ipaddress, self.account.name)) + self.ipaddress.delete(self.apiclient) return @@ -1213,7 +1214,6 @@ class TestRouterRules(cloudstackTestCase): domainid=self.account.domainid, networkid=self.defaultNetworkId ) - self.cleanup.append(self.ipaddress) self.createNetworkRules(rule=value, ipaddressobj=self.ipaddress, @@ -1247,7 +1247,7 @@ class TestRouterRules(cloudstackTestCase): # 1. listIpForwardingRules should not return the deleted rule anymore # 2. attempt to do ssh should now fail - self.removeNetworkRules(rule=value, ipaddressobj=self.ipaddress) + self.removeNetworkRules(rule=value) response = self.getCommandResultFromRouter(router, "ip addr") logger.debug(response)