File: test_extensions.py

package info (click to toggle)
python-neutron-lib 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,660 kB
  • sloc: python: 22,829; sh: 137; makefile: 24
file content (222 lines) | stat: -rw-r--r-- 8,221 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright 2016 OpenStack Foundation
# All Rights Reserved.
#
#    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 neutron_lib.api import extensions
from neutron_lib import fixture
from neutron_lib.services import base as service_base
from neutron_lib.tests import _base as base


class InheritFromExtensionDescriptor(extensions.ExtensionDescriptor):
    """Class to inherit from ExtensionDescriptor to test its methods

    Because ExtensionDescriptor is an abstract class, in order to test methods
    we need to create something to inherit from it so we have something
    instantiatable.  The only things defined here are those that are required
    because in ExtensionDescriptor they are marked as @abc.abstractmethod.
    """

    def get_name(self):
        pass

    def get_alias(self):
        pass

    def get_description(self):
        pass

    def get_updated(self):
        pass

    def update_attributes_map_save(self, extended_attributes,
                                   extension_attrs_map=None):
        """Update attributes map for this extension.

        This is default method for extending an extension's attributes map.
        An extension can use this method and supplying its own resource
        attribute map in extension_attrs_map argument to extend all its
        attributes that needs to be extended.

        If an extension does not implement update_attributes_map, the method
        does nothing and just return.
        """
        if not extension_attrs_map:
            return

        for resource, attrs in extension_attrs_map.items():
            extended_attrs = extended_attributes.get(resource)
            if extended_attrs:
                attrs.update(extended_attrs)


class TestExtensionDescriptor(base.BaseTestCase):

    def _setup_attribute_maps(self):
        self.extended_attributes = {'resource_one': {'one': 'first'},
                                    'resource_two': {'two': 'second'}}
        self.extension_attrs_map = {'resource_one': {'three': 'third'}}

    def test_update_attributes_map_works(self):
        self._setup_attribute_maps()
        extension_description = InheritFromExtensionDescriptor()
        extension_description.update_attributes_map(self.extended_attributes,
                                                    self.extension_attrs_map)
        self.assertEqual(self.extension_attrs_map,
                         {'resource_one': {'one': 'first',
                                           'three': 'third'}})

    def test_update_attributes_map_short_circuit_exit(self):
        self._setup_attribute_maps()
        extension_description = InheritFromExtensionDescriptor()
        extension_description.update_attributes_map(self.extended_attributes)
        self.assertEqual(self.extension_attrs_map,
                         {'resource_one': {'three': 'third'}})


class DummyPlugin(service_base.ServicePluginBase):

    supported_extension_aliases = ['flash']

    def get_plugin_type(self):
        return 'Flash Gordon'

    def get_plugin_description(self):
        return 'Legend!'


class TestExtensionIsSupported(base.BaseTestCase):

    def setUp(self):
        super().setUp()
        self._plugin = DummyPlugin()

    def test_extension_exists(self):
        self.assertTrue(extensions.is_extension_supported(self._plugin,
                                                          "flash"))

    def test_extension_does_not_exist(self):
        self.assertFalse(extensions.is_extension_supported(self._plugin,
                                                           "gordon"))


class TestAPIExtensionDescriptor(base.BaseTestCase):

    # API definition attributes; acts as an API definition module
    NAME = 'Test API'
    ALIAS = 'test-api'
    DESCRIPTION = 'A test API definition'
    UPDATED_TIMESTAMP = '2017-02-01T10:00:00-00:00'
    RESOURCE_ATTRIBUTE_MAP = {'ports': {}}
    SUB_RESOURCE_ATTRIBUTE_MAP = {'ports': {'debug': {}}}
    REQUIRED_EXTENSIONS = ['l3']
    OPTIONAL_EXTENSIONS = ['fw']

    def setUp(self):
        super().setUp()
        self.extn = _APIDefinition()
        self.empty_extn = _EmptyAPIDefinition()
        self.useFixture(fixture.APIDefinitionFixture(self))

    def test__assert_api_definition_no_defn(self):
        self.assertRaises(NotImplementedError,
                          _NoAPIDefinition._assert_api_definition)

    def test__assert_api_definition_no_attr(self):
        self.assertRaises(
            NotImplementedError, self.extn._assert_api_definition, attr='NOPE')

    def test_get_name(self):
        self.assertEqual(self.NAME, self.extn.get_name())

    def test_get_name_unset(self):
        self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_name)

    def test_get_alias(self):
        self.assertEqual(self.ALIAS, self.extn.get_alias())

    def test_get_alias_unset(self):
        self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_alias)

    def test_get_description(self):
        self.assertEqual(self.DESCRIPTION, self.extn.get_description())

    def test_get_description_unset(self):
        self.assertRaises(NotImplementedError,
                          _EmptyAPIDefinition.get_description)

    def test_get_updated(self):
        self.assertEqual(self.UPDATED_TIMESTAMP, self.extn.get_updated())

    def test_get_updated_unset(self):
        self.assertRaises(NotImplementedError, _EmptyAPIDefinition.get_updated)

    def test_get_extended_resources_v2(self):
        self.assertEqual(
            dict(list(self.RESOURCE_ATTRIBUTE_MAP.items()) +
                 list(self.SUB_RESOURCE_ATTRIBUTE_MAP.items())),
            self.extn.get_extended_resources('2.0'))

    def test_get_extended_resources_v2_unset(self):
        self.assertRaises(NotImplementedError,
                          self.empty_extn.get_extended_resources, '2.0')

    def test_get_extended_resources_v1(self):
        self.assertEqual({}, self.extn.get_extended_resources('1.0'))

    def test_get_extended_resources_v1_unset(self):
        self.assertEqual({}, self.empty_extn.get_extended_resources('1.0'))

    def test_get_required_extensions(self):
        self.assertEqual(self.REQUIRED_EXTENSIONS,
                         self.extn.get_required_extensions())

    def test_get_required_extensions_unset(self):
        self.assertRaises(NotImplementedError,
                          self.empty_extn.get_required_extensions)

    def test_get_optional_extensions(self):
        self.assertEqual(self.OPTIONAL_EXTENSIONS,
                         self.extn.get_optional_extensions())

    def test_get_optional_extensions_unset(self):
        self.assertRaises(NotImplementedError,
                          self.empty_extn.get_optional_extensions)

    def test_update_attributes_map_extensions_unset(self):
        self.assertRaises(NotImplementedError,
                          self.empty_extn.update_attributes_map, {})

    def test_update_attributes_map_with_ext_attrs(self):
        base_attrs = {'ports': {'a': 'A'}}
        ext_attrs = {'ports': {'b': 'B'}}
        self.extn.update_attributes_map(base_attrs, ext_attrs)
        self.assertEqual({'ports': {'a': 'A', 'b': 'B'}}, ext_attrs)

    def test_update_attributes_map_without_ext_attrs(self):
        base_attrs = {'ports': {'a': 'A'}}
        self.extn.update_attributes_map(base_attrs)
        self.assertIn('a', self.extn.get_extended_resources('2.0')['ports'])


class _APIDefinition(extensions.APIExtensionDescriptor):
    api_definition = TestAPIExtensionDescriptor


class _NoAPIDefinition(extensions.APIExtensionDescriptor):
    pass


class _EmptyAPIDefinition(extensions.APIExtensionDescriptor):
    api_definition = {}