Debian Codegraph @

oslo_limit/tests/test_fixture.py

raw symbols SCIP
1 # Copyright 2021 Red Hat, Inc
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 from oslotest import base
15
16 from oslo_config import cfg
17 from oslo_config import fixture as config_fixture
18
19 from oslo_limit import exception
20 from oslo_limit import fixture
21 from oslo_limit import limit
22 from oslo_limit import opts
23
24 CONF = cfg.CONF
25
26
27 class TestFixture(base.BaseTestCase):
28 def setUp(self):
29 super().setUp()
30
31 self.config_fixture = self.useFixture(config_fixture.Config(CONF))
32 self.config_fixture.config(
33 group='oslo_limit', endpoint_id='ENDPOINT_ID'
34 )
35 opts.register_opts(CONF)
36
37 # Set up some default projects, registered limits,
38 # and project limits
39 reglimits = {'widgets': 100, 'sprockets': 50}
40 projlimits = {
41 'project2': {'widgets': 10},
42 }
43 self.useFixture(fixture.LimitFixture(reglimits, projlimits))
44
45 # Some fake usage for projects
46 self.usage = {
47 'project1': {'sprockets': 10, 'widgets': 10},
48 'project2': {'sprockets': 3, 'widgets': 3},
49 }
50
51 def proj_usage(project_id, resource_names):
52 return self.usage[project_id]
53
54 # An enforcer to play with
55 self.enforcer = limit.Enforcer(proj_usage)
56
57 def test_project_under_registered_limit_only(self):
58 # Project1 has quota of 50 and 100 each, so no problem with
59 # 10+1 usage.
60 self.enforcer.enforce('project1', {'sprockets': 1, 'widgets': 1})
61
62 def test_project_over_registered_limit_only(self):
63 # Project1 has quota of 100 widgets, usage of 112 is over
64 # quota.
65 self.assertRaises(
66 exception.ProjectOverLimit,
67 self.enforcer.enforce,
68 'project1',
69 {'sprockets': 1, 'widgets': 102},
70 )
71
72 def test_project_over_registered_limit(self):
73 # delta=1 should be under the registered limit of 50
74 self.enforcer.enforce('project2', {'sprockets': 1})
75
76 # delta=50 should be over the registered limit of 50
77 self.assertRaises(
78 exception.ProjectOverLimit,
79 self.enforcer.enforce,
80 'project2',
81 {'sprockets': 50},
82 )
83
84 def test_project_over_project_limits(self):
85 # delta=7 is usage=10, right at our project limit of 10
86 self.enforcer.enforce('project2', {'widgets': 7})
87
88 # delta=10 is usage 13, over our project limit of 10
89 self.assertRaises(
90 exception.ProjectOverLimit,
91 self.enforcer.enforce,
92 'project2',
93 {'widgets': 10},
94 )
95
96 def test_calculate_usage(self):
97 # Make sure the usage calculator works with the fixture too
98 u = self.enforcer.calculate_usage('project2', ['widgets'])
99 self.assertEqual(3, u['widgets'].usage)
100 self.assertEqual(10, u['widgets'].limit)
101
102 u = self.enforcer.calculate_usage('project1', ['widgets', 'sprockets'])
103 self.assertEqual(10, u['sprockets'].usage)
104 self.assertEqual(10, u['widgets'].usage)
105 # Since project1 has no project limits, make sure we get the
106 # registered limit values
107 self.assertEqual(50, u['sprockets'].limit)
108 self.assertEqual(100, u['widgets'].limit)