Debian Codegraph @

neutron_lib/tests/unit/api/test_extensions.py

raw
1 # Copyright 2016 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from neutron_lib.api import extensions
17 from neutron_lib import fixture
18 from neutron_lib.services import base as service_base
19 from neutron_lib.tests import _base as base
20
21
22 class InheritFromExtensionDescriptor(extensions.ExtensionDescriptor):
23 """Class to inherit from ExtensionDescriptor to test its methods
24
25 Because ExtensionDescriptor is an abstract class, in order to test methods
26 we need to create something to inherit from it so we have something
27 instantiatable. The only things defined here are those that are required
28 because in ExtensionDescriptor they are marked as @abc.abstractmethod.
29 """
30
31 def get_name(self):
32 pass
33
34 def get_alias(self):
35 pass
36
37 def get_description(self):
38 pass
39
40 def get_updated(self):
41 pass
42
43 def update_attributes_map_save(self, extended_attributes,
44 extension_attrs_map=None):
45 """Update attributes map for this extension.
46
47 This is default method for extending an extension's attributes map.
48 An extension can use this method and supplying its own resource
49 attribute map in extension_attrs_map argument to extend all its
50 attributes that needs to be extended.
51
52 If an extension does not implement update_attributes_map, the method
53 does nothing and just return.
54 """
55 if not extension_attrs_map:
56 return
57
58 for resource, attrs in extension_attrs_map.items():
59 extended_attrs = extended_attributes.get(resource)
60 if extended_attrs:
61 attrs.update(extended_attrs)
62
63
64 class TestExtensionDescriptor(base.BaseTestCase):
65
66 def _setup_attribute_maps(self):
67 self.extended_attributes = {'resource_one': {'one': 'first'},
68 'resource_two': {'two': 'second'}}
69 self.extension_attrs_map = {'resource_one': {'three': 'third'}}
70
71 def test_update_attributes_map_works(self):
72 self._setup_attribute_maps()
73 extension_description = InheritFromExtensionDescriptor()
74 extension_description.update_attributes_map(self.extended_attributes,
75 self.extension_attrs_map)
76 self.assertEqual(self.extension_attrs_map,
77 {'resource_one': {'one': 'first',
78 'three': 'third'}})
79
80 def test_update_attributes_map_short_circuit_exit(self):
81 self._setup_attribute_maps()
82 extension_description = InheritFromExtensionDescriptor()
83 extension_description.update_attributes_map(self.extended_attributes)
84 self.assertEqual(self.extension_attrs_map,
85 {'resource_one': {'three': 'third'}})
86
87
88 class DummyPlugin(service_base.ServicePluginBase):
89
90 supported_extension_aliases = ['flash']
91
92 def get_plugin_type(self):
93 return 'Flash Gordon'
94
95 def get_plugin_description(self):
96 return 'Legend!'
97
98
99 class TestExtensionIsSupported(base.BaseTestCase):
100
101 def setUp(self):
102 super().setUp()
103 self._plugin = DummyPlugin()
104
105 def test_extension_exists(self):
106 self.assertTrue(extensions.is_extension_supported(self._plugin,
107 "flash"))
108
109 def test_extension_does_not_exist(self):
110 self.assertFalse(extensions.is_extension_supported(self._plugin,
111 "gordon"))
112
113
114 class TestAPIExtensionDescriptor(base.BaseTestCase):
115
116 # API definition attributes; acts as an API definition module
117 NAME = 'Test API'
118 ALIAS = 'test-api'
119 DESCRIPTION = 'A test API definition'
120 UPDATED_TIMESTAMP = '2017-02-01T10:00:00-00:00'
121 RESOURCE_ATTRIBUTE_MAP = {'ports': {}}
122 SUB_RESOURCE_ATTRIBUTE_MAP = {'ports': {'debug': {}}}
123 REQUIRED_EXTENSIONS = ['l3']
124 OPTIONAL_EXTENSIONS = ['fw']
125
126 def setUp(self):
127 super().setUp()
128 self.extn = _APIDefinition()
129 self.empty_extn = _EmptyAPIDefinition()
130 self.useFixture(fixture.APIDefinitionFixture(self))
131
132 def test__assert_api_definition_no_defn(self):
133 self.assertRaises(NotImplementedError,
134 _NoAPIDefinition._assert_api_definition)
135
136 def test__assert_api_definition_no_attr(self):
137 self.assertRaises(
138 NotImplementedError, self.extn._assert_api_definition, attr='NOPE')
139
140 def test_get_name(self):
141 self.assertEqual(self.NAME, self.extn.get_name())
142
143 def test_get_name_unset(self):
144 self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_name)
145
146 def test_get_alias(self):
147 self.assertEqual(self.ALIAS, self.extn.get_alias())
148
149 def test_get_alias_unset(self):
150 self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_alias)
151
152 def test_get_description(self):
153 self.assertEqual(self.DESCRIPTION, self.extn.get_description())
154
155 def test_get_description_unset(self):
156 self.assertRaises(NotImplementedError,
157 _EmptyAPIDefinition.get_description)
158
159 def test_get_updated(self):
160 self.assertEqual(self.UPDATED_TIMESTAMP, self.extn.get_updated())
161
162 def test_get_updated_unset(self):
163 self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_updated)
164
165 def test_get_extended_resources_v2(self):
166 self.assertEqual(
167 dict(list(self.RESOURCE_ATTRIBUTE_MAP.items()) +
168 list(self.SUB_RESOURCE_ATTRIBUTE_MAP.items())),
169 self.extn.get_extended_resources('2.0'))
170
171 def test_get_extended_resources_v2_unset(self):
172 self.assertRaises(NotImplementedError,
173 self.empty_extn.get_extended_resources, '2.0')
174
175 def test_get_extended_resources_v1(self):
176 self.assertEqual({}, self.extn.get_extended_resources('1.0'))
177
178 def test_get_extended_resources_v1_unset(self):
179 self.assertEqual({}, self.empty_extn.get_extended_resources('1.0'))
180
181 def test_get_required_extensions(self):
182 self.assertEqual(self.REQUIRED_EXTENSIONS,
183 self.extn.get_required_extensions())
184
185 def test_get_required_extensions_unset(self):
186 self.assertRaises(NotImplementedError,
187 self.empty_extn.get_required_extensions)
188
189 def test_get_optional_extensions(self):
190 self.assertEqual(self.OPTIONAL_EXTENSIONS,
191 self.extn.get_optional_extensions())
192
193 def test_get_optional_extensions_unset(self):
194 self.assertRaises(NotImplementedError,
195 self.empty_extn.get_optional_extensions)
196
197 def test_update_attributes_map_extensions_unset(self):
198 self.assertRaises(NotImplementedError,
199 self.empty_extn.update_attributes_map, {})
200
201 def test_update_attributes_map_with_ext_attrs(self):
202 base_attrs = {'ports': {'a': 'A'}}
203 ext_attrs = {'ports': {'b': 'B'}}
204 self.extn.update_attributes_map(base_attrs, ext_attrs)
205 self.assertEqual({'ports': {'a': 'A', 'b': 'B'}}, ext_attrs)
206
207 def test_update_attributes_map_without_ext_attrs(self):
208 base_attrs = {'ports': {'a': 'A'}}
209 self.extn.update_attributes_map(base_attrs)
210 self.assertIn('a', self.extn.get_extended_resources('2.0')['ports'])
211
212
213 class _APIDefinition(extensions.APIExtensionDescriptor):
214 api_definition = TestAPIExtensionDescriptor
215
216
217 class _NoAPIDefinition(extensions.APIExtensionDescriptor):
218 pass
219
220
221 class _EmptyAPIDefinition(extensions.APIExtensionDescriptor):
222 api_definition = {}