File: test_host_info.py

package info (click to toggle)
python-os-vif 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 948 kB
  • sloc: python: 5,503; makefile: 25; sh: 2
file content (193 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (4)
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
# 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 vif_plug_linux_bridge import constants as lb_constants
from vif_plug_ovs import constants as ovs_constants

from os_vif import exception
from os_vif import objects
from os_vif.tests.unit import base


class TestHostInfo(base.TestCase):

    def setUp(self):
        super(TestHostInfo, self).setUp()

        objects.register_all()

        self.host_info = objects.host_info.HostInfo(
            plugin_info=[
                objects.host_info.HostPluginInfo(
                    plugin_name=lb_constants.PLUGIN_NAME,
                    vif_info=[
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFBridge",
                            min_version="1.0",
                            max_version="3.0"
                        ),
                    ]),
                objects.host_info.HostPluginInfo(
                    plugin_name=ovs_constants.PLUGIN_NAME,
                    vif_info=[
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFBridge",
                            min_version="2.0",
                            max_version="7.0"
                        ),
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFOpenVSwitch",
                            min_version="1.0",
                            max_version="2.0"
                        ),
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFVHostUser",
                            min_version="1.0",
                            max_version="2.0"
                        ),
                    ])
            ])
        # https://bugs.launchpad.net/oslo.versionedobjects/+bug/1563787
        self.host_info.obj_reset_changes(recursive=True)

    def test_serialization(self):
        json = self.host_info.obj_to_primitive()
        self.assertEqual("os_vif", json["versioned_object.namespace"])

        host_info = objects.host_info.HostInfo.obj_from_primitive(json)
        # Copied from test_vif.py:
        #
        # The __eq__ function works by using obj_to_primitive()
        # and this includes a list of changed fields. Very
        # occassionally the ordering of the list of changes
        # varies, causing bogus equality failures. This is
        # arguably a bug in oslo.versionedobjects since the
        # set of changes fields should not affect equality
        # comparisons. Remove this hack once this is fixed:
        #
        # https://bugs.launchpad.net/oslo.versionedobjects/+bug/1563787
        host_info.obj_reset_changes(recursive=True)

        self.assertEqual(self.host_info, host_info)

    def test_plugin_existance(self):
        self.assertTrue(self.host_info.has_plugin(ovs_constants.PLUGIN_NAME))
        self.assertFalse(self.host_info.has_plugin("fishfood"))

    def test_plugin_fetch(self):
        plugin = self.host_info.get_plugin(ovs_constants.PLUGIN_NAME)
        self.assertEqual(ovs_constants.PLUGIN_NAME, plugin.plugin_name)

        self.assertRaises(exception.NoMatchingPlugin,
                          self.host_info.get_plugin,
                          "fishfood")

    def test_vif_existance(self):
        plugin = self.host_info.get_plugin(ovs_constants.PLUGIN_NAME)
        self.assertTrue(plugin.has_vif("VIFOpenVSwitch"))
        self.assertFalse(plugin.has_vif("VIFFishFood"))

    def test_vif_fetch(self):
        plugin = self.host_info.get_plugin(ovs_constants.PLUGIN_NAME)

        vif = plugin.get_vif("VIFOpenVSwitch")
        self.assertEqual("VIFOpenVSwitch", vif.vif_object_name)

        self.assertRaises(exception.NoMatchingVIFClass,
                          plugin.get_vif,
                          "VIFFishFood")

    def test_common_version_no_obj(self):
        info = objects.host_info.HostVIFInfo(
            vif_object_name="VIFFishFood",
            min_version="1.0",
            max_version="1.8")

        self.assertRaises(exception.NoMatchingVIFClass,
                          info.get_common_version)

    def test_common_version_no_version(self):
        info = objects.host_info.HostVIFInfo(
            vif_object_name="VIFOpenVSwitch",
            min_version="1729.0",
            max_version="8753.0")

        self.assertRaises(exception.NoSupportedVIFVersion,
                          info.get_common_version)

    def test_common_version_ok(self):
        info = objects.host_info.HostVIFInfo(
            vif_object_name="VIFOpenVSwitch",
            min_version="1.0",
            max_version="10.0")

        ver = info.get_common_version()
        self.assertEqual(objects.vif.VIFOpenVSwitch.VERSION,
                         ver)

    def test_filtering(self):
        host_info = objects.host_info.HostInfo(
            plugin_info=[
                objects.host_info.HostPluginInfo(
                    plugin_name=lb_constants.PLUGIN_NAME,
                    vif_info=[
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFBridge",
                            min_version="1.0",
                            max_version="3.0"
                        ),
                    ]),
                objects.host_info.HostPluginInfo(
                    plugin_name=ovs_constants.PLUGIN_NAME,
                    vif_info=[
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFBridge",
                            min_version="2.0",
                            max_version="7.0"
                        ),
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFOpenVSwitch",
                            min_version="1.0",
                            max_version="2.0"
                        ),
                        objects.host_info.HostVIFInfo(
                            vif_object_name="VIFVHostUser",
                            min_version="1.0",
                            max_version="2.0"
                        ),
                    ])
            ])

        host_info.filter_vif_types(["VIFBridge", "VIFOpenVSwitch"])

        self.assertEqual(len(host_info.plugin_info), 2)

        plugin = host_info.plugin_info[0]
        self.assertEqual(len(plugin.vif_info), 1)
        self.assertEqual(plugin.vif_info[0].vif_object_name,
                         "VIFBridge")

        plugin = host_info.plugin_info[1]
        self.assertEqual(len(plugin.vif_info), 2)
        self.assertEqual(plugin.vif_info[0].vif_object_name,
                         "VIFBridge")
        self.assertEqual(plugin.vif_info[1].vif_object_name,
                         "VIFOpenVSwitch")

        host_info.filter_vif_types(["VIFOpenVSwitch"])

        self.assertEqual(len(host_info.plugin_info), 1)

        plugin = host_info.plugin_info[0]
        self.assertEqual(len(plugin.vif_info), 1)
        self.assertEqual(plugin.vif_info[0].vif_object_name,
                         "VIFOpenVSwitch")