Esempio n. 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 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