Beispiel #1
0
    def test_create_keypair_from_file(self):
        """
        Tests the creation of an existing public keypair from a file
        :return:
        """
        keys = nova_utils.create_keys()
        file_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        pub_file = None
        try:
            pub_file = open(os.path.expanduser(self.pub_file_path))
            file_key = pub_file.read()
            self.assertEqual(self.keypair_creator.get_keypair().public_key,
                             file_key)
        finally:
            if pub_file:
                pub_file.close()

        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)
Beispiel #2
0
    def test_create_keypair_save_both(self):
        """
        Tests the creation of a generated keypair and saves both private and
        public key files[
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        pub_file = None
        try:
            pub_file = open(os.path.expanduser(self.pub_file_path))
            file_key = pub_file.read()
            self.assertEqual(self.keypair_creator.get_keypair().public_key,
                             file_key)
        finally:
            if pub_file:
                pub_file.close()

        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)

        self.assertTrue(os.path.isfile(self.priv_file_path))
Beispiel #3
0
    def get_keypair_creators(self, outputs_pk_key=None):
        """
        Returns a list of keypair creator objects as configured by the heat
        template
        :return: list() of OpenStackKeypair objects
        """

        out = list()

        keypairs = heat_utils.get_stack_keypairs(self.__heat_cli, self.__nova,
                                                 self.__stack)

        for keypair in keypairs:
            settings = settings_utils.create_keypair_config(
                self.__heat_cli, self.__stack, keypair, outputs_pk_key)
            creator = OpenStackKeypair(self._os_creds, settings)
            out.append(creator)

            try:
                creator.initialize()
            except Exception as e:
                logger.error(
                    'Unexpected error initializing volume type creator - %s',
                    e)

        return out
Beispiel #4
0
    def setUp(self):
        """
        Instantiates the CreateClusterTemplate object that is responsible for
        downloading and creating an OS template config file within OpenStack
        """
        super(self.__class__, self).__start__()

        self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.cluster_type_name = self.guid + '-cluster-type'
        self.magnum = magnum_utils.magnum_client(self.os_creds,
                                                 self.os_session)

        metadata = self.image_metadata
        if not metadata:
            metadata = dict()
        if 'extra_properties' not in metadata:
            metadata['extra_properties'] = dict()
        metadata['extra_properties']['os_distro'] = 'cirros'

        os_image_settings = openstack_tests.cirros_image_settings(
            name=self.guid + '-image', image_metadata=metadata)

        self.image_creator = OpenStackImage(self.os_creds, os_image_settings)

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

        keypair_priv_filepath = 'tmp/' + self.guid
        keypair_pub_filepath = keypair_priv_filepath + '.pub'

        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.guid + '-keypair',
                          public_filepath=keypair_pub_filepath,
                          private_filepath=keypair_priv_filepath))

        self.cluster_template_creator = None

        self.cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name)

        try:
            self.image_creator.create()
            self.flavor_creator.create()
            self.keypair_creator.create()
        except:
            self.tearDown()
            raise
Beispiel #5
0
    def test_create_keypair_only(self):
        """
        Tests the creation of a generated keypair without saving to file
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(name=self.keypair_name))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)
Beispiel #6
0
    def test_create_keypair_gen_files_delete_1(self):
        """
        Tests the creation of a generated keypair and ensures that the files
        are deleted on clean()
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertFalse(file_utils.file_exists(self.pub_file_path))
        self.assertFalse(file_utils.file_exists(self.priv_file_path))
Beispiel #7
0
    def test_create_keypair_exist_files_delete(self):
        """
        Tests the creation of an existing public keypair and ensures the files
        are deleted on clean
        :return:
        """
        keys = nova_utils.create_keys()
        file_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path,
                                      priv_file_path=self.priv_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path,
                          delete_on_clean=True))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertFalse(file_utils.file_exists(self.pub_file_path))
        self.assertFalse(file_utils.file_exists(self.priv_file_path))
Beispiel #8
0
    def test_create_delete_keypair(self):
        """
        Tests the creation then deletion of an OpenStack keypair to ensure
        clean() does not raise an Exception.
        """
        # Create Image
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(name=self.keypair_name))
        created_keypair = self.keypair_creator.create()
        self.assertIsNotNone(created_keypair)

        # Delete Image manually
        nova_utils.delete_keypair(self.nova, created_keypair)

        self.assertIsNone(
            nova_utils.get_keypair_by_name(self.nova, self.keypair_name))

        # Must not throw an exception when attempting to cleanup non-existent
        # image
        self.keypair_creator.clean()
        self.assertIsNone(self.keypair_creator.get_keypair())
Beispiel #9
0
    def setUp(self):
        self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.cluster_type_name = self.guid + '-cluster-type'
        self.magnum = magnum_utils.magnum_client(
            self.os_creds, self.os_session)

        metadata = self.image_metadata
        if not metadata:
            metadata = dict()
        if 'extra_properties' not in metadata:
            metadata['extra_properties'] = dict()
        metadata['extra_properties']['os_distro'] = 'cirros'

        os_image_settings = openstack_tests.cirros_image_settings(
            name=self.guid + '-image', image_metadata=metadata)

        self.image_creator = OpenStackImage(self.os_creds, os_image_settings)

        self.flavor_creator = OpenStackFlavor(
            self.os_creds, FlavorConfig(
                name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))

        keypair_priv_filepath = 'tmp/' + self.guid
        keypair_pub_filepath = keypair_priv_filepath + '.pub'

        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(
                name=self.guid + '-keypair',
                public_filepath=keypair_pub_filepath,
                private_filepath=keypair_priv_filepath))

        self.cluster_template = None

        try:
            self.image_creator.create()
            self.flavor_creator.create()
            self.keypair_creator.create()
        except:
            self.tearDown()
            raise
Beispiel #10
0
    def test_vping_ssh(self, create_vm, path_exists, flavor_create,
                       get_port_ip, vm_active, ssh_active, ssh_client,
                       scp_client, trans_script, do_vping_ssh, ext_net_name):
        os_vm_inst = mock.MagicMock(name='get_console_output')
        os_vm_inst.get_console_output.return_value = 'vPing OK'
        ssh_client = mock.MagicMock(name='get_transport')
        ssh_client.get_transport.return_value = None
        scp_client = mock.MagicMock(name='put')
        scp_client.put.return_value = None

        with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
                        return_value=OpenStackImage(self.os_creds, None)), \
                mock.patch('snaps.openstack.utils.deploy_utils.create_network',
                           return_value=OpenStackNetwork(
                               self.os_creds,
                               NetworkConfig(
                                   name='foo',
                                   subnet_settings=[
                                       SubnetConfig(
                                           name='bar',
                                           cidr='10.0.0.1/24')]))), \
                mock.patch('snaps.openstack.utils.deploy_utils.'
                           'create_vm_instance',
                           return_value=OpenStackVmInstance(
                               self.os_creds,
                               VmInstanceConfig(
                                   name='foo', flavor='bar',
                                   port_settings=[PortConfig(
                                       name='foo', network_name='bar')]),
                               None)), \
                mock.patch('snaps.openstack.utils.deploy_utils.create_keypair',
                           return_value=OpenStackKeypair(
                               self.os_creds, KeypairConfig(name='foo'))), \
                mock.patch('snaps.openstack.utils.deploy_utils.create_router',
                           return_value=OpenStackRouter(
                               self.os_creds, RouterConfig(name='foo'))), \
                mock.patch('snaps.openstack.utils.deploy_utils.'
                           'create_security_group',
                           return_value=OpenStackSecurityGroup(
                               self.os_creds,
                               SecurityGroupConfig(name='foo'))), \
                mock.patch('snaps.openstack.create_instance.'
                           'OpenStackVmInstance.'
                           'get_vm_inst', return_value=os_vm_inst), \
                mock.patch('snaps.openstack.create_instance.'
                           'OpenStackVmInstance.'
                           'ssh_client', return_value=ssh_client):
            self.assertEquals(TestCase.EX_OK, self.vping_ssh.run())
Beispiel #11
0
def create_keypair(os_creds, keypair_settings, cleanup=False):
    """
    Creates a keypair that can be applied to an instance
    :param os_creds: The OpenStack credentials object
    :param keypair_settings: The KeypairConfig object
    :param cleanup: Denotes whether or not this is being called for cleanup
    :return: A reference to the keypair creator object
    """
    keypair_creator = OpenStackKeypair(os_creds, keypair_settings)

    if cleanup:
        keypair_creator.initialize()
    else:
        keypair_creator.create()
    return keypair_creator
Beispiel #12
0
class CreateKeypairsTests(OSIntegrationTestCase):
    """
    Tests for the OpenStackKeypair class
    """
    def setUp(self):
        super(self.__class__, self).__start__()

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.priv_file_path = 'tmp/' + guid
        self.pub_file_path = self.priv_file_path + '.pub'
        self.nova = nova_utils.nova_client(self.os_creds, self.os_session)
        self.keypair_name = guid

        self.keypair_creator = None

    def tearDown(self):
        """
        Cleanup of created keypair
        """
        if self.keypair_creator:
            self.keypair_creator.clean()

        try:
            os.remove(self.pub_file_path)
        except:
            pass

        try:
            os.remove(self.priv_file_path)
        except:
            pass

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

    def test_create_keypair_only(self):
        """
        Tests the creation of a generated keypair without saving to file
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(name=self.keypair_name))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

    def test_create_keypair_large_key(self):
        """
        Tests the creation of a generated keypair without saving to file
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(name=self.keypair_name,
                                         key_size=10000))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

    def test_create_delete_keypair(self):
        """
        Tests the creation then deletion of an OpenStack keypair to ensure
        clean() does not raise an Exception.
        """
        # Create Image
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(name=self.keypair_name))
        created_keypair = self.keypair_creator.create()
        self.assertIsNotNone(created_keypair)

        # Delete Image manually
        nova_utils.delete_keypair(self.nova, created_keypair)

        self.assertIsNone(
            nova_utils.get_keypair_by_name(self.nova, self.keypair_name))

        # Must not throw an exception when attempting to cleanup non-existent
        # image
        self.keypair_creator.clean()
        self.assertIsNone(self.keypair_creator.get_keypair())

    def test_create_keypair_save_pub_only(self):
        """
        Tests the creation of a generated keypair and saves the public key only
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        pub_file = None
        try:
            pub_file = open(os.path.expanduser(self.pub_file_path))
            file_key = pub_file.read()
            self.assertEqual(self.keypair_creator.get_keypair().public_key,
                             file_key)
        finally:
            if pub_file:
                pub_file.close()

    def test_create_keypair_save_both(self):
        """
        Tests the creation of a generated keypair and saves both private and
        public key files[
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        pub_file = None
        try:
            pub_file = open(os.path.expanduser(self.pub_file_path))
            file_key = pub_file.read()
            self.assertEqual(self.keypair_creator.get_keypair().public_key,
                             file_key)
        finally:
            if pub_file:
                pub_file.close()

        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)

        self.assertTrue(os.path.isfile(self.priv_file_path))

    def test_create_keypair_from_file(self):
        """
        Tests the creation of an existing public keypair from a file
        :return:
        """
        keys = nova_utils.create_keys()
        file_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        pub_file = None
        try:
            pub_file = open(os.path.expanduser(self.pub_file_path))
            file_key = pub_file.read()
            self.assertEqual(self.keypair_creator.get_keypair().public_key,
                             file_key)
        finally:
            if pub_file:
                pub_file.close()

        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)
Beispiel #13
0
class CreateClusterTemplateTests(OSIntegrationTestCase):
    """
    Test for the OpenStackClusterTemplate class defined in py
    without any QoS Specs or Encryption
    """
    def setUp(self):
        """
        Instantiates the CreateClusterTemplate object that is responsible for
        downloading and creating an OS template config file within OpenStack
        """
        super(self.__class__, self).__start__()

        self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.cluster_type_name = self.guid + '-cluster-type'
        self.magnum = magnum_utils.magnum_client(self.os_creds,
                                                 self.os_session)

        metadata = self.image_metadata
        if not metadata:
            metadata = dict()
        if 'extra_properties' not in metadata:
            metadata['extra_properties'] = dict()
        metadata['extra_properties']['os_distro'] = 'cirros'

        os_image_settings = openstack_tests.cirros_image_settings(
            name=self.guid + '-image', image_metadata=metadata)

        self.image_creator = OpenStackImage(self.os_creds, os_image_settings)

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

        keypair_priv_filepath = 'tmp/' + self.guid
        keypair_pub_filepath = keypair_priv_filepath + '.pub'

        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.guid + '-keypair',
                          public_filepath=keypair_pub_filepath,
                          private_filepath=keypair_priv_filepath))

        self.cluster_template_creator = None

        self.cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name)

        try:
            self.image_creator.create()
            self.flavor_creator.create()
            self.keypair_creator.create()
        except:
            self.tearDown()
            raise

    def tearDown(self):
        """
        Cleans the template config
        """
        if self.cluster_template_creator:
            try:
                self.cluster_template_creator.clean()
            except:
                pass
        if self.keypair_creator:
            try:
                self.keypair_creator.clean()
            except:
                pass
        if self.flavor_creator:
            try:
                self.flavor_creator.clean()
            except:
                pass
        if self.image_creator:
            try:
                self.image_creator.clean()
            except:
                pass

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

    def test_create_cluster_template(self):
        """
        Tests the creation of an OpenStack cluster template.
        """
        # Create ClusterTemplate
        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, self.cluster_template_config)
        created_cluster_template = self.cluster_template_creator.create()
        self.assertIsNotNone(created_cluster_template)
        self.assertEqual(self.cluster_template_config.name,
                         created_cluster_template.name)

        retrieved_cluster_template1 = magnum_utils.get_cluster_template(
            self.magnum, template_config=self.cluster_template_config)
        self.assertIsNotNone(retrieved_cluster_template1)
        self.assertEqual(created_cluster_template, retrieved_cluster_template1)

        retrieved_cluster_template2 = magnum_utils.get_cluster_template_by_id(
            self.magnum, created_cluster_template.id)
        self.assertEqual(created_cluster_template, retrieved_cluster_template2)

    def test_create_delete_cluster_template(self):
        """
        Tests the creation then deletion of an OpenStack template config to
        ensure clean() does not raise an Exception.
        """
        # Create ClusterTemplate
        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, self.cluster_template_config)
        created_cluster_template = self.cluster_template_creator.create()
        self.assertIsNotNone(created_cluster_template)

        self.cluster_template_creator.clean()

        tmplt = magnum_utils.get_cluster_template(
            self.magnum, template_name=self.cluster_template_config.name)
        self.assertIsNone(tmplt)

    def test_create_same_cluster_template(self):
        """
        Tests the creation of an OpenStack cluster_template when one already
        exists.
        """
        # Create ClusterTemplate
        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, self.cluster_template_config)
        cluster_template1 = self.cluster_template_creator.create()

        retrieved_cluster_template = magnum_utils.get_cluster_template(
            self.magnum, template_config=self.cluster_template_config)
        self.assertEqual(cluster_template1, retrieved_cluster_template)

        # Should be retrieving the instance data
        os_cluster_template_2 = OpenStackClusterTemplate(
            self.os_creds, self.cluster_template_config)
        cluster_template2 = os_cluster_template_2.create()
        self.assertEqual(cluster_template2, cluster_template2)

    def test_create_cluster_template_bad_flavor(self):
        """
        Tests the creation of an OpenStack cluster template raises an
        exception with an invalid flavor.
        """
        # Create ClusterTemplate
        cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor='foo')

        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, cluster_template_config)

        with self.assertRaises(BadRequest):
            self.cluster_template_creator.create()

    def test_create_cluster_template_bad_master_flavor(self):
        """
        Tests the creation of an OpenStack cluster template raises an
        exception with an invalid master flavor.
        """
        # Create ClusterTemplate
        cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name,
            master_flavor='foo')

        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, cluster_template_config)

        with self.assertRaises(BadRequest):
            self.cluster_template_creator.create()

    def test_create_cluster_template_bad_image(self):
        """
        Tests the creation of an OpenStack cluster template raises an
        exception with an invalid image.
        """
        # Create ClusterTemplate
        cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image='foo',
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name)

        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, cluster_template_config)

        with self.assertRaises(BadRequest):
            self.cluster_template_creator.create()

    def test_create_cluster_template_bad_network_driver(self):
        """
        Tests the creation of an OpenStack cluster template raises an
        exception with an invalid keypair.
        """
        # Create ClusterTemplate
        cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name,
            network_driver='foo')

        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, cluster_template_config)

        with self.assertRaises(BadRequest):
            self.cluster_template_creator.create()

    def test_create_cluster_template_bad_volume_driver(self):
        """
        Tests the creation of an OpenStack cluster template raises an
        exception with an invalid keypair.
        """
        # Create ClusterTemplate
        cluster_template_config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name,
            volume_driver='foo')

        self.cluster_template_creator = OpenStackClusterTemplate(
            self.os_creds, cluster_template_config)

        with self.assertRaises(BadRequest):
            self.cluster_template_creator.create()
Beispiel #14
0
class CreateKeypairsCleanupTests(OSIntegrationTestCase):
    """
    Tests for the OpenStackKeypair#clean method to ensure key files are deleted
    when required
    """
    def setUp(self):
        super(self.__class__, self).__start__()

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.priv_file_path = 'tmp/' + guid
        self.pub_file_path = self.priv_file_path + '.pub'
        self.nova = nova_utils.nova_client(self.os_creds, self.os_session)
        self.keypair_name = guid

        self.keypair_creator = None

    def tearDown(self):
        """
        Cleanup of created keypair
        """
        if self.keypair_creator:
            self.keypair_creator.clean()

        try:
            os.remove(self.pub_file_path)
        except:
            pass

        try:
            os.remove(self.priv_file_path)
        except:
            pass

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

    def test_create_keypair_gen_files_delete_1(self):
        """
        Tests the creation of a generated keypair and ensures that the files
        are deleted on clean()
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertFalse(file_utils.file_exists(self.pub_file_path))
        self.assertFalse(file_utils.file_exists(self.priv_file_path))

    def test_create_keypair_gen_files_delete_2(self):
        """
        Tests the creation of a generated keypair and ensures that the files
        are deleted on clean()
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path,
                          delete_on_clean=True))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertFalse(file_utils.file_exists(self.pub_file_path))
        self.assertFalse(file_utils.file_exists(self.priv_file_path))

    def test_create_keypair_gen_files_keep(self):
        """
        Tests the creation of a generated keypair and ensures that the files
        are not deleted on clean()
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path,
                          delete_on_clean=False))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertTrue(file_utils.file_exists(self.pub_file_path))
        self.assertTrue(file_utils.file_exists(self.priv_file_path))

    def test_create_keypair_exist_files_keep(self):
        """
        Tests the creation of an existing public keypair and ensures the files
        are not deleted on clean
        :return:
        """
        keys = nova_utils.create_keys()
        file_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path,
                                      priv_file_path=self.priv_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path,
                          delete_on_clean=False))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertTrue(file_utils.file_exists(self.pub_file_path))
        self.assertTrue(file_utils.file_exists(self.priv_file_path))

    def test_create_keypair_exist_files_delete(self):
        """
        Tests the creation of an existing public keypair and ensures the files
        are deleted on clean
        :return:
        """
        keys = nova_utils.create_keys()
        file_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path,
                                      priv_file_path=self.priv_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds,
            KeypairConfig(name=self.keypair_name,
                          public_filepath=self.pub_file_path,
                          private_filepath=self.priv_file_path,
                          delete_on_clean=True))
        self.keypair_creator.create()
        self.keypair_creator.clean()

        self.assertFalse(file_utils.file_exists(self.pub_file_path))
        self.assertFalse(file_utils.file_exists(self.priv_file_path))
