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.config.project import ProjectConfig
22 from snaps.config.user import UserConfig
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(
28 'snaps.openstack.tests.conf', 'os_env.yaml')
31 class OSComponentTestCase(unittest.TestCase):
33 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None,
34 image_metadata=None, log_level=logging.DEBUG):
36 Super for test classes requiring a connection to OpenStack
37 :param method_name: default 'runTest'
38 :param os_creds: the OSCreds object, when null it searches for the file
39 in the package snaps.openstack.tests.conf.os_env.yaml
40 :param ext_net_name: the name of the external network that is used for
41 creating routers for floating IPs
42 :param image_metadata: ability to override images being used in the
43 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(
54 dev_os_env_file=dev_os_env_file)
56 self.ext_net_name = ext_net_name
58 if not self.ext_net_name and file_utils.file_exists(dev_os_env_file):
59 test_conf = file_utils.read_yaml(dev_os_env_file)
60 self.ext_net_name = test_conf.get('ext_net')
62 self.image_metadata = image_metadata
64 keystone = keystone_utils.keystone_client(self.os_creds)
65 self.project_id = keystone_utils.get_project(
66 keystone=keystone, project_name=self.os_creds.project_name).id
69 def parameterize(testcase_klass, os_creds, ext_net_name,
70 image_metadata=None, log_level=logging.DEBUG):
71 """ Create a suite containing all tests taken from the given
72 subclass, passing them the parameter 'param'.
74 test_loader = unittest.TestLoader()
75 test_names = test_loader.getTestCaseNames(testcase_klass)
76 suite = unittest.TestSuite()
77 for name in test_names:
78 suite.addTest(testcase_klass(name, os_creds, ext_net_name,
79 image_metadata, log_level))
83 class OSIntegrationTestCase(OSComponentTestCase):
85 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None,
86 use_keystone=True, flavor_metadata=None, image_metadata=None,
87 netconf_override=None, log_level=logging.DEBUG):
89 Super for integration tests requiring a connection to OpenStack
90 :param method_name: default 'runTest'
91 :param os_creds: the OSCreds object, when null it searches for the file
92 in the package snaps.openstack.tests.conf.os_env.yaml
93 :param ext_net_name: the name of the external network that is used for
94 creating routers for floating IPs
95 :param use_keystone: when true, these tests will create a new
96 user/project under which to run the test
97 :param image_metadata: dict() containing the URLs for the disk, kernel,
98 and ramdisk images when multi-part images are
99 required. See below for a simple example
100 image_metadata={'disk_url': '{URI}/cirros-0.3.4-x86_64-disk.img',
101 'kernel_url': '{URI}/cirros-0.3.4-x86_64-kernel',
102 'ramdisk_url': '{URI}/cirros-0.3.4-x86_64-initramfs'})
103 :param flavor_metadata: dict() to be sent directly into the Nova client
104 generally used for page sizes
105 :param netconf_override: dict() containing the configured network_type,
106 physical_network and segmentation_id
107 :param log_level: the logging level of your test run (default DEBUG)
109 super(OSIntegrationTestCase, self).__init__(
110 method_name=method_name, os_creds=os_creds,
111 ext_net_name=ext_net_name, image_metadata=image_metadata,
113 self.netconf_override = netconf_override
114 self.use_keystone = use_keystone
116 self.flavor_metadata = flavor_metadata
119 def parameterize(testcase_klass, os_creds, ext_net_name,
120 use_keystone=False, flavor_metadata=None,
121 image_metadata=None, netconf_override=None,
122 log_level=logging.DEBUG):
124 Create a suite containing all tests taken from the given
125 subclass, passing them the parameter 'param'.
127 test_loader = unittest.TestLoader()
128 test_names = test_loader.getTestCaseNames(testcase_klass)
129 suite = unittest.TestSuite()
130 for name in test_names:
131 suite.addTest(testcase_klass(name, os_creds, ext_net_name,
132 use_keystone, flavor_metadata,
133 image_metadata, netconf_override,
138 Super for test classes that should be run within their own project/tenant
139 as they can run for quite some time
143 Creates a project and user to be leveraged by subclass test methods. If
144 implementing class uses this method, it must call __clean__() else you
145 will be left with unwanted users and tenants
147 self.project_creator = None
148 self.user_creator = None
149 self.admin_os_creds = self.os_creds
152 if self.use_keystone:
153 self.keystone = keystone_utils.keystone_client(self.admin_os_creds)
154 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())[:-19]
155 project_name = guid + '-proj'
156 self.project_creator = deploy_utils.create_project(
157 self.admin_os_creds, ProjectConfig(
159 domain=self.admin_os_creds.project_domain_name))
161 self.user_creator = deploy_utils.create_user(
162 self.admin_os_creds, UserConfig(
163 name=guid + '-user', password=guid,
164 project_name=project_name, roles={
165 'admin': self.project_creator.project_settings.name},
166 domain_name=self.admin_os_creds.user_domain_name))
168 self.os_creds = self.user_creator.get_os_creds(
169 self.project_creator.project_settings.name)
171 # add user to project
172 self.project_creator.assoc_user(self.user_creator.get_user())
173 self.project_id = self.project_creator.get_project().id
177 Cleans up test user and project.
178 Must be called at the end of child classes tearDown() if __start__() is
179 called during setUp() else these objects will persist after the test is
183 keystone_utils.delete_role(self.keystone, self.role)
185 if self.project_creator:
186 self.project_creator.clean()
188 if self.user_creator:
189 self.user_creator.clean()