summaryrefslogtreecommitdiffstats
path: root/snaps/openstack
diff options
context:
space:
mode:
Diffstat (limited to 'snaps/openstack')
-rw-r--r--snaps/openstack/create_instance.py1
-rw-r--r--snaps/openstack/tests/conf/os_credentials_tests.py24
-rw-r--r--snaps/openstack/tests/create_instance_tests.py5
-rw-r--r--snaps/openstack/tests/create_keypairs_tests.py34
-rw-r--r--snaps/openstack/utils/glance_utils.py38
-rw-r--r--snaps/openstack/utils/nova_utils.py49
6 files changed, 102 insertions, 49 deletions
diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py
index d5917a8..997b5a5 100644
--- a/snaps/openstack/create_instance.py
+++ b/snaps/openstack/create_instance.py
@@ -618,6 +618,7 @@ class OpenStackVmInstance:
if len(self.__floating_ips) > 0:
ssh = self.ssh_client()
if ssh:
+ ssh.close()
return True
return False
diff --git a/snaps/openstack/tests/conf/os_credentials_tests.py b/snaps/openstack/tests/conf/os_credentials_tests.py
index e7c34b9..4a2ce3d 100644
--- a/snaps/openstack/tests/conf/os_credentials_tests.py
+++ b/snaps/openstack/tests/conf/os_credentials_tests.py
@@ -56,17 +56,17 @@ class ProxySettingsUnitTests(unittest.TestCase):
def test_minimum(self):
proxy_settings = ProxySettings(host='foo', port=1234)
self.assertEqual('foo', proxy_settings.host)
- self.assertEqual(1234, proxy_settings.port)
+ self.assertEqual('1234', proxy_settings.port)
self.assertEqual('foo', proxy_settings.https_host)
- self.assertEqual(1234, proxy_settings.https_port)
+ self.assertEqual('1234', proxy_settings.https_port)
self.assertIsNone(proxy_settings.ssh_proxy_cmd)
def test_minimum_kwargs(self):
proxy_settings = ProxySettings(**{'host': 'foo', 'port': 1234})
self.assertEqual('foo', proxy_settings.host)
- self.assertEqual(1234, proxy_settings.port)
+ self.assertEqual('1234', proxy_settings.port)
self.assertEqual('foo', proxy_settings.https_host)
- self.assertEqual(1234, proxy_settings.https_port)
+ self.assertEqual('1234', proxy_settings.https_port)
self.assertIsNone(proxy_settings.ssh_proxy_cmd)
def test_all(self):
@@ -74,9 +74,9 @@ class ProxySettingsUnitTests(unittest.TestCase):
host='foo', port=1234, https_host='bar', https_port=2345,
ssh_proxy_cmd='proxy command')
self.assertEqual('foo', proxy_settings.host)
- self.assertEqual(1234, proxy_settings.port)
+ self.assertEqual('1234', proxy_settings.port)
self.assertEqual('bar', proxy_settings.https_host)
- self.assertEqual(2345, proxy_settings.https_port)
+ self.assertEqual('2345', proxy_settings.https_port)
self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
def test_all_kwargs(self):
@@ -84,9 +84,9 @@ class ProxySettingsUnitTests(unittest.TestCase):
**{'host': 'foo', 'port': 1234, 'https_host': 'bar',
'https_port': 2345, 'ssh_proxy_cmd': 'proxy command'})
self.assertEqual('foo', proxy_settings.host)
- self.assertEqual(1234, proxy_settings.port)
+ self.assertEqual('1234', proxy_settings.port)
self.assertEqual('bar', proxy_settings.https_host)
- self.assertEqual(2345, proxy_settings.https_port)
+ self.assertEqual('2345', proxy_settings.https_port)
self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
@@ -245,7 +245,7 @@ class OSCredsUnitTests(unittest.TestCase):
self.assertEqual('admin', os_creds.interface)
self.assertFalse(os_creds.cacert)
self.assertEqual('foo', os_creds.proxy_settings.host)
- self.assertEqual(1234, os_creds.proxy_settings.port)
+ self.assertEqual('1234', os_creds.proxy_settings.port)
self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
self.assertIsNone(os_creds.region_name)
@@ -269,7 +269,7 @@ class OSCredsUnitTests(unittest.TestCase):
self.assertEqual('admin', os_creds.interface)
self.assertFalse(os_creds.cacert)
self.assertEqual('foo', os_creds.proxy_settings.host)
- self.assertEqual(1234, os_creds.proxy_settings.port)
+ self.assertEqual('1234', os_creds.proxy_settings.port)
self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
self.assertEqual('test_region', os_creds.region_name)
@@ -290,7 +290,7 @@ class OSCredsUnitTests(unittest.TestCase):
self.assertEqual('admin', os_creds.interface)
self.assertFalse(os_creds.cacert)
self.assertEqual('foo', os_creds.proxy_settings.host)
- self.assertEqual(1234, os_creds.proxy_settings.port)
+ self.assertEqual('1234', os_creds.proxy_settings.port)
self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
def test_proxy_settings_dict_kwargs(self):
@@ -312,6 +312,6 @@ class OSCredsUnitTests(unittest.TestCase):
self.assertEqual('admin', os_creds.interface)
self.assertFalse(os_creds.cacert)
self.assertEqual('foo', os_creds.proxy_settings.host)
- self.assertEqual(1234, os_creds.proxy_settings.port)
+ self.assertEqual('1234', os_creds.proxy_settings.port)
self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
self.assertEqual('test_region', os_creds.region_name)
diff --git a/snaps/openstack/tests/create_instance_tests.py b/snaps/openstack/tests/create_instance_tests.py
index 75b0ed3..1922146 100644
--- a/snaps/openstack/tests/create_instance_tests.py
+++ b/snaps/openstack/tests/create_instance_tests.py
@@ -1717,7 +1717,10 @@ def validate_ssh_client(instance_creator):
if ssh_active:
ssh_client = instance_creator.ssh_client()
if ssh_client:
- out = ssh_client.exec_command('pwd')[1]
+ try:
+ out = ssh_client.exec_command('pwd')[1]
+ finally:
+ ssh_client.close()
else:
return False
diff --git a/snaps/openstack/tests/create_keypairs_tests.py b/snaps/openstack/tests/create_keypairs_tests.py
index 0b35095..7b75d05 100644
--- a/snaps/openstack/tests/create_keypairs_tests.py
+++ b/snaps/openstack/tests/create_keypairs_tests.py
@@ -285,9 +285,15 @@ class CreateKeypairsTests(OSIntegrationTestCase):
self.keypair_creator.get_keypair())
self.assertEqual(self.keypair_creator.get_keypair(), keypair)
- file_key = open(os.path.expanduser(self.pub_file_path)).read()
- self.assertEqual(self.keypair_creator.get_keypair().public_key,
- file_key)
+ 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):
"""
@@ -305,7 +311,16 @@ class CreateKeypairsTests(OSIntegrationTestCase):
self.keypair_creator.get_keypair())
self.assertEqual(self.keypair_creator.get_keypair(), keypair)
- file_key = open(os.path.expanduser(self.pub_file_path)).read()
+ 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)
@@ -328,7 +343,16 @@ class CreateKeypairsTests(OSIntegrationTestCase):
self.keypair_creator.get_keypair())
self.assertEqual(self.keypair_creator.get_keypair(), keypair)
- file_key = open(os.path.expanduser(self.pub_file_path)).read()
+ 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)
diff --git a/snaps/openstack/utils/glance_utils.py b/snaps/openstack/utils/glance_utils.py
index 49bfe95..ad9c5e5 100644
--- a/snaps/openstack/utils/glance_utils.py
+++ b/snaps/openstack/utils/glance_utils.py
@@ -124,22 +124,30 @@ def __create_image_v1(glance, image_settings):
'name': image_settings.name, 'disk_format': image_settings.format,
'container_format': 'bare', 'is_public': image_settings.public}
- if image_settings.extra_properties:
- kwargs['properties'] = image_settings.extra_properties
-
- if image_settings.url:
- kwargs['location'] = image_settings.url
- elif image_settings.image_file:
- image_file = open(image_settings.image_file, 'rb')
- kwargs['data'] = image_file
- else:
- logger.warn('Unable to create image with name - %s. No file or URL',
- image_settings.name)
- return None
+ image_file = None
- created_image = glance.images.create(**kwargs)
- return Image(name=image_settings.name, image_id=created_image.id,
- size=created_image.size, properties=created_image.properties)
+ try:
+ if image_settings.extra_properties:
+ kwargs['properties'] = image_settings.extra_properties
+
+ if image_settings.url:
+ kwargs['location'] = image_settings.url
+ elif image_settings.image_file:
+ image_file = open(image_settings.image_file, 'rb')
+ kwargs['data'] = image_file
+ else:
+ logger.warn(
+ 'Unable to create image with name - %s. No file or URL',
+ image_settings.name)
+ return None
+
+ created_image = glance.images.create(**kwargs)
+ return Image(name=image_settings.name, image_id=created_image.id,
+ size=created_image.size,
+ properties=created_image.properties)
+ finally:
+ if image_file:
+ image_file.close()
def __create_image_v2(glance, image_settings):
diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py
index ab434f1..b148bc5 100644
--- a/snaps/openstack/utils/nova_utils.py
+++ b/snaps/openstack/utils/nova_utils.py
@@ -232,12 +232,18 @@ def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
if not os.path.isdir(pub_dir):
os.mkdir(pub_dir)
- public_handle = open(pub_expand_file, 'wb')
- public_bytes = keys.public_key().public_bytes(
- serialization.Encoding.OpenSSH,
- serialization.PublicFormat.OpenSSH)
- public_handle.write(public_bytes)
- public_handle.close()
+
+ public_handle = None
+ try:
+ public_handle = open(pub_expand_file, 'wb')
+ public_bytes = keys.public_key().public_bytes(
+ serialization.Encoding.OpenSSH,
+ serialization.PublicFormat.OpenSSH)
+ public_handle.write(public_bytes)
+ finally:
+ if public_handle:
+ public_handle.close()
+
os.chmod(pub_expand_file, 0o400)
logger.info("Saved public key to - " + pub_expand_file)
if priv_file_path:
@@ -246,13 +252,19 @@ def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
priv_dir = os.path.dirname(priv_expand_file)
if not os.path.isdir(priv_dir):
os.mkdir(priv_dir)
- private_handle = open(priv_expand_file, 'wb')
- private_handle.write(
- keys.private_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PrivateFormat.TraditionalOpenSSL,
- encryption_algorithm=serialization.NoEncryption()))
- private_handle.close()
+
+ private_handle = None
+ try:
+ private_handle = open(priv_expand_file, 'wb')
+ private_handle.write(
+ keys.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=serialization.NoEncryption()))
+ finally:
+ if private_handle:
+ private_handle.close()
+
os.chmod(priv_expand_file, 0o400)
logger.info("Saved private key to - " + priv_expand_file)
@@ -265,9 +277,14 @@ def upload_keypair_file(nova, name, file_path):
:param file_path: the path to the public key file
:return: the keypair object
"""
- with open(os.path.expanduser(file_path), 'rb') as fpubkey:
- logger.info('Saving keypair to - ' + file_path)
- return upload_keypair(nova, name, fpubkey.read())
+ fpubkey = None
+ try:
+ with open(os.path.expanduser(file_path), 'rb') as fpubkey:
+ logger.info('Saving keypair to - ' + file_path)
+ return upload_keypair(nova, name, fpubkey.read())
+ finally:
+ if fpubkey:
+ fpubkey.close()
def upload_keypair(nova, name, key):