Beispiel #1
0
def CreateNetwork(self, networktype):
    """Create a network of given type (isolated/shared/isolated in VPC)"""

    network = None

    if networktype == ISOLATED_NETWORK:
        try:
            network = Network.create(
                self.apiclient, self.testdata["isolated_network"],
                networkofferingid=self.isolated_network_offering.id,
                accountid=self.account.name,
                domainid=self.account.domainid,
                zoneid=self.zone.id)
            self.cleanup.append(network)
        except Exception as e:
            self.fail("Isolated network creation failed because: %s" % e)

    elif networktype == SHARED_NETWORK:
        physical_network, vlan = get_free_vlan(self.apiclient, self.zone.id)

        # create network using the shared network offering created
        self.testdata["shared_network"]["acltype"] = "domain"
        self.testdata["shared_network"]["vlan"] = vlan
        self.testdata["shared_network"]["networkofferingid"] = \
            self.shared_network_offering.id
        self.testdata["shared_network"]["physicalnetworkid"] = \
            physical_network.id

        self.testdata["shared_network"] = \
            setSharedNetworkParams(self.testdata["shared_network"])

        try:
            network = Network.create(
                self.apiclient,
                self.testdata["shared_network"],
                networkofferingid=self.shared_network_offering.id,
                zoneid=self.zone.id)
            self.cleanup.append(network)
        except Exception as e:
            self.fail("Shared Network creation failed because: %s" % e)

    elif networktype == VPC_NETWORK:
        self.testdata["vpc"]["cidr"] = "10.1.1.1/16"
        self.debug("creating a VPC network in the account: %s" %
                   self.account.name)
        vpc = VPC.create(self.apiclient,
                         self.testdata["vpc"],
                         vpcofferingid=self.vpc_off.id,
                         zoneid=self.zone.id,
                         account=self.account.name,
                         domainid=self.account.domainid
                         )
        self.cleanup.append(vpc)
        self.vpcid = vpc.id
        vpcs = VPC.list(self.apiclient, id=vpc.id)
        self.assertEqual(
            validateList(vpcs)[0], PASS,
            "VPC list validation failed, vpc list is %s" % vpcs
        )

        network = Network.create(
            self.apiclient,
            self.testdata["isolated_network"],
            networkofferingid=self.isolated_network_offering_vpc.id,
            accountid=self.account.name,
            domainid=self.account.domainid,
            zoneid=self.zone.id,
            vpcid=vpc.id,
            gateway="10.1.1.1",
            netmask="255.255.255.0")
        self.cleanup.append(network)
    return network
    def setUpClass(cls):
        cls.testClient = super(TestMulipleNicSupport, cls).getClsTestClient()
        cls.apiclient = cls.testClient.getApiClient()
        cls.testdata = cls.testClient.getParsedTestDataConfig()
        cls.services = cls.testClient.getParsedTestDataConfig()
        zone = get_zone(cls.apiclient, cls.testClient.getZoneForTests())
        cls.zone = Zone(zone.__dict__)
        cls._cleanup = []

        cls.skip = False
        if str(cls.zone.securitygroupsenabled) != "True":
            cls.skip = True
            return

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

        # Get Domain and templates
        cls.domain = get_domain(cls.apiclient)
        cls.services['mode'] = cls.zone.networktype

        cls.template = get_template(cls.apiclient,
                                    cls.zone.id,
                                    hypervisor="KVM")
        if cls.template == FAILED:
            cls.skip = True
            return

        # Create new domain, account, network and VM
        cls.user_domain = Domain.create(
            cls.apiclient,
            services=cls.testdata["acl"]["domain2"],
            parentdomainid=cls.domain.id)

        # Create account
        cls.account1 = Account.create(cls.apiclient,
                                      cls.testdata["acl"]["accountD2"],
                                      admin=True,
                                      domainid=cls.user_domain.id)

        # Create small service offering
        cls.service_offering = ServiceOffering.create(
            cls.apiclient, cls.testdata["service_offerings"]["small"])

        cls._cleanup.append(cls.service_offering)
        cls.services["network"]["zoneid"] = cls.zone.id
        cls.network_offering = NetworkOffering.create(
            cls.apiclient,
            cls.services["network_offering"],
        )
        # Enable Network offering
        cls.network_offering.update(cls.apiclient, state='Enabled')

        cls._cleanup.append(cls.network_offering)
        cls.testdata["virtual_machine"]["zoneid"] = cls.zone.id
        cls.testdata["virtual_machine"]["template"] = cls.template.id

        if cls.zone.securitygroupsenabled:
            # Enable networking for reaching to VM thorugh SSH
            security_group = SecurityGroup.create(
                cls.apiclient,
                cls.testdata["security_group"],
                account=cls.account1.name,
                domainid=cls.account1.domainid)

            # Authorize Security group to SSH to VM
            ingress_rule = security_group.authorize(
                cls.apiclient,
                cls.testdata["ingress_rule"],
                account=cls.account1.name,
                domainid=cls.account1.domainid)

            # Authorize Security group to SSH to VM
            ingress_rule2 = security_group.authorize(
                cls.apiclient,
                cls.testdata["ingress_rule_ICMP"],
                account=cls.account1.name,
                domainid=cls.account1.domainid)

        cls.testdata["shared_network_offering_sg"]["specifyVlan"] = 'True'
        cls.testdata["shared_network_offering_sg"]["specifyIpRanges"] = 'True'
        cls.shared_network_offering = NetworkOffering.create(
            cls.apiclient,
            cls.testdata["shared_network_offering_sg"],
            conservemode=False)

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

        physical_network, vlan = get_free_vlan(cls.apiclient, cls.zone.id)
        cls.testdata["shared_network_sg"][
            "physicalnetworkid"] = physical_network.id

        random_subnet_number = random.randrange(90, 99)
        cls.testdata["shared_network_sg"][
            "name"] = "Shared-Network-SG-Test-vlan" + str(random_subnet_number)
        cls.testdata["shared_network_sg"][
            "displaytext"] = "Shared-Network-SG-Test-vlan" + str(
                random_subnet_number)
        cls.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        cls.testdata["shared_network_sg"]["startip"] = "192.168." + str(
            random_subnet_number) + ".240"
        cls.testdata["shared_network_sg"]["endip"] = "192.168." + str(
            random_subnet_number) + ".250"
        cls.testdata["shared_network_sg"]["gateway"] = "192.168." + str(
            random_subnet_number) + ".254"
        cls.network1 = Network.create(
            cls.apiclient,
            cls.testdata["shared_network_sg"],
            networkofferingid=cls.shared_network_offering.id,
            zoneid=cls.zone.id,
            accountid=cls.account1.name,
            domainid=cls.account1.domainid)

        random_subnet_number = random.randrange(100, 110)
        cls.testdata["shared_network_sg"][
            "name"] = "Shared-Network-SG-Test-vlan" + str(random_subnet_number)
        cls.testdata["shared_network_sg"][
            "displaytext"] = "Shared-Network-SG-Test-vlan" + str(
                random_subnet_number)
        cls.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        cls.testdata["shared_network_sg"]["startip"] = "192.168." + str(
            random_subnet_number) + ".240"
        cls.testdata["shared_network_sg"]["endip"] = "192.168." + str(
            random_subnet_number) + ".250"
        cls.testdata["shared_network_sg"]["gateway"] = "192.168." + str(
            random_subnet_number) + ".254"
        cls.network2 = Network.create(
            cls.apiclient,
            cls.testdata["shared_network_sg"],
            networkofferingid=cls.shared_network_offering.id,
            zoneid=cls.zone.id,
            accountid=cls.account1.name,
            domainid=cls.account1.domainid)

        random_subnet_number = random.randrange(111, 120)
        cls.testdata["shared_network_sg"][
            "name"] = "Shared-Network-SG-Test-vlan" + str(random_subnet_number)
        cls.testdata["shared_network_sg"][
            "displaytext"] = "Shared-Network-SG-Test-vlan" + str(
                random_subnet_number)
        cls.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        cls.testdata["shared_network_sg"]["startip"] = "192.168." + str(
            random_subnet_number) + ".240"
        cls.testdata["shared_network_sg"]["endip"] = "192.168." + str(
            random_subnet_number) + ".250"
        cls.testdata["shared_network_sg"]["gateway"] = "192.168." + str(
            random_subnet_number) + ".254"
        cls.network3 = Network.create(
            cls.apiclient,
            cls.testdata["shared_network_sg"],
            networkofferingid=cls.shared_network_offering.id,
            zoneid=cls.zone.id,
            accountid=cls.account1.name,
            domainid=cls.account1.domainid)

        try:
            cls.virtual_machine1 = VirtualMachine.create(
                cls.apiclient,
                cls.testdata["virtual_machine"],
                accountid=cls.account1.name,
                domainid=cls.account1.domainid,
                serviceofferingid=cls.service_offering.id,
                templateid=cls.template.id,
                securitygroupids=[security_group.id],
                networkids=cls.network1.id)
            for nic in cls.virtual_machine1.nic:
                if nic.isdefault:
                    cls.virtual_machine1.ssh_ip = nic.ipaddress
                    cls.virtual_machine1.default_network_id = nic.networkid
                    break
        except Exception as e:
            cls.fail("Exception while deploying virtual machine: %s" % e)

        try:
            cls.virtual_machine2 = VirtualMachine.create(
                cls.apiclient,
                cls.testdata["virtual_machine"],
                accountid=cls.account1.name,
                domainid=cls.account1.domainid,
                serviceofferingid=cls.service_offering.id,
                templateid=cls.template.id,
                securitygroupids=[security_group.id],
                networkids=[str(cls.network1.id),
                            str(cls.network2.id)])
            for nic in cls.virtual_machine2.nic:
                if nic.isdefault:
                    cls.virtual_machine2.ssh_ip = nic.ipaddress
                    cls.virtual_machine2.default_network_id = nic.networkid
                    break
        except Exception as e:
            cls.fail("Exception while deploying virtual machine: %s" % e)

        cls._cleanup.append(cls.virtual_machine1)
        cls._cleanup.append(cls.virtual_machine2)
        cls._cleanup.append(cls.network1)
        cls._cleanup.append(cls.network2)
        cls._cleanup.append(cls.network3)
        cls._cleanup.append(cls.shared_network_offering)
        if cls.zone.securitygroupsenabled:
            cls._cleanup.append(security_group)
        cls._cleanup.append(cls.account1)
        cls._cleanup.append(cls.user_domain)
    def test_01_create_network_without_start_end_ip(self):
        """Create Shared network without start and end ip

            Steps:
            # 1. Update the global setting allow.empty.start.end.ipaddress to true
            # 2. Create a shared network without specifying start or end ip
            # 3. This should create the network
            # 4. Now Update the global setting allow.empty.start.end.ipaddress to false
            # 5. Create a shared network without specifying start or end ip
            # 6. Exception should be thrown since start and end ip are not specified
        :return:
        """
        # Create network offering
        self.network_offering = NetworkOffering.create(
            self.apiclient, self.testdata["network_offering_shared"])

        NetworkOffering.update(self.network_offering,
                               self.apiclient,
                               id=self.network_offering.id,
                               state="enabled")

        physical_network, vlan = get_free_vlan(self.apiclient, self.zone.id)
        self.testdata["shared_network_sg"][
            "physicalnetworkid"] = physical_network.id

        random_subnet_number = random.randrange(100, 199)
        self.testdata["shared_network_sg"]["specifyVlan"] = 'True'
        self.testdata["shared_network_sg"]["specifyIpRanges"] = 'True'
        self.testdata["shared_network_sg"][
            "name"] = "Shared-Network-SG-Test-vlan" + str(random_subnet_number)
        self.testdata["shared_network_sg"][
            "displaytext"] = "Shared-Network-SG-Test-vlan" + str(
                random_subnet_number)
        self.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        self.testdata["shared_network_sg"]["startip"] = None
        self.testdata["shared_network_sg"]["endip"] = None
        self.testdata["shared_network_sg"]["gateway"] = "192.168." + str(
            random_subnet_number) + ".254"
        self.testdata["shared_network_sg"]["netmask"] = "255.255.255.0"
        self.testdata["shared_network_sg"]["acltype"] = "account"

        # Update the global setting to true
        Configurations.update(self.apiclient,
                              name="allow.empty.start.end.ipaddress",
                              value="true")

        # Create network
        network = Network.create(self.apiclient,
                                 self.testdata["shared_network_sg"],
                                 networkofferingid=self.network_offering.id,
                                 zoneid=self.zone.id,
                                 accountid=self.account.name,
                                 domainid=self.account.domainid)

        self.logger.info("network id is %s" % network.id)
        self.cleanup.append(network)

        # Update the global setting to false
        Configurations.update(self.apiclient,
                              name="allow.empty.start.end.ipaddress",
                              value="false")

        # Exception should be thrown
        with self.assertRaises(Exception):
            self.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
                random_subnet_number)
            network2 = Network.create(
                self.apiclient,
                self.testdata["shared_network_sg"],
                networkofferingid=self.network_offering.id,
                zoneid=self.zone.id,
                accountid=self.account.name,
                domainid=self.account.domainid)

        # Restore the setting to default value which is true
        Configurations.update(self.apiclient,
                              name="allow.empty.start.end.ipaddress",
                              value="true")

        self.cleanup.append(self.network_offering)
    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")
    def test_02_create_network_with_same_name(self):
        """Create Shared network with same name in same account

            Steps:
            # 1. Update the global setting allow.duplicate.networkname to true
            # 2. Create a shared network in an account
            # 3. Try to create another shared network with same name in the same account
            # 4. No exception should be thrown as multiple networks with same name can be created
            # 5. Now update the global setting allow.duplicate.networkname to false
            # 6. Try to create another shared network with same name in the same account
            # 7. Exception should be thrown as network with same name cant be created in the same account
        :return:
        """
        # Update the global setting to true
        Configurations.update(self.apiclient,
                              name="allow.duplicate.networkname",
                              value="true")

        # Create network offering
        self.network_offering = NetworkOffering.create(
            self.apiclient, self.testdata["network_offering_shared"])

        NetworkOffering.update(self.network_offering,
                               self.apiclient,
                               id=self.network_offering.id,
                               state="enabled")

        physical_network, vlan = get_free_vlan(self.apiclient, self.zone.id)
        self.testdata["shared_network_sg"][
            "physicalnetworkid"] = physical_network.id

        random_subnet_number = random.randrange(100, 199)
        self.testdata["shared_network_sg"]["specifyVlan"] = 'True'
        self.testdata["shared_network_sg"]["specifyIpRanges"] = 'True'
        self.testdata["shared_network_sg"][
            "name"] = "Shared-Network-SG-Test-vlan-1"
        self.testdata["shared_network_sg"][
            "displaytext"] = "Shared-Network-SG-Test-vlan-1"
        self.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        self.testdata["shared_network_sg"]["startip"] = "192.168." + str(
            random_subnet_number) + ".1"
        self.testdata["shared_network_sg"]["endip"] = "192.168." + str(
            random_subnet_number) + ".10"
        self.testdata["shared_network_sg"]["gateway"] = "192.168." + str(
            random_subnet_number) + ".254"
        self.testdata["shared_network_sg"]["netmask"] = "255.255.255.0"
        self.testdata["shared_network_sg"]["acltype"] = "account"

        # Create the first network
        network3 = Network.create(self.apiclient,
                                  self.testdata["shared_network_sg"],
                                  networkofferingid=self.network_offering.id,
                                  zoneid=self.zone.id,
                                  accountid=self.account.name,
                                  domainid=self.account.domainid)
        self.cleanup.append(network3)

        # Create the second network with same name. No exception should be thrown
        random_subnet_number = random.randrange(100, 199)
        self.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
            random_subnet_number)
        network4 = Network.create(self.apiclient,
                                  self.testdata["shared_network_sg"],
                                  networkofferingid=self.network_offering.id,
                                  zoneid=self.zone.id,
                                  accountid=self.account.name,
                                  domainid=self.account.domainid)

        self.cleanup.append(network4)

        # Update the global setting to true
        Configurations.update(self.apiclient,
                              name="allow.duplicate.networkname",
                              value="false")

        # Exception should be thrown while creating another network with same name
        with self.assertRaises(Exception):
            random_subnet_number = random.randrange(100, 199)
            self.testdata["shared_network_sg"]["vlan"] = "vlan://" + str(
                random_subnet_number)
            network5 = Network.create(
                self.apiclient,
                self.testdata["shared_network_sg"],
                networkofferingid=self.network_offering.id,
                zoneid=self.zone.id,
                accountid=self.account.name,
                domainid=self.account.domainid)

        # Update the global setting to original value
        Configurations.update(self.apiclient,
                              name="allow.duplicate.networkname",
                              value="true")

        self.cleanup.append(self.network_offering)
