Exemplo n.º 1
0
Arquivo: demo.py Projeto: opnfv/snaps
                                 subnet_settings=[subnet_settings])
network = OpenStackNetwork(os_creds, network_settings)
network.create()

# Flavors
from snaps.config.flavor import FlavorConfig
from snaps.openstack.create_flavor import OpenStackFlavor

flavor_settings = FlavorConfig(name='test-flavor', ram=256, disk=10, vcpus=2)
flavor = OpenStackFlavor(os_creds, flavor_settings)
flavor.create()

# Instances
from snaps.config.network import PortConfig
from snaps.openstack.create_instance import OpenStackVmInstance

port_settings = PortConfig(name='test-port',
                           network_name=network_settings.name)
instance_settings = VmInstanceConfig(name='test-inst',
                                     flavor=flavor_settings.name,
                                     port_settings=[port_settings])

vm_inst = OpenStackVmInstance(os_creds, instance_settings, image_settings)
vm_inst.create(block=True)

# Cleanup
vm_inst.clean()
flavor.clean()
network.clean()
image.clean()
Exemplo n.º 2
0
    def tearDown(self):
        """
        Cleans the stack and image
        """
        if self.stack:
            try:
                heat_utils.delete_stack(self.heat_client, self.stack)
                # Wait until stack deployment has completed
                end_time = (time.time() + stack_config.STACK_COMPLETE_TIMEOUT)
                is_deleted = False
                while time.time() < end_time:
                    status = heat_utils.get_stack_status(
                        self.heat_client, self.stack.id)
                    if status == stack_config.STATUS_DELETE_COMPLETE:
                        is_deleted = True
                        break
                    elif status == stack_config.STATUS_DELETE_FAILED:
                        is_deleted = False
                        break

                    time.sleep(3)

                if not is_deleted:
                    nova = nova_utils.nova_client(self.os_creds,
                                                  self.os_session)
                    keystone = keystone_utils.keystone_client(
                        self.os_creds, self.os_session)
                    neutron = neutron_utils.neutron_client(
                        self.os_creds, self.os_session)
                    glance = glance_utils.glance_client(
                        self.os_creds, self.os_session)

                    servers = heat_utils.get_stack_servers(
                        self.heat_client, nova, neutron, keystone, self.stack,
                        self.os_creds.project_name)
                    for server in servers:
                        vm_settings = settings_utils.create_vm_inst_config(
                            nova, keystone, neutron, server,
                            self.os_creds.project_name)
                        img_settings = settings_utils.determine_image_config(
                            glance, server, [
                                self.image_creator1.image_settings,
                                self.image_creator2.image_settings
                            ])
                        vm_creator = OpenStackVmInstance(
                            self.os_creds, vm_settings, img_settings)
                        vm_creator.initialize()
                        vm_creator.clean()
                        vm_creator.vm_deleted(block=True)

                    heat_utils.delete_stack(self.heat_client, self.stack)
                    time.sleep(20)
            except:
                raise

        if self.image_creator1:
            try:
                self.image_creator1.clean()
            except:
                pass

        if self.image_creator2:
            try:
                self.image_creator2.clean()
            except:
                pass

        if self.keypair1_settings:
            expanded_path = os.path.expanduser(
                self.keypair1_settings.private_filepath)
            os.chmod(expanded_path, 0o755)
            os.remove(expanded_path)

        if self.keypair2_settings:
            expanded_path = os.path.expanduser(
                self.keypair2_settings.private_filepath)
            os.chmod(expanded_path, 0o755)
            os.remove(expanded_path)

        super(self.__class__, self).__clean__()
