def clean(self): """ Removes and deletes the keypair. """ if self.__keypair: try: nova_utils.delete_keypair(self._nova, self.__keypair) except NotFound: pass self.__keypair = None if self.__delete_keys_on_clean: if (self.keypair_settings.public_filepath and file_utils.file_exists( self.keypair_settings.public_filepath)): expanded_path = os.path.expanduser( self.keypair_settings.public_filepath) os.chmod(expanded_path, 0o755) os.remove(expanded_path) logger.info('Deleted public key file [%s]', expanded_path) if (self.keypair_settings.private_filepath and file_utils.file_exists( self.keypair_settings.private_filepath)): expanded_path = os.path.expanduser( self.keypair_settings.private_filepath) os.chmod(expanded_path, 0o755) os.remove(expanded_path) logger.info('Deleted private key file [%s]', expanded_path) super(self.__class__, self).clean()
def test_write_str_to_file(self): """ Ensure the file_utils.fileExists() method returns false with a directory """ test_val = 'test string' test_file = file_utils.save_string_to_file(test_val, self.tmpFile) result1 = file_utils.file_exists(self.tmpFile) self.assertTrue(result1) result2 = file_utils.file_exists(test_file.name) self.assertTrue(result2) file_contents = file_utils.read_file(self.tmpFile) self.assertEqual(test_val, file_contents)
def testFileNotExist(self): """ Ensure the file_utils.fileExists() method returns false with a bogus file """ result = file_utils.file_exists('/foo/bar.txt') self.assertFalse(result)
def testFileIsDirectory(self): """ Ensure the file_utils.fileExists() method returns false with a directory """ result = file_utils.file_exists(self.test_dir) self.assertFalse(result)
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_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))
def testFileExists(self): """ Ensure the file_utils.fileExists() method returns false with a directory """ if not os.path.exists(self.tmpFile): self.tmp_file_opened = open(self.tmpFile, 'wb') if not os.path.exists(self.tmpFile): os.makedirs(self.tmpFile) result = file_utils.file_exists(self.tmpFile) self.assertTrue(result)
def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, flavor_metadata=None, image_metadata=None, log_level=logging.DEBUG): """ Super for test classes requiring a connection to OpenStack :param method_name: default 'runTest' :param os_creds: the OSCreds object, when null it searches for the file in the package snaps.openstack.tests.conf.os_env.yaml :param ext_net_name: the name of the external network that is used for creating routers for floating IPs :param flavor_metadata: dict() to be sent directly into the Nova client generally used for page sizes :param image_metadata: ability to override images being used in the tests (see examples/image-metadata) :param log_level: the logging level of your test run (default DEBUG) """ super(OSComponentTestCase, self).__init__(method_name) logging.basicConfig(level=log_level) self.ext_net_name = None self.flavor_metadata = None if os_creds: self.os_creds = os_creds else: if file_utils.file_exists(dev_os_env_file): self.os_creds = openstack_tests.get_credentials( dev_os_env_file=dev_os_env_file) test_conf = file_utils.read_yaml(dev_os_env_file) self.ext_net_name = test_conf.get('ext_net') os_env_dict = file_utils.read_yaml(dev_os_env_file) flavor_metadata = os_env_dict.get('flavor_metadata') if flavor_metadata: self.flavor_metadata = {'metadata': flavor_metadata} else: raise Exception('Unable to obtain OSCreds') self.os_session = keystone_utils.keystone_session(self.os_creds) self.image_metadata = image_metadata if not self.ext_net_name: self.ext_net_name = ext_net_name if not self.flavor_metadata: self.flavor_metadata = flavor_metadata
def __init__(self, **kwargs): """ Constructor :param username: The user (required) :param password: The user's password (required) :param auth_url: The OpenStack cloud's authorization URL (required) :param project_name: The project/tenant name :param identity_api_version: The OpenStack's API version to use for Keystone clients :param image_api_version: The OpenStack's API version to use for Glance clients :param network_api_version: The OpenStack's API version to use for Neutron clients :param compute_api_version: The OpenStack's API version to use for Nova clients :param heat_api_version: The OpenStack's API version to use for Heat clients :param volume_api_version: The OpenStack's API version to use for Cinder clients :param magnum_api_version: The OpenStack's API version to use for magnum clients :param user_domain_id: Used for v3 APIs (default='default') :param user_domain_name: Used for v3 APIs (default='Default') :param project_domain_id: Used for v3 APIs (default='default') :param project_domain_name: Used for v3 APIs (default='Default') :param interface: Used to specify the endpoint type for keystone as public, admin, internal :param proxy_settings: instance of os_credentials.ProxySettings class :param cacert: True for https or the certification file for https verification (default=False) :param region_name: the region (optional default = None) """ self.username = kwargs.get('username') self.password = kwargs.get('password') self.auth_url = kwargs.get('auth_url') self.project_name = kwargs.get('project_name') if kwargs.get('identity_api_version') is None: self.identity_api_version = keystone_utils.V3_VERSION_NUM else: self.identity_api_version = float(kwargs['identity_api_version']) if kwargs.get('image_api_version') is None: self.image_api_version = glance_utils.VERSION_2 else: self.image_api_version = float(kwargs['image_api_version']) if kwargs.get('network_api_version') is None: self.network_api_version = 2 else: self.network_api_version = float(kwargs['network_api_version']) if kwargs.get('compute_api_version') is None: self.compute_api_version = 2 else: self.compute_api_version = float(kwargs['compute_api_version']) if kwargs.get('heat_api_version') is None: self.heat_api_version = 1 else: val = kwargs['heat_api_version'] ver = float(val) self.heat_api_version = int(ver) if kwargs.get('volume_api_version') is None: self.volume_api_version = cinder_utils.VERSION_2 else: self.volume_api_version = float(kwargs['volume_api_version']) if kwargs.get('magnum_api_version') is None: self.magnum_api_version = 1 else: self.magnum_api_version = float(kwargs['magnum_api_version']) self.user_domain_id = kwargs.get('user_domain_id', 'default') if kwargs.get('user_domain_name') is None: self.user_domain_name = 'Default' else: self.user_domain_name = kwargs['user_domain_name'] self.project_domain_id = kwargs.get('project_domain_id', 'default') if kwargs.get('project_domain_name') is None: self.project_domain_name = 'Default' else: self.project_domain_name = kwargs['project_domain_name'] if kwargs.get('interface') is None: self.interface = 'public' else: self.interface = kwargs['interface'] self.region_name = kwargs.get('region_name', None) self.cacert = False if kwargs.get('cacert') is not None: if isinstance(kwargs.get('cacert'), str): if file_utils.file_exists(kwargs['cacert']): self.cacert = kwargs['cacert'] else: self.cacert = str2bool(kwargs['cacert']) else: self.cacert = kwargs['cacert'] if isinstance(kwargs.get('proxy_settings'), ProxySettings): self.proxy_settings = kwargs.get('proxy_settings') elif isinstance(kwargs.get('proxy_settings'), dict): self.proxy_settings = ProxySettings(**kwargs.get('proxy_settings')) else: self.proxy_settings = None if (not self.username or not self.password or not self.auth_url or not self.project_name): raise OSCredsError('username, password, auth_url, and project_name' ' are required') self.auth_url = self.__scrub_auth_url()