1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 from snaps import file_utils
21 from snaps.openstack.create_project import ProjectSettings
22 from snaps.openstack.create_user import UserSettings
23 from snaps.openstack.tests import openstack_tests
24 from snaps.openstack.utils import deploy_utils, keystone_utils
27 dev_os_env_file = pkg_resources.resource_filename('snaps.openstack.tests.conf', 'os_env.yaml')
29 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
31 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34 class OSComponentTestCase(unittest.TestCase):
36 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, image_metadata=None,
37 log_level=logging.DEBUG):
39 Super for test classes requiring a connection to OpenStack
40 :param method_name: default 'runTest'
41 :param os_creds: the OSCreds object, when null it searches for the file {cwd}/openstack/tests/conf/os_env.yaml
42 :param ext_net_name: the name of the external network that is used for creating routers for floating IPs
43 :param image_metadata: ability to override images being used in the tests (see examples/image-metadata)
44 :param log_level: the logging level of your test run (default DEBUG)
46 super(OSComponentTestCase, self).__init__(method_name)
48 logging.basicConfig(level=log_level)
51 self.os_creds = os_creds
53 self.os_creds = openstack_tests.get_credentials(dev_os_env_file=dev_os_env_file)
55 self.ext_net_name = ext_net_name
57 if not self.ext_net_name and file_utils.file_exists(dev_os_env_file):
58 test_conf = file_utils.read_yaml(dev_os_env_file)
59 self.ext_net_name = test_conf.get('ext_net')
61 self.image_metadata = image_metadata
64 def parameterize(testcase_klass, os_creds, ext_net_name, image_metadata=None, log_level=logging.DEBUG):
65 """ Create a suite containing all tests taken from the given
66 subclass, passing them the parameter 'param'.
68 test_loader = unittest.TestLoader()
69 test_names = test_loader.getTestCaseNames(testcase_klass)
70 suite = unittest.TestSuite()
71 for name in test_names:
72 suite.addTest(testcase_klass(name, os_creds, ext_net_name, image_metadata, log_level))
76 class OSIntegrationTestCase(OSComponentTestCase):
78 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, use_keystone=False,
79 flavor_metadata=None, image_metadata=None, log_level=logging.DEBUG):
81 Super for integration tests requiring a connection to OpenStack
82 :param method_name: default 'runTest'
83 :param os_creds: the OSCreds object, when null it searches for the file {cwd}/openstack/tests/conf/os_env.yaml
84 :param ext_net_name: the name of the external network that is used for creating routers for floating IPs
85 :param use_keystone: when true, these tests will create a new user/project under which to run the test
86 :param image_metadata: dict() containing the URLs for the disk, kernel, and ramdisk images when multi-part
88 image_metadata={'disk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img',
89 'kernel_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel',
90 'ramdisk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'})
91 :param flavor_metadata: dict() to be sent directly into the Nova client generally used for page sizes
92 :param log_level: the logging level of your test run (default DEBUG)
94 super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_creds=os_creds,
95 ext_net_name=ext_net_name, image_metadata=image_metadata,
97 self.use_keystone = use_keystone
99 self.flavor_metadata = flavor_metadata
102 def parameterize(testcase_klass, os_creds, ext_net_name, use_keystone=False, flavor_metadata=None,
103 image_metadata=None, log_level=logging.DEBUG):
105 Create a suite containing all tests taken from the given
106 subclass, passing them the parameter 'param'.
108 test_loader = unittest.TestLoader()
109 test_names = test_loader.getTestCaseNames(testcase_klass)
110 suite = unittest.TestSuite()
111 for name in test_names:
112 suite.addTest(testcase_klass(name, os_creds, ext_net_name, use_keystone, flavor_metadata, image_metadata,
117 Super for test classes that should be run within their own project/tenant as they can run for quite some time
121 Creates a project and user to be leveraged by subclass test methods. If implementing class uses this method,
122 it must call __clean__() else you will be left with unwanted users and tenants
124 self.project_creator = None
125 self.user_creator = None
126 self.admin_os_creds = self.os_creds
129 if self.use_keystone:
130 self.keystone = keystone_utils.keystone_client(self.os_creds)
131 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())[:-19]
132 project_name = guid + '-proj'
133 self.project_creator = deploy_utils.create_project(self.admin_os_creds, ProjectSettings(name=project_name))
135 self.user_creator = deploy_utils.create_user(
136 self.admin_os_creds, UserSettings(name=guid + '-user', password=guid, project_name=project_name))
137 self.os_creds = self.user_creator.get_os_creds(self.project_creator.project_settings.name)
139 # add user to project
140 self.project_creator.assoc_user(self.user_creator.get_user())
144 Cleans up test user and project.
145 Must be called at the end of child classes tearDown() if __start__() is called during setUp() else these
146 objects will persist after the test is run
149 keystone_utils.delete_role(self.keystone, self.role)
151 if self.project_creator:
152 self.project_creator.clean()
154 if self.user_creator:
155 self.user_creator.clean()