cinder/cinder/tests/unit/policies/test_base.py
Eric Harney b83c446e52 Tests: Make tests less random
Remove a bunch of uuid.uuid4() calls from tests, and use static UUIDs
instead.

We generally use static UUIDs to make test runs reproducible and to make
it easier to track odd failures that repeat in the gate.

Readability of tests is also improved by using meaningful names like
VOLUME_ID or SNAPSHOT_ID rather than just uuid4().

So, do more of that.

Some care has to be taken in test classes that create lots of volumes,
such as BackupBaseDriverTestCase, because random UUIDs were previously
allowing volumes used by different tests to not collide with each other.
More static IDs have been added to account for this.

Change-Id: I372d27367b730f4606bc0ea84a9b322d45d1cbbe
2020-05-08 11:22:02 -04:00

90 lines
3.2 KiB
Python

#
# 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.
from oslo_serialization import jsonutils
import webob
from cinder.api import microversions as mv
from cinder import context as cinder_context
from cinder import objects
from cinder.tests.unit.api import fakes
from cinder.tests.unit import fake_constants
from cinder.tests.unit.image import fake as fake_image
from cinder.tests.unit import test
class CinderPolicyTests(test.TestCase):
def setUp(self):
super(CinderPolicyTests, self).setUp()
self.project_id = fake_constants.PROJECT_ID
self.other_project_id = fake_constants.PROJECT2_ID
self.admin_context = cinder_context.RequestContext(
user_id=fake_constants.USER_ID, project_id=self.project_id,
roles=['admin']
)
self.user_context = cinder_context.RequestContext(
user_id=fake_constants.USER2_ID, project_id=self.project_id,
roles=['non-admin']
)
self.other_user_context = cinder_context.RequestContext(
user_id=fake_constants.USER3_ID, project_id=self.other_project_id,
roles=['non-admin']
)
fake_image.mock_image_service(self)
def _get_request_response(self, context, path, method, body=None,
microversion=mv.BASE_VERSION):
request = webob.Request.blank(path)
request.content_type = 'application/json'
request.headers = mv.get_mv_header(microversion)
request.method = method
if body:
request.headers["content-type"] = "application/json"
request.body = jsonutils.dump_as_bytes(body)
return request.get_response(
fakes.wsgi_app(fake_auth_context=context)
)
def _create_fake_volume(self, context, status=None, attach_status=None,
metadata=None, admin_metadata=None):
vol = {
'display_name': 'fake_volume1',
'status': 'available',
'project_id': context.project_id
}
if status:
vol['status'] = status
if attach_status:
vol['attach_status'] = attach_status
if metadata:
vol['metadata'] = metadata
if admin_metadata:
vol['admin_metadata'] = admin_metadata
volume = objects.Volume(context=context, **vol)
volume.create()
return volume
def _create_fake_type(self, context):
vol_type = {
'name': 'fake_volume1',
'extra_specs': {},
'is_public': True,
'projects': [],
'description': 'A fake volume type'
}
volume_type = objects.VolumeType(context=context, **vol_type)
volume_type.create()
return volume_type