3a632e50e4ebb0c9f953f93ea96b0b0d18e07422
[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 uuid
17 import unittest
18
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
24
25 dev_os_env_file = 'openstack/tests/conf/os_env.yaml'
26
27 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
28 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
29 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30
31
32 class OSComponentTestCase(unittest.TestCase):
33
34     def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, image_metadata=None,
35                  log_level=logging.DEBUG):
36         """
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)
43         """
44         super(OSComponentTestCase, self).__init__(method_name)
45
46         logging.basicConfig(level=log_level)
47
48         if os_creds:
49             self.os_creds = os_creds
50         else:
51             self.os_creds = openstack_tests.get_credentials(dev_os_env_file=dev_os_env_file)
52
53         self.ext_net_name = ext_net_name
54
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')
58
59         self.image_metadata = image_metadata
60
61     @staticmethod
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'.
65         """
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))
71         return suite
72
73
74 class OSIntegrationTestCase(OSComponentTestCase):
75
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):
78         """
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
85                                images are required.
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)
91         """
92         super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_creds=os_creds,
93                                                     ext_net_name=ext_net_name, image_metadata=image_metadata,
94                                                     log_level=log_level)
95         self.use_keystone = use_keystone
96         self.keystone = None
97         self.flavor_metadata = flavor_metadata
98
99     @staticmethod
100     def parameterize(testcase_klass, os_creds, ext_net_name, use_keystone=False, flavor_metadata=None,
101                      image_metadata=None, log_level=logging.DEBUG):
102         """
103         Create a suite containing all tests taken from the given
104         subclass, passing them the parameter 'param'.
105         """
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,
111                                          log_level))
112         return suite
113
114     """
115     Super for test classes that should be run within their own project/tenant as they can run for quite some time
116     """
117     def __start__(self):
118         """
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
121         """
122         self.project_creator = None
123         self.user_creator = None
124         self.admin_os_creds = self.os_creds
125         self.role = None
126
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))
132
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)
136
137             # add user to project
138             self.project_creator.assoc_user(self.user_creator.get_user())
139
140     def __clean__(self):
141         """
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
145         """
146         if self.role:
147             keystone_utils.delete_role(self.keystone, self.role)
148
149         if self.project_creator:
150             self.project_creator.clean()
151
152         if self.user_creator:
153             self.user_creator.clean()