Beispiel #15
0
class MagnumUtilsClusterTypeTests(OSComponentTestCase):
    """
    Tests individual functions within magnum_utils.py
    """

    def setUp(self):
        self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.cluster_type_name = self.guid + '-cluster-type'
        self.magnum = magnum_utils.magnum_client(
            self.os_creds, self.os_session)

        metadata = self.image_metadata
        if not metadata:
            metadata = dict()
        if 'extra_properties' not in metadata:
            metadata['extra_properties'] = dict()
        metadata['extra_properties']['os_distro'] = 'cirros'

        os_image_settings = openstack_tests.cirros_image_settings(
            name=self.guid + '-image', image_metadata=metadata)

        self.image_creator = OpenStackImage(self.os_creds, os_image_settings)

        self.flavor_creator = OpenStackFlavor(
            self.os_creds, FlavorConfig(
                name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))

        keypair_priv_filepath = 'tmp/' + self.guid
        keypair_pub_filepath = keypair_priv_filepath + '.pub'

        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairConfig(
                name=self.guid + '-keypair',
                public_filepath=keypair_pub_filepath,
                private_filepath=keypair_priv_filepath))

        self.cluster_template = None

        try:
            self.image_creator.create()
            self.flavor_creator.create()
            self.keypair_creator.create()
        except:
            self.tearDown()
            raise

    def tearDown(self):
        if self.cluster_template:
            try:
                magnum_utils.delete_cluster_template(
                    self.magnum, self.cluster_template.id)
            except:
                pass
        if self.keypair_creator:
            try:
                self.keypair_creator.clean()
            except:
                pass
        if self.flavor_creator:
            try:
                self.flavor_creator.clean()
            except:
                pass
        if self.image_creator:
            try:
                self.image_creator.clean()
            except:
                pass

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

    def test_create_cluster_template_simple(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name)

        self.cluster_template = magnum_utils.create_cluster_template(
            self.magnum, config)
        self.assertIsNotNone(self.cluster_template)
        self.assertTrue(
            validate_cluster_template(config, self.cluster_template))

        template_by_name = magnum_utils.get_cluster_template(
            self.magnum, template_name=config.name)
        self.assertEqual(self.cluster_template, template_by_name)
        template_by_id = magnum_utils.get_cluster_template_by_id(
            self.magnum, self.cluster_template.id)
        self.assertEqual(self.cluster_template, template_by_id)

    def test_create_cluster_template_all(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            network_driver='flannel', external_net=self.ext_net_name,
            floating_ip_enabled=True, docker_volume_size=100,
            server_type=ServerType.vm,
            flavor=self.flavor_creator.flavor_settings.name,
            master_flavor=self.flavor_creator.flavor_settings.name,
            coe=ContainerOrchestrationEngine.kubernetes,
            fixed_net='foo', fixed_subnet='bar',
            registry_enabled=True, insecure_registry='localhost',
            docker_storage_driver=DockerStorageDriver.overlay,
            dns_nameserver='8.8.4.4', public=True, tls_disabled=True,
            http_proxy=None, https_proxy=None, volume_driver='cinder',
            master_lb_enabled=False, labels={'foo': 'bar'})

        self.cluster_template = magnum_utils.create_cluster_template(
            self.magnum, config)
        self.assertIsNotNone(self.cluster_template)
        self.assertTrue(
            validate_cluster_template(config, self.cluster_template))

        template_by_name = magnum_utils.get_cluster_template(
            self.magnum, template_name=config.name)
        self.assertEqual(self.cluster_template, template_by_name)
        template_by_id = magnum_utils.get_cluster_template_by_id(
            self.magnum, self.cluster_template.id)
        self.assertEqual(self.cluster_template, template_by_id)

    def test_create_cluster_template_bad_image(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image='foo',
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name)

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)

    def test_create_cluster_template_bad_ext_net(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net='foo',
            flavor=self.flavor_creator.flavor_settings.name)

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)

    def test_create_cluster_template_bad_flavor(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor='foo')

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)

    def test_create_cluster_template_bad_master_flavor(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            flavor=self.flavor_creator.flavor_settings.name,
            master_flavor='foo')

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)

    def test_create_cluster_template_bad_network_driver(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            network_driver='foo')

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)

    def test_create_cluster_template_bad_volume_driver(self):
        config = ClusterTemplateConfig(
            name=self.cluster_type_name,
            image=self.image_creator.image_settings.name,
            keypair=self.keypair_creator.keypair_settings.name,
            external_net=self.ext_net_name,
            volume_driver='foo')

        with self.assertRaises(BadRequest):
            self.cluster_template = magnum_utils.create_cluster_template(
                self.magnum, config)
    def deploy_orchestrator(self):
        """
        Deploy Cloudify Manager.
        network, security group, fip, VM creation
        """
        # network creation

        start_time = time.time()
        self.__logger.info("Creating keypair ...")
        kp_file = os.path.join(self.data_dir, "cloudify_vrouter.pem")
        keypair_settings = KeypairSettings(name='cloudify_vrouter_kp',
                                           private_filepath=kp_file)
        keypair_creator = OpenStackKeypair(self.snaps_creds, keypair_settings)
        keypair_creator.create()
        self.created_object.append(keypair_creator)

        self.__logger.info("Creating full network ...")
        subnet_settings = SubnetSettings(name='cloudify_vrouter_subnet',
                                         cidr='10.67.79.0/24')
        network_settings = NetworkSettings(name='cloudify_vrouter_network',
                                           subnet_settings=[subnet_settings])
        network_creator = OpenStackNetwork(self.snaps_creds, network_settings)
        network_creator.create()
        self.created_object.append(network_creator)
        ext_net_name = snaps_utils.get_ext_net_name(self.snaps_creds)
        router_creator = OpenStackRouter(
            self.snaps_creds,
            RouterSettings(name='cloudify_vrouter_router',
                           external_gateway=ext_net_name,
                           internal_subnets=[subnet_settings.name]))
        router_creator.create()
        self.created_object.append(router_creator)

        # security group creation
        self.__logger.info("Creating security group for cloudify manager vm")
        sg_rules = list()
        sg_rules.append(
            SecurityGroupRuleSettings(sec_grp_name="sg-cloudify-manager",
                                      direction=Direction.ingress,
                                      protocol=Protocol.tcp,
                                      port_range_min=1,
                                      port_range_max=65535))
        sg_rules.append(
            SecurityGroupRuleSettings(sec_grp_name="sg-cloudify-manager",
                                      direction=Direction.ingress,
                                      protocol=Protocol.udp,
                                      port_range_min=1,
                                      port_range_max=65535))

        security_group_creator = OpenStackSecurityGroup(
            self.snaps_creds,
            SecurityGroupSettings(name="sg-cloudify-manager",
                                  rule_settings=sg_rules))

        security_group_creator.create()
        self.created_object.append(security_group_creator)

        # orchestrator VM flavor
        self.__logger.info("Get or create flavor for cloudify manager vm ...")

        flavor_settings = FlavorSettings(
            name=self.orchestrator['requirements']['flavor']['name'],
            ram=self.orchestrator['requirements']['flavor']['ram_min'],
            disk=50,
            vcpus=2)
        flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings)
        flavor_creator.create()
        self.created_object.append(flavor_creator)
        image_settings = ImageSettings(
            name=self.orchestrator['requirements']['os_image'],
            image_user='******',
            exists=True)

        port_settings = PortSettings(name='cloudify_manager_port',
                                     network_name=network_settings.name)

        manager_settings = VmInstanceSettings(
            name='cloudify_manager',
            flavor=flavor_settings.name,
            port_settings=[port_settings],
            security_group_names=[
                security_group_creator.sec_grp_settings.name
            ],
            floating_ip_settings=[
                FloatingIpSettings(
                    name='cloudify_manager_fip',
                    port_name=port_settings.name,
                    router_name=router_creator.router_settings.name)
            ])

        manager_creator = OpenStackVmInstance(self.snaps_creds,
                                              manager_settings, image_settings,
                                              keypair_settings)

        self.__logger.info("Creating cloudify manager VM")
        manager_creator.create()
        self.created_object.append(manager_creator)

        public_auth_url = os_utils.get_endpoint('identity')

        self.__logger.info("Set creds for cloudify manager")
        cfy_creds = dict(keystone_username=self.tenant_name,
                         keystone_password=self.tenant_name,
                         keystone_tenant_name=self.tenant_name,
                         keystone_url=public_auth_url)

        cfy_client = CloudifyClient(host=manager_creator.get_floating_ip().ip,
                                    username='******',
                                    password='******',
                                    tenant='default_tenant')

        self.orchestrator['object'] = cfy_client

        self.cfy_manager_ip = manager_creator.get_floating_ip().ip

        self.__logger.info("Attemps running status of the Manager")
        cfy_status = None
        retry = 10
        while str(cfy_status) != 'running' and retry:
            try:
                cfy_status = cfy_client.manager.get_status()['status']
                self.__logger.debug("The current manager status is %s",
                                    cfy_status)
            except Exception:  # pylint: disable=broad-except
                self.__logger.warning("Cloudify Manager isn't " +
                                      "up and running. Retrying ...")
            retry = retry - 1
            time.sleep(30)

        if str(cfy_status) == 'running':
            self.__logger.info("Cloudify Manager is up and running")
        else:
            raise Exception("Cloudify Manager isn't up and running")

        self.__logger.info("Put OpenStack creds in manager")
        secrets_list = cfy_client.secrets.list()
        for k, val in cfy_creds.iteritems():
            if not any(d.get('key', None) == k for d in secrets_list):
                cfy_client.secrets.create(k, val)
            else:
                cfy_client.secrets.update(k, val)

        duration = time.time() - start_time

        self.__logger.info("Put private keypair in manager")
        if manager_creator.vm_ssh_active(block=True):
            ssh = manager_creator.ssh_client()
            scp = SCPClient(ssh.get_transport(), socket_timeout=15.0)
            scp.put(kp_file, '~/')
            cmd = "sudo cp ~/cloudify_vrouter.pem /etc/cloudify/"
            run_blocking_ssh_command(ssh, cmd)
            cmd = "sudo chmod 444 /etc/cloudify/cloudify_vrouter.pem"
            run_blocking_ssh_command(ssh, cmd)
            cmd = "sudo yum install -y gcc python-devel"
            run_blocking_ssh_command(ssh, cmd,
                                     "Unable to install packages on manager")

        self.details['orchestrator'].update(status='PASS', duration=duration)

        self.vnf['inputs'].update(dict(external_network_name=ext_net_name))

        return True
