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.
19 from snaps import file_utils
20 from snaps.openstack.create_project import ProjectSettings
21 from snaps.openstack.create_user import UserSettings
22 from snaps.openstack.tests import openstack_tests
23 from snaps.openstack.utils import deploy_utils, keystone_utils
25 dev_os_env_file = 'openstack/tests/conf/os_env.yaml'
27 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
28 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
29 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 class OSComponentTestCase(unittest.TestCase):
34 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, image_metadata=None,
35 log_level=logging.DEBUG):
37 Super for test classes requiring a connection to OpenStack
38 :param method_name: default 'runTest'
39 :param os_creds: the OSCreds object, when null it searches for the file {cwd}/openstack/tests/conf/os_env.yaml
40 :param ext_net_name: the name of the external network that is used for creating routers for floating IPs
41 :param image_metadata: ability to override images being used in the tests (see examples/image-metadata)
42 :param log_level: the logging level of your test run (default DEBUG)
44 super(OSComponentTestCase, self).__init__(method_name)
46 logging.basicConfig(level=log_level)
49 self.os_creds = os_creds
51 self.os_creds = openstack_tests.get_credentials(dev_os_env_file=dev_os_env_file)
53 self.ext_net_name = ext_net_name
55 if not self.ext_net_name and file_utils.file_exists(dev_os_env_file):
56 test_conf = file_utils.read_yaml(dev_os_env_file)
57 self.ext_net_name = test_conf.get('ext_net')
59 self.image_metadata = image_metadata
62 def parameterize(testcase_klass, os_creds, ext_net_name, image_metadata=None, log_level=logging.DEBUG):
63 """ Create a suite containing all tests taken from the given
64 subclass, passing them the parameter 'param'.
66 test_loader = unittest.TestLoader()
67 test_names = test_loader.getTestCaseNames(testcase_klass)
68 suite = unittest.TestSuite()
69 for name in test_names:
70 suite.addTest(testcase_klass(name, os_creds, ext_net_name, image_metadata, log_level))
74 class OSIntegrationTestCase(OSComponentTestCase):
76 def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, use_keystone=False,
77 flavor_metadata=None, image_metadata=None, log_level=logging.DEBUG):
79 Super for integration tests requiring a connection to OpenStack
80 :param method_name: default 'runTest'
81 :param os_creds: the OSCreds object, when null it searches for the file {cwd}/openstack/tests/conf/os_env.yaml
82 :param ext_net_name: the name of the external network that is used for creating routers for floating IPs
83 :param use_keystone: when true, these tests will create a new user/project under which to run the test
84 :param image_metadata: dict() containing the URLs for the disk, kernel, and ramdisk images when multi-part
86 image_metadata={'disk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img',
87 'kernel_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel',
88 'ramdisk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'})
89 :param flavor_metadata: dict() to be sent directly into the Nova client generally used for page sizes
90 :param log_level: the logging level of your test run (default DEBUG)
92 super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_creds=os_creds,
93 ext_net_name=ext_net_name, image_metadata=image_metadata,
95 self.use_keystone = use_keystone
97 self.flavor_metadata = flavor_metadata
100 def parameterize(testcase_klass, os_creds, ext_net_name, use_keystone=False, flavor_metadata=None,
101 image_metadata=None, log_level=logging.DEBUG):
103 Create a suite containing all tests taken from the given
104 subclass, passing them the parameter 'param'.
106 test_loader = unittest.TestLoader()
107 test_names = test_loader.getTestCaseNames(testcase_klass)
108 suite = unittest.TestSuite()
109 for name in test_names:
110 suite.addTest(testcase_klass(name, os_creds, ext_net_name, use_keystone, flavor_metadata, image_metadata,
115 Super for test classes that should be run within their own project/tenant as they can run for quite some time
119 Creates a project and user to be leveraged by subclass test methods. If implementing class uses this method,
120 it must call __clean__() else you will be left with unwanted users and tenants
122 self.project_creator = None
123 self.user_creator = None
124 self.admin_os_creds = self.os_creds
127 if self.use_keystone:
128 self.keystone = keystone_utils.keystone_client(self.os_creds)
129 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())[:-19]
130 project_name = guid + '-proj'
131 self.project_creator = deploy_utils.create_project(self.admin_os_creds, ProjectSettings(name=project_name))
133 self.user_creator = deploy_utils.create_user(
134 self.admin_os_creds, UserSettings(name=guid + '-user', password=guid, project_name=project_name))
135 self.os_creds = self.user_creator.get_os_creds(self.project_creator.project_settings.name)
137 # add user to project
138 self.project_creator.assoc_user(self.user_creator.get_user())
142 Cleans up test user and project.
143 Must be called at the end of child classes tearDown() if __start__() is called during setUp() else these
144 objects will persist after the test is run
147 keystone_utils.delete_role(self.keystone, self.role)
149 if self.project_creator:
150 self.project_creator.clean()
152 if self.user_creator:
153 self.user_creator.clean()