Debian Codegraph @

neutron_lib/tests/unit/db/test_model_query.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 from oslo_utils import uuidutils
16
17 from neutron_lib import constants
18 from neutron_lib.db import model_query
19 from neutron_lib import fixture
20 from neutron_lib.tests import _base
21 from neutron_lib.utils import helpers
22
23
24 # TODO(boden): find a way to test other model_query functions
25
26 class TestHooks(_base.BaseTestCase):
27
28 def setUp(self):
29 super().setUp()
30 self.useFixture(fixture.DBQueryHooksFixture())
31
32 def _mock_hook(self, x):
33 return x
34
35 def check_registered_hooks(self, registered_hooks, expected=None):
36 expected = expected if expected else {}
37 for d in registered_hooks:
38 for k in d.keys():
39 self.assertEqual(expected[k], d.get(k))
40
41 def test_register_hook(self):
42 mock_model = mock.Mock()
43 model_query.register_hook(
44 mock_model, 'hook1', self._mock_hook,
45 self._mock_hook, result_filters=self._mock_hook)
46 self.assertEqual(1, len(model_query._model_query_hooks.keys()))
47 hook_ref = helpers.make_weak_ref(self._mock_hook)
48 registered_hooks = model_query.get_hooks(mock_model)
49 self.assertEqual(1, len(registered_hooks))
50 expected_hooks = {'query': hook_ref,
51 'filter': hook_ref,
52 'result_filters': hook_ref,
53 'rbac_actions': None}
54 self.check_registered_hooks(registered_hooks, expected_hooks)
55
56 def test_register_hook_non_callables(self):
57 mock_model = mock.Mock()
58 model_query.register_hook(
59 mock_model, 'hook1', self._mock_hook, {}, result_filters={})
60 self.assertEqual(1, len(model_query._model_query_hooks.keys()))
61 hook_ref = helpers.make_weak_ref(self._mock_hook)
62 registered_hooks = model_query.get_hooks(mock_model)
63 self.assertEqual(1, len(registered_hooks))
64 expected_hooks = {'query': hook_ref,
65 'filter': {},
66 'result_filters': {},
67 'rbac_actions': None}
68 self.check_registered_hooks(registered_hooks, expected_hooks)
69
70 def test_register_hook_with_mutiple_rbacs(self):
71 mock_model = mock.Mock()
72 model_query.register_hook(
73 mock_model, 'hook1', self._mock_hook,
74 self._mock_hook, result_filters=self._mock_hook,
75 rbac_actions=[constants.ACCESS_SHARED, constants.ACCESS_EXTERNAL])
76 self.assertEqual(1, len(model_query._model_query_hooks.keys()))
77 hook_ref = helpers.make_weak_ref(self._mock_hook)
78 registered_hooks = model_query.get_hooks(mock_model)
79 self.assertEqual(1, len(registered_hooks))
80 expected_hooks = {'query': hook_ref,
81 'filter': hook_ref,
82 'result_filters': hook_ref,
83 'rbac_actions': {constants.ACCESS_SHARED,
84 constants.ACCESS_EXTERNAL}}
85 self.check_registered_hooks(registered_hooks, expected_hooks)
86
87 def test_register_hook_with_one_rbac(self):
88 mock_model = mock.Mock()
89 model_query.register_hook(
90 mock_model, 'hook1', self._mock_hook,
91 self._mock_hook, result_filters=self._mock_hook,
92 rbac_actions=constants.ACCESS_EXTERNAL)
93 self.assertEqual(1, len(model_query._model_query_hooks.keys()))
94 hook_ref = helpers.make_weak_ref(self._mock_hook)
95 registered_hooks = model_query.get_hooks(mock_model)
96 self.assertEqual(1, len(registered_hooks))
97 expected_hooks = {'query': hook_ref,
98 'filter': hook_ref,
99 'result_filters': hook_ref,
100 'rbac_actions': {constants.ACCESS_EXTERNAL}}
101 self.check_registered_hooks(registered_hooks, expected_hooks)
102
103 def test_get_values(self):
104 mock_model = mock.Mock()
105 mock_context = mock.Mock()
106 with mock.patch.object(
107 model_query, 'query_with_hooks') as query_with_hooks:
108 query_with_hooks.return_value = [['value1'], ['value2']]
109 values = model_query.get_values(mock_context, mock_model,
110 'fake_field')
111 self.assertEqual(['value1', 'value2'], values)
112 query_with_hooks.assert_called_with(
113 mock_context, mock_model, field='fake_field')
114
115 def test_get_rbac_actions_no_rbacs(self):
116 model = uuidutils.generate_uuid()
117 model_query.register_hook(model, 'hook1', mock.ANY, mock.ANY)
118 rbacs = model_query.get_rbac_actions(model)
119 self.assertEqual({constants.ACCESS_SHARED}, rbacs)
120
121 def test_get_rbac_actions_one_rbac(self):
122 model = uuidutils.generate_uuid()
123 model_query.register_hook(model, 'hook1', mock.ANY, mock.ANY,
124 rbac_actions=constants.ACCESS_EXTERNAL)
125 rbacs = model_query.get_rbac_actions(model)
126 self.assertEqual({constants.ACCESS_EXTERNAL}, rbacs)
127
128 def test_get_rbac_actions_multiple_rbac(self):
129 model = uuidutils.generate_uuid()
130 model_query.register_hook(model, 'hook1', mock.ANY, mock.ANY,
131 rbac_actions=constants.ACCESS_EXTERNAL)
132 model_query.register_hook(model, 'hook2', mock.ANY, mock.ANY,
133 rbac_actions=constants.ACCESS_EXTERNAL)
134 model_query.register_hook(model, 'hook3', mock.ANY, mock.ANY,
135 rbac_actions=constants.ACCESS_SHARED)
136 rbacs = model_query.get_rbac_actions(model)
137 self.assertEqual({constants.ACCESS_EXTERNAL, constants.ACCESS_SHARED},
138 rbacs)