Merge "Renamed test application and added new command line arguments."
[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 unittest
16 import uuid
17
18 from snaps import file_utils
19 import openstack_tests
20 import logging
21
22 from snaps.openstack.create_project import ProjectSettings
23 from snaps.openstack.create_user import UserSettings
24 from snaps.openstack.utils import deploy_utils, keystone_utils
25
26 dev_os_env_file = 'openstack/tests/conf/os_env.yaml'
27
28 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29 # To run these tests from an IDE, the CWD must be set to the snaps directory of this project
30 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31
32
33 class OSComponentTestCase(unittest.TestCase):
34
35     """
36     Super for test classes requiring a connection to OpenStack
37     """
38     def __init__(self, method_name='runTest', os_env_file=None, ext_net_name=None, http_proxy_str=None,
39                  ssh_proxy_cmd=None, log_level=logging.DEBUG):
40         super(OSComponentTestCase, self).__init__(method_name)
41
42         logging.basicConfig(level=log_level)
43
44         self.os_creds = openstack_tests.get_credentials(os_env_file=os_env_file, proxy_settings_str=http_proxy_str,
45                                                         ssh_proxy_cmd=ssh_proxy_cmd, dev_os_env_file=dev_os_env_file)
46         self.ext_net_name = ext_net_name
47
48         if not self.ext_net_name and file_utils.file_exists(dev_os_env_file):
49             test_conf = file_utils.read_yaml(dev_os_env_file)
50             self.ext_net_name = test_conf.get('ext_net')
51
52     @staticmethod
53     def parameterize(testcase_klass, os_env_file, ext_net_name, http_proxy_str=None, ssh_proxy_cmd=None,
54                      log_level=logging.DEBUG):
55         """ Create a suite containing all tests taken from the given
56             subclass, passing them the parameter 'param'.
57         """
58         test_loader = unittest.TestLoader()
59         test_names = test_loader.getTestCaseNames(testcase_klass)
60         suite = unittest.TestSuite()
61         for name in test_names:
62             suite.addTest(testcase_klass(name, os_env_file, ext_net_name, http_proxy_str, ssh_proxy_cmd, log_level))
63         return suite
64
65
66 class OSIntegrationTestCase(OSComponentTestCase):
67
68     """
69     Super for test classes requiring a connection to OpenStack
70     """
71     def __init__(self, method_name='runTest', os_env_file=None, ext_net_name=None, http_proxy_str=None,
72                  ssh_proxy_cmd=None, use_keystone=False, flavor_metadata=None, image_metadata=None,
73                  log_level=logging.DEBUG):
74         super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_env_file=os_env_file,
75                                                     ext_net_name=ext_net_name, http_proxy_str=http_proxy_str,
76                                                     ssh_proxy_cmd=ssh_proxy_cmd, log_level=log_level)
77         self.use_keystone = use_keystone
78         self.keystone = None
79         self.flavor_metadata = flavor_metadata
80         self.image_metadata = image_metadata
81
82     @staticmethod
83     def parameterize(testcase_klass, os_env_file, ext_net_name, http_proxy_str=None, ssh_proxy_cmd=None,
84                      use_keystone=False, flavor_metadata=None, image_metadata=None, log_level=logging.DEBUG):
85         """ Create a suite containing all tests taken from the given
86             subclass, passing them the parameter 'param'.
87         """
88         test_loader = unittest.TestLoader()
89         test_names = test_loader.getTestCaseNames(testcase_klass)
90         suite = unittest.TestSuite()
91         for name in test_names:
92             suite.addTest(testcase_klass(name, os_env_file, ext_net_name, http_proxy_str, ssh_proxy_cmd, use_keystone,
93                                          flavor_metadata, image_metadata, log_level))
94         return suite
95
96     """
97     Super for test classes that should be run within their own project/tenant as they can run for quite some time
98     """
99     def __start__(self):
100         """
101         Creates a project and user to be leveraged by subclass test methods. If implementing class uses this method,
102         it must call __clean__() else you will be left with unwanted users and tenants
103         """
104         self.project_creator = None
105         self.user_creator = None
106         self.admin_os_creds = self.os_creds
107         self.role = None
108
109         if self.use_keystone:
110             self.keystone = keystone_utils.keystone_client(self.os_creds)
111             guid = self.__class__.__name__ + '-' + str(uuid.uuid4())[:-19]
112             project_name = guid + '-proj'
113             self.project_creator = deploy_utils.create_project(self.admin_os_creds, ProjectSettings(name=project_name))
114
115             self.user_creator = deploy_utils.create_user(
116                 self.admin_os_creds, UserSettings(name=guid + '-user', password=guid, project_name=project_name))
117             self.os_creds = self.user_creator.get_os_creds(self.project_creator.project_settings.name)
118
119             # add user to project
120             self.project_creator.assoc_user(self.user_creator.get_user())
121
122     def __clean__(self):
123         """
124         Cleans up test user and project.
125         Must be called at the end of child classes tearDown() if __start__() is called during setUp() else these
126         objects will persist after the test is run
127         """
128         if self.role:
129             keystone_utils.delete_role(self.keystone, self.role)
130
131         if self.project_creator:
132             self.project_creator.clean()
133
134         if self.user_creator:
135             self.user_creator.clean()