Refactoring of NetworkSettings to extend NetworkConfig
[snaps.git] / snaps / provisioning / tests / ansible_utils_tests.py
index 4c8dea6..b78249e 100644 (file)
@@ -18,6 +18,11 @@ import uuid
 import os
 import pkg_resources
 from scp import SCPClient
+
+from snaps.config.flavor import FlavorConfig
+from snaps.config.keypair import KeypairConfig
+from snaps.config.network import PortConfig
+
 from snaps.openstack import create_flavor
 from snaps.openstack import create_image
 from snaps.openstack import create_instance
@@ -100,14 +105,14 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
             # Create Flavor
             self.flavor_creator = create_flavor.OpenStackFlavor(
                 self.admin_os_creds,
-                create_flavor.FlavorSettings(name=guid + '-flavor-name',
-                                             ram=2048, disk=10, vcpus=2,
-                                             metadata=self.flavor_metadata))
+                FlavorConfig(
+                    name=guid + '-flavor-name', ram=2048, disk=10, vcpus=2,
+                    metadata=self.flavor_metadata))
             self.flavor_creator.create()
 
             # Create Key/Pair
             self.keypair_creator = create_keypairs.OpenStackKeypair(
-                self.os_creds, create_keypairs.KeypairSettings(
+                self.os_creds, KeypairConfig(
                     name=self.keypair_name,
                     public_filepath=self.keypair_pub_filepath,
                     private_filepath=self.keypair_priv_filepath))
@@ -132,7 +137,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
             # Create instance
             ports_settings = list()
             ports_settings.append(
-                create_network.PortSettings(
+                PortConfig(
                     name=self.port_1_name,
                     network_name=self.pub_net_config.network_settings.name))
 
@@ -225,10 +230,10 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         Should this not be performed, the creation of the host ssh key will
         cause your ansible calls to fail.
         """
-        vm = self.inst_creator.create(block=True)
+        self.inst_creator.create(block=True)
 
         priv_ip = self.inst_creator.get_port_ip(self.port_1_name)
-        self.assertTrue(check_dhcp_lease(self.nova, vm, priv_ip))
+        self.assertTrue(check_dhcp_lease(self.inst_creator, priv_ip))
 
         # Apply Security Group
         self.inst_creator.add_security_group(
@@ -239,9 +244,14 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
 
         ssh_client = self.inst_creator.ssh_client()
         self.assertIsNotNone(ssh_client)
-        out = ssh_client.exec_command('pwd')[1].channel.in_buffer.read(1024)
-        self.assertIsNotNone(out)
-        self.assertGreater(len(out), 1)
+
+        try:
+            out = ssh_client.exec_command('pwd')[1].channel.in_buffer.read(
+                1024)
+            self.assertIsNotNone(out)
+            self.assertGreater(len(out), 1)
+        finally:
+            ssh_client.close()
 
         # Need to use the first floating IP as subsequent ones are currently
         # broken with Apex CO
@@ -257,14 +267,26 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         ssh = ansible_utils.ssh_client(ip, user, priv_key,
                                        self.os_creds.proxy_settings)
         self.assertIsNotNone(ssh)
-        scp = SCPClient(ssh.get_transport())
-        scp.get('~/hello.txt', self.test_file_local_path)
+        scp = None
+        try:
+            scp = SCPClient(ssh.get_transport())
+            scp.get('~/hello.txt', self.test_file_local_path)
+        finally:
+            if scp:
+                scp.close()
+            ssh.close()
 
         self.assertTrue(os.path.isfile(self.test_file_local_path))
 
-        with open(self.test_file_local_path) as f:
-            file_contents = f.readline()
-            self.assertEqual('Hello World!', file_contents)
+        test_file = None
+
+        try:
+            with open(self.test_file_local_path) as test_file:
+                file_contents = test_file.readline()
+                self.assertEqual('Hello World!', file_contents)
+        finally:
+            if test_file:
+                test_file.close()
 
     def test_apply_template_playbook(self):
         """
@@ -277,10 +299,10 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         Should this not be performed, the creation of the host ssh key will
         cause your ansible calls to fail.
         """
-        vm = self.inst_creator.create(block=True)
+        self.inst_creator.create(block=True)
 
         priv_ip = self.inst_creator.get_port_ip(self.port_1_name)
-        self.assertTrue(check_dhcp_lease(self.nova, vm, priv_ip))
+        self.assertTrue(check_dhcp_lease(self.inst_creator, priv_ip))
 
         # Apply Security Group
         self.inst_creator.add_security_group(
@@ -310,11 +332,23 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         ssh = ansible_utils.ssh_client(ip, user, priv_key,
                                        self.os_creds.proxy_settings)
         self.assertIsNotNone(ssh)
-        scp = SCPClient(ssh.get_transport())
-        scp.get('/tmp/hello.txt', self.test_file_local_path)
+        scp = None
+
+        try:
+            scp = SCPClient(ssh.get_transport())
+            scp.get('/tmp/hello.txt', self.test_file_local_path)
+        finally:
+            if scp:
+                scp.close()
+            ssh.close()
 
         self.assertTrue(os.path.isfile(self.test_file_local_path))
 
-        with open(self.test_file_local_path) as f:
-            file_contents = f.readline()
-            self.assertEqual('Hello Foo!', file_contents)
+        test_file = None
+        try:
+            with open(self.test_file_local_path) as test_file:
+                file_contents = test_file.readline()
+                self.assertEqual('Hello Foo!', file_contents)
+        finally:
+            if test_file:
+                test_file.close()