Use neutron to create floating IPs.
[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.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
25
26
27 dev_os_env_file = pkg_resources.resource_filename('snaps.openstack.tests.conf', 'os_env.yaml')
28
29 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
31 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32
33
34 class OSComponentTestCase(unittest.TestCase):
35
36     def __init__(self, method_name='runTest', os_creds=None, ext_net_name=None, image_metadata=None,
37                  log_level=logging.DEBUG):
38         """
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)
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(dev_os_env_file=dev_os_env_file)
54
55         self.ext_net_name = ext_net_name
56
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')
60
61         self.image_metadata = image_metadata
62
63     @staticmethod
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'.
67         """
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))
73         return suite
74
75
76 class OSIntegrationTestCase(OSComponentTestCase):
77
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):
80         """
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
87                                images are required.
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)
93         """
94         super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_creds=os_creds,
95                                                     ext_net_name=ext_net_name, image_metadata=image_metadata,
96                                                     log_level=log_level)
97         self.use_keystone = use_keystone
98         self.keystone = None
99         self.flavor_metadata = flavor_metadata
100
101     @staticmethod
102     def parameterize(testcase_klass, os_creds, ext_net_name, use_keystone=False, flavor_metadata=None,
103                      image_metadata=None, log_level=logging.DEBUG):
104         """
105         Create a suite containing all tests taken from the given
106         subclass, passing them the parameter 'param'.
107         """
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,
113                                          log_level))
114         return suite
115
116     """
117     Super for test classes that should be run within their own project/tenant as they can run for quite some time
118     """
119     def __start__(self):
120         """
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
123         """
124         self.project_creator = None
125         self.user_creator = None
126         self.admin_os_creds = self.os_creds
127         self.role = None
128
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))
134
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)
138
139             # add user to project
140             self.project_creator.assoc_user(self.user_creator.get_user())
141
142     def __clean__(self):
143         """
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
147         """
148         if self.role:
149             keystone_utils.delete_role(self.keystone, self.role)
150
151         if self.project_creator:
152             self.project_creator.clean()
153
154         if self.user_creator:
155             self.user_creator.clean()