Debian Codegraph @

magnum_tempest_plugin/tests/api/base.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import inspect
14 import logging
15
16 from tempest.common import credentials_factory as common_creds
17
18 from magnum_tempest_plugin.common import base
19 from magnum_tempest_plugin.common import config
20 from magnum_tempest_plugin.common import manager
21
22
23 COPY_LOG_HELPER = "magnum/tests/contrib/copy_instance_logs.sh"
24
25
26 class BaseTempestTest(base.BaseMagnumTest):
27 """Sets up configuration required for functional tests"""
28
29 ic_class_list = []
30 ic_method_list = []
31 LOG = logging.getLogger(__name__)
32
33 def __init__(self, *args, **kwargs):
34 super(BaseTempestTest, self).__init__(*args, **kwargs)
35
36 @classmethod
37 def setUpClass(cls):
38 super(BaseTempestTest, cls).setUpClass()
39 config.Config.setUp()
40
41 @classmethod
42 def tearDownClass(cls):
43 super(BaseTempestTest, cls).tearDownClass()
44 cls.clear_credentials(clear_class_creds=True)
45
46 def tearDown(self):
47 super(BaseTempestTest, self).tearDown()
48 self.clear_credentials(clear_method_creds=True)
49
50 @classmethod
51 def clear_credentials(cls,
52 clear_class_creds=False,
53 clear_method_creds=False):
54 if clear_class_creds:
55 for ic in cls.ic_class_list:
56 ic.clear_creds()
57 if clear_method_creds:
58 for ic in cls.ic_method_list:
59 ic.clear_creds()
60
61 @classmethod
62 def get_credentials(cls, name=None,
63 type_of_creds="default",
64 class_cleanup=False):
65 (creds, _) = cls.get_credentials_with_keypair(name, type_of_creds,
66 class_cleanup)
67 return creds
68
69 @classmethod
70 def get_credentials_with_keypair(cls, name=None,
71 type_of_creds="default",
72 class_cleanup=False):
73 if name is None:
74 # Get name of test method
75 name = inspect.stack()[1][3]
76 if len(name) > 32:
77 name = name[0:32]
78
79 # Choose type of isolated creds
80 ic = common_creds.get_credentials_provider(
81 name,
82 identity_version=config.Config.auth_version
83 )
84
85 if class_cleanup:
86 cls.ic_class_list.append(ic)
87 else:
88 cls.ic_method_list.append(ic)
89
90 creds = None
91 if "admin" == type_of_creds:
92 creds = ic.get_admin_creds()
93 elif "alt" == type_of_creds:
94 creds = ic.get_alt_creds()
95 elif "default" == type_of_creds:
96 creds = ic.get_primary_creds()
97 else:
98 creds = ic.get_credentials(type_of_creds)
99
100 keypair = None
101
102 if config.Config.keypair_name:
103 _, keypairs_client = cls.get_clients(creds, type_of_creds,
104 'keypair_setup')
105 try:
106 keypairs_client.delete_keypair(config.Config.keypair_name)
107 except Exception:
108 cls.LOG.debug("Creating keypair %s",
109 config.Config.keypair_name)
110 keypair_body = keypairs_client.create_keypair(
111 name=config.Config.keypair_name
112 )
113
114 cls.LOG.debug("Keypair body: %s", keypair_body)
115 keypair = keypair_body['keypair']['private_key']
116
117 return (creds, keypair)
118
119 @classmethod
120 def get_clients(cls, creds, type_of_creds, request_type):
121 if "admin" == type_of_creds:
122 manager_inst = manager.AdminManager(credentials=creds.credentials,
123 request_type=request_type)
124 elif "alt" == type_of_creds:
125 manager_inst = manager.AltManager(credentials=creds.credentials,
126 request_type=request_type)
127 elif "default" == type_of_creds:
128 manager_inst = manager.DefaultManager(
129 credentials=creds.credentials, request_type=request_type)
130 else:
131 manager_inst = manager.DefaultManager(
132 credentials=creds.credentials, request_type=request_type)
133
134 # create client with isolated creds
135 return (manager_inst.client, manager_inst.keypairs_client)
136
137 @classmethod
138 def get_clients_with_existing_creds(cls,
139 name=None,
140 creds=None,
141 type_of_creds="default",
142 request_type=None,
143 class_cleanup=False):
144 if creds is None:
145 return cls.get_clients_with_new_creds(name,
146 type_of_creds,
147 request_type,
148 class_cleanup)
149 else:
150 return cls.get_clients(creds, type_of_creds, request_type)
151
152 @classmethod
153 def get_clients_with_new_creds(cls,
154 name=None,
155 type_of_creds="default",
156 request_type=None,
157 class_cleanup=False):
158 """Creates isolated creds.
159
160 :param name: name, will be used for dynamic creds
161 :param type_of_creds: admin, alt or default
162 :param request_type: ClusterTemplate or service
163 :returns: MagnumClient -- client with isolated creds.
164 :returns: KeypairClient -- allows for creating of keypairs
165 """
166 creds = cls.get_credentials(name, type_of_creds, class_cleanup)
167 return cls.get_clients(creds, type_of_creds, request_type)