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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
"""
Copyright 2014 Rackspace
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 abc
import fixtures
import logging
import os
from oslo_utils import uuidutils
import oslotest.base as oslotest
from testtools import testcase
from barbican.tests import fixture as barbican_fixture
from barbican.tests import utils
from functionaltests.common import client
from functionaltests.common import config
CONF = config.get_config()
conf_host_href_used = CONF.keymanager.server_host_href_set
conf_multiple_backends_enabled = CONF.keymanager.\
server_multiple_backends_enabled
class TestCase(oslotest.BaseTestCase):
max_payload_size = 20000
max_sized_payload = b'a' * max_payload_size
oversized_payload = b'a' * (max_payload_size + 1)
max_field_size = 255
max_sized_field = 'a' * max_field_size
oversized_field = 'a' * (max_field_size + 1)
log_format = ('%(asctime)s %(process)d %(levelname)-8s '
'[%(name)s] %(message)s')
@classmethod
def setUpClass(cls):
cls.LOG = logging.getLogger(cls._get_full_case_name())
super().setUpClass()
def setUp(self):
self.LOG.info('Starting: %s', self._testMethodName)
super().setUp()
self.client = client.BarbicanClient()
stdout_capture = os.environ.get('OS_STDOUT_CAPTURE')
stderr_capture = os.environ.get('OS_STDERR_CAPTURE')
log_capture = os.environ.get('OS_LOG_CAPTURE')
if ((stdout_capture and stdout_capture.lower() == 'true') or
stdout_capture == '1'):
stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
if ((stderr_capture and stderr_capture.lower() == 'true') or
stderr_capture == '1'):
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
if ((log_capture and log_capture.lower() == 'true') or
log_capture == '1'):
self.useFixture(fixtures.LoggerFixture(nuke_handlers=False,
format=self.log_format,
level=logging.DEBUG))
self.useFixture(barbican_fixture.WarningsFixture())
def tearDown(self):
super().tearDown()
self.LOG.info('Finished: %s\n', self._testMethodName)
@classmethod
def _get_full_case_name(cls):
name = '{module}:{case_name}'.format(
module=cls.__module__,
case_name=cls.__name__
)
return name
class PagingTestCase(TestCase, metaclass=abc.ABCMeta):
def setUp(self):
# super(PagingTestCase, self).setUp()
# self._all_fetched_resources = []
self.skipTest("paging tests temporarily disabled")
def tearDown(self):
super(PagingTestCase, self).tearDown()
def _set_filter_field(self, model):
filter = uuidutils.generate_uuid()
self.set_filter_field(filter, model)
return filter
def _validate_resource_group(self, resources=[], next_ref=None,
prev_ref=None,
expected_size=0,
next_ref_should_be_none=True,
prev_ref_should_be_none=True):
"""Validate the returned group of resources.
Will check for:
1. there is a returned group (ie not None)
2. size of the returned group
3. no duplicates within the returned group
4. no duplicates across multiple calls
5. valid next resource ref
6. valid previous resource ref
:param resources: the list of resources
:param expected_size: the expected size of the list
:param next_ref: next href
:param prev_ref: previous href
:param next_ref_should_be_none: should next href be none?
:param next_ref_should_be_none: should prev href be none?
:param all_fetched_resources: running list of all resources (used to
detect duplicates across multiple calls)
"""
self.assertIsNotNone(resources)
self.assertEqual(expected_size, len(resources))
self.assertEqual(next_ref_should_be_none, next_ref is None)
self.assertEqual(prev_ref_should_be_none, prev_ref is None)
# check for duplicates within this group
self.assertEqual(len(resources), len(set(resources)))
# check for duplicates across calls
if len(self._all_fetched_resources):
duplicates = [entity for entity in resources if entity in
self._all_fetched_resources]
self.assertEqual(0, len(duplicates))
# add to our running list of resource refs
self._all_fetched_resources.extend(resources)
@abc.abstractmethod
def create_model(self):
pass
@abc.abstractmethod
def create_resources(self, count=0, model=None):
pass
@abc.abstractmethod
def get_resources(self, limit=10, offset=0, filter=""):
pass
@abc.abstractmethod
def set_filter_field(self, filter, model):
pass
@testcase.attr('positive')
def test_paging_with_limits_and_offsets(self):
"""Covers resource paging limit and offset attributes."""
test_model = self.create_model()
number_of_resource_groups = 5
resources_per_group = 10
filter = self._set_filter_field(test_model)
# create a number of resources
self.create_resources(
count=number_of_resource_groups * resources_per_group,
model=test_model)
# validate all groups of resources
for i in range(1, number_of_resource_groups + 1):
resp, resources, next_ref, prev_ref = self.get_resources(
limit=resources_per_group,
offset=(i - 1) * resources_per_group,
filter=filter)
self.assertEqual(200, resp.status_code)
check_next = i == number_of_resource_groups
check_prev = i == 1
self._validate_resource_group(resources=resources,
next_ref=next_ref, prev_ref=prev_ref,
expected_size=resources_per_group,
next_ref_should_be_none=check_next,
prev_ref_should_be_none=check_prev)
@testcase.attr('positive')
def test_paging_with_offset_zero_and_varying_limits(self):
"""Covers listing resources with limit attribute.
Use limits from 1 to twice the number of resources we expect. Always
use offset=0 so we start from the beginning.
"""
res_count = 25
test_model = self.create_model()
filter = self._set_filter_field(test_model)
self.create_resources(count=res_count, model=test_model)
minimum_limit = 1
maximum_limit = res_count * 2
offset = 0
for limit in range(minimum_limit, maximum_limit):
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self.assertEqual(200, resp.status_code)
check_next = limit >= res_count
check_prev = offset == 0
self._validate_resource_group(resources=resources,
next_ref=next_ref, prev_ref=prev_ref,
expected_size=min(limit, res_count),
next_ref_should_be_none=check_next,
prev_ref_should_be_none=check_prev)
@testcase.attr('positive')
def test_paging_exceeding_paging_max_limit(self):
"""Covers case of listing resources with a limit that exceeds max.
Create a number of resources over the max paging limit, then try
to get them all in one call. It should only return the max, with
a next link to get the rest.
"""
max_allowable_limit = 100
number_of_resources = max_allowable_limit + 10
test_model = self.create_model()
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
resp, resources, next_ref, prev_ref = self.get_resources(
limit=number_of_resources, offset=0, filter=filter)
self.assertEqual(200, resp.status_code)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=max_allowable_limit,
next_ref_should_be_none=False,
prev_ref_should_be_none=True)
limit, offset = utils.get_limit_and_offset_from_ref(next_ref)
# new offset and limit should both be the same as the max limit
self.assertEqual(str(max_allowable_limit), limit)
self.assertEqual(str(max_allowable_limit), offset)
# now get the rest
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self.assertEqual(200, resp.status_code)
expected_size = number_of_resources - max_allowable_limit
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=expected_size,
next_ref_should_be_none=True,
prev_ref_should_be_none=False)
@testcase.attr('positive')
def test_paging_next_option_start_in_middle(self):
"""Covers getting a list of resources and using the next reference."""
number_of_resources = 150
test_model = self.create_model()
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
# First set of resources
limit = number_of_resources // 10
offset = number_of_resources // 2
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self.assertEqual(200, resp.status_code)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=limit,
next_ref_should_be_none=False,
prev_ref_should_be_none=False)
limit, offset = utils.get_limit_and_offset_from_ref(next_ref)
# Next set of resources
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=int(limit),
next_ref_should_be_none=False,
prev_ref_should_be_none=False)
@testcase.attr('positive')
def test_paging_with_default_limit_and_varying_offsets(self):
"""Covers listing resources with various offsets.
Use offsets from 0 to the number of resources we expect. Always
use default limit.
"""
number_of_resources = 15
test_model = self.create_model()
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
minimum_offset = 0
maximum_offset = number_of_resources
limit = 10
for offset in range(minimum_offset, maximum_offset):
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self.assertEqual(200, resp.status_code)
check_next = offset + limit >= number_of_resources
check_prev = offset == 0
expected_size = min(limit, number_of_resources - offset)
self._validate_resource_group(resources=resources,
next_ref=next_ref,
prev_ref=prev_ref,
expected_size=expected_size,
next_ref_should_be_none=check_next,
prev_ref_should_be_none=check_prev)
@testcase.attr('positive')
def test_resources_get_paging_prev_option_start_in_middle(self):
"""Covers getting a list of resources and using the next reference."""
number_of_resources = 150
test_model = self.create_model()
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
# First set of resources
limit = number_of_resources // 10
offset = number_of_resources // 2
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self.assertEqual(200, resp.status_code)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=limit,
next_ref_should_be_none=False,
prev_ref_should_be_none=False)
limit, offset = utils.get_limit_and_offset_from_ref(prev_ref)
# Previous set of resources
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit, offset=offset, filter=filter)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref,
expected_size=int(limit),
next_ref_should_be_none=False,
prev_ref_should_be_none=False)
@testcase.attr('positive')
def test_paging_with_non_integer_limits_and_offsets(self):
"""Covers resource paging limit and offset attributes."""
test_model = self.create_model()
number_of_resources = 25
# create a number of resources
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
# pass in non-integer values for limit and offset
resp, resources, next_ref, prev_ref = self.get_resources(
limit='not-an-int-limit',
offset='not-an-int-offset', filter=filter)
self.assertEqual(200, resp.status_code)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref, expected_size=10,
next_ref_should_be_none=False,
prev_ref_should_be_none=True)
@testcase.attr('positive')
def test_paging_with_default_limit_and_large_offsets(self):
"""Covers resource paging limit and offset attributes."""
test_model = self.create_model()
number_of_resources = 25
# create a number of resources
filter = self._set_filter_field(test_model)
self.create_resources(count=number_of_resources, model=test_model)
large_offset = 265613988875874769338781322035779626829233452653394495
limit = 10
# pass in non-integer values for limit and offset
resp, resources, next_ref, prev_ref = self.get_resources(
limit=limit,
offset=large_offset, filter=filter)
self.assertEqual(200, resp.status_code)
self._validate_resource_group(resources=resources, next_ref=next_ref,
prev_ref=prev_ref, expected_size=10,
next_ref_should_be_none=False,
prev_ref_should_be_none=True)
|