File: base.py

package info (click to toggle)
python-openstacksdk 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,352 kB
  • sloc: python: 122,960; sh: 153; makefile: 23
file content (307 lines) | stat: -rw-r--r-- 10,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import operator
import os
import time
import uuid

from keystoneauth1 import discover

import openstack.config
from openstack import connection
from openstack.tests import base


#: Defines the OpenStack Client Config (OCC) cloud key in your OCC config
#: file, typically in $HOME/.config/openstack/clouds.yaml. That configuration
#: will determine where the functional tests will be run and what resource
#: defaults will be used to run the functional tests.
TEST_CONFIG = openstack.config.OpenStackConfig()
TEST_CLOUD_NAME = os.getenv('OS_CLOUD', 'devstack-admin')
TEST_CLOUD_REGION = openstack.config.get_cloud_region(cloud=TEST_CLOUD_NAME)


def _get_resource_value(resource_key):
    return TEST_CONFIG.get_extra_config('functional').get(resource_key)


def _disable_keep_alive(conn):
    sess = conn.config.get_session()
    sess.keep_alive = False


class BaseFunctionalTest(base.TestCase):
    user_cloud: connection.Connection
    user_cloud_alt: connection.Connection
    operator_cloud: connection.Connection

    _wait_for_timeout_key = ''

    def setUp(self):
        super().setUp()
        self.conn = connection.Connection(config=TEST_CLOUD_REGION)
        _disable_keep_alive(self.conn)

        self._demo_name = os.environ.get('OPENSTACKSDK_DEMO_CLOUD', 'devstack')
        if not self._demo_name:
            raise self.failureException(
                "OPENSTACKSDK_OPERATOR_CLOUD must be set to a non-empty value"
            )

        self._demo_name_alt = os.environ.get(
            'OPENSTACKSDK_DEMO_CLOUD_ALT',
            'devstack-alt',
        )
        if not self._demo_name_alt:
            raise self.failureException(
                "OPENSTACKSDK_OPERATOR_CLOUD must be set to a non-empty value"
            )

        self._op_name = os.environ.get(
            'OPENSTACKSDK_OPERATOR_CLOUD',
            'devstack-admin',
        )
        if not self._op_name:
            raise self.failureException(
                "OPENSTACKSDK_OPERATOR_CLOUD must be set to a non-empty value"
            )

        self.config = openstack.config.OpenStackConfig()
        self._set_user_cloud()
        self._set_operator_cloud()

        self.identity_version = self.user_cloud.config.get_api_version(
            'identity'
        )

        self.flavor = self._pick_flavor()
        self.image = self._pick_image()

        # Defines default timeout for wait_for methods used
        # in the functional tests
        self._wait_for_timeout = int(
            os.getenv(
                self._wait_for_timeout_key,
                os.getenv('OPENSTACKSDK_FUNC_TEST_TIMEOUT', 300),
            )
        )

    def _set_user_cloud(self, **kwargs):
        user_config = self.config.get_one(cloud=self._demo_name, **kwargs)
        self.user_cloud = connection.Connection(config=user_config)
        _disable_keep_alive(self.user_cloud)

        user_config_alt = self.config.get_one(
            cloud=self._demo_name_alt, **kwargs
        )
        self.user_cloud_alt = connection.Connection(config=user_config_alt)
        _disable_keep_alive(self.user_cloud_alt)

    def _set_operator_cloud(self, **kwargs):
        operator_config = self.config.get_one(cloud=self._op_name, **kwargs)
        self.operator_cloud = connection.Connection(config=operator_config)
        _disable_keep_alive(self.operator_cloud)

    def _pick_flavor(self):
        """Pick a sensible flavor to run tests with.

        This returns None if the compute service is not present (e.g.
        ironic-only deployments).
        """
        if not self.user_cloud.has_service('compute'):
            return None

        flavors = self.user_cloud.list_flavors(get_extra=False)
        # self.add_info_on_exception('flavors', flavors)

        flavor_name = os.environ.get('OPENSTACKSDK_FLAVOR')

        if not flavor_name:
            flavor_name = _get_resource_value('flavor_name')

        if flavor_name:
            for flavor in flavors:
                if flavor.name == flavor_name:
                    return flavor

            raise self.failureException(
                "Cloud does not have flavor '%s'",
                flavor_name,
            )

        # Enable running functional tests against RAX, which requires
        # performance flavors be used for boot from volume

        for flavor in sorted(flavors, key=operator.attrgetter('ram')):
            if 'performance' in flavor.name:
                return flavor

        # Otherwise, pick the smallest flavor with a ephemeral disk configured

        for flavor in sorted(flavors, key=operator.attrgetter('ram')):
            if flavor.disk:
                return flavor

        raise self.failureException('No sensible flavor found')

    def _pick_image(self):
        """Pick a sensible image to run tests with.

        This returns None if the image service is not present.
        """
        if not self.user_cloud.has_service('image'):
            return None

        images = self.user_cloud.list_images()
        # self.add_info_on_exception('images', images)

        image_name = os.environ.get('OPENSTACKSDK_IMAGE')

        if not image_name:
            image_name = _get_resource_value('image_name')

        if image_name:
            for image in images:
                if image.name == image_name:
                    return image

            raise self.failureException(
                "Cloud does not have image '%s'",
                image_name,
            )

        for image in images:
            if image.name.startswith('cirros') and image.name.endswith('-uec'):
                return image

        for image in images:
            if (
                image.name.startswith('cirros')
                and image.disk_format == 'qcow2'
            ):
                return image

        for image in images:
            if image.name.lower().startswith('ubuntu'):
                return image
        for image in images:
            if image.name.lower().startswith('centos'):
                return image

        raise self.failureException('No sensible image found')

    def addEmptyCleanup(self, func, *args, **kwargs):
        def cleanup():
            result = func(*args, **kwargs)
            self.assertIsNone(result)

        self.addCleanup(cleanup)

    def require_service(self, service_type, min_microversion=None, **kwargs):
        """Method to check whether a service exists

        Usage::

            class TestMeter(base.BaseFunctionalTest):
                def setUp(self):
                    super(TestMeter, self).setUp()
                    self.require_service('metering')

        :returns: True if the service exists, otherwise False.
        """
        if not self.conn.has_service(service_type):
            self.skipTest(f'Service {service_type} not found in cloud')

        if not min_microversion:
            return

        data = self.conn.session.get_endpoint_data(
            service_type=service_type, **kwargs
        )

        if not (
            data.min_microversion
            and data.max_microversion
            and discover.version_between(
                data.min_microversion,
                data.max_microversion,
                min_microversion,
            )
        ):
            self.skipTest(
                f'Service {service_type} does not provide microversion '
                f'{min_microversion}'
            )

    def getUniqueString(self, prefix=None):
        """Generate unique resource name"""
        # Globally unique names can only rely on some form of uuid
        # unix_t is also used to easier determine orphans when running real
        # functional tests on a real cloud
        return (
            prefix if prefix else ''
        ) + f"{int(time.time())}-{uuid.uuid4().hex}"

    def create_temporary_project(self):
        """Create a new temporary project.

        This is useful for tests that modify things like quotas, which would
        cause issues for other tests.
        """
        project_name = self.getUniqueString('project-')
        project = self.operator_cloud.get_project(project_name)
        if not project:
            params = {
                'name': project_name,
                'description': f'Temporary project created for {self.id()}',
                # assume identity API v3 for now
                'domain_id': self.operator_cloud.get_domain('default')['id'],
            }
            project = self.operator_cloud.create_project(**params)

        # Grant the current user access to the project
        user_id = self.operator_cloud.current_user_id
        role_assignment = self.operator_cloud.list_role_assignments(
            {'user': user_id, 'project': project['id']}
        )
        if not role_assignment:
            self.operator_cloud.grant_role(
                'member', user=user_id, project=project['id'], wait=True
            )

        self.addCleanup(self._delete_temporary_project, project)

        return project

    def _delete_temporary_project(self, project):
        self.operator_cloud.revoke_role(
            'member',
            user=self.operator_cloud.current_user_id,
            project=project.id,
        )
        self.operator_cloud.delete_project(project.id)


class KeystoneBaseFunctionalTest(BaseFunctionalTest):
    def setUp(self):
        super().setUp()

        use_keystone_v2 = os.environ.get('OPENSTACKSDK_USE_KEYSTONE_V2', False)
        if use_keystone_v2:
            # keystone v2 has special behavior for the admin
            # interface and some of the operations, so make a new cloud
            # object with interface set to admin.
            # We only do it for keystone tests on v2 because otherwise
            # the admin interface is not a thing that wants to actually
            # be used
            self._set_operator_cloud(interface='admin')