7e910a488d126397ed7755e912e3f9e11763f478
[snaps.git] / snaps / openstack / tests / os_source_file_test.py
1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 import logging
16 import pkg_resources
17 import uuid
18 import unittest
19
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
25
26
27 dev_os_env_file = pkg_resources.resource_filename(
28     'snaps.openstack.tests.conf', 'os_env.yaml')
29
30
31 class OSComponentTestCase(unittest.TestCase):
32
33     def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None,
34                  image_metadata=None, log_level=logging.DEBUG):
35         """
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)
45         """
46         super(OSComponentTestCase, self).__init__(method_name)
47
48         logging.basicConfig(level=log_level)
49
50         if os_creds:
51             self.os_creds = os_creds
52         else:
53             self.os_creds = openstack_tests.get_credentials(
54                 dev_os_env_file=dev_os_env_file)
55
56         self.ext_net_name = ext_net_name
57
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')
61
62         self.image_metadata = image_metadata
63
64     @staticmethod
65     def parameterize(testcase_klass, os_creds, ext_net_name,
66                      image_metadata=None, log_level=logging.DEBUG):
67         """ Create a suite containing all tests taken from the given
68             subclass, passing them the parameter 'param'.
69         """
70         test_loader = unittest.TestLoader()
71         test_names = test_loader.getTestCaseNames(testcase_klass)
72         suite = unittest.TestSuite()
73         for name in test_names:
74             suite.addTest(testcase_klass(name, os_creds, ext_net_name,
75                                          image_metadata, log_level))
76         return suite
77
78
79 class OSIntegrationTestCase(OSComponentTestCase):
80
81     def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None,
82                  use_keystone=True, flavor_metadata=None, image_metadata=None,
83                  netconf_override=None, log_level=logging.DEBUG):
84         """
85         Super for integration tests requiring a connection to OpenStack
86         :param method_name: default 'runTest'
87         :param os_creds: the OSCreds object, when null it searches for the file
88                          in the package snaps.openstack.tests.conf.os_env.yaml
89         :param ext_net_name: the name of the external network that is used for
90                              creating routers for floating IPs
91         :param use_keystone: when true, these tests will create a new
92                              user/project under which to run the test
93         :param image_metadata: dict() containing the URLs for the disk, kernel,
94                                and ramdisk images when multi-part images are
95                                required. See below for a simple example
96         image_metadata={'disk_url': '{URI}/cirros-0.3.4-x86_64-disk.img',
97                         'kernel_url': '{URI}/cirros-0.3.4-x86_64-kernel',
98                         'ramdisk_url': '{URI}/cirros-0.3.4-x86_64-initramfs'})
99         :param flavor_metadata: dict() to be sent directly into the Nova client
100                                 generally used for page sizes
101         :param netconf_override: dict() containing the configured network_type,
102                                physical_network and segmentation_id
103         :param log_level: the logging level of your test run (default DEBUG)
104         """
105         super(OSIntegrationTestCase, self).__init__(
106             method_name=method_name, os_creds=os_creds,
107             ext_net_name=ext_net_name, image_metadata=image_metadata,
108             log_level=log_level)
109         self.netconf_override = netconf_override
110         self.use_keystone = use_keystone
111         self.keystone = None
112         self.flavor_metadata = flavor_metadata
113
114     @staticmethod
115     def parameterize(testcase_klass, os_creds, ext_net_name,
116                      use_keystone=False, flavor_metadata=None,
117                      image_metadata=None, netconf_override=None,
118                      log_level=logging.DEBUG):
119         """
120         Create a suite containing all tests taken from the given
121         subclass, passing them the parameter 'param'.
122         """
123         test_loader = unittest.TestLoader()
124         test_names = test_loader.getTestCaseNames(testcase_klass)
125         suite = unittest.TestSuite()
126         for name in test_names:
127             suite.addTest(testcase_klass(name, os_creds, ext_net_name,
128                                          use_keystone, flavor_metadata,
129                                          image_metadata, netconf_override,
130                                          log_level))
131         return suite
132
133     """
134     Super for test classes that should be run within their own project/tenant
135     as they can run for quite some time
136     """
137     def __start__(self):
138         """
139         Creates a project and user to be leveraged by subclass test methods. If
140         implementing class uses this method, it must call __clean__() else you
141         will be left with unwanted users and tenants
142         """
143         self.project_creator = None
144         self.user_creator = None
145         self.admin_os_creds = self.os_creds
146         self.role = None
147
148         if self.use_keystone:
149             self.keystone = keystone_utils.keystone_client(self.admin_os_creds)
150             guid = self.__class__.__name__ + '-' + str(uuid.uuid4())[:-19]
151             project_name = guid + '-proj'
152             self.project_creator = deploy_utils.create_project(
153                 self.admin_os_creds, ProjectConfig(
154                     name=project_name,
155                     domain=self.admin_os_creds.project_domain_name))
156
157             self.user_creator = deploy_utils.create_user(
158                 self.admin_os_creds, UserConfig(
159                     name=guid + '-user', password=guid,
160                     project_name=project_name, roles={
161                         'admin': self.project_creator.project_settings.name},
162                     domain_name=self.admin_os_creds.user_domain_name))
163
164             self.os_creds = self.user_creator.get_os_creds(
165                 self.project_creator.project_settings.name)
166
167             # add user to project
168             self.project_creator.assoc_user(self.user_creator.get_user())
169
170     def __clean__(self):
171         """
172         Cleans up test user and project.
173         Must be called at the end of child classes tearDown() if __start__() is
174         called during setUp() else these objects will persist after the test is
175         run
176         """
177         if self.role:
178             keystone_utils.delete_role(self.keystone, self.role)
179
180         if self.project_creator:
181             self.project_creator.clean()
182
183         if self.user_creator:
184             self.user_creator.clean()