Пример #1
0
    def createNewVlanRange(cls):
        """ Increment current cidr of vlan range present in network
            and create new range
        """
        publicIpRange = PublicIpRange.list(cls.api_client)
        cls.startIp = publicIpRange[0].startip
        cls.endIp = publicIpRange[0].endip
        cls.gateway = publicIpRange[0].gateway
        cls.netmask = publicIpRange[0].netmask
        # Pass ip address and mask length to IPNetwork to findout the CIDR
        ip = IPNetwork(cls.startIp + "/" + cls.netmask)
        # Take random increment factor to avoid adding the same vlan ip range
        # in each test case
        networkIncrementFactor = random.randint(1, 255)
        new_cidr = ip.__iadd__(networkIncrementFactor)
        ip2 = IPNetwork(new_cidr)
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        cls.testdata["vlan_ip_range"]["startip"] = test_startIp
        cls.testdata["vlan_ip_range"]["endip"] = test_endIp
        cls.testdata["vlan_ip_range"]["gateway"] = test_gateway
        cls.testdata["vlan_ip_range"]["netmask"] = cls.netmask
        cls.testdata["vlan_ip_range"]["zoneid"] = cls.zone.id
        cls.testdata["vlan_ip_range"]["podid"] = cls.pod.id

        return PublicIpRange.create(cls.api_client,
                                    cls.testdata["vlan_ip_range"])
    def createNewVlanRange(cls):
        """ Increment current cidr of vlan range present in network
            and create new range
        """
        publicIpRange = PublicIpRange.list(cls.api_client)
        cls.startIp = publicIpRange[0].startip
        cls.endIp = publicIpRange[0].endip
        cls.gateway = publicIpRange[0].gateway
        cls.netmask = publicIpRange[0].netmask
        # Pass ip address and mask length to IPNetwork to findout the CIDR
        ip = IPNetwork(cls.startIp + "/" + cls.netmask)
        # Take random increment factor to avoid adding the same vlan ip range
        # in each test case
        networkIncrementFactor = random.randint(1,255)
        new_cidr = ip.__iadd__(networkIncrementFactor)
        ip2 = IPNetwork(new_cidr)
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        cls.testdata["vlan_ip_range"]["startip"] = test_startIp
        cls.testdata["vlan_ip_range"]["endip"] = test_endIp
        cls.testdata["vlan_ip_range"]["gateway"] = test_gateway
        cls.testdata["vlan_ip_range"]["netmask"] = cls.netmask
        cls.testdata["vlan_ip_range"]["zoneid"] = cls.zone.id
        cls.testdata["vlan_ip_range"]["podid"] = cls.pod.id

        return PublicIpRange.create(
                cls.api_client,
                cls.testdata["vlan_ip_range"])