Beispiel #6
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 CreateNetwork(self, networktype):
    """Create a network of given type (isolated/shared/isolated in VPC)"""

    network = None

    if networktype == ISOLATED_NETWORK:
        try:
            network = Network.create(
                self.apiclient,
                self.testdata["isolated_network"],
                networkofferingid=self.isolated_network_offering.id,
                accountid=self.account.name,
                domainid=self.account.domainid,
                zoneid=self.zone.id)
            self.cleanup.append(network)
        except Exception as e:
            self.fail("Isolated network creation failed because: %s" % e)

    elif networktype == SHARED_NETWORK:
        physical_network, vlan = get_free_vlan(self.apiclient, self.zone.id)

        # create network using the shared network offering created
        self.testdata["shared_network"]["acltype"] = "domain"
        self.testdata["shared_network"]["vlan"] = vlan
        self.testdata["shared_network"]["networkofferingid"] = \
            self.shared_network_offering.id
        self.testdata["shared_network"]["physicalnetworkid"] = \
            physical_network.id

        self.testdata["shared_network"] = \
            setSharedNetworkParams(self.testdata["shared_network"])

        try:
            network = Network.create(
                self.apiclient,
                self.testdata["shared_network"],
                networkofferingid=self.shared_network_offering.id,
                zoneid=self.zone.id)
            self.cleanup.append(network)
        except Exception as e:
            self.fail("Shared Network creation failed because: %s" % e)

    elif networktype == VPC_NETWORK:
        self.testdata["vpc"]["cidr"] = "10.1.1.1/16"
        self.debug("creating a VPC network in the account: %s" %
                   self.account.name)
        vpc = VPC.create(self.apiclient,
                         self.testdata["vpc"],
                         vpcofferingid=self.vpc_off.id,
                         zoneid=self.zone.id,
                         account=self.account.name,
                         domainid=self.account.domainid)
        self.cleanup.append(vpc)
        self.vpcid = vpc.id
        vpcs = VPC.list(self.apiclient, id=vpc.id)
        self.assertEqual(
            validateList(vpcs)[0], PASS,
            "VPC list validation failed, vpc list is %s" % vpcs)

        network = Network.create(
            self.apiclient,
            self.testdata["isolated_network"],
            networkofferingid=self.isolated_network_offering_vpc.id,
            accountid=self.account.name,
            domainid=self.account.domainid,
            zoneid=self.zone.id,
            vpcid=vpc.id,
            gateway="10.1.1.1",
            netmask="255.255.255.0")
        self.cleanup.append(network)
    return network
    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)
    def test_03_network_create(self):
        """ Test create network in project
        """
        # Validate the following
        # 1. Create a project.
        # 2. Add virtual/direct network resource to the project. User shared
        #    network resource for the project
        # 3. Verify any number of Project level Virtual/Direct networks can be
        #    created and used for vm deployment within the project.
        # 4. Verify shared networks (zone and domain wide) from outside the
        #    project can also be used in a project.

        # Create project as a domain admin
        project = Project.create(
            self.apiclient, self.services["project"], account=self.account.name, domainid=self.account.domainid
        )
        # Cleanup created project at end of test
        self.cleanup.append(project)
        self.debug("Created project with domain admin with ID: %s" % project.id)

        network_offerings = list_network_offerings(
            self.apiclient, projectid=project.id, supportedServices="SourceNat", type="isolated", state="Enabled"
        )
        self.assertEqual(isinstance(network_offerings, list), True, "Check for the valid network offerings")
        network_offering = network_offerings[0]

        self.debug("creating a network with network offering ID: %s" % network_offering.id)
        self.services["network"]["zoneid"] = self.zone.id
        network = Network.create(
            self.apiclient, self.services["network"], networkofferingid=network_offering.id, projectid=project.id
        )
        self.debug("Created network with ID: %s" % network.id)
        networks = Network.list(self.apiclient, projectid=project.id, listall=True)
        self.assertEqual(isinstance(networks, list), True, "Check for the valid network list response")

        self.debug("Deploying VM with network: %s" % network.id)

        virtual_machine = VirtualMachine.create(
            self.apiclient,
            self.services["server"],
            templateid=self.template.id,
            networkids=[str(network.id)],
            serviceofferingid=self.service_offering.id,
            projectid=project.id,
        )
        self.debug("Deployed VM with ID: %s" % virtual_machine.id)
        # Verify VM state
        self.assertEqual(virtual_machine.state, "Running", "Check VM state is Running or not")

        network_offerings = list_network_offerings(
            self.apiclient,
            state="Enabled",
            guestiptype="Shared",
            name="DefaultSharedNetworkOffering",
            displaytext="Offering for Shared networks",
        )
        self.assertEqual(isinstance(network_offerings, list), True, "Check for the valid network offerings")
        network_offering = network_offerings[0]

        self.debug("creating a shared network in domain: %s" % self.domain.id)

        # Getting physical network and free vlan in it
        physical_network, vlan = get_free_vlan(self.apiclient, self.zone.id)

        self.services["domain_network"]["vlan"] = vlan
        self.services["domain_network"]["physicalnetworkid"] = physical_network.id

        # Generating random subnet number for shared network creation
        shared_network_subnet_number = random.randrange(1, 254)

        self.services["domain_network"]["gateway"] = "172.16." + str(shared_network_subnet_number) + ".1"
        self.services["domain_network"]["startip"] = "172.16." + str(shared_network_subnet_number) + ".2"
        self.services["domain_network"]["endip"] = "172.16." + str(shared_network_subnet_number) + ".20"

        domain_network = Network.create(
            self.apiclient,
            self.services["domain_network"],
            domainid=self.domain.id,
            networkofferingid=network_offering.id,
            zoneid=self.zone.id,
        )
        self.cleanup.append(domain_network)
        self.debug("Created network with ID: %s" % domain_network.id)

        virtual_machine = VirtualMachine.create(
            self.apiclient,
            self.services["server"],
            templateid=self.template.id,
            networkids=[str(domain_network.id)],
            serviceofferingid=self.service_offering.id,
            projectid=project.id,
        )
        self.debug("Deployed VM with ID: %s" % virtual_machine.id)
        # Verify VM state
        self.assertEqual(virtual_machine.state, "Running", "Check VM state is Running or not")

        # Delete VM before network gets deleted in cleanup
        virtual_machine.delete(self.apiclient, expunge=True)
        return