+
+
+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.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, KeypairSettings(
+ 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, KeypairSettings(
+ 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, KeypairSettings(
+ 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()
+ nova_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, KeypairSettings(
+ 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()
+ nova_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, KeypairSettings(
+ 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))