Removed current working directory logic retrieving test file resources. 93/35893/1
authorspisarski <s.pisarski@cablelabs.com>
Wed, 7 Jun 2017 20:56:00 +0000 (14:56 -0600)
committerspisarski <s.pisarski@cablelabs.com>
Wed, 7 Jun 2017 20:56:00 +0000 (14:56 -0600)
Some of the SNAPS tests leverage file resources contained within the
project. These resources were being accessed via relative paths which
required the test clients to ensure that the CWD was the <repo>/snaps
directory. Replaced that logic to leverage the import pkg_resources
making the tests much more flexible and robust.

JIRA: SNAPS-89

Change-Id: Ic9c429ee53e4dd785641e11e1ed4de5aeeab54d1
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
examples/__init__.py [new file with mode: 0644]
examples/heat/__init__.py [new file with mode: 0644]
snaps/custom_image_test_runner.py
snaps/openstack/tests/conf/__init__.py [new file with mode: 0644]
snaps/openstack/tests/create_stack_tests.py
snaps/openstack/tests/os_source_file_test.py
snaps/openstack/utils/tests/heat_utils_tests.py
snaps/provisioning/tests/ansible_utils_tests.py
snaps/provisioning/tests/playbooks/__init__.py [new file with mode: 0644]
snaps/test_runner.py
snaps/tests/file_utils_tests.py

diff --git a/examples/__init__.py b/examples/__init__.py
new file mode 100644 (file)
index 0000000..271c742
--- /dev/null
@@ -0,0 +1,15 @@
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
+#                    and others.  All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+__author__ = 'spisarski'
diff --git a/examples/heat/__init__.py b/examples/heat/__init__.py
new file mode 100644 (file)
index 0000000..271c742
--- /dev/null
@@ -0,0 +1,15 @@
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
+#                    and others.  All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+__author__ = 'spisarski'
index a3e3897..4b86bf6 100644 (file)
@@ -14,7 +14,6 @@
 # limitations under the License.
 import argparse
 import logging
-import os
 import unittest
 
 from snaps import test_suite_builder
@@ -48,9 +47,6 @@ def __run_tests(source_filename, ext_net_name, proxy_settings, ssh_proxy_cmd, us
     """
     os_creds = openstack_tests.get_credentials(os_env_file=source_filename, proxy_settings_str=proxy_settings,
                                                ssh_proxy_cmd=ssh_proxy_cmd)
-    # To ensure any files referenced via a relative path will begin from the diectory in which this file resides
-    os.chdir(os.path.dirname(os.path.realpath(__file__)))
-
     image_creators = __create_images(os_creds)
 
     meta_list = list()
diff --git a/snaps/openstack/tests/conf/__init__.py b/snaps/openstack/tests/conf/__init__.py
new file mode 100644 (file)
index 0000000..271c742
--- /dev/null
@@ -0,0 +1,15 @@
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
+#                    and others.  All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+__author__ = 'spisarski'
index fa75475..bee1340 100644 (file)
@@ -12,6 +12,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import pkg_resources
 import time
 
 from heatclient.exc import HTTPBadRequest
@@ -145,12 +146,17 @@ class CreateStackSuccessTests(OSIntegrationTestCase):
         self.env_values = {'image_name': self.image_creator.image_settings.name,
                            'flavor_name': self.flavor_creator.flavor_settings.name}
 
+        self.heat_tmplt_path = pkg_resources.resource_filename('examples.heat', 'test_heat_template.yaml')
+
     def tearDown(self):
         """
         Cleans the stack and downloaded stack file
         """
         if self.stack_creator:
-            self.stack_creator.clean()
+            try:
+                self.stack_creator.clean()
+            except:
+                pass
 
         if self.image_creator:
             try:
@@ -173,7 +179,7 @@ class CreateStackSuccessTests(OSIntegrationTestCase):
         # Create Stack
         # Set the default stack settings, then set any custom parameters sent from the app
         stack_settings = StackSettings(name=self.__class__.__name__ + '-' + str(self.guid) + '-stack',
-                                       template_path='../examples/heat/test_heat_template.yaml',
+                                       template_path=self.heat_tmplt_path,
                                        env_values=self.env_values)
         self.stack_creator = create_stack.OpenStackHeatStack(self.os_creds, stack_settings)
         created_stack = self.stack_creator.create()
@@ -193,7 +199,7 @@ class CreateStackSuccessTests(OSIntegrationTestCase):
         # Create Stack
         # Set the default stack settings, then set any custom parameters sent from the app
         template_dict = heat_utils.parse_heat_template_str(
-            file_utils.read_file('../examples/heat/test_heat_template.yaml'))
+            file_utils.read_file(self.heat_tmplt_path))
         stack_settings = StackSettings(name=self.__class__.__name__ + '-' + str(self.guid) + '-stack',
                                        template=template_dict,
                                        env_values=self.env_values)
@@ -214,7 +220,7 @@ class CreateStackSuccessTests(OSIntegrationTestCase):
         """
         # Create Stack
         template_dict = heat_utils.parse_heat_template_str(
-            file_utils.read_file('../examples/heat/test_heat_template.yaml'))
+            file_utils.read_file(self.heat_tmplt_path))
         stack_settings = StackSettings(name=self.__class__.__name__ + '-' + str(self.guid) + '-stack',
                                        template=template_dict,
                                        env_values=self.env_values)
