diff --git a/openstack_dashboard/don/ovs/collector.py b/openstack_dashboard/don/ovs/collector.py index 2593ae2..0c64856 100644 --- a/openstack_dashboard/don/ovs/collector.py +++ b/openstack_dashboard/don/ovs/collector.py @@ -56,27 +56,49 @@ info = { } -def add_new_command(cmd_dict, cmd_key, cmd): - if cmd_dict.has_key(cmd_key): +def add_new_command(cmds, cmd_key, cmd): + """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') - return - cmd_dict[cmd_key] = cmd + else: + cmds[cmd_key] = cmd -def record_linuxbridge(bridge, interface_list): - brctl_dict = info['brctl'] - if brctl_dict.has_key(bridge): +# TODO(abregman): create collector class and make bridges its attribute +bridges = {} + + +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!') - brctl_dict[bridge] = {'interfaces': interface_list} + bridges[bridge] = {'interfaces': interface_list} -def get_bridge_entry(br): - bridge_dict = info['bridges'] - if not bridge_dict.has_key(br): - common.error('Bridge ' + br + ' does not exist! Supported bridges: ' + - str(bridge_dict.keys())) +def get_bridge_entry(bridge): + """Returns given bridge dictionary. + + :param bridge: the name of the bridge. + """ + bridges = info['bridges'] + if not bridges.has_key(bridge): + common.error('Bridge ' + bridge + + ' does not exist! Supported bridges: ' + + str(bridges.keys())) return None - return bridge_dict.get(br) + else: + return bridges.get(bridge) # 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 # next bridge if bridge: - record_linuxbridge(bridge, interfaces) + record_linuxbridge(bridges, bridge, interfaces) interfaces = [] bridge = m.group(1) interfaces.append(m.group(2)) @@ -152,7 +174,7 @@ def brctl_show_parser(parse_this): # handle the last bridge if bridge: - record_linuxbridge(bridge, interfaces) + record_linuxbridge(bridges, bridge, interfaces) ''' ubuntu@ubuntu-VirtualBox:~/don$ sudo ovs-vsctl show diff --git a/openstack_dashboard/tests/test_collector.py b/openstack_dashboard/tests/test_collector.py index 077bad3..f05c749 100644 --- a/openstack_dashboard/tests/test_collector.py +++ b/openstack_dashboard/tests/test_collector.py @@ -36,3 +36,36 @@ class TestOvsCollector(base.TestCase): f.write("\n".join(lines)) env = collector.get_env(os.path.basename(self.tmp_fp)) 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))