Beispiel #17
0
    def deploy_orchestrator(self):
        # pylint: disable=too-many-locals,too-many-statements
        """
        Deploy Cloudify Manager.

        network, security group, fip, VM creation
        """
        start_time = time.time()

        # orchestrator VM flavor
        self.__logger.info("Get or create flavor for cloudify manager vm ...")
        flavor_settings = FlavorConfig(
            name="{}-{}".format(
                self.orchestrator['requirements']['flavor']['name'],
                self.uuid),
            ram=self.orchestrator['requirements']['flavor']['ram_min'],
            disk=50,
            vcpus=2)
        flavor_creator = OpenStackFlavor(self.snaps_creds, flavor_settings)
        flavor_creator.create()
        self.created_object.append(flavor_creator)

        self.__logger.info("Creating a second user to bypass issues ...")
        user_creator = OpenStackUser(
            self.snaps_creds,
            UserConfig(
                name='cloudify_network_bug-{}'.format(self.uuid),
                password=str(uuid.uuid4()),
                project_name=self.tenant_name,
                domain_name=self.snaps_creds.user_domain_name,
                roles={'_member_': self.tenant_name}))
        user_creator.create()
        self.created_object.append(user_creator)

        snaps_creds = user_creator.get_os_creds(self.snaps_creds.project_name)
        self.__logger.debug("snaps creds: %s", snaps_creds)

        self.__logger.info("Creating keypair ...")
        kp_file = os.path.join(self.data_dir, "cloudify_ims.pem")
        keypair_settings = KeypairConfig(
            name='cloudify_ims_kp-{}'.format(self.uuid),
            private_filepath=kp_file)
        keypair_creator = OpenStackKeypair(snaps_creds, keypair_settings)
        keypair_creator.create()
        self.created_object.append(keypair_creator)

        # needs some images
        self.__logger.info("Upload some OS images if it doesn't exist")
        for image_name, image_file in self.images.iteritems():
            self.__logger.info("image: %s, file: %s", image_name, image_file)
            if image_file and image_name:
                image_creator = OpenStackImage(
                    snaps_creds,
                    ImageConfig(
                        name=image_name, image_user='******',
                        img_format='qcow2', image_file=image_file))
                image_creator.create()
                self.created_object.append(image_creator)

        # network creation
        self.__logger.info("Creating full network ...")
        subnet_settings = SubnetConfig(
            name='cloudify_ims_subnet-{}'.format(self.uuid),
            cidr='10.67.79.0/24',
            dns_nameservers=[env.get('NAMESERVER')])
        network_settings = NetworkConfig(
            name='cloudify_ims_network-{}'.format(self.uuid),
            subnet_settings=[subnet_settings])
        network_creator = OpenStackNetwork(snaps_creds, network_settings)
        network_creator.create()
        self.created_object.append(network_creator)
        ext_net_name = snaps_utils.get_ext_net_name(snaps_creds)
        router_creator = OpenStackRouter(
            snaps_creds,
            RouterConfig(
                name='cloudify_ims_router-{}'.format(self.uuid),
                external_gateway=ext_net_name,
                internal_subnets=[subnet_settings.name]))
        router_creator.create()
        self.created_object.append(router_creator)

        # security group creation
        self.__logger.info("Creating security group for cloudify manager vm")
        sg_rules = list()
        sg_rules.append(
            SecurityGroupRuleConfig(
                sec_grp_name="sg-cloudify-manager-{}".format(self.uuid),
                direction=Direction.ingress, protocol=Protocol.tcp,
                port_range_min=1, port_range_max=65535))
        sg_rules.append(
            SecurityGroupRuleConfig(
                sec_grp_name="sg-cloudify-manager-{}".format(self.uuid),
                direction=Direction.ingress, protocol=Protocol.udp,
                port_range_min=1, port_range_max=65535))
        security_group_creator = OpenStackSecurityGroup(
            snaps_creds,
            SecurityGroupConfig(
                name="sg-cloudify-manager-{}".format(self.uuid),
                rule_settings=sg_rules))
        security_group_creator.create()
        self.created_object.append(security_group_creator)

        image_settings = ImageConfig(
            name=self.orchestrator['requirements']['os_image'],
            image_user='******',
            exists=True)
        port_settings = PortConfig(
            name='cloudify_manager_port-{}'.format(self.uuid),
            network_name=network_settings.name)
        manager_settings = VmInstanceConfig(
            name='cloudify_manager-{}'.format(self.uuid),
            flavor=flavor_settings.name,
            port_settings=[port_settings],
            security_group_names=[
                security_group_creator.sec_grp_settings.name],
            floating_ip_settings=[FloatingIpConfig(
                name='cloudify_manager_fip-{}'.format(self.uuid),
                port_name=port_settings.name,
                router_name=router_creator.router_settings.name)])
        manager_creator = OpenStackVmInstance(
            snaps_creds, manager_settings, image_settings,
            keypair_settings)
        self.__logger.info("Creating cloudify manager VM")
        manager_creator.create()
        self.created_object.append(manager_creator)

        public_auth_url = keystone_utils.get_endpoint(snaps_creds, 'identity')

        cfy_creds = dict(
            keystone_username=snaps_creds.username,
            keystone_password=snaps_creds.password,
            keystone_tenant_name=snaps_creds.project_name,
            keystone_url=public_auth_url,
            region=snaps_creds.region_name,
            user_domain_name=snaps_creds.user_domain_name,
            project_domain_name=snaps_creds.project_domain_name)
        self.__logger.info("Set creds for cloudify manager %s", cfy_creds)

        cfy_client = CloudifyClient(
            host=manager_creator.get_floating_ip().ip,
            username='******', password='******', tenant='default_tenant')

        self.orchestrator['object'] = cfy_client

        self.__logger.info("Attemps running status of the Manager")
        for loop in range(10):
            try:
                self.__logger.debug(
                    "status %s", cfy_client.manager.get_status())
                cfy_status = cfy_client.manager.get_status()['status']
                self.__logger.info(
                    "The current manager status is %s", cfy_status)
                if str(cfy_status) != 'running':
                    raise Exception("Cloudify Manager isn't up and running")
                self.__logger.info("Put OpenStack creds in manager")
                secrets_list = cfy_client.secrets.list()
                for k, val in cfy_creds.iteritems():
                    if not any(d.get('key', None) == k for d in secrets_list):
                        cfy_client.secrets.create(k, val)
                    else:
                        cfy_client.secrets.update(k, val)
                break
            except Exception:  # pylint: disable=broad-except
                self.logger.info(
                    "try %s: Cloudify Manager isn't up and running", loop + 1)
                time.sleep(30)
        else:
            self.logger.error("Cloudify Manager isn't up and running")
            return False

        duration = time.time() - start_time

        if manager_creator.vm_ssh_active(block=True):
            self.__logger.info("Put private keypair in manager")
            ssh = manager_creator.ssh_client()
            scp = SCPClient(ssh.get_transport(), socket_timeout=15.0)
            scp.put(kp_file, '~/')
            cmd = "sudo cp ~/cloudify_ims.pem /etc/cloudify/"
            self.run_blocking_ssh_command(ssh, cmd)
            cmd = "sudo chmod 444 /etc/cloudify/cloudify_ims.pem"
            self.run_blocking_ssh_command(ssh, cmd)
            # cmd2 is badly unpinned by Cloudify
            cmd = "sudo yum install -y gcc python-devel python-cmd2"
            self.run_blocking_ssh_command(
                ssh, cmd, "Unable to install packages on manager")
            self.run_blocking_ssh_command(ssh, 'cfy status')
        else:
            self.__logger.error("Cannot connect to manager")
            return False

        self.details['orchestrator'].update(status='PASS', duration=duration)

        self.vnf['inputs'].update(dict(
            external_network_name=ext_net_name,
            network_name=network_settings.name,
            key_pair_name=keypair_settings.name
        ))
        self.result = 1/3 * 100
        return True