| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
from oslo_config import cfg |
| 16 |
from stevedore import extension |
| 17 |
|
| 18 |
import os_vif |
| 19 |
from os_vif import exception |
| 20 |
from os_vif import objects |
| 21 |
from os_vif import opts |
| 22 |
from os_vif import plugin |
| 23 |
from os_vif.tests.unit import base |
| 24 |
|
| 25 |
|
| 26 |
class DemoPlugin(plugin.PluginBase): |
| 27 |
|
| 28 |
CONFIG_OPTS = ( |
| 29 |
cfg.BoolOpt("make_it_work", |
| 30 |
default=False, |
| 31 |
help="Make everything work correctly by setting this"), |
| 32 |
cfg.IntOpt("sleep_time", |
| 33 |
default=0, |
| 34 |
help="How long to artifically sleep") |
| 35 |
) |
| 36 |
|
| 37 |
def describe(self): |
| 38 |
pass |
| 39 |
|
| 40 |
def plug(self, vif, instance_info, config): |
| 41 |
pass |
| 42 |
|
| 43 |
def unplug(self, vif, instance_info, config): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class DemoPluginNoConfig(plugin.PluginBase): |
| 48 |
|
| 49 |
def describe(self): |
| 50 |
pass |
| 51 |
|
| 52 |
def plug(self, vif, instance_info, config): |
| 53 |
pass |
| 54 |
|
| 55 |
def unplug(self, vif, instance_info, config): |
| 56 |
pass |
| 57 |
|
| 58 |
|
| 59 |
class TestOSVIF(base.TestCase): |
| 60 |
|
| 61 |
def setUp(self): |
| 62 |
super(TestOSVIF, self).setUp() |
| 63 |
os_vif._EXT_MANAGER = None |
| 64 |
|
| 65 |
@mock.patch('stevedore.extension.ExtensionManager') |
| 66 |
def test_initialize(self, mock_EM): |
| 67 |
self.assertIsNone(os_vif._EXT_MANAGER) |
| 68 |
|
| 69 |
|
| 70 |
os_vif.initialize() |
| 71 |
os_vif.initialize() |
| 72 |
mock_EM.assert_called_once_with( |
| 73 |
invoke_on_load=False, namespace='os_vif') |
| 74 |
self.assertIsNotNone(os_vif._EXT_MANAGER) |
| 75 |
|
| 76 |
def test_load_plugin(self): |
| 77 |
obj = DemoPlugin.load("demo") |
| 78 |
self.assertTrue(hasattr(cfg.CONF, "os_vif_demo")) |
| 79 |
self.assertTrue(hasattr(cfg.CONF.os_vif_demo, "make_it_work")) |
| 80 |
self.assertTrue(hasattr(cfg.CONF.os_vif_demo, "sleep_time")) |
| 81 |
self.assertEqual(cfg.CONF.os_vif_demo.make_it_work, False) |
| 82 |
self.assertEqual(cfg.CONF.os_vif_demo.sleep_time, 0) |
| 83 |
|
| 84 |
self.assertEqual(obj.config, cfg.CONF.os_vif_demo) |
| 85 |
|
| 86 |
def test_load_plugin_no_config(self): |
| 87 |
obj = DemoPluginNoConfig.load("demonocfg") |
| 88 |
self.assertFalse(hasattr(cfg.CONF, "os_vif_demonocfg")) |
| 89 |
|
| 90 |
self.assertIsNone(obj.config) |
| 91 |
|
| 92 |
def test_plug_not_initialized(self): |
| 93 |
self.assertRaises( |
| 94 |
exception.LibraryNotInitialized, |
| 95 |
os_vif.plug, None, None) |
| 96 |
|
| 97 |
def test_unplug_not_initialized(self): |
| 98 |
self.assertRaises( |
| 99 |
exception.LibraryNotInitialized, |
| 100 |
os_vif.plug, None, None) |
| 101 |
|
| 102 |
@mock.patch.object(DemoPlugin, "plug") |
| 103 |
def test_plug(self, mock_plug): |
| 104 |
plg = extension.Extension(name="demo", |
| 105 |
entry_point="os-vif", |
| 106 |
plugin=DemoPlugin, |
| 107 |
obj=None) |
| 108 |
with mock.patch( |
| 109 |
'stevedore.extension.ExtensionManager.names', |
| 110 |
return_value=['foobar'], |
| 111 |
), mock.patch( |
| 112 |
'stevedore.extension.ExtensionManager.__getitem__', |
| 113 |
return_value=plg, |
| 114 |
): |
| 115 |
os_vif.initialize() |
| 116 |
info = objects.instance_info.InstanceInfo() |
| 117 |
vif = objects.vif.VIFBridge( |
| 118 |
id='9a12694f-f95e-49fa-9edb-70239aee5a2c', |
| 119 |
plugin='foobar') |
| 120 |
os_vif.plug(vif, info) |
| 121 |
mock_plug.assert_called_once_with(vif, info) |
| 122 |
|
| 123 |
@mock.patch.object(DemoPlugin, "unplug") |
| 124 |
def test_unplug(self, mock_unplug): |
| 125 |
plg = extension.Extension(name="demo", |
| 126 |
entry_point="os-vif", |
| 127 |
plugin=DemoPlugin, |
| 128 |
obj=None) |
| 129 |
with mock.patch( |
| 130 |
'stevedore.extension.ExtensionManager.names', |
| 131 |
return_value=['foobar'] |
| 132 |
), mock.patch( |
| 133 |
'stevedore.extension.ExtensionManager.__getitem__', |
| 134 |
return_value=plg, |
| 135 |
): |
| 136 |
os_vif.initialize() |
| 137 |
info = objects.instance_info.InstanceInfo() |
| 138 |
vif = objects.vif.VIFBridge( |
| 139 |
id='9a12694f-f95e-49fa-9edb-70239aee5a2c', |
| 140 |
plugin='foobar') |
| 141 |
os_vif.unplug(vif, info) |
| 142 |
mock_unplug.assert_called_once_with(vif, info) |
| 143 |
|
| 144 |
def test_host_info_all(self): |
| 145 |
os_vif.initialize() |
| 146 |
info = os_vif.host_info() |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
self.assertTrue(len(info.plugin_info) >= 3) |
| 151 |
|
| 152 |
plugins = {p.plugin_name: p for p in info.plugin_info} |
| 153 |
in_tree_plugin_names = ("linux_bridge", "ovs", "noop") |
| 154 |
self.assertTrue(all(name in plugins for name in in_tree_plugin_names)) |
| 155 |
lb = plugins["linux_bridge"] |
| 156 |
self.assertTrue(any("VIFBridge" == vif.vif_object_name |
| 157 |
for vif in lb.vif_info)) |
| 158 |
|
| 159 |
ovs = plugins["ovs"] |
| 160 |
self.assertTrue(len(ovs.vif_info) >= 4) |
| 161 |
vif_names = (vif.vif_object_name for vif in ovs.vif_info) |
| 162 |
ovs_vifs = ("VIFBridge", "VIFOpenVSwitch", |
| 163 |
"VIFVHostUser", "VIFHostDevice") |
| 164 |
self.assertTrue(all(name in ovs_vifs for name in vif_names)) |
| 165 |
|
| 166 |
noop = plugins["noop"] |
| 167 |
self.assertTrue(any("VIFVHostUser" == vif.vif_object_name |
| 168 |
for vif in noop.vif_info)) |
| 169 |
|
| 170 |
def test_host_info_filtered(self): |
| 171 |
os_vif.initialize() |
| 172 |
info = os_vif.host_info(permitted_vif_type_names=["VIFOpenVSwitch"]) |
| 173 |
|
| 174 |
self.assertEqual(len(info.plugin_info), 1) |
| 175 |
|
| 176 |
self.assertEqual(info.plugin_info[0].plugin_name, "ovs") |
| 177 |
vif_info = info.plugin_info[0].vif_info |
| 178 |
self.assertEqual(len(vif_info), 1) |
| 179 |
self.assertEqual(vif_info[0].vif_object_name, "VIFOpenVSwitch") |
| 180 |
|
| 181 |
def test_list_opts_entrypoints(self): |
| 182 |
list_opts = opts.list_plugins_opts() |
| 183 |
for group in list_opts: |
| 184 |
for opt in group[1]: |
| 185 |
self.assertTrue("oslo_config.cfg" == opt.__module__) |
| 186 |
self.assertGreaterEqual(len(list_opts), 3) |