File: test_allocation.py

package info (click to toggle)
python-osc-placement 4.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 640 kB
  • sloc: python: 4,039; makefile: 25; sh: 2
file content (75 lines) | stat: -rw-r--r-- 2,773 bytes parent folder | download | duplicates (3)
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
# 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_lib import exceptions
from oslotest import base

from osc_placement.resources import allocation


class TestAllocation(base.BaseTestCase):
    def test_parse_allocations(self):
        rp1 = str(uuid.uuid4())
        rp2 = str(uuid.uuid4())
        allocations = [
            'rp={},VCPU=4,MEMORY_MB=16324'.format(rp1),
            'rp={},VCPU=4,DISK_GB=4096'.format(rp2)]
        expected = {
            rp1: {'VCPU': 4, 'MEMORY_MB': 16324},
            rp2: {'VCPU': 4, 'DISK_GB': 4096},
        }
        self.assertDictEqual(
            expected, allocation.parse_allocations(allocations))

    def test_merge_allocations(self):
        rp1 = str(uuid.uuid4())
        allocations = [
            'rp={},VCPU=4,MEMORY_MB=16324'.format(rp1),
            'rp={},VCPU=4,DISK_GB=4096'.format(rp1)]
        expected = {
            rp1: {'VCPU': 4, 'MEMORY_MB': 16324, 'DISK_GB': 4096}}
        self.assertEqual(expected, allocation.parse_allocations(allocations))

    def test_fail_if_cannot_merge_allocations(self):
        rp1 = str(uuid.uuid4())
        allocations = [
            'rp={},VCPU=4,MEMORY_MB=16324'.format(rp1),
            'rp={},VCPU=8,DISK_GB=4096'.format(rp1)]
        ex = self.assertRaises(
            exceptions.CommandError, allocation.parse_allocations, allocations)
        self.assertEqual(
            'Conflict detected for resource provider %s resource class VCPU' %
            rp1, str(ex))

    def test_fail_if_incorrect_format(self):
        allocations = ['incorrect_format']
        self.assertRaisesRegex(
            ValueError,
            'Incorrect allocation',
            allocation.parse_allocations, allocations)
        allocations = ['=,']
        self.assertRaisesRegex(
            ValueError,
            '2 is required',
            allocation.parse_allocations, allocations)
        allocations = ['abc=155']
        self.assertRaisesRegex(
            ValueError,
            'Incorrect allocation',
            allocation.parse_allocations, allocations)
        allocations = ['abc=155,xyz=999']
        self.assertRaisesRegex(
            ValueError,
            'parameter is required',
            allocation.parse_allocations, allocations)