def test_nova_massively_spawn_v_ms_with_boot_local(self):
        """This test case creates a lot of VMs with boot local, checks it
        state and availability and then deletes it.

        Steps:
            1. Boot 10-100 instances from image.
            2. Check that list of instances contains created VMs.
            3. Check state of created instances
            4. Add the floating ips to the instances
            5. Ping the instances by the floating ips
        """
        initial_instances = self.nova.servers.list()
        primary_name = "testVM_543356"
        count = 10
        image_dict = {im.name: im.id for im in self.nova.images.list()}
        image_id = image_dict["TestVM"]
        flavor_dict = {f.name: f.id for f in self.nova.flavors.list()}
        flavor_id = flavor_dict["m1.micro"]
        networks = self.neutron.list_networks()["networks"]
        net_dict = {net["name"]: net["id"] for net in networks}
        net_internal_id = net_dict["admin_internal_net"]

        self.floating_ips = [self.nova.floating_ips.create()
                             for _ in xrange(count)]
        fip_new = [fip_info.ip for fip_info in self.floating_ips]
        fip_all = [fip_info.ip for fip_info in self.nova.floating_ips.list()]
        for fip in fip_new:
            self.assertIn(fip, fip_all)

        self.nova.servers.create(primary_name, image_id, flavor_id,
                                 max_count=count,
                                 security_groups=[self.sec_group.name],
                                 nics=[{"net-id": net_internal_id}])
        start_time = time()
        timeout = 5
        while len(self.nova.servers.list()) < len(initial_instances) + count \
                and time() < start_time + timeout * 60:
            sleep(5)

        instances = [inst for inst in self.nova.servers.list()
                     if inst not in initial_instances]
        self.instances = [inst.id for inst in instances]
        for inst_id in self.instances:
            self.assertTrue(common_functions.check_inst_status(self.nova,
                                                               inst_id,
                                                               'ACTIVE'))
        fip_dict = {}
        for inst in instances:
            fip = fip_new.pop()
            inst.add_floating_ip(fip)
            fip_dict[inst.id] = fip

        for inst_id in self.instances:
            self.assertTrue(common_functions.check_ip(
                self.nova, inst_id, fip_dict[inst_id]))

        for inst_id in self.instances:
            ping = common_functions.ping_command(fip_dict[inst_id], i=8)
            self.assertTrue(ping,
                            "Instance {} is not reachable".format(inst_id))
    def test_nova_launch_v_m_from_volume_with_all_flavours(self):
        """This test case checks creation of instance from volume with all
        types of flavor. For this test needs 2 nodes with compute role:
        20Gb RAM and 150GB disk for each

        Steps:
            1. Create bootable volume
            1. Create a floating ip
            2. Create an instance from an image with some flavor
            3. Add the floating ip to the instance
            4. Ping the instance by the floating ip
            5. Delete the floating ip
            6. delete the instance
            7. Repeat all steps for all types of flavor
        """
        image_id = [image.id for image in self.nova.images.list() if
                    image.name == 'TestVM'][0]
        networks = self.neutron.list_networks()['networks']
        net = [net['id'] for net in networks if not net['router:external']][0]
        flavor_list = self.nova.flavors.list()
        for flavor in flavor_list:
            floating_ip = self.nova.floating_ips.create()
            self.floating_ips.append(floating_ip)
            self.assertIn(floating_ip.ip, [fip_info.ip for fip_info in
                                           self.nova.floating_ips.list()])
            volume = common_functions.create_volume(self.cinder, image_id)
            self.volumes.append(volume)
            bdm = {'vda': volume.id}
            inst = common_functions.create_instance(self.nova,
                                                    "inst_543360_{}"
                                                    .format(flavor.name),
                                                    flavor.id, net,
                                                    [self.sec_group.name],
                                                    block_device_mapping=bdm,
                                                    inst_list=self.instances)
            inst_id = inst.id
            self.instances.append(inst_id)
            inst.add_floating_ip(floating_ip.ip)
            self.assertTrue(common_functions.check_ip(self.nova, inst_id,
                                                      floating_ip.ip))
            ping = common_functions.ping_command(floating_ip.ip)
            self.assertTrue(ping, "Instance is not reachable")
    def test_nova_launch_v_m_from_volume_with_all_flavours(self):
        """This test case checks creation of instance from volume with all
        types of flavor. For this test we need node with compute role:
        8 VCPUs, 16+GB RAM and 160+GB disk for any compute

        Steps:
            1. Create bootable volume
            1. Create a floating ip
            2. Create an instance from an image with some flavor
            3. Add the floating ip to the instance
            4. Ping the instance by the floating ip
            5. Delete the floating ip
            6. delete the instance
            7. Repeat all steps for all types of flavor
        """
        image_id = [image.id for image in self.nova.images.list() if
                    image.name == 'TestVM'][0]
        net = self.get_admin_int_net_id()
        flavor_list = self.nova.flavors.list()
        volume = common_functions.create_volume(self.cinder, image_id)
        self.volumes.append(volume)
        bdm = {'vda': volume.id}
        for flavor in flavor_list:
            floating_ip = self.nova.floating_ips.create()
            self.floating_ips.append(floating_ip)
            self.assertIn(floating_ip.ip, [fip_info.ip for fip_info in
                                           self.nova.floating_ips.list()])
            inst = common_functions.create_instance(self.nova,
                                                    "inst_543360_{}"
                                                    .format(flavor.name),
                                                    flavor.id, net,
                                                    [self.sec_group.id],
                                                    block_device_mapping=bdm,
                                                    inst_list=self.instances)
            inst.add_floating_ip(floating_ip.ip)
            self.assertTrue(common_functions.check_ip(self.nova, inst.id,
                                                      floating_ip.ip))
            ping = common_functions.ping_command(floating_ip.ip)
            common_functions.delete_instance(self.nova, inst.id)
            self.assertTrue(ping, "Instance is not reachable")
    def test_nova_launch_v_m_from_image_with_all_flavours(self):
        """This test case checks creation of instance from image with all
        types of flavor. For this test we need node with compute role:
        8 VCPUs, 16+GB RAM and 160+GB disk for any compute

        Steps:
            1. Create a floating ip
            2. Create an instance from an image with some flavor
            3. Add the floating ip to the instance
            4. Ping the instance by the floating ip
            5. Delete the floating ip
            6. delete the instance
            7. Repeat all steps for all types of flavor
        """
        networks = self.neutron.list_networks()['networks']
        net = [net['id'] for net in networks
               if not net['router:external']][0]
        image_id = [image.id for image in self.nova.images.list() if
                    image.name == 'TestVM'][0]
        flavor_list = self.nova.flavors.list()
        for flavor in flavor_list:
            floating_ip = self.nova.floating_ips.create()
            self.floating_ips.append(floating_ip)
            self.assertIn(floating_ip.ip, [fip_info.ip for fip_info in
                                           self.nova.floating_ips.list()])
            inst = common_functions.create_instance(self.nova,
                                                    "inst_543358_{}"
                                                    .format(flavor.name),
                                                    flavor.id, net,
                                                    [self.sec_group.name],
                                                    image_id=image_id,
                                                    inst_list=self.instances)
            inst.add_floating_ip(floating_ip.ip)
            self.assertTrue(common_functions.check_ip(self.nova, inst.id,
                                                      floating_ip.ip))
            ping = common_functions.ping_command(floating_ip.ip)
            common_functions.delete_instance(self.nova, inst.id)
            self.assertTrue(ping, "Instance is not reachable")
    def test_nova_massively_spawn_v_ms_boot_from_cinder(self):
        """This test case creates a lot of VMs which boot from Cinder, checks
        it state and availability and then deletes it.

        Steps:
            1. Create 10-100 volumes.
            2. Boot 10-100 instances from volumes.
            3. Check that list of instances contains created VMs.
            4. Check state of created instances
            5. Add the floating ips to the instances
            6. Ping the instances by the floating ips
        """
        initial_instances = self.nova.servers.list()
        count = 10
        primary_name = "testVM_543357"
        image_dict = {im.name: im.id for im in self.nova.images.list()}
        image_id = image_dict["TestVM"]
        flavor_dict = {f.name: f.id for f in self.nova.flavors.list()}
        flavor_id = flavor_dict["m1.tiny"]
        net_internal_id = self.get_admin_int_net_id()

        initial_volumes = self.cinder.volumes.list()
        for i in range(count):
            common_functions.create_volume(self.cinder, image_id, size=1)
        self.volumes = [volume for volume in self.cinder.volumes.list()
                        if volume not in initial_volumes]
        msg = "Count of created volumes is incorrect!"
        self.assertEqual(len(self.volumes), 10, msg)

        self.floating_ips = [self.nova.floating_ips.create()
                             for _ in range(count)]
        fip_new = [fip_info.ip for fip_info in self.floating_ips]
        fip_all = [fip_info.ip for fip_info in self.nova.floating_ips.list()]
        for fip in fip_new:
            self.assertIn(fip, fip_all)

        for volume in self.volumes:
            bdm = {'vda': volume.id}
            self.nova.servers.create(primary_name, '', flavor_id,
                                     security_groups=[self.sec_group.id],
                                     block_device_mapping=bdm,
                                     nics=[{"net-id": net_internal_id}])
        start_time = time()
        timeout = 5
        while len(self.nova.servers.list()) < len(initial_instances) + count \
                and time() < start_time + timeout * 60:
            sleep(5)

        instances = [inst for inst in self.nova.servers.list()
                     if inst not in initial_instances]
        self.instances = [inst.id for inst in instances]
        for inst_id in self.instances:
            self.assertTrue(common_functions.check_inst_status(self.nova,
                                                               inst_id,
                                                               'ACTIVE'))
        fip_dict = {}
        for inst in instances:
            fip = fip_new.pop()
            inst.add_floating_ip(fip)
            fip_dict[inst.id] = fip

        for inst_id in self.instances:
            self.assertTrue(common_functions.check_ip(
                self.nova, inst_id, fip_dict[inst_id]))

        for inst_id in self.instances:
            ping = common_functions.ping_command(fip_dict[inst_id], i=8)
            self.assertTrue(ping,
                            "Instance {} is not reachable".format(inst_id))
    def setUp(self):
        super(self.__class__, self).setUp()

        # Get path on node to 'images' dir
        self.image_name = os.path.join(settings.TEST_IMAGE_PATH, settings.WIN_SERVER_QCOW2)

        self.uid_list = []

        # timeouts (in minutes)
        self.ping_timeout = 3
        self.hypervisor_timeout = 10

        self.amount_of_images_before = len(list(self.glance.images.list()))
        self.image = None
        self.our_own_flavor_was_created = False
        self.expected_flavor_id = 3
        self.instance = None
        self.security_group_name = "ms_compatibility"
        # protect for multiple definition of the same group
        for sg in self.nova.security_groups.list():
            if sg.name == self.security_group_name:
                self.nova.security_groups.delete(sg)
        # adding required security group
        self.the_security_group = self.nova.security_groups.create(
            name=self.security_group_name, description="Windows Compatibility"
        )
        # Add rules for ICMP, TCP/22
        self.icmp_rule = self.nova.security_group_rules.create(
            self.the_security_group.id, ip_protocol="icmp", from_port=-1, to_port=-1, cidr="0.0.0.0/0"
        )
        self.tcp_rule = self.nova.security_group_rules.create(
            self.the_security_group.id, ip_protocol="tcp", from_port=22, to_port=22, cidr="0.0.0.0/0"
        )
        # Add both rules to default group
        self.default_security_group_id = 0
        for sg in self.nova.security_groups.list():
            if sg.name == "default":
                self.default_security_group_id = sg.id
                break
        self.icmp_rule_default = self.nova.security_group_rules.create(
            self.default_security_group_id, ip_protocol="icmp", from_port=-1, to_port=-1, cidr="0.0.0.0/0"
        )
        self.tcp_rule_default = self.nova.security_group_rules.create(
            self.default_security_group_id, ip_protocol="tcp", from_port=22, to_port=22, cidr="0.0.0.0/0"
        )
        # adding floating ip
        self.floating_ip = self.nova.floating_ips.create(self.nova.floating_ip_pools.list()[0].name)

        # creating of the image
        self.image = self.glance.images.create(name="MyTestSystem", disk_format="qcow2", container_format="bare")
        with open(self.image_name, "rb") as win_image_file:
            self.glance.images.upload(self.image.id, win_image_file)
        # check that required image in active state
        is_activated = False
        while not is_activated:
            for image_object in self.glance.images.list():
                if image_object.id == self.image.id:
                    self.image = image_object
                    logger.info("Image in the {} state".format(self.image.status))
                    if self.image.status == "active":
                        is_activated = True
                        break
            time.sleep(1)

        # Default - the first
        network_id = self.nova.networks.list()[0].id
        # More detailed check of network list
        for network in self.nova.networks.list():
            if "internal" in network.label:
                network_id = network.id
        logger.info("Starting with network interface id {}".format(network_id))

        # TODO(mlaptev) add check flavor parameters vs. vm parameters
        # Collect information about the medium flavor and create a copy of it
        for flavor in self.nova.flavors.list():
            # TODO(rpromyshlennikov): change flavor to medium if we will have
            # memory issues on windows and CI will be ready for it
            if "small" in flavor.name and "copy.of." not in flavor.name:
                new_flavor_name = "copy.of." + flavor.name
                new_flavor_id = common_functions.get_flavor_id_by_name(self.nova, new_flavor_name)
                # delete the flavor if it already exists
                if new_flavor_id is not None:
                    common_functions.delete_flavor(self.nova, new_flavor_id)
                # create the flavor for our needs
                expected_flavor = self.nova.flavors.create(
                    name=new_flavor_name, ram=flavor.ram, vcpus=1, disk=flavor.disk  # Only one VCPU
                )
                self.expected_flavor_id = expected_flavor.id
                self.our_own_flavor_was_created = True
                break
        logger.info("Starting with flavor {}".format(self.nova.flavors.get(self.expected_flavor_id)))
        # nova boot
        self.instance = common_functions.create_instance(
            nova_client=self.nova,
            inst_name="MyTestSystemWithNova",
            flavor_id=self.expected_flavor_id,
            net_id=network_id,
            security_groups=[self.the_security_group.name, "default"],
            image_id=self.image.id,
        )

        logger.info("Using following floating ip {}".format(self.floating_ip.ip))

        self.instance.add_floating_ip(self.floating_ip)

        self.assertTrue(common_functions.check_ip(self.nova, self.instance.id, self.floating_ip.ip))

        self.wait_instance_to_boot()
    def setUp(self):
        """

        :return: Nothing
        """
        self.amount_of_images_before = len(list(self.glance.images.list()))
        self.image = None
        self.our_own_flavor_was_created = False
        self.expected_flavor_id = 3
        self.node_to_boot = None
        self.security_group_name = "ms_compatibility"
        # protect for multiple definition of the same group
        for sg in self.nova.security_groups.list():
            if sg.name == self.security_group_name:
                self.nova.security_groups.delete(sg)
        # adding required security group
        self.the_security_group = self.nova.security_groups.create(
                name=self.security_group_name,
                description="Windows Compatibility")
        # Add rules for ICMP, TCP/22
        self.icmp_rule = self.nova.security_group_rules.create(
                self.the_security_group.id,
                ip_protocol="icmp",
                from_port=-1,
                to_port=-1,
                cidr="0.0.0.0/0")
        self.tcp_rule = self.nova.security_group_rules.create(
                self.the_security_group.id,
                ip_protocol="tcp",
                from_port=22,
                to_port=22,
                cidr="0.0.0.0/0")
        # Add both rules to default group
        self.default_security_group_id = 0
        for sg in self.nova.security_groups.list():
            if sg.name == 'default':
                self.default_security_group_id = sg.id
                break
        self.icmp_rule_default = self.nova.security_group_rules.create(
                self.default_security_group_id,
                ip_protocol="icmp",
                from_port=-1,
                to_port=-1,
                cidr="0.0.0.0/0")
        self.tcp_rule_default = self.nova.security_group_rules.create(
                self.default_security_group_id,
                ip_protocol="tcp",
                from_port=22,
                to_port=22,
                cidr="0.0.0.0/0")
        # adding floating ip
        self.floating_ip = self.nova.floating_ips.create(
                self.nova.floating_ip_pools.list()[0].name)

        # creating of the image
        self.image = self.glance.images.create(
                name='MyTestSystem',
                disk_format='qcow2',
                container_format='bare')
        self.glance.images.upload(
                self.image.id,
                open('/tmp/trusty-server-cloudimg-amd64-disk1.img', 'rb'))
        # check that required image in active state
        is_activated = False
        while not is_activated:
            for image_object in self.glance.images.list():
                if image_object.id == self.image.id:
                    self.image = image_object
                    logger.info("Image in the {} state".
                                 format(self.image.status))
                    if self.image.status == 'active':
                        is_activated = True
                        break
            time.sleep(1)

        # Default - the first
        network_id = self.nova.networks.list()[0].id
        # More detailed check of network list
        for network in self.nova.networks.list():
            if 'internal' in network.label:
                network_id = network.id
        logger.info("Starting with network interface id {}".
                     format(network_id))

        # TODO: add check flavor parameters vs. vm parameters
        # Collect information about the medium flavor and create a copy of it
        for flavor in self.nova.flavors.list():
            if 'medium' in flavor.name and 'copy.of.' not in flavor.name:
                new_flavor_name = "copy.of." + flavor.name
                new_flavor_id = common_functions.get_flavor_id_by_name(
                        self.nova,
                        new_flavor_name)
                # delete the flavor if it already exists
                if new_flavor_id is not None:
                    common_functions.delete_flavor(
                            self.nova,
                            new_flavor_id)
                # create the flavor for our needs
                expected_flavor = self.nova.flavors.create(
                        name=new_flavor_name,
                        ram=flavor.ram,
                        vcpus=1,  # Only one VCPU
                        disk=flavor.disk
                )
                self.expected_flavor_id = expected_flavor.id
                self.our_own_flavor_was_created = True
                break
        logger.info("Starting with flavor {}".format(
                self.nova.flavors.get(self.expected_flavor_id)))
        # nova boot
        self.node_to_boot = common_functions.create_instance(
                    nova_client=self.nova,
                    inst_name="MyTestSystemWithNova",
                    flavor_id=self.expected_flavor_id,
                    net_id=network_id,
                    security_groups=[self.the_security_group.name, 'default'],
                    image_id=self.image.id)
        # check that boot returns expected results
        self.assertEqual(self.node_to_boot.status, 'ACTIVE',
                         "The node not in active state!")

        logger.info("Using following floating ip {}".format(
                self.floating_ip.ip))

        self.node_to_boot.add_floating_ip(self.floating_ip)

        self.assertTrue(common_functions.check_ip(
                self.nova,
                self.node_to_boot.id,
                self.floating_ip.ip))