Modify collector and add new tests

Added the following tests:

* test_get_bridge_entry

* test_record_linuxbridge

* test_add_new_command

Change-Id: Ifd74938e30e182c5243b5de89864bf8a03386ab6
This commit is contained in:
Arie
2016-10-21 21:24:21 +03:00
parent e834150135
commit 7655ba63c7
2 changed files with 71 additions and 16 deletions

View File

@@ -56,27 +56,49 @@ info = {
} }
def add_new_command(cmd_dict, cmd_key, cmd): def add_new_command(cmds, cmd_key, cmd):
if cmd_dict.has_key(cmd_key): """Add given command to the commands dictionary.
:param cmds: the commands dictionary.
:param cmd_key: the key of the dictionary where to add the command.
:param cmd: the dictionary of the command.
"""
if cmds.has_key(cmd_key):
common.error(cmd_key + ' already exists in command dictionary') common.error(cmd_key + ' already exists in command dictionary')
return else:
cmd_dict[cmd_key] = cmd cmds[cmd_key] = cmd
def record_linuxbridge(bridge, interface_list): # TODO(abregman): create collector class and make bridges its attribute
brctl_dict = info['brctl'] bridges = {}
if brctl_dict.has_key(bridge):
def record_linuxbridge(bridges, bridge, interface_list):
"""Add given bridge and its interfaces to the bridges dictionary.
The bridge is coming from brctl command.
:param bridge: the name of the bridge.
:param interface_list: the interfaces of the bridge.
"""
if bridges.has_key(bridge):
common.error('Bridge ' + bridge + ' repeated! Overwriting!') common.error('Bridge ' + bridge + ' repeated! Overwriting!')
brctl_dict[bridge] = {'interfaces': interface_list} bridges[bridge] = {'interfaces': interface_list}
def get_bridge_entry(br): def get_bridge_entry(bridge):
bridge_dict = info['bridges'] """Returns given bridge dictionary.
if not bridge_dict.has_key(br):
common.error('Bridge ' + br + ' does not exist! Supported bridges: ' + :param bridge: the name of the bridge.
str(bridge_dict.keys())) """
bridges = info['bridges']
if not bridges.has_key(bridge):
common.error('Bridge ' + bridge +
' does not exist! Supported bridges: ' +
str(bridges.keys()))
return None return None
return bridge_dict.get(br) else:
return bridges.get(bridge)
# Parser functions (for each command). Each function has the sample input # Parser functions (for each command). Each function has the sample input
@@ -141,7 +163,7 @@ def brctl_show_parser(parse_this):
# We already have a bridge, that means we are now lookign at the # We already have a bridge, that means we are now lookign at the
# next bridge # next bridge
if bridge: if bridge:
record_linuxbridge(bridge, interfaces) record_linuxbridge(bridges, bridge, interfaces)
interfaces = [] interfaces = []
bridge = m.group(1) bridge = m.group(1)
interfaces.append(m.group(2)) interfaces.append(m.group(2))
@@ -152,7 +174,7 @@ def brctl_show_parser(parse_this):
# handle the last bridge # handle the last bridge
if bridge: if bridge:
record_linuxbridge(bridge, interfaces) record_linuxbridge(bridges, bridge, interfaces)
''' '''
ubuntu@ubuntu-VirtualBox:~/don$ sudo ovs-vsctl show ubuntu@ubuntu-VirtualBox:~/don$ sudo ovs-vsctl show

View File

@@ -36,3 +36,36 @@ class TestOvsCollector(base.TestCase):
f.write("\n".join(lines)) f.write("\n".join(lines))
env = collector.get_env(os.path.basename(self.tmp_fp)) env = collector.get_env(os.path.basename(self.tmp_fp))
self.assertEqual(env, dict(x='2', y='3')) self.assertEqual(env, dict(x='2', y='3'))
def test_add_new_command(self):
commands = {}
cmd_key = 'netns_test'
cmd = {'cmd': 'echo netns test'}
collector.add_new_command(commands, cmd_key, cmd)
self.assertEqual(len(commands), 1)
self.assertEqual(commands[cmd_key], cmd)
# Try to add the same command again
collector.add_new_command(commands, cmd_key, cmd)
self.assertEqual(len(commands), 1)
def test_record_linuxbridge(self):
bridges = {}
bridge = 'test_bridge'
interfaces_list = 'eth0'
collector.record_linuxbridge(bridges, bridge, interfaces_list)
self.assertEqual(len(bridges), 1)
self.assertEqual(bridges[bridge], dict(interfaces=interfaces_list))
def test_get_bridge_entry(self):
supported_bridge = 'br-ex'
false_bridge = 'br-test'
self.assertIsNone(collector.get_bridge_entry(false_bridge))
self.assertEqual(dict(ports={}),
collector.get_bridge_entry(supported_bridge))