@@ -253,7 +259,7 @@ class CreateStackSuccessTests(OSIntegrationTestCase):
         """
         # Create Stack
         template_dict = heat_utils.parse_heat_template_str(
-            file_utils.read_file('../examples/heat/test_heat_template.yaml'))
+            file_utils.read_file(self.heat_tmplt_path))
         stack_settings = StackSettings(name=self.__class__.__name__ + '-' + str(self.guid) + '-stack',
                                        template=template_dict,
                                        env_values=self.env_values)
@@ -283,6 +289,7 @@ class CreateStackNegativeTests(OSIntegrationTestCase):
 
         self.stack_name = self.__class__.__name__ + '-' + str(uuid.uuid4())
         self.stack_creator = None
+        self.heat_tmplt_path = pkg_resources.resource_filename('examples.heat', 'test_heat_template.yaml')
 
     def tearDown(self):
         if self.stack_creator:
@@ -293,7 +300,7 @@ class CreateStackNegativeTests(OSIntegrationTestCase):
         """
         Expect an StackCreationError when the stack file does not exist
         """
-        stack_settings = StackSettings(name=self.stack_name, template_path='../examples/heat/test_heat_template.yaml')
+        stack_settings = StackSettings(name=self.stack_name, template_path=self.heat_tmplt_path)
         self.stack_creator = create_stack.OpenStackHeatStack(self.os_creds, stack_settings)
         with self.assertRaises(HTTPBadRequest):
             self.stack_creator.create()
index 3a632e5..4b421e8 100644 (file)
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import logging
+import pkg_resources
 import uuid
 import unittest
 
@@ -22,7 +23,8 @@ from snaps.openstack.create_user import UserSettings
 from snaps.openstack.tests import openstack_tests
 from snaps.openstack.utils import deploy_utils, keystone_utils
 
-dev_os_env_file = 'openstack/tests/conf/os_env.yaml'
+
+dev_os_env_file = pkg_resources.resource_filename('snaps.openstack.tests.conf', 'os_env.yaml')
 
 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
index 08387d8..3d9fe84 100644 (file)
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import logging
+import pkg_resources
 import uuid
 
 import time
@@ -86,8 +87,8 @@ class HeatUtilsCreateStackTests(OSComponentTestCase):
 
         env_values = {'image_name': self.image_creator.image_settings.name,
                       'flavor_name': self.flavor_creator.flavor_settings.name}
-        self.stack_settings = StackSettings(name=stack_name, template_path='../examples/heat/test_heat_template.yaml',
-                                            env_values=env_values)
+        heat_tmplt_path = pkg_resources.resource_filename('examples.heat', 'test_heat_template.yaml')
+        self.stack_settings = StackSettings(name=stack_name, template_path=heat_tmplt_path, env_values=env_values)
         self.stack = None
         self.heat_client = heat_utils.heat_client(self.os_creds)
 
index 76714b8..cddedcd 100644 (file)
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 import os
+import pkg_resources
 import uuid
 from scp import SCPClient
 from snaps.openstack.create_security_group import SecurityGroupRuleSettings, Direction, Protocol, \
@@ -191,7 +192,8 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         user = self.inst_creator.get_image_user()
         priv_key = self.inst_creator.keypair_settings.private_filepath
 
-        retval = self.inst_creator.apply_ansible_playbook('provisioning/tests/playbooks/simple_playbook.yml')
+        relative_pb_path = pkg_resources.resource_filename('snaps.provisioning.tests.playbooks', 'simple_playbook.yml')
+        retval = self.inst_creator.apply_ansible_playbook(relative_pb_path)
         self.assertEqual(0, retval)
 
         ssh = ansible_utils.ssh_client(ip, user, priv_key, self.os_creds.proxy_settings)
@@ -228,8 +230,9 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         user = self.inst_creator.get_image_user()
         priv_key = self.inst_creator.keypair_settings.private_filepath
 
-        retval = self.inst_creator.apply_ansible_playbook('provisioning/tests/playbooks/template_playbook.yml',
-                                                          variables={'name': 'Foo'})
+        relative_pb_path = pkg_resources.resource_filename('snaps.provisioning.tests.playbooks',
+                                                           'template_playbook.yml')
+        retval = self.inst_creator.apply_ansible_playbook(relative_pb_path, variables={'name': 'Foo'})
         self.assertEqual(0, retval)
 
         ssh = ansible_utils.ssh_client(ip, user, priv_key, self.os_creds.proxy_settings)
diff --git a/snaps/provisioning/tests/playbooks/__init__.py b/snaps/provisioning/tests/playbooks/__init__.py
new file mode 100644 (file)
index 0000000..271c742
--- /dev/null
@@ -0,0 +1,15 @@
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
+#                    and others.  All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+__author__ = 'spisarski'
index 54caccb..d207bf3 100644 (file)
@@ -15,7 +15,6 @@
 import argparse
 import json
 import logging
-import os
 import unittest
 
 from snaps import test_suite_builder, file_utils
@@ -124,9 +123,6 @@ def main(arguments):
         logger.error('Environment file or external network not defined')
         exit(1)
 
-    # To ensure any files referenced via a relative path will begin from the diectory in which this file resides
-    os.chdir(os.path.dirname(os.path.realpath(__file__)))
-
     i = 0
     while i < int(arguments.num_runs):
         result = unittest.TextTestRunner(verbosity=2).run(suite)
index a28231b..62d96e8 100644 (file)
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import os
+import pkg_resources
 import unittest
 import shutil
 import uuid
@@ -95,7 +96,8 @@ class FileUtilsTests(unittest.TestCase):
         Tests that the OS Environment file is correctly parsed
         :return:
         """
-        os_env_dict = file_utils.read_os_env_file('openstack/tests/conf/overcloudrc_test')
+        rc_file_path = pkg_resources.resource_filename('snaps.openstack.tests.conf', 'overcloudrc_test')
+        os_env_dict = file_utils.read_os_env_file(rc_file_path)
         self.assertEqual('test_pw', os_env_dict['OS_PASSWORD'])
         self.assertEqual('http://foo:5000/v2.0/', os_env_dict['OS_AUTH_URL'])
         self.assertEqual('admin', os_env_dict['OS_USERNAME'])