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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
# 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 uuid
from osc_placement.tests.functional import base
class TestAllocation(base.BaseTestCase):
def setUp(self):
super(TestAllocation, self).setUp()
self.rp1 = self.resource_provider_create()
self.resource_inventory_set(
self.rp1['uuid'], 'VCPU=4', 'MEMORY_MB=1024')
def test_allocation_show_not_found(self):
consumer_uuid = str(uuid.uuid4())
result = self.resource_allocation_show(consumer_uuid)
self.assertEqual([], result)
def test_allocation_create(self):
consumer_uuid = str(uuid.uuid4())
created_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])]
)
retrieved_alloc = self.resource_allocation_show(consumer_uuid)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 2,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc)
# Test that specifying --project-id and --user-id before microversion
# 1.8 does not result in an error but display a warning.
output, warning = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
project_id='fake-project', user_id='fake-user',
may_print_to_stderr=True)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 3,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, output)
self.assertIn(
'--project-id and --user-id options do not affect allocation for '
'--os-placement-api-version less than 1.8', warning)
# Test that specifying --consumer-type before microversion 1.38 does
# not result in an error but display a warning.
output, warning = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
consumer_type='fake-type', may_print_to_stderr=True)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 4,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, output)
self.assertIn(
'--consumer-type option does not affect allocation for '
'--os-placement-api-version less than 1.38', warning)
def test_allocation_create_empty(self):
consumer_uuid = str(uuid.uuid4())
exc = self.assertRaises(base.CommandException,
self.resource_allocation_set,
consumer_uuid, [])
self.assertIn('At least one resource allocation must be specified',
str(exc))
def test_allocation_delete(self):
consumer_uuid = str(uuid.uuid4())
self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])]
)
self.assertTrue(self.resource_allocation_show(consumer_uuid))
self.resource_allocation_delete(consumer_uuid)
self.assertEqual([], self.resource_allocation_show(consumer_uuid))
def test_allocation_delete_not_found(self):
consumer_uuid = str(uuid.uuid4())
msg = "No allocations for consumer '{}'".format(consumer_uuid)
exc = self.assertRaises(base.CommandException,
self.resource_allocation_delete, consumer_uuid)
self.assertIn(msg, str(exc))
class TestAllocation18(base.BaseTestCase):
VERSION = '1.8'
def test_allocation_create(self):
consumer_uuid = str(uuid.uuid4())
project_id = str(uuid.uuid4())
user_id = str(uuid.uuid4())
rp1 = self.resource_provider_create()
self.resource_inventory_set(
rp1['uuid'],
'VCPU=4',
'VCPU:max_unit=4',
'MEMORY_MB=1024',
'MEMORY_MB:max_unit=1024')
created_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(rp1['uuid']),
'rp={},MEMORY_MB=512'.format(rp1['uuid'])],
project_id=project_id, user_id=user_id
)
retrieved_alloc = self.resource_allocation_show(consumer_uuid)
expected = [
{'resource_provider': rp1['uuid'],
'generation': 2,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc)
class TestAllocation112(base.BaseTestCase):
VERSION = '1.12'
def setUp(self):
super(TestAllocation112, self).setUp()
self.rp1 = self.resource_provider_create()
self.resource_inventory_set(
self.rp1['uuid'], 'VCPU=4', 'MEMORY_MB=1024')
def test_allocation_update(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
created_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
project_id=project_uuid, user_id=user_uuid
)
retrieved_alloc = self.resource_allocation_show(consumer_uuid)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 2,
'project_id': project_uuid,
'user_id': user_uuid,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc)
def test_allocation_update_to_empty(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid'])],
project_id=project_uuid,
user_id=user_uuid,
)
result = self.resource_allocation_unset(
consumer_uuid, columns=("resources",))
self.assertEqual([], result)
def test_allocation_show_empty(self):
alloc = self.resource_allocation_show(
str(uuid.uuid4()), columns=('resources',))
self.assertEqual([], alloc)
class TestAllocation128(TestAllocation112):
"""Tests allocation set command with --os-placement-api-version 1.28.
The 1.28 microversion adds the consumer_generation parameter to the
GET and PUT /allocations/{consumer_id} APIs.
"""
VERSION = '1.28'
def test_allocation_update(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
# First create the initial set of allocations using rp1.
created_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
project_id=project_uuid, user_id=user_uuid
)
retrieved_alloc = self.resource_allocation_show(consumer_uuid)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 2,
'project_id': project_uuid,
'user_id': user_uuid,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc)
# Now update the allocations which should use the consumer generation.
updated_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=4'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=1024'.format(self.rp1['uuid'])],
project_id=project_uuid, user_id=user_uuid
)
expected[0].update({
'generation': expected[0]['generation'] + 1,
'resources': {'VCPU': 4, 'MEMORY_MB': 1024}
})
self.assertEqual(expected, updated_alloc)
class TestAllocation138(TestAllocation128):
"""Tests allocation set command with --os-placement-api-version 1.38.
The 1.38 microversion adds the consumer_type parameter to the
GET and PUT /allocations/{consumer_id} APIs
"""
VERSION = '1.38'
def test_allocation_update(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
# First create the initial set of allocations using rp1.
created_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
project_id=project_uuid, user_id=user_uuid,
consumer_type='INSTANCE'
)
retrieved_alloc = self.resource_allocation_show(consumer_uuid)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 2,
'project_id': project_uuid,
'user_id': user_uuid,
'resources': {'VCPU': 2, 'MEMORY_MB': 512},
'consumer_type': 'INSTANCE'}
]
self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc)
# Now update the allocations which should use the consumer generation.
updated_alloc = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=4'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=1024'.format(self.rp1['uuid'])],
project_id=project_uuid, user_id=user_uuid,
consumer_type='MIGRATION'
)
expected[0].update({
'generation': expected[0]['generation'] + 1,
'resources': {'VCPU': 4, 'MEMORY_MB': 1024},
'consumer_type': 'MIGRATION'
})
self.assertEqual(expected, updated_alloc)
def test_allocation_update_to_empty(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid'])],
project_id=project_uuid,
user_id=user_uuid,
consumer_type="INSTANCE",
)
result = self.resource_allocation_unset(
consumer_uuid, columns=('resources', 'consumer_type'))
self.assertEqual([], result)
class TestAllocationUnsetOldVersion(base.BaseTestCase):
def test_invalid_version(self):
"""Negative test to ensure the unset command requires >= 1.12."""
consumer_uuid = str(uuid.uuid4())
self.assertCommandFailed('requires at least version 1.12',
self.resource_allocation_unset, consumer_uuid)
class TestAllocationUnset112(base.BaseTestCase):
VERSION = '1.12'
def setUp(self):
super(TestAllocationUnset112, self).setUp()
# Create four providers with inventory.
self.rp1 = self.resource_provider_create()
self.rp2 = self.resource_provider_create()
self.rp3 = self.resource_provider_create()
self.rp4 = self.resource_provider_create()
self.resource_inventory_set(
self.rp1['uuid'], 'VCPU=4', 'MEMORY_MB=1024')
self.resource_inventory_set(
self.rp2['uuid'], 'VGPU=1')
self.resource_inventory_set(
self.rp3['uuid'], 'VCPU=4', 'MEMORY_MB=1024', 'VGPU=1')
self.resource_inventory_set(
self.rp4['uuid'], 'VCPU=4', 'MEMORY_MB=1024', 'VGPU=1')
self.consumer_uuid1 = str(uuid.uuid4())
self.consumer_uuid2 = str(uuid.uuid4())
self.project_uuid = str(uuid.uuid4())
self.user_uuid = str(uuid.uuid4())
# Create allocations against rp1 and rp2 for consumer1.
self.resource_allocation_set(
self.consumer_uuid1,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid']),
'rp={},VGPU=1'.format(self.rp2['uuid'])],
project_id=self.project_uuid, user_id=self.user_uuid)
# Create allocations against rp3 and rp4 for consumer1.
self.resource_allocation_set(
self.consumer_uuid2,
['rp={},VCPU=1'.format(self.rp3['uuid']),
'rp={},MEMORY_MB=256'.format(self.rp3['uuid']),
'rp={},VGPU=1'.format(self.rp3['uuid']),
'rp={},VCPU=1'.format(self.rp4['uuid']),
'rp={},MEMORY_MB=256'.format(self.rp4['uuid'])],
project_id=self.project_uuid, user_id=self.user_uuid)
def test_allocation_unset_one_provider(self):
"""Tests removing allocations for one specific provider."""
# Remove the allocation for rp1.
updated_allocs = self.resource_allocation_unset(
self.consumer_uuid1, provider=self.rp1['uuid'])
expected = [
{'resource_provider': self.rp2['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VGPU': 1}}
]
self.assertEqual(expected, updated_allocs)
def test_allocation_unset_one_resource_class(self):
"""Tests removing allocations for resource classes."""
updated_allocs = self.resource_allocation_unset(
self.consumer_uuid2, resource_class=['MEMORY_MB'])
expected = [
{'resource_provider': self.rp3['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VCPU': 1, 'VGPU': 1}},
{'resource_provider': self.rp4['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VCPU': 1}}
]
self.assertEqual(expected, updated_allocs)
def test_allocation_unset_resource_classes(self):
"""Tests removing allocations for resource classes."""
updated_allocs = self.resource_allocation_unset(
self.consumer_uuid2, resource_class=['VCPU', 'MEMORY_MB'])
expected = [
{'resource_provider': self.rp3['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VGPU': 1}}
]
self.assertEqual(expected, updated_allocs)
def test_allocation_unset_provider_and_rc(self):
"""Tests removing allocations of resource classes for a provider ."""
updated_allocs = self.resource_allocation_unset(
self.consumer_uuid2, provider=self.rp3['uuid'],
resource_class=['VCPU', 'MEMORY_MB'])
expected = [
{'resource_provider': self.rp3['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VGPU': 1}},
{'resource_provider': self.rp4['uuid'],
'generation': 3,
'project_id': self.project_uuid,
'user_id': self.user_uuid,
'resources': {'VCPU': 1, 'MEMORY_MB': 256}},
]
self.assertEqual(expected, updated_allocs)
def test_allocation_unset_remove_all_providers(self):
"""Tests removing all allocations by omitting the --provider option."""
# For this test pass use_json=False to make sure we get nothing back
# in the output since there are no more allocations.
updated_allocs = self.resource_allocation_unset(
self.consumer_uuid1, use_json=False)
self.assertEqual('', updated_allocs.strip())
class TestAllocationUnset128(TestAllocationUnset112):
"""Tests allocation unset command with --os-placement-api-version 1.28.
The 1.28 microversion adds the consumer_generation parameter to the
GET and PUT /allocations/{consumer_id} APIs.
"""
VERSION = '1.28'
|