Пример #3
0
    def test_network_services_VPC_CreatePF(self):
        """ Test Create VPC PF rules on acquired public ip when VpcVirtualRouter is Running
        """

        # Validate the following
        # 1. Create a VPC with cidr - 10.1.1.1/16
        # 2. Create a Network offering - NO1 with all supported services
        # 3. Add network1(10.1.1.1/24) using N01 to this VPC.
        # 4. Deploy vm1 in network1.
        # 5. Use the Create PF rule for vm in network1.
        # 6. Successfully ssh into the Guest VM using the PF rule

        network_1 = self.create_network(self.services["network_offering"])
        vm_1 = self.deployvm_in_network(network_1)
        self.public_ip_range = PublicIpRange.create(
                                    self.apiclient,
                                    self.services["publiciprange"]
                               )
        self.cleanup.append(self.public_ip_range)
        logger.debug("Dedicating Public IP range to the account");
        dedicate_public_ip_range_response = PublicIpRange.dedicate(
                                                self.apiclient,
                                                self.public_ip_range.vlan.id,
                                                account=self.account.name,
                                                domainid=self.account.domainid
                                            )
        public_ip_1 = self.acquire_publicip(network_1)
        self.create_StaticNatRule_For_VM( vm_1, public_ip_1, network_1)
        self.check_ssh_into_vm(vm_1, public_ip_1, testnegative=False)
        self.public_ip_range.release(self.apiclient)
        self.cleanup.remove(self.public_ip_range)
        return
    def test_05_add_overlapped_ip_range(self):
        """Test adding overlapped ip range in existing cidr

            1.Add ip range in new cidr e.g:10.147.40.10-10.147.40.100
            2.Add ip range overlapped with the ip range in
              step1 e.g.10.147.40.90-150
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(10)
        test_endIp = ip.__add__(30)
        test_startIp2 = ip.__add__(20)
        test_endIp2 = ip.__add__(40)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        self.debug(
            "Creating new ip range with startip:%s and endip: %s".format(
                test_startIp, test_endIp))
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add overlapped ip range
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        # Try to create ip range overlapped with exiting ip range
        self.debug("Adding overlapped ip range")
        try:
            new_vlan2 = PublicIpRange.create(self.apiclient,
                                             self.services["vlan_ip_range"])
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(cs.errorMsg.find(
                "already has IPs that overlap with the new range") > 0,
                            msg="Fail:CS allowed adding overlapped ip\
                        ranges in guest cidr")
            return
        # Test will reach here if there is a bug in overlap ip range checking
        self.cleanup.append(new_vlan2)
        self.fail("CS should not accept overlapped ip ranges in\
                    guest traffic, but it allowed")
        return
    def test_08_add_iprange_subset(self):
        """Test adding ip range subset to existing CIDR

            1.Add IP range in new CIDR
            2.Try to add ip range subset to CIDR added in step1
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(2)
        test_endIp = ip.__add__(10)
        test_startIp2 = ip.__add__(20)
        test_endIp2 = ip.__add__(30)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add ip range superset to the existing CIDR
        # Following code finds the netmask superset to existing CIDR
        cidr = ip2.cidr
        mask_len = 2**(32 - (cidr.prefixlen + 1))
        netmask = IPAddress(self.netmask)
        subset = netmask.__iadd__(mask_len)
        # Add this superset netmask to services
        self.services["vlan_ip_range"]["netmask"] = subset
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        self.debug("Adding ip range subset to existing cidr")
        try:
            new_vlan2 = PublicIpRange.create(self.apiclient,
                                             self.services["vlan_ip_range"])
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(
                cs.errorMsg.find("subset") > 0,
                msg="Fail: CS allowed adding ip range subset to existing CIDR")
            return
        # Test will reach here if there is a bug in allowing superset ip range
        self.cleanup.append(new_vlan2)
        self.fail(
            "CS should not allow adding ip range subset to existing CIDR")
        return
    def test_04_add_noncontiguous_ip_range(self):
        """Test adding non-contiguous ip range in existing cidr

            1.Add ip range in new cidr
            1.Add non-contigous ip range in cidr added at step1
            2.Verify the ip range using list APIs
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(50)
        test_endIp = ip.__add__(60)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add non-contiguous ip range in exiting cidr
        test_startIp2 = ip.__add__(10)
        test_endIp2 = ip.__add__(20)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        # create new vlan ip range
        self.debug("Adding non contiguous ip range")
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        return
 def test_01_add_ip_same_cidr(self):
     """Test add guest ip range in the existing cidr
     """
     # call increment_cidr function to get exiting cidr from the setup and
     # increment it
     ip2 = self.increment_cidr()
     test_nw = ip2.network
     ip = IPAddress(test_nw)
     # Add IP range(5 IPs) in the new CIDR
     test_gateway = ip.__add__(1)
     test_startIp = ip.__add__(3)
     test_endIp = ip.__add__(10)
     test_startIp2 = ip.__add__(11)
     test_endIp2 = ip.__add__(15)
     # Populating services with new IP range
     self.services["vlan_ip_range"]["startip"] = test_startIp
     self.services["vlan_ip_range"]["endip"] = test_endIp
     self.services["vlan_ip_range"]["gateway"] = test_gateway
     self.services["vlan_ip_range"]["netmask"] = self.netmask
     self.services["vlan_ip_range"]["zoneid"] = self.zone.id
     self.services["vlan_ip_range"]["podid"] = self.pod.id
     # create new vlan ip range
     self.debug("Creating new ip range with new cidr in the same vlan")
     new_vlan = PublicIpRange.create(
         self.apiclient,
         self.services["vlan_ip_range"])
     self.debug(
         "Created new vlan range with startip:%s and endip:%s" %
         (test_startIp, test_endIp))
     self.cleanup.append(new_vlan)
     new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
     # Compare list output with configured values
     self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
     # Add few more ips in the same CIDR
     self.services["vlan_ip_range"]["startip"] = test_startIp2
     self.services["vlan_ip_range"]["endip"] = test_endIp2
     self.debug("Creating new ip range in the existing CIDR")
     new_vlan2 = PublicIpRange.create(
         self.apiclient,
         self.services["vlan_ip_range"])
     self.debug(
         "Created new vlan range with startip:%s and endip:%s" %
         (test_startIp2, test_endIp2))
     self.cleanup.append(new_vlan2)
     # list new vlan ip range
     new_vlan2_res = new_vlan2.list(self.apiclient, id=new_vlan2.vlan.id)
     # Compare list output with configured values
     self.verify_vlan_range(new_vlan2_res, self.services["vlan_ip_range"])
     return
    def test_04_add_noncontiguous_ip_range(self):
        """Test adding non-contiguous ip range in existing cidr

            1.Add ip range in new cidr
            1.Add non-contigous ip range in cidr added at step1
            2.Verify the ip range using list APIs
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(50)
        test_endIp = ip.__add__(60)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add non-contiguous ip range in exiting cidr
        test_startIp2 = ip.__add__(10)
        test_endIp2 = ip.__add__(20)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        # create new vlan ip range
        self.debug("Adding non contiguous ip range")
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        return
Пример #9
0
    def test_02_create_ipv6_public_ip_range_fail(self):
        """Test to add IPv6 public IP range failure

        # Validate the following:
        # 1. createVlanIpRange should return valid info for new public range
        # 2. The Cloud Database contains the valid information
        """
        ipv6_publiciprange_service = self.services["publicip6range"]
        cidr = ipv6_publiciprange_service["ip6cidr"]
        x = cidr.split("/")
        x[1] = "72"
        cidr = "/".join(x)
        ipv6_publiciprange_service["ip6cidr"] = cidr
        ipv6_publiciprange_service["zoneid"] = self.zone.id
        try:
            ipv6_publiciprange = PublicIpRange.create(
                self.apiclient,
                ipv6_publiciprange_service
            )
        except Exception as e:
            self.debug("IPv6 public range creation failed as expected %s " % e)
            ipv6_publiciprange = None
        if ipv6_publiciprange != None:
            self.debug("Created IPv6 public range with ID: %s. Deleting it before failure" % ipv6_publiciprange.id)
            self.cleanup.append(ipv6_publiciprange)
            self.fail("IPv6 guest prefix created despite CIDR size greater than 64")
        return
    def test_02_add_ip_diff_cidr(self):
        """Test add ip range in a new cidr

           Steps:
           1.Get public vlan range (guest cidr) from the setup
           2.Add IP range to a new cidr
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        self.debug("Adding new ip range in different CIDR in same vlan")
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        return
 def test_01_add_ip_same_cidr(self):
     """Test add guest ip range in the existing cidr
     """
     # call increment_cidr function to get exiting cidr from the setup and
     # increment it
     ip2 = self.increment_cidr()
     test_nw = ip2.network
     ip = IPAddress(test_nw)
     # Add IP range(5 IPs) in the new CIDR
     test_gateway = ip.__add__(1)
     test_startIp = ip.__add__(3)
     test_endIp = ip.__add__(10)
     test_startIp2 = ip.__add__(11)
     test_endIp2 = ip.__add__(15)
     # Populating services with new IP range
     self.services["vlan_ip_range"]["startip"] = test_startIp
     self.services["vlan_ip_range"]["endip"] = test_endIp
     self.services["vlan_ip_range"]["gateway"] = test_gateway
     self.services["vlan_ip_range"]["netmask"] = self.netmask
     self.services["vlan_ip_range"]["zoneid"] = self.zone.id
     self.services["vlan_ip_range"]["podid"] = self.pod.id
     # create new vlan ip range
     self.debug("Creating new ip range with new cidr in the same vlan")
     new_vlan = PublicIpRange.create(self.apiclient,
                                     self.services["vlan_ip_range"])
     self.debug("Created new vlan range with startip:%s and endip:%s" %
                (test_startIp, test_endIp))
     self.cleanup.append(new_vlan)
     new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
     # Compare list output with configured values
     self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
     # Add few more ips in the same CIDR
     self.services["vlan_ip_range"]["startip"] = test_startIp2
     self.services["vlan_ip_range"]["endip"] = test_endIp2
     self.debug("Creating new ip range in the existing CIDR")
     new_vlan2 = PublicIpRange.create(self.apiclient,
                                      self.services["vlan_ip_range"])
     self.debug("Created new vlan range with startip:%s and endip:%s" %
                (test_startIp2, test_endIp2))
     self.cleanup.append(new_vlan2)
     # list new vlan ip range
     new_vlan2_res = new_vlan2.list(self.apiclient, id=new_vlan2.vlan.id)
     # Compare list output with configured values
     self.verify_vlan_range(new_vlan2_res, self.services["vlan_ip_range"])
     return
Пример #12
0
    def test_01_create_ipv6_public_ip_range(self):
        """Test to add IPv6 public IP range

        # Validate the following:
        # 1. createVlanIpRange should return valid info for new public range
        # 2. The Cloud Database contains the valid information
        """
        ipv6_publiciprange_service = self.services["publicip6range"]
        ipv6_publiciprange_service["zoneid"] = self.zone.id
        ipv6_publiciprange = PublicIpRange.create(
            self.apiclient,
            ipv6_publiciprange_service
        )
        self.cleanup.append(ipv6_publiciprange)

        self.debug("Created IPv6 public IP range with ID: %s" % ipv6_publiciprange.vlan.id)
        ipv6_publiciprange = ipv6_publiciprange.vlan

        public_ip_ranges = PublicIpRange.list(
                    self.apiclient,
                    id=ipv6_publiciprange.id
                )
        self.assertEqual(
            isinstance(public_ip_ranges, list),
            True,
            "Check list response returns a valid list"
        )
        self.assertNotEqual(
            len(public_ip_ranges),
            0,
            "Check public IP range is created"
        )
        public_ip_range = public_ip_ranges[0]

        self.assertEqual(
            public_ip_range.id,
            ipv6_publiciprange.id,
            "Check server id"
        )
        self.assertEqual(
            public_ip_range.ip6cidr,
            ipv6_publiciprange_service["ip6cidr"],
            "Check ip6cidr for IPv6 public IP range"
        )
        return
Пример #13
0
 def getPublicIpv6Range(cls):
     list_public_ip_range_response = PublicIpRange.list(
         cls.apiclient,
         zoneid=cls.zone.id
     )
     ipv4_range_vlan = None
     if isinstance(list_public_ip_range_response, list) == True and len(list_public_ip_range_response) > 0:
         for ip_range in list_public_ip_range_response:
             if ip_range.ip6cidr != None and ip_range.ip6gateway != None:
                 return ip_range
             if ip_range.netmask != None and ip_range.gateway != None:
                 vlan = ip_range.vlan
                 if ipv4_range_vlan == None and vlan.startswith("vlan://"):
                     vlan = vlan.replace("vlan://", "")
                     ipv4_range_vlan = int(vlan)
     ipv6_publiciprange_service = cls.services["publicip6range"]
     ipv6_publiciprange_service["zoneid"] = cls.zone.id
     ipv6_publiciprange_service["vlan"] = ipv4_range_vlan
     ipv6_publiciprange = PublicIpRange.create(
         cls.apiclient,
         ipv6_publiciprange_service
     )
     cls._cleanup.append(ipv6_publiciprange)
     return ipv6_publiciprange
    def test_03_del_ip_range(self):
        """Test delete ip range

           Steps:
           1.Add ip range in same/new cidr
           2.delete the ip range added at step1
           3.Verify the ip range deletion using list APIs
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        self.debug("Creating new ip range in the new cidr")
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Delete the above IP range
        self.debug("Deleting new ip range added in new cidr")
        new_vlan.delete(self.apiclient)
        # listing vlan ip ranges with the id should through exception , if not
        # mark the test case as failed
        try:
            new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(
                cs.errorMsg.find("entity does not exist") > 0,
                msg="Failed to delete IP range")
        return
 def increment_cidr(self):
     """Takes CIDR as input and will increment by one and returns the new CIDR
     """
     publicIpRange = PublicIpRange.list(self.apiclient)
     self.startIp = publicIpRange[0].startip
     self.endIp = publicIpRange[0].endip
     self.gateway = publicIpRange[0].gateway
     self.netmask = publicIpRange[0].netmask
     # Pass ip address and mask length to IPNetwork to findout the CIDR
     ip = IPNetwork(self.startIp + "/" + self.netmask)
     # Take random increment factor to avoid adding the same vlan ip range
     # in each test case
     networkIncrementFactor = random.randint(1, 255)
     new_cidr = ip.__iadd__(networkIncrementFactor)
     ip2 = IPNetwork(new_cidr)
     return ip2
 def increment_cidr(self):
     """Takes CIDR as input and will increment by one and returns the new CIDR
     """
     publicIpRange = PublicIpRange.list(self.apiclient)
     self.startIp = publicIpRange[0].startip
     self.endIp = publicIpRange[0].endip
     self.gateway = publicIpRange[0].gateway
     self.netmask = publicIpRange[0].netmask
     # Pass ip address and mask length to IPNetwork to findout the CIDR
     ip = IPNetwork(self.startIp + "/" + self.netmask)
     # Take random increment factor to avoid adding the same vlan ip range
     # in each test case
     networkIncrementFactor = random.randint(1,255)
     new_cidr = ip.__iadd__(networkIncrementFactor)
     ip2 = IPNetwork(new_cidr)
     return ip2
    def test_03_del_ip_range(self):
        """Test delete ip range

           Steps:
           1.Add ip range in same/new cidr
           2.delete the ip range added at step1
           3.Verify the ip range deletion using list APIs
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        self.debug("Creating new ip range in the new cidr")
        new_vlan = PublicIpRange.create(self.apiclient,
                                        self.services["vlan_ip_range"])
        self.debug("Created new vlan range with startip:%s and endip:%s" %
                   (test_startIp, test_endIp))
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Delete the above IP range
        self.debug("Deleting new ip range added in new cidr")
        new_vlan.delete(self.apiclient)
        # listing vlan ip ranges with the id should through exception , if not
        # mark the test case as failed
        try:
            new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(cs.errorMsg.find("entity does not exist") > 0,
                            msg="Failed to delete IP range")
        return
    def test_02_add_ip_diff_cidr(self):
        """Test add ip range in a new cidr

           Steps:
           1.Get public vlan range (guest cidr) from the setup
           2.Add IP range to a new cidr
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range(5 IPs) in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(3)
        test_endIp = ip.__add__(10)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        self.debug("Adding new ip range in different CIDR in same vlan")
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        return
Пример #19
0
            h = Host(tmp_dict)
