summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItsuro Oda <oda@valinux.co.jp>2018-01-22 14:03:32 +0900
committerItsuro Oda <oda@valinux.co.jp>2018-02-08 11:51:06 +0900
commitec2499d5f4163c98245605418e57ebf175306f1d (patch)
tree393b8710837c326d4137af0da7cf23bb6a9c83cc
parent3b288be3f756f0ccd7a739cb20013ec982d60554 (diff)
support vlan network
Notes
Notes (review): Code-Review+2: Tetsuro Nakamura <nakamura.tetsuro@lab.ntt.co.jp> Workflow+1: Tetsuro Nakamura <nakamura.tetsuro@lab.ntt.co.jp> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 08 Feb 2018 04:09:28 +0000 Reviewed-on: https://review.openstack.org/541971 Project: openstack/networking-spp Branch: refs/heads/master
-rw-r--r--networking_spp/agent/spp_agent.py70
-rw-r--r--networking_spp/agent/spp_api.py26
-rw-r--r--networking_spp/mech_driver/mech_spp.py14
-rw-r--r--networking_spp/tests/unit/test_mech_spp.py21
-rw-r--r--networking_spp/tests/unit/test_spp_agent.py55
-rw-r--r--networking_spp/tests/unit/test_spp_api.py67
-rw-r--r--networking_spp/tests/unit/test_spp_manager.py6
7 files changed, 210 insertions, 49 deletions
diff --git a/networking_spp/agent/spp_agent.py b/networking_spp/agent/spp_agent.py
index ceaf5df..136ac80 100644
--- a/networking_spp/agent/spp_agent.py
+++ b/networking_spp/agent/spp_agent.py
@@ -184,51 +184,84 @@ class SppAgent(object):
184 def _init_vhost_mac_address(self, info): 184 def _init_vhost_mac_address(self, info):
185 table = info.get("classifier_table", []) 185 table = info.get("classifier_table", [])
186 for entry in table: 186 for entry in table:
187 if entry["type"] == "mac" and entry["port"].startswith("ring:"): 187 if (entry["type"] in ["mac", "vlan"] and
188 entry["port"].startswith("ring:")):
188 ring_id = int(entry["port"][len("ring:"):]) 189 ring_id = int(entry["port"][len("ring:"):])
189 vhost_id = ring_id // 2 190 vhost_id = ring_id // 2
191 mac_string = entry["value"]
192 if entry["type"] == "vlan":
193 _vlan_id, mac_string = mac_string.split('/')
190 # match format of neutron standard 194 # match format of neutron standard
191 mac = str(netaddr.EUI(entry["value"], 195 mac = str(netaddr.EUI(mac_string,
192 dialect=netaddr.mac_unix_expanded)) 196 dialect=netaddr.mac_unix_expanded))
193 self.vhostusers[vhost_id].mac_address = mac 197 self.vhostusers[vhost_id].mac_address = mac
194 198
195 def set_classifier_table(self, vhost_id, mac_address): 199 def set_classifier_table(self, vhost_id, mac_address, vlan_id):
196 vhost = self.vhostusers[vhost_id] 200 vhost = self.vhostusers[vhost_id]
197 if vhost.mac_address == mac_address: 201 if vhost.mac_address == mac_address:
198 LOG.debug("classifier table already set: %d: %s", 202 LOG.debug("classifier table already set: %d: %s",
199 vhost_id, mac_address) 203 vhost_id, mac_address)
200 return 204 return
201 205
202 port = _rx_ring_port(vhost_id) 206 rx_port = _rx_ring_port(vhost_id)
203 self.spp_vf_api.set_classifier_table(vhost.sec_id, mac_address, port) 207 if vlan_id is None:
208 self.spp_vf_api.set_classifier_table(vhost.sec_id, mac_address,
209 rx_port)
210 else:
211 forwarder = "forward_%d_rx" % vhost_id
212 self.spp_vf_api.port_del(vhost.sec_id, _vhost_port(vhost_id), "rx",
213 forwarder)
214 self.spp_vf_api.port_add(vhost.sec_id, _vhost_port(vhost_id), "rx",
215 forwarder, "add_vlantag", vlan_id)
216 self.spp_vf_api.port_del(vhost.sec_id, rx_port, "tx", "classifier")
217 self.spp_vf_api.port_add(vhost.sec_id, rx_port, "tx", "classifier",
218 "del_vlantag")
219 self.spp_vf_api.set_classifier_table_with_vlan(vhost.sec_id,
220 mac_address,
221 rx_port, vlan_id)
222 self.spp_vf_api.flush(vhost.sec_id)
204 223
205 vhost.mac_address = mac_address 224 vhost.mac_address = mac_address
206 225
207 def clear_classifier_table(self, vhost_id, mac_address): 226 def clear_classifier_table(self, vhost_id, mac_address, vlan_id):
208 vhost = self.vhostusers[vhost_id] 227 vhost = self.vhostusers[vhost_id]
209 if vhost.mac_address == "unuse": 228 if vhost.mac_address == "unuse":
210 LOG.debug("classifier table already clear: %d", vhost_id) 229 LOG.debug("classifier table already clear: %d", vhost_id)
211 return 230 return
212 231
213 port = _rx_ring_port(vhost_id) 232 rx_port = _rx_ring_port(vhost_id)
214 self.spp_vf_api.clear_classifier_table(vhost.sec_id, mac_address, port) 233 if vlan_id is None:
234 self.spp_vf_api.clear_classifier_table(vhost.sec_id, mac_address,
235 rx_port)
236 else:
237 forwarder = "forward_%d_rx" % vhost_id
238 self.spp_vf_api.port_del(vhost.sec_id, _vhost_port(vhost_id), "rx",
239 forwarder)
240 self.spp_vf_api.port_add(vhost.sec_id, _vhost_port(vhost_id), "rx",
241 forwarder)
242 self.spp_vf_api.port_del(vhost.sec_id, rx_port, "tx", "classifier")
243 self.spp_vf_api.port_add(vhost.sec_id, rx_port, "tx", "classifier")
244 self.spp_vf_api.clear_classifier_table_with_vlan(vhost.sec_id,
245 mac_address,
246 rx_port, vlan_id)
247 self.spp_vf_api.flush(vhost.sec_id)
215 248
216 vhost.mac_address = "unuse" 249 vhost.mac_address = "unuse"
217 250
218 def _plug_port(self, port_id, vhost_id, mac_address): 251 def _plug_port(self, port_id, vhost_id, mac_address, vlan_id):
219 LOG.info("plug port %s: mac: %s, vhost: %d", port_id, 252 LOG.info("plug port %s: mac: %s, vhost: %d, vlan_id: %s", port_id,
220 mac_address, vhost_id) 253 mac_address, vhost_id, vlan_id)
221 254
222 self.set_classifier_table(vhost_id, mac_address) 255 self.set_classifier_table(vhost_id, mac_address, vlan_id)
223 256
224 key = etcd_key.port_status_key(self.host, port_id) 257 key = etcd_key.port_status_key(self.host, port_id)
225 self.etcd.put(key, "up") 258 self.etcd.put(key, "up")
226 259
227 def _unplug_port(self, port_id, vhost_id, mac_address): 260 def _unplug_port(self, port_id, vhost_id, mac_address, vlan_id):
228 LOG.info("unplug port %s: mac: %s, vhost: %d", port_id, 261 LOG.info("unplug port %s: mac: %s, vhost: %d, vlan_id: %s", port_id,
229 mac_address, vhost_id) 262 mac_address, vhost_id, vlan_id)
230 263
231 self.clear_classifier_table(vhost_id, mac_address) 264 self.clear_classifier_table(vhost_id, mac_address, vlan_id)
232 265
233 phys = self.vhostusers[vhost_id].physical_network 266 phys = self.vhostusers[vhost_id].physical_network
234 key = etcd_key.vhost_key(self.host, phys, vhost_id) 267 key = etcd_key.vhost_key(self.host, phys, vhost_id)
@@ -250,12 +283,13 @@ class SppAgent(object):
250 data = json.loads(value) 283 data = json.loads(value)
251 vhost_id = data['vhost_id'] 284 vhost_id = data['vhost_id']
252 mac_address = data['mac_address'] 285 mac_address = data['mac_address']
286 vlan_id = data.get('vlan_id')
253 # get action again to get the newest value 287 # get action again to get the newest value
254 op = self.etcd.get(etcd_key.action_key(self.host, port_id)) 288 op = self.etcd.get(etcd_key.action_key(self.host, port_id))
255 if op == "plug": 289 if op == "plug":
256 self._plug_port(port_id, vhost_id, mac_address) 290 self._plug_port(port_id, vhost_id, mac_address, vlan_id)
257 else: 291 else:
258 self._unplug_port(port_id, vhost_id, mac_address) 292 self._unplug_port(port_id, vhost_id, mac_address, vlan_id)
259 293
260 def port_plug_watch(self): 294 def port_plug_watch(self):
261 LOG.info("SPP port_plug_watch stated") 295 LOG.info("SPP port_plug_watch stated")
diff --git a/networking_spp/agent/spp_api.py b/networking_spp/agent/spp_api.py
index 950471d..e6ae2d8 100644
--- a/networking_spp/agent/spp_api.py
+++ b/networking_spp/agent/spp_api.py
@@ -45,9 +45,19 @@ class SppVfApi(object):
45 .format(**locals())) 45 .format(**locals()))
46 self._exec_command(sec_id, command) 46 self._exec_command(sec_id, command)
47 47
48 def port_add(self, sec_id, port, direction, comp_name): 48 def port_add(self, sec_id, port, direction, comp_name,
49 op=None, vlan_id=None):
49 command = ("port add {port} {direction} {comp_name}" 50 command = ("port add {port} {direction} {comp_name}"
50 .format(**locals())) 51 .format(**locals()))
52 if op:
53 command += " %s" % op
54 if op == "add_vlantag":
55 command += " %d 0" % vlan_id
56 self._exec_command(sec_id, command)
57
58 def port_del(self, sec_id, port, direction, comp_name):
59 command = ("port del {port} {direction} {comp_name}"
60 .format(**locals()))
51 self._exec_command(sec_id, command) 61 self._exec_command(sec_id, command)
52 62
53 def flush(self, sec_id): 63 def flush(self, sec_id):
@@ -60,10 +70,20 @@ class SppVfApi(object):
60 command = ("classifier_table add mac {mac_address} {port}" 70 command = ("classifier_table add mac {mac_address} {port}"
61 .format(**locals())) 71 .format(**locals()))
62 self._exec_command(sec_id, command) 72 self._exec_command(sec_id, command)
63 self.flush(sec_id)
64 73
65 def clear_classifier_table(self, sec_id, mac_address, port): 74 def clear_classifier_table(self, sec_id, mac_address, port):
66 command = ("classifier_table del mac {mac_address} {port}" 75 command = ("classifier_table del mac {mac_address} {port}"
67 .format(**locals())) 76 .format(**locals()))
68 self._exec_command(sec_id, command) 77 self._exec_command(sec_id, command)
69 self.flush(sec_id) 78
79 def set_classifier_table_with_vlan(self, sec_id, mac_address, port,
80 vlan_id):
81 command = ("classifier_table add vlan {vlan_id} {mac_address} {port}"
82 .format(**locals()))
83 self._exec_command(sec_id, command)
84
85 def clear_classifier_table_with_vlan(self, sec_id, mac_address, port,
86 vlan_id):
87 command = ("classifier_table del vlan {vlan_id} {mac_address} {port}"
88 .format(**locals()))
89 self._exec_command(sec_id, command)
diff --git a/networking_spp/mech_driver/mech_spp.py b/networking_spp/mech_driver/mech_spp.py
index f53cae6..d4a4f94 100644
--- a/networking_spp/mech_driver/mech_spp.py
+++ b/networking_spp/mech_driver/mech_spp.py
@@ -42,6 +42,9 @@ class SppMechanismDriver(api.MechanismDriver):
42 42
43 def _try_to_bind(self, context, segment): 43 def _try_to_bind(self, context, segment):
44 port_id = context.current['id'] 44 port_id = context.current['id']
45 if self.etcd.get(etcd_key.bind_port_key(context.host, port_id)):
46 # check already tried to bind at first.
47 return
45 phys = segment[api.PHYSICAL_NETWORK] 48 phys = segment[api.PHYSICAL_NETWORK]
46 prefix = etcd_key.vhost_phys_prefix(context.host, phys) 49 prefix = etcd_key.vhost_phys_prefix(context.host, phys)
47 vhost_id = None 50 vhost_id = None
@@ -56,7 +59,7 @@ class SppMechanismDriver(api.MechanismDriver):
56 LOG.warn("no vhost available for port %s", port_id) 59 LOG.warn("no vhost available for port %s", port_id)
57 return 60 return
58 61
59 self._add_bind_port(context, vhost_id) 62 self._add_bind_port(context, vhost_id, segment[api.SEGMENTATION_ID])
60 if not self._wait_plug_port(context.host, port_id): 63 if not self._wait_plug_port(context.host, port_id):
61 return 64 return
62 65
@@ -87,14 +90,17 @@ class SppMechanismDriver(api.MechanismDriver):
87 return 90 return
88 91
89 for segment in context.segments_to_bind: 92 for segment in context.segments_to_bind:
90 if (segment[api.NETWORK_TYPE] in [constants.TYPE_FLAT] and 93 if (segment[api.NETWORK_TYPE] in [constants.TYPE_FLAT,
91 segment[api.PHYSICAL_NETWORK] in phys_nets): 94 constants.TYPE_VLAN]
95 and segment[api.PHYSICAL_NETWORK] in phys_nets):
92 self._try_to_bind(context, segment) 96 self._try_to_bind(context, segment)
93 return 97 return
94 98
95 def _add_bind_port(self, context, vhost_id): 99 def _add_bind_port(self, context, vhost_id, vlan_id):
96 port = context.current 100 port = context.current
97 value = {'mac_address': port['mac_address'], 'vhost_id': int(vhost_id)} 101 value = {'mac_address': port['mac_address'], 'vhost_id': int(vhost_id)}
102 if vlan_id is not None:
103 value['vlan_id'] = vlan_id
98 value = json.dumps(value) 104 value = json.dumps(value)
99 key = etcd_key.bind_port_key(context.host, port['id']) 105 key = etcd_key.bind_port_key(context.host, port['id'])
100 self.etcd.put(key, value) 106 self.etcd.put(key, value)
diff --git a/networking_spp/tests/unit/test_mech_spp.py b/networking_spp/tests/unit/test_mech_spp.py
index fcf90bc..678dd04 100644
--- a/networking_spp/tests/unit/test_mech_spp.py
+++ b/networking_spp/tests/unit/test_mech_spp.py
@@ -67,6 +67,22 @@ class SppMechanismTestCase(base.BaseTestCase):
67 'SppMechanismDriver._spp_agent_alive') 67 'SppMechanismDriver._spp_agent_alive')
68 @mock.patch('networking_spp.mech_driver.mech_spp.' 68 @mock.patch('networking_spp.mech_driver.mech_spp.'
69 'SppMechanismDriver._try_to_bind') 69 'SppMechanismDriver._try_to_bind')
70 def test_bind_port_call_try_to_bind_vlan(self, mocked_try_to_bind,
71 mocked_spp_agent_alive):
72 # conditions to call _try_to_bind
73 agent_conf = [{'physical_network': 'phy_net'}]
74 self.driver.etcd.get.return_value = json.dumps(agent_conf)
75 mocked_spp_agent_alive.return_value = True
76 segment = {api.NETWORK_TYPE: constants.TYPE_VLAN,
77 api.PHYSICAL_NETWORK: 'phy_net'}
78 self.context.segments_to_bind = [segment]
79 self.driver.bind_port(self.context)
80 mocked_try_to_bind.aseert_called()
81
82 @mock.patch('networking_spp.mech_driver.mech_spp.'
83 'SppMechanismDriver._spp_agent_alive')
84 @mock.patch('networking_spp.mech_driver.mech_spp.'
85 'SppMechanismDriver._try_to_bind')
70 def test_bind_port_not_call_try_to_bind(self, mocked_try_to_bind, 86 def test_bind_port_not_call_try_to_bind(self, mocked_try_to_bind,
71 mocked_spp_agent_alive): 87 mocked_spp_agent_alive):
72 # conditions not to call _try_to_bind 88 # conditions not to call _try_to_bind
@@ -207,7 +223,7 @@ class SppMechanismTestCase(base.BaseTestCase):
207 mocked_action_key.side_effect = lambda host, port: host + port 223 mocked_action_key.side_effect = lambda host, port: host + port
208 mocked_bind_port_key.side_effect = lambda host, port: host + port 224 mocked_bind_port_key.side_effect = lambda host, port: host + port
209 self.driver.etcd.put.side_effect = self._put 225 self.driver.etcd.put.side_effect = self._put
210 self.driver._add_bind_port(self.context, '1') 226 self.driver._add_bind_port(self.context, '1', None)
211 args, w = self.driver.etcd.put.call_args 227 args, w = self.driver.etcd.put.call_args
212 self.assertEqual(args[0], 'host1AAAABBBB') 228 self.assertEqual(args[0], 'host1AAAABBBB')
213 self.assertEqual(self.driver.etcd.put.call_count, 2) 229 self.assertEqual(self.driver.etcd.put.call_count, 2)
@@ -227,12 +243,14 @@ class SppMechanismTestCase(base.BaseTestCase):
227 self.context.host = 'host1' 243 self.context.host = 'host1'
228 segment = {api.NETWORK_TYPE: constants.TYPE_FLAT, 244 segment = {api.NETWORK_TYPE: constants.TYPE_FLAT,
229 api.PHYSICAL_NETWORK: 'phy1', 245 api.PHYSICAL_NETWORK: 'phy1',
246 api.SEGMENTATION_ID: None,
230 api.ID: 'id'} 247 api.ID: 'id'}
231 mocked_vhost_phys_prefix.side_effect = \ 248 mocked_vhost_phys_prefix.side_effect = \
232 lambda a, b: '/x/y/%s/%s/' % (a, b) 249 lambda a, b: '/x/y/%s/%s/' % (a, b)
233 value = [('/x/y/host1/phy1/1', '1234AAAA'), 250 value = [('/x/y/host1/phy1/1', '1234AAAA'),
234 ('/x/y/host1/phy1/2', 'EEEE4444'), 251 ('/x/y/host1/phy1/2', 'EEEE4444'),
235 ('/x/y/host1/phy1/3', 'None')] 252 ('/x/y/host1/phy1/3', 'None')]
253 self.driver.etcd.get.return_value = None
236 self.driver.etcd.get_prefix.return_value = value 254 self.driver.etcd.get_prefix.return_value = value
237 self.driver.etcd.replace.return_value = True 255 self.driver.etcd.replace.return_value = True
238 mocked_wait_plug_port.return_value = True 256 mocked_wait_plug_port.return_value = True
@@ -269,6 +287,7 @@ class SppMechanismTestCase(base.BaseTestCase):
269 value = [('/x/y/host1/phy1/1', '1234AAAA'), 287 value = [('/x/y/host1/phy1/1', '1234AAAA'),
270 ('/x/y/host1/phy1/2', 'EEEE4444'), 288 ('/x/y/host1/phy1/2', 'EEEE4444'),
271 ('/x/y/host1/phy1/3', '99999999')] 289 ('/x/y/host1/phy1/3', '99999999')]
290 self.driver.etcd.get.return_value = None
272 self.driver.etcd.get_prefix.return_value = value 291 self.driver.etcd.get_prefix.return_value = value
273 self.driver.etcd.replace.return_value = True 292 self.driver.etcd.replace.return_value = True
274 293
diff --git a/networking_spp/tests/unit/test_spp_agent.py b/networking_spp/tests/unit/test_spp_agent.py
index c3cb08e..9419d0f 100644
--- a/networking_spp/tests/unit/test_spp_agent.py
+++ b/networking_spp/tests/unit/test_spp_agent.py
@@ -207,7 +207,7 @@ class SppAgentTestCase(base.BaseTestCase):
207 self.agent.vhostusers[1] = mock.Mock() 207 self.agent.vhostusers[1] = mock.Mock()
208 self.agent.vhostusers[1].mac_address = mac 208 self.agent.vhostusers[1].mac_address = mac
209 209
210 ret = self.agent.set_classifier_table(1, mac) 210 ret = self.agent.set_classifier_table(1, mac, None)
211 self.assertEqual(ret, None) 211 self.assertEqual(ret, None)
212 self.assertEqual(self.agent.spp_vf_api.set_classifier_table.call_count, 212 self.assertEqual(self.agent.spp_vf_api.set_classifier_table.call_count,
213 0) 213 0)
@@ -216,11 +216,25 @@ class SppAgentTestCase(base.BaseTestCase):
216 # conditions to exec correctly 216 # conditions to exec correctly
217 mac = '12:ab:34:cd:56:ef' 217 mac = '12:ab:34:cd:56:ef'
218 self.agent.vhostusers[1] = mock.Mock() 218 self.agent.vhostusers[1] = mock.Mock()
219 self.agent.vhostusers[1].mac_address = '32:ab:34:cd:56:ef' 219 self.agent.vhostusers[1].mac_address = 'unuse'
220 220
221 self.agent.set_classifier_table(1, mac) 221 self.agent.set_classifier_table(1, mac, None)
222 self.assertEqual(self.agent.spp_vf_api.set_classifier_table.call_count, 222 self.assertEqual(self.agent.spp_vf_api.set_classifier_table.call_count,
223 1) 223 1)
224 self.assertEqual(self.agent.spp_vf_api.flush.call_count, 1)
225 self.assertEqual(self.agent.vhostusers[1].mac_address, mac)
226
227 def test_set_classifier_table_with_vlan(self):
228 mac = '12:ab:34:cd:56:ef'
229 self.agent.vhostusers[1] = mock.Mock()
230 self.agent.vhostusers[1].mac_address = 'unuse'
231
232 self.agent.set_classifier_table(1, mac, 10)
233 self.assertEqual(self.agent.spp_vf_api.port_add.call_count, 2)
234 self.assertEqual(self.agent.spp_vf_api.port_del.call_count, 2)
235 self.assertEqual(
236 self.agent.spp_vf_api.set_classifier_table_with_vlan.call_count, 1)
237 self.assertEqual(self.agent.spp_vf_api.flush.call_count, 1)
224 self.assertEqual(self.agent.vhostusers[1].mac_address, mac) 238 self.assertEqual(self.agent.vhostusers[1].mac_address, mac)
225 239
226 def test_clear_classifier_table_con1(self): 240 def test_clear_classifier_table_con1(self):
@@ -229,7 +243,7 @@ class SppAgentTestCase(base.BaseTestCase):
229 self.agent.vhostusers[1] = mock.Mock() 243 self.agent.vhostusers[1] = mock.Mock()
230 self.agent.vhostusers[1].mac_address = mac 244 self.agent.vhostusers[1].mac_address = mac
231 245
232 ret = self.agent.clear_classifier_table(1, mac) 246 ret = self.agent.clear_classifier_table(1, mac, None)
233 self.assertEqual(ret, None) 247 self.assertEqual(ret, None)
234 count = self.agent.spp_vf_api.clear_classifier_table.call_count 248 count = self.agent.spp_vf_api.clear_classifier_table.call_count
235 self.assertEqual(count, 0) 249 self.assertEqual(count, 0)
@@ -240,9 +254,23 @@ class SppAgentTestCase(base.BaseTestCase):
240 self.agent.vhostusers[1] = mock.Mock() 254 self.agent.vhostusers[1] = mock.Mock()
241 self.agent.vhostusers[1].mac_address = mac 255 self.agent.vhostusers[1].mac_address = mac
242 256
243 self.agent.clear_classifier_table(1, mac) 257 self.agent.clear_classifier_table(1, mac, None)
244 count = self.agent.spp_vf_api.clear_classifier_table.call_count 258 count = self.agent.spp_vf_api.clear_classifier_table.call_count
245 self.assertEqual(count, 1) 259 self.assertEqual(count, 1)
260 self.assertEqual(self.agent.spp_vf_api.flush.call_count, 1)
261 self.assertEqual(self.agent.vhostusers[1].mac_address, 'unuse')
262
263 def test_clear_classifier_table_with_vlan(self):
264 mac = '12:ab:34:cd:56:ef'
265 self.agent.vhostusers[1] = mock.Mock()
266 self.agent.vhostusers[1].mac_address = mac
267
268 self.agent.clear_classifier_table(1, mac, 10)
269 self.assertEqual(self.agent.spp_vf_api.port_add.call_count, 2)
270 self.assertEqual(self.agent.spp_vf_api.port_del.call_count, 2)
271 cnt = self.agent.spp_vf_api.clear_classifier_table_with_vlan.call_count
272 self.assertEqual(cnt, 1)
273 self.assertEqual(self.agent.spp_vf_api.flush.call_count, 1)
246 self.assertEqual(self.agent.vhostusers[1].mac_address, 'unuse') 274 self.assertEqual(self.agent.vhostusers[1].mac_address, 'unuse')
247 275
248 @mock.patch('networking_spp.agent.spp_agent.SppAgent.set_classifier_table') 276 @mock.patch('networking_spp.agent.spp_agent.SppAgent.set_classifier_table')
@@ -254,8 +282,8 @@ class SppAgentTestCase(base.BaseTestCase):
254 self.agent.host = 'host1' 282 self.agent.host = 'host1'
255 key = '/spp/openstack/port_status/host1/111' 283 key = '/spp/openstack/port_status/host1/111'
256 284
257 self.agent._plug_port(port_id, vhost_id, mac) 285 self.agent._plug_port(port_id, vhost_id, mac, None)
258 m_set.assert_called_with(vhost_id, mac) 286 m_set.assert_called_with(vhost_id, mac, None)
259 self.agent.etcd.put.assert_called_with(key, "up") 287 self.agent.etcd.put.assert_called_with(key, "up")
260 288
261 @mock.patch('networking_spp.common.etcd_key.bind_port_key') 289 @mock.patch('networking_spp.common.etcd_key.bind_port_key')
@@ -281,8 +309,8 @@ class SppAgentTestCase(base.BaseTestCase):
281 self.agent.vhostusers = {1: vhostuser} 309 self.agent.vhostusers = {1: vhostuser}
282 m_vhost_key.return_value = 'key' 310 m_vhost_key.return_value = 'key'
283 311
284 self.agent._unplug_port(port_id, vhost_id, mac) 312 self.agent._unplug_port(port_id, vhost_id, mac, None)
285 m_clear.assert_called_with(vhost_id, mac) 313 m_clear.assert_called_with(vhost_id, mac, None)
286 m_vhost_key.assert_called_with('host', 'phy_net', vhost_id) 314 m_vhost_key.assert_called_with('host', 'phy_net', vhost_id)
287 self.agent.etcd.replace.assert_called_once() 315 self.agent.etcd.replace.assert_called_once()
288 m_bind_port_key.assert_called_with('host', port_id) 316 m_bind_port_key.assert_called_with('host', port_id)
@@ -308,18 +336,19 @@ class SppAgentTestCase(base.BaseTestCase):
308 self.agent.etcd.get.side_effect = [value, 'plug'] 336 self.agent.etcd.get.side_effect = [value, 'plug']
309 337
310 self.agent._do_plug_unplug(port_id) 338 self.agent._do_plug_unplug(port_id)
311 m_plug_port.assert_called_with(port_id, 1, '12:34:56:78:90:ab') 339 m_plug_port.assert_called_with(port_id, 1, '12:34:56:78:90:ab', None)
312 340
313 @mock.patch('networking_spp.agent.spp_agent.SppAgent._unplug_port') 341 @mock.patch('networking_spp.agent.spp_agent.SppAgent._unplug_port')
314 def test_do_plug_unplug_con3(self, m_unplug_port): 342 def test_do_plug_unplug_con3(self, m_unplug_port):
315 # conditions to exec the else statement 343 # conditions to exec the else statement
316 port_id = 111 344 port_id = 111
317 data = {"vhost_id": 1, 'mac_address': '12:34:56:78:90:ab'} 345 data = {"vhost_id": 1, 'mac_address': '12:34:56:78:90:ab',
346 'vlan_id': 10}
318 value = json.dumps(data) 347 value = json.dumps(data)
319 self.agent.etcd.get.side_effect = [value, ''] 348 self.agent.etcd.get.side_effect = [value, 'unplug']
320 349
321 self.agent._do_plug_unplug(port_id) 350 self.agent._do_plug_unplug(port_id)
322 m_unplug_port.assert_called_with(port_id, 1, '12:34:56:78:90:ab') 351 m_unplug_port.assert_called_with(port_id, 1, '12:34:56:78:90:ab', 10)
323 352
324 @mock.patch('networking_spp.common.etcd_key.action_host_prefix') 353 @mock.patch('networking_spp.common.etcd_key.action_host_prefix')
325 @mock.patch('networking_spp.agent.spp_agent.SppAgent._do_plug_unplug') 354 @mock.patch('networking_spp.agent.spp_agent.SppAgent._do_plug_unplug')
diff --git a/networking_spp/tests/unit/test_spp_api.py b/networking_spp/tests/unit/test_spp_api.py
index e2ae3ac..ef9a754 100644
--- a/networking_spp/tests/unit/test_spp_api.py
+++ b/networking_spp/tests/unit/test_spp_api.py
@@ -77,6 +77,41 @@ class SppVfApiTestCase(base.BaseTestCase):
77 77
78 @mock.patch('networking_spp.agent.spp_api.SppVfApi' 78 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
79 '._exec_command') 79 '._exec_command')
80 def test_port_add_add_vlantag(self, m_exec_command):
81 port = 'port'
82 direction = 'd'
83 comp_name = 'cn'
84 op = 'add_vlantag'
85 vlan_id = 10
86 command = "port add %s %s %s %s %s 0" % (port, direction, comp_name,
87 op, vlan_id)
88 self.spp_api.port_add(self.sec_id, port, direction, comp_name,
89 op, vlan_id)
90 m_exec_command.assert_called_with(self.sec_id, command)
91
92 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
93 '._exec_command')
94 def test_port_add_del_vlantag(self, m_exec_command):
95 port = 'port'
96 direction = 'd'
97 comp_name = 'cn'
98 op = 'del_vlantag'
99 command = "port add %s %s %s %s" % (port, direction, comp_name, op)
100 self.spp_api.port_add(self.sec_id, port, direction, comp_name, op)
101 m_exec_command.assert_called_with(self.sec_id, command)
102
103 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
104 '._exec_command')
105 def test_port_del(self, m_exec_command):
106 port = 'port'
107 direction = 'd'
108 comp_name = 'cn'
109 command = "port del %s %s %s" % (port, direction, comp_name)
110 self.spp_api.port_del(self.sec_id, port, direction, comp_name)
111 m_exec_command.assert_called_with(self.sec_id, command)
112
113 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
114 '._exec_command')
80 def test_flush(self, m_exec_command): 115 def test_flush(self, m_exec_command):
81 self.spp_api.flush(self.sec_id) 116 self.spp_api.flush(self.sec_id)
82 m_exec_command.assert_called_with(self.sec_id, 'flush') 117 m_exec_command.assert_called_with(self.sec_id, 'flush')
@@ -89,24 +124,40 @@ class SppVfApiTestCase(base.BaseTestCase):
89 124
90 @mock.patch('networking_spp.agent.spp_api.SppVfApi' 125 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
91 '._exec_command') 126 '._exec_command')
92 @mock.patch('networking_spp.agent.spp_api.SppVfApi' 127 def test_set_classifier_table(self, m_exec_command):
93 '.flush')
94 def test_set_classifier_table(self, m_flush, m_exec_command):
95 mac = 'mac' 128 mac = 'mac'
96 port = 'port' 129 port = 'port'
97 command = "classifier_table add mac %s %s" % (mac, port) 130 command = "classifier_table add mac %s %s" % (mac, port)
98 self.spp_api.set_classifier_table(self.sec_id, mac, port) 131 self.spp_api.set_classifier_table(self.sec_id, mac, port)
99 m_exec_command.assert_called_with(self.sec_id, command) 132 m_exec_command.assert_called_with(self.sec_id, command)
100 m_flush.assert_called_with(self.sec_id)
101 133
102 @mock.patch('networking_spp.agent.spp_api.SppVfApi' 134 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
103 '._exec_command') 135 '._exec_command')
104 @mock.patch('networking_spp.agent.spp_api.SppVfApi' 136 def test_clear_classifier_table(self, m_exec_command):
105 '.flush')
106 def test_clear_classifier_table(self, m_flush, m_exec_command):
107 mac = 'mac' 137 mac = 'mac'
108 port = 'port' 138 port = 'port'
109 command = "classifier_table del mac %s %s" % (mac, port) 139 command = "classifier_table del mac %s %s" % (mac, port)
110 self.spp_api.clear_classifier_table(self.sec_id, mac, port) 140 self.spp_api.clear_classifier_table(self.sec_id, mac, port)
111 m_exec_command.assert_called_with(self.sec_id, command) 141 m_exec_command.assert_called_with(self.sec_id, command)
112 m_flush.assert_called_with(self.sec_id) 142
143 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
144 '._exec_command')
145 def test_set_classifier_table_with_vlan(self, m_exec_command):
146 mac = 'mac'
147 port = 'port'
148 vlan_id = 10
149 command = "classifier_table add vlan %s %s %s" % (vlan_id, mac, port)
150 self.spp_api.set_classifier_table_with_vlan(self.sec_id, mac, port,
151 vlan_id)
152 m_exec_command.assert_called_with(self.sec_id, command)
153
154 @mock.patch('networking_spp.agent.spp_api.SppVfApi'
155 '._exec_command')
156 def test_clear_classifier_table_with_vlan(self, m_exec_command):
157 mac = 'mac'
158 port = 'port'
159 vlan_id = 10
160 command = "classifier_table del vlan %s %s %s" % (vlan_id, mac, port)
161 self.spp_api.clear_classifier_table_with_vlan(self.sec_id, mac, port,
162 vlan_id)
163 m_exec_command.assert_called_with(self.sec_id, command)
diff --git a/networking_spp/tests/unit/test_spp_manager.py b/networking_spp/tests/unit/test_spp_manager.py
index b0fae57..fc968f8 100644
--- a/networking_spp/tests/unit/test_spp_manager.py
+++ b/networking_spp/tests/unit/test_spp_manager.py
@@ -15,7 +15,6 @@
15 15
16import json 16import json
17import mock 17import mock
18from time import sleep
19 18
20from networking_spp.agent import spp_manager 19from networking_spp.agent import spp_manager
21from networking_spp.agent.spp_manager import SPP_PRIMATY_SERVICE 20from networking_spp.agent.spp_manager import SPP_PRIMATY_SERVICE
@@ -296,5 +295,8 @@ class SppManagerTestCase(base.BaseTestCase):
296 self.assertEqual(m_wait_service.call_count, 0) 295 self.assertEqual(m_wait_service.call_count, 0)
297 296
298 def test_wait_service_initialized(self): 297 def test_wait_service_initialized(self):
298 def dummy(sec):
299 pass
300
299 # exec try statement 301 # exec try statement
300 self.assertEqual(None, spp_manager._wait_service_initialized(sleep, 2)) 302 self.assertEqual(None, spp_manager._wait_service_initialized(dummy, 2))