Exemplo n.º 3
0
class NovaUtilsInstanceVolumeTests(OSComponentTestCase):
    """
    Tests the creation of VM instances via nova_utils.py
    """

    def setUp(self):
        """
        Setup objects required by VM instances
        :return:
        """

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())

        self.nova = nova_utils.nova_client(self.os_creds, self.os_session)
        self.cinder = cinder_utils.cinder_client(
            self.os_creds, self.os_session)

        self.image_creator = None
        self.network_creator = None
        self.flavor_creator = None
        self.volume_creator = None
        self.instance_creator = None

        try:
            image_settings = openstack_tests.cirros_image_settings(
                name=guid + '-image', image_metadata=self.image_metadata)
            self.image_creator = OpenStackImage(
                self.os_creds, image_settings=image_settings)
            self.image_creator.create()

            network_settings = openstack_tests.get_priv_net_config(
                project_name=self.os_creds.project_name,
                net_name="{}-{}".format(guid, 'net'),
                subnet_name="{}-{}".format(guid, 'subnet')).network_settings

            self.network_creator = OpenStackNetwork(
                self.os_creds, network_settings)
            self.network_creator.create()

            flavor_settings = openstack_tests.get_flavor_config(
                name=guid + '-flavor', ram=256, disk=10, vcpus=1,
                metadata=self.flavor_metadata)
            self.flavor_creator = OpenStackFlavor(
                self.os_creds, flavor_settings)
            self.flavor_creator.create()

            # Create Volume
            volume_settings = VolumeConfig(
                name=self.__class__.__name__ + '-' + str(guid))
            self.volume_creator = OpenStackVolume(
                self.os_creds, volume_settings)
            self.volume_creator.create(block=True)

            port_settings = PortConfig(
                name=guid + '-port', network_name=network_settings.name)
            instance_settings = VmInstanceConfig(
                name=guid + '-vm_inst',
                flavor=self.flavor_creator.flavor_settings.name,
                port_settings=[port_settings])
            self.instance_creator = OpenStackVmInstance(
                self.os_creds, instance_settings, image_settings)
            self.instance_creator.create(block=True)
        except:
            self.tearDown()
            raise

    def tearDown(self):
        """
        Cleanup deployed resources
        :return:
        """
        if self.instance_creator:
            try:
                self.instance_creator.clean()
            except:
                pass
        if self.volume_creator:
            try:
                self.volume_creator.clean()
            except:
                pass
        if self.flavor_creator:
            try:
                self.flavor_creator.clean()
            except:
                pass
        if self.network_creator:
            try:
                self.network_creator.clean()
            except:
                pass
        if self.image_creator:
            try:
                self.image_creator.clean()
            except:
                pass

        super(self.__class__, self).__clean__()

    def test_add_remove_volume(self):
        """
        Tests the nova_utils.attach_volume() and detach_volume functions with
        a timeout value
        :return:
        """

        self.assertIsNotNone(self.volume_creator.get_volume())
        self.assertEqual(0, len(self.volume_creator.get_volume().attachments))

        # Attach volume to VM
        neutron = neutron_utils.neutron_client(
            self.os_creds, self.os_session)
        keystone = keystone_utils.keystone_client(
            self.os_creds, self.os_session)
        self.assertIsNotNone(nova_utils.attach_volume(
            self.nova, neutron, keystone, self.instance_creator.get_vm_inst(),
            self.volume_creator.get_volume(), self.os_creds.project_name))

        vol_attach = None
        vol_detach = None
        attached = False
        start_time = time.time()
        while time.time() < start_time + 120:
            vol_attach = cinder_utils.get_volume_by_id(
                self.cinder, self.volume_creator.get_volume().id)

            if len(vol_attach.attachments) > 0:
                attached = True
                break

            time.sleep(3)

        self.assertTrue(attached)
        self.assertIsNotNone(vol_attach)

        keystone = keystone_utils.keystone_client(
            self.os_creds, self.os_session)
        vm_attach = nova_utils.get_server_object_by_id(
            self.nova, neutron, keystone,
            self.instance_creator.get_vm_inst().id, self.os_creds.project_name)

        # Validate Attachment
        self.assertIsNotNone(vol_attach)
        self.assertEqual(self.volume_creator.get_volume().id, vol_attach.id)
        self.assertEqual(1, len(vol_attach.attachments))
        self.assertEqual(vm_attach.volume_ids[0]['id'],
                         vol_attach.attachments[0]['volume_id'])

        # Detach volume to VM
        self.assertIsNotNone(nova_utils.detach_volume(
            self.nova, neutron, keystone, self.instance_creator.get_vm_inst(),
            self.volume_creator.get_volume(), self.os_creds.project_name))

        start_time = time.time()
        while time.time() < start_time + 120:
            vol_detach = cinder_utils.get_volume_by_id(
                self.cinder, self.volume_creator.get_volume().id)
            if len(vol_detach.attachments) == 0:
                attached = False
                break

            time.sleep(3)

        self.assertFalse(attached)
        self.assertIsNotNone(vol_detach)

        vm_detach = nova_utils.get_server_object_by_id(
            self.nova, neutron, keystone,
            self.instance_creator.get_vm_inst().id, self.os_creds.project_name)

        # Validate Detachment
        self.assertIsNotNone(vol_detach)
        self.assertEqual(self.volume_creator.get_volume().id, vol_detach.id)

        self.assertEqual(0, len(vol_detach.attachments))
        self.assertEqual(0, len(vm_detach.volume_ids))

    def test_attach_volume_nowait(self):
        """
        Tests the nova_utils.attach_volume() with a timeout value that is too
        small to have the volume attachment data to be included on the VmInst
        object that was supposed to be returned
        """

        self.assertIsNotNone(self.volume_creator.get_volume())
        self.assertEqual(0, len(self.volume_creator.get_volume().attachments))

        # Attach volume to VM
        neutron = neutron_utils.neutron_client(self.os_creds, self.os_session)
        keystone = keystone_utils.keystone_client(
            self.os_creds, self.os_session)
        with self.assertRaises(NovaException):
            nova_utils.attach_volume(
                self.nova, neutron, keystone,
                self.instance_creator.get_vm_inst(),
                self.volume_creator.get_volume(), self.os_creds.project_name,
                0)

    def test_detach_volume_nowait(self):
        """
        Tests the nova_utils.detach_volume() with a timeout value that is too
        small to have the volume attachment data to be included on the VmInst
        object that was supposed to be returned
        """

        self.assertIsNotNone(self.volume_creator.get_volume())
        self.assertEqual(0, len(self.volume_creator.get_volume().attachments))

        # Attach volume to VM
        neutron = neutron_utils.neutron_client(self.os_creds, self.os_session)
        keystone = keystone_utils.keystone_client(
            self.os_creds, self.os_session)
        nova_utils.attach_volume(
            self.nova, neutron, keystone, self.instance_creator.get_vm_inst(),
            self.volume_creator.get_volume(), self.os_creds.project_name)

        # Check VmInst for attachment
        keystone = keystone_utils.keystone_client(
            self.os_creds, self.os_session)
        latest_vm = nova_utils.get_server_object_by_id(
            self.nova, neutron, keystone,
            self.instance_creator.get_vm_inst().id, self.os_creds.project_name)
        self.assertEqual(1, len(latest_vm.volume_ids))

        # Check Volume for attachment
        vol_attach = None
        attached = False
        start_time = time.time()
        while time.time() < start_time + 120:
            vol_attach = cinder_utils.get_volume_by_id(
                self.cinder, self.volume_creator.get_volume().id)

            if len(vol_attach.attachments) > 0:
                attached = True
                break

            time.sleep(3)

        self.assertTrue(attached)
        self.assertIsNotNone(vol_attach)

        # Detach volume
        with self.assertRaises(NovaException):
            nova_utils.detach_volume(
                self.nova, neutron, keystone,
                self.instance_creator.get_vm_inst(),
                self.volume_creator.get_volume(), self.os_creds.project_name,
                0)