#            h.forced = 'True'
            h.id = host.id
            h.delete(apiClient)

    clusters = Cluster.list(apiClient)
    if clusters:
      for cluster in clusters:
        print "cluster name={}, id={}".format(cluster.name, cluster.id)
        if cluster.allocationstate == 'Enabled':
          print "Delete Cluster"
          c = Cluster(tmp_dict)
          c.id = cluster.id
          c.delete(apiClient)

    ipranges = PublicIpRange.list(apiClient)
    if ipranges:
      for iprange in ipranges:
        print "ip range name={}, id={}".format(iprange.name, iprange.id)

    if clusters:
      nets = PhysicalNetwork.list(apiClient)
      if nets:
        for net in nets:
          print "net name={}, id={}".format(net.name, net.id)
          print "Delete PhysicalNetwork"
          n = PhysicalNetwork(tmp_dict)
          n.id = net.id
          n.delete(apiClient)

    pods = Pod.list(apiClient)
    def test_06_add_ip_range_overlapped_with_two_ranges(self):
        """Test adding overlapped ip range with two existing cidr

            1.Add ip range in new cidr e.g:10.147.40.2-10.147.40.10
            2.Add another ip range in the same cidr
              e.g:10.147.40.20-10.147.40.30
            3.Add ip range overlapped with both the ip ranges
              e.g.10.147.40.10-20
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(2)
        test_endIp = ip.__add__(5)
        test_startIp2 = ip.__add__(7)
        test_endIp2 = ip.__add__(10)
        test_startIp3 = ip.__add__(5)
        test_endIp3 = ip.__add__(7)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add 2nd IP range in the same CIDR
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        new_vlan2 = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp2, test_endIp2))
        self.cleanup.append(new_vlan2)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp3
        self.services["vlan_ip_range"]["endip"] = test_endIp3
        # Try to create ip range overlapped with exiting ip range
        self.debug("Adding ip range overlapped with two cidrs")
        try:
            new_vlan3 = PublicIpRange.create(
                self.apiclient,
                self.services["vlan_ip_range"])
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(
                cs.errorMsg.find("already has IPs that overlap with the new range") > 0,
                msg="Fail:CS allowed adding overlapped ip\
                        ranges in guest cidr")
            return
        # Test will reach here if there is a bug in overlap ip range checking
        self.cleanup.append(new_vlan3)
        self.fail(
            "CS should not accept overlapped ip ranges in guest\
                    traffic, but it allowed")
        return
    def test_08_add_iprange_subset(self):
        """Test adding ip range subset to existing CIDR

            1.Add IP range in new CIDR
            2.Try to add ip range subset to CIDR added in step1
        """
        # call increment_cidr function to get exiting cidr from the setup and
        # increment it
        ip2 = self.increment_cidr()
        test_nw = ip2.network
        ip = IPAddress(test_nw)
        # Add IP range in the new CIDR
        test_gateway = ip.__add__(1)
        test_startIp = ip.__add__(2)
        test_endIp = ip.__add__(10)
        test_startIp2 = ip.__add__(20)
        test_endIp2 = ip.__add__(30)
        # Populating services with new IP range
        self.services["vlan_ip_range"]["startip"] = test_startIp
        self.services["vlan_ip_range"]["endip"] = test_endIp
        self.services["vlan_ip_range"]["gateway"] = test_gateway
        self.services["vlan_ip_range"]["netmask"] = self.netmask
        self.services["vlan_ip_range"]["zoneid"] = self.zone.id
        self.services["vlan_ip_range"]["podid"] = self.pod.id
        # create new vlan ip range
        new_vlan = PublicIpRange.create(
            self.apiclient,
            self.services["vlan_ip_range"])
        self.debug(
            "Created new vlan range with startip:%s and endip:%s" %
            (test_startIp, test_endIp))
        self.cleanup.append(new_vlan)
        new_vlan_res = new_vlan.list(self.apiclient, id=new_vlan.vlan.id)
        # Compare list output with configured values
        self.verify_vlan_range(new_vlan_res, self.services["vlan_ip_range"])
        # Add ip range superset to the existing CIDR
        # Following code finds the netmask superset to existing CIDR
        cidr = ip2.cidr
        mask_len = 2 ** (32 - (cidr.prefixlen + 1))
        netmask = IPAddress(self.netmask)
        subset = netmask.__iadd__(mask_len)
        # Add this superset netmask to services
        self.services["vlan_ip_range"]["netmask"] = subset
        self.services["vlan_ip_range"]["startip"] = test_startIp2
        self.services["vlan_ip_range"]["endip"] = test_endIp2
        self.debug("Adding ip range subset to existing cidr")
        try:
            new_vlan2 = PublicIpRange.create(
                self.apiclient,
                self.services["vlan_ip_range"])
        except CloudstackAPIException as cs:
            self.debug(cs.errorMsg)
            self.assertTrue(
                cs.errorMsg.find("subset") > 0,
                msg="Fail: CS allowed adding ip range subset to existing CIDR")
            return
        # Test will reach here if there is a bug in allowing superset ip range
        self.cleanup.append(new_vlan2)
        self.fail(
            "CS should not allow adding ip range subset to existing CIDR")
        return
    def test_01_acquire_public_ips_in_isolated_network_with_single_vr(self):
        """ Acquire IPs in multiple subnets in isolated networks with single VR

        # Steps
        # 1. Create network offering with single VR, and enable it
        # 2. create isolated network with the network offering
        # 3. create a vm in the network.
        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP
        # 4. get a free public ip, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP and new ip
        # 5. remove the port forwarding rule, and release the new ip
        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic IP, eth2 -> source nat IP

        # 6. create new public ip range 1
        # 7. get a free ip 4 in new ip range 2, assign to network, and enable static nat to vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1
        # 8. get a free ip in new ip range, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 2,
        # 9. get a free ip in new ip range, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 2, new ip 3
        # 10. release new ip 2
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 3
        # 11. release new ip 1
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3
        # 12. create new public ip range 2
        # 13. get a free ip 4 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4
        # 14. get a free ip 5 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/5
        # 15. get a free ip 6 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/5/6
        # 16. release new ip 5
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/6
        # 17. release new ip 4
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 6
        # 18. release new ip 3
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth4 -> new ip 6
        # 19. restart network
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth4 -> new ip 6
        # 20. reboot router
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        # 21. restart network with cleanup
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        # 22. restart network with cleanup, makeredundant=true
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        """

        # Create new domain1
        self.domain1 = Domain.create(self.apiclient,
                                     services=self.services["acl"]["domain1"],
                                     parentdomainid=self.domain.id)
        # Create account1
        self.account1 = Account.create(self.apiclient,
                                       self.services["acl"]["accountD1"],
                                       domainid=self.domain1.id)
        self.cleanup.append(self.account1)
        self.cleanup.append(self.domain1)

        # 1. Create network offering with single VR, and enable it
        self.network_offering = NetworkOffering.create(
            self.apiclient,
            self.services["isolated_network_offering"],
        )
        self.network_offering.update(self.apiclient, state='Enabled')
        self.cleanup.append(self.network_offering)

        # 2. create isolated network with the network offering
        self.services["network"]["zoneid"] = self.zone.id
        self.services["network"]["networkoffering"] = self.network_offering.id
        self.network1 = Network.create(self.apiclient,
                                       self.services["network"],
                                       self.account1.name,
                                       self.account1.domainid)

        # 3. create a vm in the network.
        try:
            self.virtual_machine1 = VirtualMachine.create(
                self.apiclient,
                self.services["virtual_machine"],
                accountid=self.account1.name,
                domainid=self.account1.domainid,
                serviceofferingid=self.service_offering.id,
                templateid=self.template.id,
                zoneid=self.zone.id,
                networkids=self.network1.id)
        except Exception as e:
            self.fail("Exception while deploying virtual machine: %s" % e)

        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_router_publicnic_state(router, host, "eth2")

        # 4. get a free public ip, assign to network, and create port forwarding rules (ssh) to the vm
        ipaddress = PublicIPAddress.create(
            self.apiclient,
            zoneid=self.zone.id,
            networkid=self.network1.id,
        )
        nat_rule = NATRule.create(self.apiclient,
                                  self.virtual_machine1,
                                  self.services["natrule"],
                                  ipaddressid=ipaddress.ipaddress.id,
                                  openfirewall=True)
        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP/new ip
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress.ipaddress.ipaddress,
                                             "eth2", True)
            self.verify_router_publicnic_state(router, host, "eth2")

        # 5. release the new ip
        ipaddress.delete(self.apiclient)

        #   verify the available nics in VR should be "eth0,eth1,eth2"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress.ipaddress.ipaddress,
                                             "eth2", False)
            self.verify_router_publicnic_state(router, host, "eth2")

        # 6. create new public ip range 1
        self.services["publiciprange"]["zoneid"] = self.zone.id
        self.services["publiciprange"]["forvirtualnetwork"] = "true"
        random_subnet_number = random.randrange(10, 50)
        self.services["publiciprange"]["vlan"] = get_free_vlan(
            self.apiclient, self.zone.id)[1]
        self.services["publiciprange"]["gateway"] = "172.16." + str(
            random_subnet_number) + ".1"
        self.services["publiciprange"]["startip"] = "172.16." + str(
            random_subnet_number) + ".2"
        self.services["publiciprange"]["endip"] = "172.16." + str(
            random_subnet_number) + ".10"
        self.services["publiciprange"]["netmask"] = "255.255.255.0"
        self.public_ip_range1 = PublicIpRange.create(
            self.apiclient, self.services["publiciprange"])
        self.cleanup.append(self.public_ip_range1)

        # 7. get a free ip 4 in new ip range 2, assign to network, and enable static nat to vm
        ip_address_1 = self.get_free_ipaddress(self.public_ip_range1.vlan.id)
        ipaddress_1 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_1)

        StaticNATRule.enable(self.apiclient,
                             virtualmachineid=self.virtual_machine1.id,
                             ipaddressid=ipaddress_1.ipaddress.id,
                             networkid=self.network1.id)

        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_1.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 8. get a free ip in new ip range, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 2,
        ip_address_2 = self.get_free_ipaddress(self.public_ip_range1.vlan.id)
        ipaddress_2 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_2)

        nat_rule = NATRule.create(self.apiclient,
                                  self.virtual_machine1,
                                  self.services["natrule"],
                                  ipaddressid=ipaddress_2.ipaddress.id,
                                  openfirewall=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_1.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_2.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 9. get a free ip in new ip range, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 2, new ip 3
        ip_address_3 = self.get_free_ipaddress(self.public_ip_range1.vlan.id)
        ipaddress_3 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_3)

        nat_rule = NATRule.create(self.apiclient,
                                  self.virtual_machine1,
                                  self.services["natrule"],
                                  ipaddressid=ipaddress_3.ipaddress.id,
                                  openfirewall=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_1.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_2.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 10. release new ip 2
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 1, new ip 3
        ipaddress_2.delete(self.apiclient)

        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_1.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_2.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 11. release new ip 1
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3
        ipaddress_1.delete(self.apiclient)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_1.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_2.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 12. create new public ip range 2
        self.services["publiciprange"]["zoneid"] = self.zone.id
        self.services["publiciprange"]["forvirtualnetwork"] = "true"
        self.services["publiciprange"]["vlan"] = get_free_vlan(
            self.apiclient, self.zone.id)[1]
        self.services["publiciprange"]["gateway"] = "172.16." + str(
            random_subnet_number + 1) + ".1"
        self.services["publiciprange"]["startip"] = "172.16." + str(
            random_subnet_number + 1) + ".2"
        self.services["publiciprange"]["endip"] = "172.16." + str(
            random_subnet_number + 1) + ".10"
        self.services["publiciprange"]["netmask"] = "255.255.255.0"
        self.public_ip_range2 = PublicIpRange.create(
            self.apiclient, self.services["publiciprange"])
        self.cleanup.append(self.public_ip_range2)

        # 13. get a free ip 4 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4

        ip_address_4 = self.get_free_ipaddress(self.public_ip_range2.vlan.id)
        ipaddress_4 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_4)

        StaticNATRule.enable(self.apiclient,
                             virtualmachineid=self.virtual_machine1.id,
                             ipaddressid=ipaddress_4.ipaddress.id,
                             networkid=self.network1.id)

        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(
                router, host, "eth0,eth1,eth2,eth3,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3|eth4")

        # 14. get a free ip 5 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/5
        ip_address_5 = self.get_free_ipaddress(self.public_ip_range2.vlan.id)
        ipaddress_5 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_5)

        nat_rule = NATRule.create(self.apiclient,
                                  self.virtual_machine1,
                                  self.services["natrule"],
                                  ipaddressid=ipaddress_5.ipaddress.id,
                                  openfirewall=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(
                router, host, "eth0,eth1,eth2,eth3,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3|eth4")

        # 15. get a free ip 6 in new ip range 2, assign to network, and create port forwarding rules (ssh) to the vm
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/5/6
        ip_address_6 = self.get_free_ipaddress(self.public_ip_range2.vlan.id)
        ipaddress_6 = PublicIPAddress.create(self.apiclient,
                                             zoneid=self.zone.id,
                                             networkid=self.network1.id,
                                             ipaddress=ip_address_6)

        nat_rule = NATRule.create(self.apiclient,
                                  self.virtual_machine1,
                                  self.services["natrule"],
                                  ipaddressid=ipaddress_6.ipaddress.id,
                                  openfirewall=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(
                router, host, "eth0,eth1,eth2,eth3,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3|eth4")

        # 16. release new ip 5
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 4/6
        ipaddress_5.delete(self.apiclient)

        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(
                router, host, "eth0,eth1,eth2,eth3,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3|eth4")

        # 17. release new ip 4
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 3, eth4 -> new ip 6
        ipaddress_4.delete(self.apiclient)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(
                router, host, "eth0,eth1,eth2,eth3,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_3.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3|eth4")

        # 18. release new ip 3
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth4 -> new ip 6
        ipaddress_3.delete(self.apiclient)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth4")

        # 19. restart network
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth4,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth4 -> new ip 6
        self.network1.restart(self.apiclient)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth4,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth4", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth4", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth4")

        # 20. reboot router
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        if len(routers) > 0:
            router = routers[0]
            cmd = rebootRouter.rebootRouterCmd()
            cmd.id = router.id
            self.apiclient.rebootRouter(cmd)
            router = self.get_router(router.id)
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")

        # 21. restart network with cleanup
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        self.network1.restart(self.apiclient, cleanup=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth3", True)

        # 22. restart network with cleanup, makeredundant=true
        #   verify the available nics in VR should be "eth0,eth1,eth2,eth3,"
        #   verify the IPs in VR. eth0 -> guest nic, eth2 -> source nat IP, eth3 -> new ip 6
        self.network1.restart(self.apiclient, cleanup=True, makeredundant=True)
        routers = self.get_routers(self.network1.id)
        for router in routers:
            host = self.get_router_host(router)
            self.verify_network_interfaces_in_router(router, host,
                                                     "eth0,eth1,eth2,eth3,")
            guestIp, controlIp, sourcenatIp = self.get_router_ips(router)
            self.verify_ip_address_in_router(router, host, guestIp, "eth0",
                                             True)
            self.verify_ip_address_in_router(router, host, controlIp, "eth1",
                                             True)
            self.verify_ip_address_in_router(router, host, sourcenatIp, "eth2",
                                             True)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_4.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_5.ipaddress.ipaddress,
                                             "eth3", False)
            self.verify_ip_address_in_router(router, host,
                                             ipaddress_6.ipaddress.ipaddress,
                                             "eth3", True)
            self.verify_router_publicnic_state(router, host, "eth2|eth3")
Пример #23
0
    def test_iptable_rules(self):
        """Test iptable rules in case we have IP associated with a network which is in
            different pubic IP range from that of public IP range that has source NAT IP.
            When IP is associated we should see a rule '-i eth3 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT' in FORWARD table.
            When IP is dis-associated we should see a rule in the FORWARD table is deleted.
        """

        # Validate the following:
        # 1. Create a new public IP range and dedicate to a account
        # 2. Acquire a IP from new public range
        # 3. Create a firewall rule to open up the port, so that IP is associated with network
        # 5. Login to VR and verify routing tables, there should be Table_eth3
        # 6. Delete firewall rule, since its last IP, routing table Table_eth3 should be deleted

        self.services["extrapubliciprange"]["zoneid"] = self.services["zoneid"]
        self.public_ip_range = PublicIpRange.create(
                                    self.apiclient,
                                    self.services["extrapubliciprange"]
                               )
        self.cleanup.append(self.public_ip_range)

        logger.debug("Dedicating Public IP range to the account");
        dedicate_public_ip_range_response = PublicIpRange.dedicate(
                                                self.apiclient,
                                                self.public_ip_range.vlan.id,
                                                account=self.account.name,
                                                domainid=self.account.domainid
                                            )
        ip_address = PublicIPAddress.create(
            self.apiclient,
            self.account.name,
            self.zone.id,
            self.account.domainid,
            self.services["virtual_machine"]
        )
        self.cleanup.append(ip_address)
        # Check if VM is in Running state before creating NAT and firewall rules
        vm_response = VirtualMachine.list(
            self.apiclient,
            id=self.virtual_machine.id
        )

        self.assertEqual(
            isinstance(vm_response, list),
            True,
            "Check list VM returns a valid list"
        )

        self.assertNotEqual(
            len(vm_response),
            0,
            "Check Port Forwarding Rule is created"
        )
        self.assertEqual(
            vm_response[0].state,
            'Running',
            "VM state should be Running before creating a NAT rule."
        )

        # Open up firewall port for SSH
        firewall_rule = FireWallRule.create(
            self.apiclient,
            ipaddressid=ip_address.ipaddress.id,
            protocol=self.services["natrule"]["protocol"],
            cidrlist=['0.0.0.0/0'],
            startport=self.services["natrule"]["publicport"],
            endport=self.services["natrule"]["publicport"]
        )
        self.cleanup.append(firewall_rule)
        # Get the router details associated with account
        routers = list_routers(
            self.apiclient,
            account=self.account.name,
            domainid=self.account.domainid,
        )
        router = routers[0]

        if (self.hypervisor.lower() == 'vmware'
                or self.hypervisor.lower() == 'hyperv'):
            result = get_process_status(
                self.apiclient.connection.mgtSvr,
                22,
                self.apiclient.connection.user,
                self.apiclient.connection.passwd,
                router.linklocalip,
                'iptables -t filter -L FORWARD  -v',
                hypervisor=self.hypervisor
            )
        else:
            hosts = list_hosts(
                self.apiclient,
                id=router.hostid,
            )
            self.assertEqual(
                isinstance(hosts, list),
                True,
                "Check for list hosts response return valid data"
            )
            host = hosts[0]
            host.user = self.hostConfig['username']
            host.passwd = self.hostConfig['password']
            try:
                result = get_process_status(
                    host.ipaddress,
                    22,
                    host.user,
                    host.passwd,
                    router.linklocalip,
                    'iptables -t filter -L FORWARD  -v'
                )
            except KeyError:
                self.skipTest(
                    "Provide a marvin config file with host\
                            credentials to run %s" %
                    self._testMethodName)

        logger.debug("iptables -t filter -L FORWARD  -v: %s" % result)
        res = str(result)
        self.assertEqual(
            res.count("eth3   eth0    anywhere             anywhere             state RELATED,ESTABLISHED"),
            1,
            "Check to ensure there is a iptable rule to accept the RELATED,ESTABLISHED traffic"
        )
        firewall_rule.delete(self.apiclient)
        self.cleanup.remove(firewall_rule)
Пример #24
0
    def test_static_nat_on_ip_from_non_src_nat_ip_range(self):
        """Test for static nat on a IP which is in pubic IP range different
           from public IP range that has source NAT IP associated with network
        """

        # Validate the following:
        # 1. Create a new public IP range and dedicate to a account
        # 2. Acquire a IP from new public range
        # 3. Enable static NAT on acquired IP from new range
        # 4. Create a firewall rule to open up the port
        # 5. Test SSH works to the VM

        self.services["extrapubliciprange"]["zoneid"] = self.services["zoneid"]
        self.public_ip_range = PublicIpRange.create(
                                    self.apiclient,
                                    self.services["extrapubliciprange"]
                               )
        self.cleanup.append(self.public_ip_range)
        logger.debug("Dedicating Public IP range to the account");
        dedicate_public_ip_range_response = PublicIpRange.dedicate(
                                                self.apiclient,
                                                self.public_ip_range.vlan.id,
                                                account=self.account.name,
                                                domainid=self.account.domainid
                                            )
        ip_address = PublicIPAddress.create(
            self.apiclient,
            self.account.name,
            self.zone.id,
            self.account.domainid,
            self.services["virtual_machine"]
        )
        self.cleanup.append(ip_address)
        # Check if VM is in Running state before creating NAT and firewall rules
        vm_response = VirtualMachine.list(
            self.apiclient,
            id=self.virtual_machine.id
        )

        self.assertEqual(
            isinstance(vm_response, list),
            True,
            "Check list VM returns a valid list"
        )

        self.assertNotEqual(
            len(vm_response),
            0,
            "Check Port Forwarding Rule is created"
        )
        self.assertEqual(
            vm_response[0].state,
            'Running',
            "VM state should be Running before creating a NAT rule."
        )

        # Open up firewall port for SSH
        fwr = FireWallRule.create(
            self.apiclient,
            ipaddressid=ip_address.ipaddress.id,
            protocol=self.services["natrule"]["protocol"],
            cidrlist=['0.0.0.0/0'],
            startport=self.services["natrule"]["publicport"],
            endport=self.services["natrule"]["publicport"]
        )
        self.cleanup.append(fwr)

        # Create Static NAT rule
        StaticNATRule.enable(
            self.apiclient,
            ip_address.ipaddress.id,
            self.virtual_machine.id,
            self.defaultNetworkId
        )

        try:
            logger.debug("SSHing into VM with IP address %s with NAT IP %s" %
                       (
                           self.virtual_machine.ipaddress,
                           ip_address.ipaddress.ipaddress
                       ))
            self.virtual_machine.get_ssh_client(ip_address.ipaddress.ipaddress)
        except Exception as e:
            self.fail(
                "SSH Access failed for %s: %s" %
                (self.virtual_machine.ipaddress, e)
            )

        StaticNATRule.disable(
            self.apiclient,
            ip_address.ipaddress.id,
            self.virtual_machine.id
        )
Пример #25
0
    def setUpClass(cls):
        cls.testClient = super(TestPublicIp, cls).getClsTestClient()
        cls.apiclient = cls.testClient.getApiClient()
        cls.services = cls.testClient.getParsedTestDataConfig()

        # Get Zone, Domain and templates
        cls.zone = get_zone(cls.apiclient, cls.testClient.getZoneForTests())
        cls.services['mode'] = cls.zone.networktype
        cls.logger = logging.getLogger("TestPublicIp")
        cls.domain = get_domain(cls.apiclient)
        cls.services["virtual_machine"]["zoneid"] = cls.zone.id

        cls.template = get_template(
            cls.apiclient,
            cls.zone.id,
            cls.services["ostype"]
        )

        cls.use_system_ips_config_name = "use.system.public.ips"
        cls.use_system_ips_config = Configurations.list(
            cls.apiclient,
            name=cls.use_system_ips_config_name
        )
        cls.use_system_ips_config_value = cls.use_system_ips_config[0].value
        Configurations.update(
            cls.apiclient,
            name=cls.use_system_ips_config_name,
            value="false"
        )

        cls._cleanup = []
        cls.unsupportedHypervisor = False
        cls.hypervisor = cls.testClient.getHypervisorInfo()
        if cls.hypervisor.lower() in ['lxc']:
            cls.unsupportedHypervisor = True
            return

        # Create new domain1
        cls.domain1 = Domain.create(
            cls.apiclient,
            services=cls.services["acl"]["domain1"],
            parentdomainid=cls.domain.id)

        # Create account1
        cls.account1 = Account.create(
            cls.apiclient,
            cls.services["account"],
            # cls.services["acl"]["accountD1"],
            admin=True,
            domainid=cls.domain1.id
        )

        # Create new sub-domain
        cls.sub_domain = Domain.create(
            cls.apiclient,
            services=cls.services["acl"]["domain11"],
            parentdomainid=cls.domain1.id)

        # Create account for sub-domain
        cls.sub_account = Account.create(
            cls.apiclient,
            cls.services["acl"]["accountD11"],
            domainid=cls.sub_domain.id
        )

        # Create new domain2
        cls.domain2 = Domain.create(
            cls.apiclient,
            services=cls.services["acl"]["domain2"],
            parentdomainid=cls.domain.id)

        # Create account2
        cls.account2 = Account.create(
            cls.apiclient,
            cls.services["acl"]["accountD2"],
            domainid=cls.domain2.id
        )

        cls.services["publiciprange"]["zoneid"] = cls.zone.id
        cls.services["publiciprange"]["forvirtualnetwork"] = "true"

        # Create public ip range 1
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        random_subnet_number = random.randrange(10,20)
        cls.services["publiciprange"]["gateway"] = "10.100." + \
                                                   str(random_subnet_number) + ".254"
        cls.services["publiciprange"]["startip"] = "10.100." + \
                                                   str(random_subnet_number) + ".1"
        cls.services["publiciprange"]["endip"] = "10.100." + \
                                                 str(random_subnet_number) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range1 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"],
            account=cls.account1.name,
            domainid=cls.account1.domainid
        )

        # dedicate ip range to sub domain
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        random_subnet_number = random.randrange(10,20)
        cls.services["publiciprange"]["gateway"] = "10.110." + \
                                                    str(random_subnet_number) + ".254"
        cls.services["publiciprange"]["startip"] = "10.110." + \
                                                    str(random_subnet_number) + ".1"
        cls.services["publiciprange"]["endip"] = "10.110." + \
                                                  str(random_subnet_number) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range2 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"],
            account=cls.sub_account.name,
            domainid=cls.sub_account.domainid
        )

        # dedicate ip range to second domain
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        random_subnet_number = random.randrange(10,20)
        cls.services["publiciprange"]["gateway"] = "10.120." + \
                                                   str(random_subnet_number) + ".254"
        cls.services["publiciprange"]["startip"] = "10.120." + \
                                                   str(random_subnet_number) + ".1"
        cls.services["publiciprange"]["endip"] = "10.120." + \
                                                 str(random_subnet_number) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range3 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"],
            account=cls.account2.name,
            domainid=cls.account2.domainid
        )

        # create vpc offering and VPC
        cls.vpc_off = VpcOffering.create(
            cls.apiclient,
            cls.services["vpc_offering"]
        )
        cls.vpc_off.update(cls.apiclient, state='Enabled')

        # create network offering
        cls.isolated_network_offering = NetworkOffering.create(
            cls.apiclient,
            cls.services["isolated_network_offering"],
            conservemode=False
        )

        NetworkOffering.update(
            cls.isolated_network_offering,
            cls.apiclient,
            id=cls.isolated_network_offering.id,
            state="enabled"
        )

        physical_network, shared_vlan = get_free_vlan(
            cls.apiclient, cls.zone.id)
        if shared_vlan is None:
            cls.fail("Failed to get free vlan id for shared network")

        cls.services["shared_network_offering"]["specifyVlan"] = "True"
        cls.services["shared_network_offering"]["specifyIpRanges"] = "True"

        cls.shared_network_offering = NetworkOffering.create(
            cls.apiclient,
            cls.services["shared_network_offering"],
            conservemode=False
        )

        NetworkOffering.update(
            cls.shared_network_offering,
            cls.apiclient,
            id=cls.shared_network_offering.id,
            state="enabled"
        )

        # create network using the shared network offering created
        cls.services["shared_network"]["acltype"] = "Domain"
        cls.services["shared_network"][
            "networkofferingid"] = cls.shared_network_offering.id
        cls.services["shared_network"][
            "physicalnetworkid"] = physical_network.id
        cls.services["shared_network"]["vlan"] = shared_vlan
        shared_network_subnet_number = random.randrange(1, 254)

        cls.services["shared_network"]["netmask"] = "255.255.255.0"
        cls.services["shared_network"]["gateway"] = "172.16." + \
                                                     str(shared_network_subnet_number) + ".254"
        cls.services["shared_network"]["startip"] = "172.16." + \
                                                     str(shared_network_subnet_number) + ".1"
        cls.services["shared_network"]["endip"] = "172.16." + \
                                                   str(shared_network_subnet_number) + ".10"

        cls.guest_network = Network.create(
            cls.apiclient,
            cls.services["shared_network"],
            networkofferingid=cls.shared_network_offering.id,
            zoneid=cls.zone.id
        )

        cls._cleanup.append(cls.guest_network)
        cls._cleanup.append(cls.shared_network_offering)
        cls._cleanup.append(cls.account1)
        cls._cleanup.append(cls.account2)
        cls._cleanup.append(cls.sub_account)
        cls._cleanup.append(cls.sub_domain)
        cls._cleanup.append(cls.domain1)
        cls._cleanup.append(cls.domain2)
        cls._cleanup.append(cls.public_ip_range1)
        cls._cleanup.append(cls.public_ip_range2)
        cls._cleanup.append(cls.public_ip_range3)
    def test_updating_nics_on_two_shared_networks(self):
        """
        Test updating vm nics on two shared networks
        Works as follows:
        - Create two shared networks, with different IP ranges (eg network1, network2).
        - Create two VMs on each shared network (eg vm1 on network1, vm2 on network2) and stop them.
        - Add additional IP range to each shared network. The two additional IP ranges have same start IP and end IP,
            but different gateway and netmask (eg network1-iprange2, network2-iprange2).
        - Update IP address of the two vms, by specifying new IP address on additional IP ranges. The new gateway and
            netmask should be updated correctly (eg vm1 on network1-iprange2, vm2 on network2-iprange2).
        """
        # - Create two shared networks, with different IP ranges (eg network1, network2).
        self.services["shared_network"]["netmask"] = "255.255.255.0"
        self.services["shared_network"]["gateway"] = "192.168.1.1"
        self.services["shared_network"]["startip"] = "192.168.1.2"
        self.services["shared_network"]["endip"] = "192.168.1.20"
        self.services["shared_network"]["vlan"] = "111"
        testnet = self.services["shared_network"]
        self.debug(f"========= network 1 : {testnet}")

        self.network1 = Network.create(
            self.apiclient,
            self.services["shared_network"],
            networkofferingid=self.shared_network_offering.id,
            zoneid=self.zone.id,
        )
        self._cleanup.append(self.network1)

        self.services["shared_network"]["netmask"] = "255.255.255.0"
        self.services["shared_network"]["gateway"] = "192.168.2.1"
        self.services["shared_network"]["startip"] = "192.168.2.2"
        self.services["shared_network"]["endip"] = "192.168.2.20"
        self.services["shared_network"]["vlan"] = "222"
        self.network2 = Network.create(
            self.apiclient,
            self.services["shared_network"],
            networkofferingid=self.shared_network_offering.id,
            zoneid=self.zone.id,
        )
        self._cleanup.append(self.network2)

        # - Create two VMs on each shared network (eg vm1 on network1, vm2 on network2) and stop them.
        vm_data = self.services["virtual_machine"]
        self.debug(f"============virtual machine data : {vm_data}")
        virtual_machine1 = VirtualMachine.create(
            self.apiclient,
            self.services["virtual_machine"],
            templateid=self.template.id,
            serviceofferingid=self.service_offering.id,
            networkids=[self.network1.id],
            zoneid=self.zone.id)
        self.cleanup.append(virtual_machine1)
        virtual_machine1.stop(self.apiclient)
        virtual_machine2 = VirtualMachine.create(
            self.apiclient,
            self.services["virtual_machine"],
            templateid=self.template.id,
            serviceofferingid=self.service_offering.id,
            networkids=[self.network2.id],
            zoneid=self.zone.id)
        self.cleanup.append(virtual_machine2)
        virtual_machine2.stop(self.apiclient)
        # - Add additional IP range to each shared network. The two additional IP ranges have same start IP and end IP,
        #   but different gateway and netmask (eg network1-iprange2, network2-iprange2).
        self.services["vlan_ip_range"]["netmask"] = "255.255.255.224"
        self.services["vlan_ip_range"]["gateway"] = "192.168.3.1"
        self.services["vlan_ip_range"]["startip"] = "192.168.3.2"
        self.services["vlan_ip_range"]["endip"] = "192.168.3.20"
        self.services["vlan_ip_range"]["vlan"] = "111"
        range1 = PublicIpRange.create(self.apiclient,
                                      self.services["vlan_ip_range"],
                                      networkid=self.network1.id)
        self.debug(f"============range 1 : {range1}")

        self.services["vlan_ip_range"]["netmask"] = "255.255.255.192"
        self.services["vlan_ip_range"]["gateway"] = "192.168.3.21"
        self.services["vlan_ip_range"]["startip"] = "192.168.3.2"
        self.services["vlan_ip_range"]["endip"] = "192.168.3.20"
        self.services["vlan_ip_range"]["vlan"] = "222"
        range2 = PublicIpRange.create(self.apiclient,
                                      self.services["vlan_ip_range"],
                                      networkid=self.network2.id)

        # - Update IP address of the two vms, by specifying new IP address on additional IP ranges. The new gateway and
        #   netmask should be updated correctly (eg vm1 on network1-iprange2, vm2 on network2-iprange2).
        nic1id = NIC.list(self.apiclient,
                          virtualmachineid=virtual_machine1.id,
                          networkid=self.network1.id)[0].id
        NIC.updateIp(self.apiclient, id=nic1id, ipaddress="192.168.3.2")
        nics = NIC.list(self.apiclient,
                        virtualmachineid=virtual_machine1.id,
                        networkid=self.network1.id)
        self.check_nic(nics, self.network1, range1)

        nic2id = NIC.list(self.apiclient,
                          virtualmachineid=virtual_machine2.id,
                          networkid=self.network2.id)[0].id
        NIC.updateIp(self.apiclient, id=nic2id, ipaddress="192.168.3.2")
        nics = NIC.list(self.apiclient,
                        virtualmachineid=virtual_machine2.id,
                        networkid=self.network2.id)
        self.check_nic(nics, self.network2, range2)

        return
    def setUpClass(cls):
        cls.testClient = super(
            TestAcquireSpecifiedPublicIp,
            cls).getClsTestClient()
        cls.apiclient = cls.testClient.getApiClient()
        cls.services = cls.testClient.getParsedTestDataConfig()

        zone = get_zone(cls.apiclient, cls.testClient.getZoneForTests())
        cls.zone = Zone(zone.__dict__)
        cls.template = get_template(cls.apiclient, cls.zone.id)
        cls._cleanup = []

        if str(cls.zone.securitygroupsenabled) == "True":
            sys.exit(1)

        cls.logger = logging.getLogger("TestAcquireSpecifiedPublicIp")
        cls.stream_handler = logging.StreamHandler()
        cls.logger.setLevel(logging.DEBUG)
        cls.logger.addHandler(cls.stream_handler)

        # Get Zone, Domain and templates
        cls.domain = get_domain(cls.apiclient)

        # Create new domain1
        cls.domain1 = Domain.create(
            cls.apiclient,
            services=cls.services["acl"]["domain1"],
            parentdomainid=cls.domain.id)

        # Create account1
        cls.account1 = Account.create(
            cls.apiclient,
            cls.services["acl"]["accountD1"],
            domainid=cls.domain1.id
        )

        # Create domain2
        cls.domain2 = Domain.create(
            cls.apiclient,
            services=cls.services["acl"]["domain2"],
            parentdomainid=cls.domain.id)

        # Create account2
        cls.account2 = Account.create(
            cls.apiclient,
            cls.services["acl"]["accountD2"],
            domainid=cls.domain2.id
        )

        cls.services["publiciprange"]["zoneid"] = cls.zone.id
        cls.services["publiciprange"]["forvirtualnetwork"] = "true"

        # Create public ip range 1
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        random_subnet_number = random.randrange(10,20)
        cls.services["publiciprange"]["gateway"] = "172.16." + \
            str(random_subnet_number) + ".1"
        cls.services["publiciprange"]["startip"] = "172.16." + \
            str(random_subnet_number) + ".2"
        cls.services["publiciprange"]["endip"] = "172.16." + \
            str(random_subnet_number) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range1 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"]
        )
        PublicIpRange.dedicate(
            cls.apiclient,
            cls.public_ip_range1.vlan.id,
            domainid=cls.account1.domainid
        )

        # Create public ip range 2
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        cls.services["publiciprange"]["gateway"] = "172.16." + \
            str(random_subnet_number + 1) + ".1"
        cls.services["publiciprange"]["startip"] = "172.16." + \
            str(random_subnet_number + 1) + ".2"
        cls.services["publiciprange"]["endip"] = "172.16." + \
            str(random_subnet_number + 1) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range2 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"]
        )
        PublicIpRange.dedicate(
            cls.apiclient,
            cls.public_ip_range2.vlan.id,
            account=cls.account1.name,
            domainid=cls.account1.domainid
        )

        # Create public ip range 3
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        cls.services["publiciprange"]["gateway"] = "172.16." + \
            str(random_subnet_number + 2) + ".1"
        cls.services["publiciprange"]["startip"] = "172.16." + \
            str(random_subnet_number + 2) + ".2"
        cls.services["publiciprange"]["endip"] = "172.16." + \
            str(random_subnet_number + 2) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range3 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"]
        )
        PublicIpRange.dedicate(
            cls.apiclient,
            cls.public_ip_range3.vlan.id,
            domainid=cls.account2.domainid
        )

        # Create public ip range 4
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        cls.services["publiciprange"]["gateway"] = "172.16." + \
            str(random_subnet_number + 3) + ".1"
        cls.services["publiciprange"]["startip"] = "172.16." + \
            str(random_subnet_number + 3) + ".2"
        cls.services["publiciprange"]["endip"] = "172.16." + \
            str(random_subnet_number + 3) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range4 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"]
        )
        PublicIpRange.dedicate(
            cls.apiclient,
            cls.public_ip_range4.vlan.id,
            account=cls.account2.name,
            domainid=cls.account2.domainid
        )

        # Create public ip range 5
        cls.services["publiciprange"]["vlan"] = get_free_vlan(
            cls.apiclient,
            cls.zone.id)[1]
        cls.services["publiciprange"]["gateway"] = "172.16." + \
            str(random_subnet_number + 4) + ".1"
        cls.services["publiciprange"]["startip"] = "172.16." + \
            str(random_subnet_number + 4) + ".2"
        cls.services["publiciprange"]["endip"] = "172.16." + \
            str(random_subnet_number + 4) + ".10"
        cls.services["publiciprange"]["netmask"] = "255.255.255.0"
        cls.public_ip_range5 = PublicIpRange.create(
            cls.apiclient,
            cls.services["publiciprange"]
        )

        cls._cleanup.append(cls.account1)
        cls._cleanup.append(cls.domain1)
        cls._cleanup.append(cls.account2)
        cls._cleanup.append(cls.domain2)
        cls._cleanup.append(cls.public_ip_range1)
        cls._cleanup.append(cls.public_ip_range2)
        cls._cleanup.append(cls.public_ip_range3)
        cls._cleanup.append(cls.public_ip_range4)
        cls._cleanup.append(cls.public_ip_range5)