File: test_fixture.py

package info (click to toggle)
python-oslo.limit 2.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 368 kB
  • sloc: python: 1,230; makefile: 25; sh: 2
file content (105 lines) | stat: -rw-r--r-- 3,931 bytes parent folder | download | duplicates (2)
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
#    Copyright 2021 Red Hat, Inc
#    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 oslotest import base

from oslo_config import cfg
from oslo_config import fixture as config_fixture

from oslo_limit import exception
from oslo_limit import fixture
from oslo_limit import limit
from oslo_limit import opts

CONF = cfg.CONF


class TestFixture(base.BaseTestCase):
    def setUp(self):
        super().setUp()

        self.config_fixture = self.useFixture(config_fixture.Config(CONF))
        self.config_fixture.config(
            group='oslo_limit',
            endpoint_id='ENDPOINT_ID'
        )
        opts.register_opts(CONF)

        # Set up some default projects, registered limits,
        # and project limits
        reglimits = {'widgets': 100,
                     'sprockets': 50}
        projlimits = {
            'project2': {'widgets': 10},
        }
        self.useFixture(fixture.LimitFixture(reglimits, projlimits))

        # Some fake usage for projects
        self.usage = {
            'project1': {'sprockets': 10,
                         'widgets': 10},
            'project2': {'sprockets': 3,
                         'widgets': 3},
        }

        def proj_usage(project_id, resource_names):
            return self.usage[project_id]

        # An enforcer to play with
        self.enforcer = limit.Enforcer(proj_usage)

    def test_project_under_registered_limit_only(self):
        # Project1 has quota of 50 and 100 each, so no problem with
        # 10+1 usage.
        self.enforcer.enforce('project1', {'sprockets': 1,
                                           'widgets': 1})

    def test_project_over_registered_limit_only(self):
        # Project1 has quota of 100 widgets, usage of 112 is over
        # quota.
        self.assertRaises(exception.ProjectOverLimit,
                          self.enforcer.enforce,
                          'project1', {'sprockets': 1,
                                       'widgets': 102})

    def test_project_over_registered_limit(self):
        # delta=1 should be under the registered limit of 50
        self.enforcer.enforce('project2', {'sprockets': 1})

        # delta=50 should be over the registered limit of 50
        self.assertRaises(exception.ProjectOverLimit,
                          self.enforcer.enforce,
                          'project2', {'sprockets': 50})

    def test_project_over_project_limits(self):
        # delta=7 is usage=10, right at our project limit of 10
        self.enforcer.enforce('project2', {'widgets': 7})

        # delta=10 is usage 13, over our project limit of 10
        self.assertRaises(exception.ProjectOverLimit,
                          self.enforcer.enforce,
                          'project2', {'widgets': 10})

    def test_calculate_usage(self):
        # Make sure the usage calculator works with the fixture too
        u = self.enforcer.calculate_usage('project2', ['widgets'])['widgets']
        self.assertEqual(3, u.usage)
        self.assertEqual(10, u.limit)

        u = self.enforcer.calculate_usage('project1', ['widgets', 'sprockets'])
        self.assertEqual(10, u['sprockets'].usage)
        self.assertEqual(10, u['widgets'].usage)
        # Since project1 has no project limits, make sure we get the
        # registered limit values
        self.assertEqual(50, u['sprockets'].limit)
        self.assertEqual(100, u['widgets'].limit)