def test_state_access_port(self, mock_symlink, mock_os_path, mock_oneline): mock_subint = MagicMock() self.iface.get_sub_interfaces = mock_subint # bridgemember is access port mock_subint = [] mock_symlink.return_value = 'br10' values = { '/sys/class/net/eth1/brport': True, '/sys/class/net/eth1/brport/bridge/bridge/stp_state': '1' } values2 = { '/sys/class/net/eth1/brport/state': '3', '/sys/class/net/eth1/brport/bridge/bridge/root_port': 'aaa', '/sys/class/net/eth1/brport/bridge/bridge/stp_state': '1', '/sys/class/net/eth1/brport/port_id': 'aaa' } mock_oneline.side_effect = mod_args_generator(values2) mock_os_path.side_effect = mod_args_generator(values) briface = linux_bridge.Bridge('br10') linux_bridge.BRIDGE_CACHE['br10'] = briface assert_equals(self.stp.state, { 'disabled': [], 'blocking': [], 'forwarding': [briface], 'root': [briface], 'intransition': [], 'stp_disabled': [] })
def test_stp_state(self, mock_read_from_sys, mock_exists, mock_subints): # test if iface is in stp bridge values = { '/sys/class/net/eth1/brport/bridge/bridge/stp_state': True } values2 = { 'brport/bridge/bridge/stp_state': '2' } mock_exists.side_effect = mod_args_generator(values) mock_read_from_sys.side_effect = mod_args_generator(values2) _output = self.iface.stp_state() assert_equals(_output, '2') # test if subint is in stp bridge # assumption here is that only one type of stp bridge is running. either its # kernel or mstpd. even though in reality both I believe can coexist. values = { '/sys/class/net/eth1/brport/bridge/bridge/stp_state': False, '/sys/class/net/eth1.100/brport/bridge/bridge/stp_state': True } values2 = { 'brport/bridge/bridge/stp_state': '2' } mock_exists.side_effect = mod_args_generator(values) mock_read_from_sys.side_effect = mod_args_generator(values2) mock_subints.return_value = ['eth1.100', 'eth1.101', 'eth1.110'] _output = self.iface.stp_state() assert_equals(_output, '2')
def test_get_list_of_bridge_members(self, mock_listdirs): bridgemems = ['eth8', 'eth9'] mock_listdirs.return_value = bridgemems assert_equals(sorted(list(self.iface.members.keys())), sorted(bridgemems)) assert_equals(isinstance(self.iface.members.get('eth8'), linux_bridge.BridgeMember), True) mock_listdirs.assert_called_with('/sys/class/net/br0/brif')
def test_get_bond_xmit_hash_policy(self, mock_file_oneline): mock_file_oneline.return_value = "layer3+4 1" assert_equals(self.iface.hash_policy, "1") mock_file_oneline.assert_called_with("/sys/class/net/bond0/bonding/xmit_hash_policy") # test failing to find something mock_file_oneline.return_value = None assert_equals(self.iface.hash_policy, None)
def test_update_port_vlan_list_delete_vlan_that_is_not_there(self, mock_iface): instance = mock_iface.return_value instance.vlan_list = ['1', '2'] self.myobject.delete_vlan = True self.myobject.vlan = '3' self.myobject.update_port_vlan_list() assert_equals(self.myobject.port_vids, ['1-2'])
def test_provider_check(mock_pkg_requires, mock_import, mock_glob, mock_os_dirname): """ test os discovery """ # return a directory with 3 OS types, each will return different priorities # choose the one with the highest priority mock_glob.return_value = ['path/providers/linux', 'path/providers/debian', 'path/providers/ubuntu'] mock_linux = MagicMock() mock_linux.name_and_priority.return_value = {'Linux': 0} mock_debian = MagicMock() mock_debian.name_and_priority.return_value = {'Debian': 1} mock_debian = MagicMock() mock_debian.name_and_priority.return_value = {'Ubuntu': 2} mock_os_dirname.return_value = 'netshowlib' values = { 'netshowlib.linux.provider_discovery': mock_linux, 'netshowlib.debian.provider_discovery': mock_debian, 'netshowlib.ubuntu.provider_discovery': mock_debian } mock_import.side_effect = mod_args_generator(values) mock_me = MagicMock() mock_me.location = '/me/and/my/loc' mock_pkg_requires.return_value = [mock_me] assert_equals(nn.provider_check(), 'ubuntu') mock_glob.assert_called_with('/me/and/my/loc/../../../share/netshow-lib/providers/*')
def test_img_install(mock_module, mock_run_cl_cmd, mock_get_slot_info, mock_switch_slot): """ Test install image """ instance = mock_module.return_value instance.params.get.side_effect = mod_args_generator(arg_values) instance.sw_version = '2.0.3' install_img(instance) cmd = '/usr/cumulus/bin/cl-img-install -f %s' % \ (arg_values.get('src')) mock_run_cl_cmd.assert_called_with(instance, cmd) instance.exit_json.assert_called_with( msg='Cumulus Linux Version 2.0.3 ' + 'successfully installed in alternate slot', changed=True) # test using when switching slots values = arg_values.copy() values['switch_slot'] = True instance.params.get.side_effect = mod_args_generator(values) mock_get_slot_info.return_value = {'1': {'version': '2.0.2', 'active': True, 'primary': True}, '2': {'version': '2.0.3'}} instance.sw_version = '2.0.3' install_img(instance) assert_equals(mock_switch_slot.call_count, 1)
def test_speed(self, mock_read_sys): values = {('speed', ): '1000', ('carrier',): '1', ('operstate',): 'up'} self.piface.iface._asic = {'asicname': 'xe2', 'initial_speed': '10000'} mock_read_sys.side_effect = mod_args_generator(values) assert_equals(self.piface.speed, '1G(sfp)')
def test_check_fw_print_env(mock_module, mock_run_cmd): slot_num = '1' instance = mock_module.return_value mock_run_cmd.return_value = ['2.0.2-a8ec422-201404161914-final'] assert_equals(check_fw_print_env(instance, slot_num), '2.0.2') cmd = '/usr/sbin/fw_printenv -n cl.ver%s' % (slot_num) mock_run_cmd.assert_called_with(instance, cmd)
def test_port_category(self, mock_is_trunk): # if trunk mock_is_trunk.return_value = True assert_equals(self.piface.port_category, 'trunk/l2') # if not trunk mock_is_trunk.return_value = False assert_equals(self.piface.port_category, 'access/l2')
def test_connector_type(self): self.piface.iface._asic = {'asicname': 'ge1', 'initial_speed': '1000'} assert_equals(self.piface.connector_type, 'rj45') self.piface.iface._connector_type = None self.piface.iface._name = 'swp2s0' self.piface.iface._asic = {'asicname': 'xe1', 'initial_speed': '1000'} assert_equals(self.piface.connector_type, '4x10g')
def test_check_mod_args( mock_module, mock_check_dsl_dependencies, mock_has_interface_config, mock_add_global_ospf, mock_config_ospf_int ): """ cl_quagga_ospf - check mod args """ instance = mock_module.return_value instance.params.get.return_value = MagicMock() main() mock_module.assert_called_with( argument_spec={ "router_id": {"type": "str"}, "area": {"type": "str"}, "reference_bandwidth": {"default": "40000", "type": "str"}, "saveconfig": { "type": "bool", "default": False, "choices": ["yes", "on", "1", "true", 1, "no", "off", "0", "false", 0], }, "state": {"type": "str", "choices": ["present", "absent"]}, "cost": {"type": "str"}, "interface": {"type": "str"}, "passive": {"type": "bool", "choices": ["yes", "on", "1", "true", 1, "no", "off", "0", "false", 0]}, "point2point": {"type": "bool", "choices": ["yes", "on", "1", "true", 1, "no", "off", "0", "false", 0]}, }, mutually_exclusive=[["reference_bandwidth", "interface"], ["router_id", "interface"]], ) assert_equals( mock_check_dsl_dependencies.call_args_list[0], mock.call(instance, ["cost", "state", "area", "point2point", "passive"], "interface", "swp1"), ) instance.exit_json.assert_called_with(msg="no change", changed=False)
def test_many_ifaces_json_output(self, mock_cli_ifaces, mock_json_ifaces): # if json is false get cli output mock_json_ifaces.return_value = 'json_output' self.showint.use_json = True _output = self.showint.print_many_ifaces() mock_json_ifaces.assert_called_with('l2') assert_equals(_output, 'json_output')
def test_platform_info(self, mock_cumulus_platform, mock_exec): mock_exec.return_value = 'dell,s6000' instance = mock_cumulus_platform.return_value instance.run.return_value = 'Get Some Cool Output' assert_equals(self.systemsummary.platform_info, 'Get Some Cool Output') mock_cumulus_platform.assert_called_with('dell,s6000')
def test_unconfigure_interfaces(mock_module, mock_unlink): mock_module.custom_currentportlist = ['swp1', 'swp2', 'bond0', 'bond1'] mock_module.custom_allowedportlist = ['swp1', 'swp2'] cl_int_policy.unconfigure_interfaces(mock_module) assert_equals(mock_unlink.call_count, 2) assert_equals(mock_module.msg, 'remove config for interfaces bond0, bond1')
def test_convert_allowed_list_to_port_range(mock_module): """ cl_int_policy - test getting allow list """ mock_module.custom_allowedportlist = [] mock_module.params = {'allowed': ['swp1', 'swp10-11', 'bond0-2']} cl_int_policy.convert_allowed_list_to_port_range(mock_module) assert_equals(mock_module.custom_allowedportlist, ['swp1', 'swp10', 'swp11', 'bond0', 'bond1', 'bond2'])
def test_getting_list_of_ports(mock_module, mock_read_dir): """ cl_int_policy - get list of current configured ports """ mock_read_dir.return_value = ['swp1', 'swp2'] mock_module.params = {'location': '/etc/network/interfaces.d'} cl_int_policy.read_current_int_dir(mock_module) mock_read_dir.assert_called_with('/etc/network/interfaces.d') assert_equals(mock_module.custom_currentportlist, ['swp1', 'swp2'])
def test_ifacelist_l2_subints(self, mock_bridgemem_test, mock_cache, mock_portname_list, mock_exists): mock_exists.return_value = True # make sure L2 subints don't get into the list mock_bridgemem_test.return_value = True mock_portname_list.return_value = ['eth1.1', 'eth2.1'] assert_equals(self.showint.ifacelist.get('all'), OrderedDict())
def test_modify_switch_config(mock_module, mock_os): """ cl_interface - test with inet method """ instance = mock_module.return_value mock_os.path.exists.return_value = False testwrite = open('/tmp/test.me', 'w') iface = loop_iface() with mock.patch('__builtin__.open') as mock_open: mock_open.return_value = testwrite modify_switch_config(instance, iface) mock_open.assert_called_with('/etc/network/ansible/lo', 'w') mock_os.path.exists.assert_called_with('/etc/network/ansible/') fstr = 'auto lo\n' fstr += 'iface lo inet loopback\n' fstr += ' address 10:3:3::3/128\n' fstr += ' address 10.3.3.3/32\n' output = open('/tmp/test.me').readlines() assert_equals(''.join(output), fstr) # test when addr_method is not present iface = empty_iface() testwrite = open('/tmp/test.me', 'w') with mock.patch('__builtin__.open') as mock_open: mock_open.return_value = testwrite modify_switch_config(instance, iface) mock_open.assert_called_with('/etc/network/ansible/swp1', 'w') fstr = 'auto swp1\n' fstr += 'iface swp1\n' output = open('/tmp/test.me').readlines() assert_equals(''.join(output), fstr)
def test_port_category(self, mock_is_l3): # if l3 mock_is_l3.return_value = True assert_equals(self.piface.port_category, 'bridge/l3') # if l3 is not true mock_is_l3.return_value = False assert_equals(self.piface.port_category, 'bridge/l2')
def test_get_todays_date(): """ cl_license. test that getting current date returns a date, not a string or None """ result = get_todays_date() assert_equals(isinstance(result, datetime), True)
def test_parse_ip_cache(self): """ testing parsing ip cache info """ _output = io.open('tests/test_netshowlib/ip_addr_show.txt').read() output = io.StringIO(_output) result = ip_address_mod.parse_ip_cache(output) assert_equals( result, { 'vnet-v0': {'ipv6': [], 'ipv4': ['192.168.1.1/23']}, 'lo': { 'ipv4': ['127.0.0.1/8'], 'ipv6': ['::1/128'] }, 'net2compute': { 'ipv4': ['192.168.50.1/24'], 'ipv6': [] }, 'virbr0': { 'ipv4': ['192.168.122.1/24'], 'ipv6': [] }, 'vnet0': { 'ipv4': [], 'ipv6': [] }, 'eth0': { 'ipv4': ['192.168.0.33/24'], 'ipv6': [] } })
def test_getting_primary_slot_num(mock_module, mock_run_cmd): """ Test getting primary slot number """ instance = mock_module.return_value mock_run_cmd.return_value = ['1'] assert_equals(get_primary_slot_num(instance), '1')
def test_run_int_doesnt_exist(self, mock_cacheinfo): mock_cacheinfo.return_value = {'eth2': {'ipv4': {'10.1.1.1': '11:22:33:44:55:66'}, 'ipv6': {}}} self.ipneigh.run() assert_equals(self.ipneigh.ipv4, {})
def test_get_bond_mode(self, mock_file_oneline): mock_file_oneline.return_value = "802.3ad 4" assert_equals(self.iface.mode, "4") mock_file_oneline.assert_called_with("/sys/class/net/bond0/bonding/mode") # test failing to find something mock_file_oneline.return_value = None assert_equals(self.iface.mode, None)
def test_appending_ansible_to_etc_network_interface(mock_module): instance = mock_module.return_value origfile = open('tests/interface_with_ansible.txt', 'r') newfile = open('tests/output.txt', 'w') mock_open = mock.Mock(side_effect=[origfile, newfile]) iface = {'name': 'swp2'} with mock.patch('__builtin__.open', mock_open): remove_config_from_etc_net_interfaces(instance, iface) expected = [call('/etc/network/interfaces', 'r'), call('/etc/network/interfaces', 'w')] assert_equals(mock_open.call_args_list, expected) f = open('tests/output.txt') output = f.readlines() assert_equals(output, ['auto lo\n', 'iface lo inet loopback\n', ' address 1.1.1.1/32\n', '\n', 'auto eth0\n', 'iface eth0 inet dhcp\n', '\n', 'auto swp1\n', 'iface swp1\n', ' speed 1000\n', '\n', '## Ansible controlled interfaces found here\n', 'source /etc/network/ansible/*\n'])
def test_generate_new_ports_conf_hash(mock_module): """ test generating ports_conf hash based on user added params """ instance = mock_module.return_value instance.params = { 'speed_40g': ['swp1-2', 'swp5'], 'speed_4_by_10g': ['swp7-8'], 'speed_40g_div_4': ['swp9'], 'speed_10g': ['swp10'] } cl_ports.generate_new_ports_conf_hash(instance) assert_equals(instance.new_ports_hash, {1: '40G', 2: '40G', 5: '40G', 7: '4x10G', 8: '4x10G', 9: '40G/4', 10: '10G'}) # test if string is empty and none type instance.params = { 'speed_40g': [''], 'speed_10g': None } cl_ports.generate_new_ports_conf_hash(instance) assert_equals(instance.new_ports_hash, {})
def test_stp_details(self, mock_read_sys, mock_listdir, mock_file_oneline, mock_is_root): mock_is_root.return_value = False mock_listdir.return_value = ['eth1', 'eth2'] values1 = { 'bridge/stp_state': '1', 'bridge/root_id': '4000.fe54007e7eeb', 'bridge/bridge_id': '8000.fe54007e7111'} values2 = { '/sys/class/net/eth1/brport/state': '3', '/sys/class/net/eth1/brport/bridge/bridge/root_port': '1', '/sys/class/net/eth1/brport/port_id': '1', '/sys/class/net/eth2/brport/state': '0', '/sys/class/net/eth2/brport/bridge/bridge/stp_state': '1', '/sys/class/net/eth2/brport/bridge/bridge/root_port': '1', '/sys/class/net/eth2/brport/port_id': '2', } mock_read_sys.side_effect = mod_args_generator(values1) mock_file_oneline.side_effect = mod_args_generator(values2) _output = self.piface.stp_details() _outputtable = _output.split('\n') assert_equals(re.split(r'\s{2,}', _outputtable[2]), ['stp_mode:', '802.1d / per bridge instance']) assert_equals(_outputtable[3].split(), ['root_port:', 'eth1']) assert_equals(_outputtable[4].split(), ['root_priority:', '16384']) assert_equals(_outputtable[5].split(), ['bridge_priority:', '32768']) assert_equals(_outputtable[6].split(), ['802.1q_tag', 'untagged'])
def test_vlan_filtering(self, mock_read_from_sys): values = {('bridge/vlan_filtering', 'br0'): '1'} mock_read_from_sys.side_effect = mod_args_generator(values) assert_equals(self.iface.vlan_filtering, 1) values = {('bridge/vlan_filtering', 'br0'): None} mock_read_from_sys.side_effect = mod_args_generator(values) assert_equals(self.iface.vlan_filtering, 0)
def test_add_ipv4(mock_module): addr = ['10.1.1.1/24'] # addr is empty instance = mock_module.return_value instance.params.get.return_value = None iface = {'config': {}} add_ipv4(instance, iface) assert_equals('address' in iface['config'], False) # addr is not empty instance.params.get.return_value = addr iface = {'ifacetype': 'lo', 'config': {}} add_ipv4(instance, iface) assert_equals(iface['config']['address'], addr[0]) # addr is none, array - remove it instance.params.get.return_value = ['none'] iface = {'config': {}} add_ipv4(instance, iface) assert_equals(iface['config']['address'], None) # addr is none , str - remote it instance.params.get.return_value = u'none' iface = {'config': {}} add_ipv4(instance, iface) assert_equals(iface['config']['address'], None) # addr is not empty. multiple entries instance.params.get.return_value = ['1', '2'] iface = {'config': {}} add_ipv4(instance, iface) assert_equals(iface['config']['address'], ['1', '2'])
def test_using_lldp_obj(mock_lldp, mock_exists): mock_exists.return_value = True lldp_out = open('tests/test_netshowlib/lldp_output.txt').read() mock_lldp.return_value = lldp_out _output = linux_lldp.Lldp('eth2').run() assert_equals(_output, [{'adj_hostname': 'right', 'adj_port': 'swp2', 'adj_mgmt_ip': '192.168.0.15', 'system_descr': 'Cumulus Linux'}])
def test_check_for_switch_running(mock_os_path_exists, mock_time, mock_module): """ Test to check that it iterates for 30 seconds before failing\ when checking for switchd startup """ mock_os_path_exists.return_value = False check_for_switchd_run_ready(mock_module) assert_equals(mock_time.call_count, 30) mock_os_path_exists.assert_called_with('/var/run/switchd.ready')
def test_build_vids(mock_module): """ cl_interface - test building desired vids config """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'vids': ['1', '10-40']} cl_int.build_vids(mock_module) assert_equals(mock_module.custom_desired_config, {'config': {'bridge-vids': '1 10-40'}})
def test_build_address(mock_module): """ cl_interface: - test building desired address config """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'ipv4': ['1.1.1.1/24']} cl_int.build_address(mock_module) assert_equals(mock_module.custom_desired_config, {'config': {'address': '1.1.1.1/24'}})
def test_int_policy_enforce(mock_module): """ cl_int_policy test if enforcing is needed """ # if current list is found in allowed list mock_module.custom_allowedportlist = ['swp1', 'swp2', 'bond0'] mock_module.custom_currentportlist = ['swp1', 'swp2'] assert_equals(cl_int_policy.int_policy_enforce(mock_module), False) # if current list is not found in allowed list mock_module.custom_currentportlist = ['swp1', 'swp2', 'bond1'] assert_equals(cl_int_policy.int_policy_enforce(mock_module), True)
def test_get_list_of_bridge_members(self, mock_listdirs): bridgemems = ['eth8', 'eth9'] mock_listdirs.return_value = bridgemems assert_equals(sorted(list(self.iface.members.keys())), sorted(bridgemems)) assert_equals( isinstance(self.iface.members.get('eth8'), linux_bridge.BridgeMember), True) mock_listdirs.assert_called_with('/sys/class/net/br0/brif')
def test_switch_asic(mock_exec_command): mock_exec_command.return_value = open( 'tests/test_netshowlib/lspci_output.txt', 'rb').read() instance = asic.switching_asic_discovery() assert_equals(isinstance(instance, asic.BroadcomAsic), True) # no asic found mock_exec_command.side_effect = linux_common.ExecCommandException instance = asic.switching_asic_discovery() assert_equals(instance, None)
def test_config_iface_when_ifacetype_is_mgmt(mock_module, mock_swp_iface): """ mgmt ports are not getting ip address when set """ instance = mock_module.return_value ifacetype = 'mgmt' iface = MagicMock() config_iface(instance, iface, ifacetype) assert_equals(mock_swp_iface.call_count, 1)
def test_config_changed_lo_config_same(mock_module, mock_exec): """ Test config change loopback config the same""" instance = mock_module.return_value instance.params.get.return_value = 'lo' mock_exec.return_value = ''.join(open('tests/lo.txt').readlines()) iface = loop_iface() assert_equals(config_changed(instance, iface), False) _msg = 'no change in interface lo configuration' instance.exit_json.assert_called_with(msg=_msg, changed=False)
def test_build_bridge_access(mock_module): """ cl_interface - test building desired bridge_access """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'bridge_access': 2} cl_int.build_bridge_access(mock_module) assert_equals(mock_module.custom_desired_config, {'config': {'bridge_access': '2'}})
def test_cli_output(self, mock_counters, mock_lldp, mock_ip_details, mock_cli_header): mock_counters.return_value = 'counters_output\n' mock_lldp.return_value = 'lldp_output\n' mock_ip_details.return_value = 'ip_details_output\n' mock_cli_header.return_value = 'cli_header_output\n' assert_equals([x for x in self.piface.cli_output().split('\n') if x != ''], ['cli_header_output', 'ip_details_output', 'counters_output', 'lldp_output'])
def test_build_pvid(mock_module): """ cl_interface - test building desired pvid """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'pvid': 2} cl_int.build_pvid(mock_module) assert_equals(mock_module.custom_desired_config, {'config': {'bridge-pvid': '2'}})
def test_stp_root_port(self, read_from_sys, mock_exec): br0 = cumulus_bridge.Bridge('br0') self.piface.iface = br0 mock_exec.return_value = io.open( 'tests/test_netshowlib/mstpctl_showall').read() values = {('bridge/vlan_filtering', 'br0', True): None, ('bridge/stp_state', 'br0', True): '2'} read_from_sys.side_effect = mod_args_generator(values) assert_equals(self.piface.root_port(), ['swp3'])
def test_build_address(mock_module): """ cl_bridge: - test building desired address config """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'ipv4': ['1.1.1.1/24'], 'ipv6': ['2001:db8:abcd::/48']} cl_int.build_address(mock_module) assert_equals(mock_module.custom_desired_config, {'config': {'address': '1.1.1.1/24 2001:db8:abcd::/48'}})
def test_install_license_passed(mock_ansible_module): """ test install license passed """ instance = mock_ansible_module.return_value instance.params = {'src': 'blah'} run_command = MagicMock() run_command.return_value = (0, 'output', None) instance.run_command = run_command cl_license.install_license(instance) run_command.assert_called_with('/usr/cumulus/bin/cl-license -i blah') assert_equals(instance.fail_json.call_count, 0)
def test_get_active_slot(mock_module): """ Test getting active slot information """ instance = mock_module.return_value cmdline = open('tests/proc_cmdline.txt') with mock.patch('__builtin__.open') as mock_open: mock_open.return_value = cmdline assert_equals(get_active_slot(instance), '2') mock_open.assert_called_with('/proc/cmdline')
def test_summary(self, mock_is_trunk, mock_access_summary, mock_trunk_summary): mock_trunk_summary.return_value = 'trunk summary' mock_access_summary.return_value = 'access summary' # if trunk mock_is_trunk.return_value = True assert_equals(self.piface.summary, 'trunk summary') # if access mock_is_trunk.return_value = False assert_equals(self.piface.summary, 'access summary')
def test_cli_output(self, mock_cli_header, mock_bondmem_details, mock_lldp, mock_full_legend, mock_one_line_legend): mock_full_legend.return_value = 'full_legend' mock_one_line_legend.return_value = 'one_line_legend' mock_cli_header.return_value = 'cli_output' mock_bondmem_details.return_value = 'bondmem_details' mock_lldp.return_value = 'lldp_output' assert_equals( self.piface.cli_output(), 'one_line_legendcli_outputbondmem_detailslldp_outputfull_legend')
def test_cl_output(self, mock_bondmems, mock_ip, mock_bond_details, mock_cli_header): mock_ip.return_value = 'ip output' mock_bondmems.return_value = 'bondmem output' mock_bond_details.return_value = 'bond details' mock_cli_header.return_value = 'cli header' _output = self.piface.cli_output() assert_equals( _output, 'cli headerbond detailsip outputbondmem outputno_lldp_entries')
def test_get_lacp_instance(self, mock_file_oneline): # test that calling iface.lacp and if iface is LACP # creates new Lacp instance mock_file_oneline.return_value = '802.3ad 4' assert_equals(isinstance(self.iface.lacp, linux_lacp.Lacp), True) mock_file_oneline.assert_called_with( '/sys/class/net/bond0/bonding/mode') # if bond is not using lacp mock_file_oneline.return_value = 'active-backup 1' assert_equals(self.iface.lacp, None)
def test_build_addr_method(mock_module): """ cl_interface - test building desired addr_method """ mock_module.custom_desired_config = {'config': {}} mock_module.params = {'addr_method': 'loopback'} cl_int.build_addr_method(mock_module) assert_equals(mock_module.custom_desired_config.get('addr_family'), 'inet') assert_equals(mock_module.custom_desired_config.get('addr_method'), 'loopback')
def test_all(self, mock_exec, mock_listdir): mock_file = 'tests/test_netshowlib/ethtool_swp.txt' mock_exec.return_value = io.open(mock_file).read() mock_listdir.return_value = [ 'swp1', 'swp2s0', 'swp2.2', 'swp3', 'swp10', 'br0' ] self.counters.run() _output = self.counters.all assert_equals(_output['rx'].get('unicast'), 100) assert_equals(_output['tx'].get('errors'), 20)
def test_compare_new_and_old_port_conf_hash_too_many_ports(mock_module): """ test comparing existing and new ports.conf config """ instance = mock_module.return_value instance.ports_conf_hash = {1: '40G', 2: '40G', 3: '10G'} # test and see if doing this out of order makes a difference instance.new_ports_hash = {3: '4x10G', 1: '10G', 5: '10G'} result = cl_ports.compare_new_and_old_port_conf_hash(instance) assert_equals(result, False) instance.fail_json.assert_called_with( msg='Port numbering is wrong. Too many or two few ports configured')
def test_merge_config(): """ cl_interface - merge creating new element in object """ new_config = {'config': {'bridge-stp': 'on'}} orig = orig_config() orig_modify = orig_config() merge_config(new_config, orig_modify) orig['config']['bridge-stp'] = 'on' assert_equals(orig_modify, orig)
def test_initial_speed(self, mock_exec): mock_exec.return_value = io.open('tests/test_netshowlib/lspci_output.txt', 'rb').read() values = { ('/var/lib/cumulus/porttab',): io.open('tests/test_netshowlib/xe_porttab'), ('/etc/bcm.d/config.bcm',): io.open('tests/test_netshowlib/config_xe.bcm') } with mock.patch('io.open') as mock_open: mock_open.side_effect = mod_args_generator(values) iface = cumulus_iface.Iface('swp1') assert_equals(iface.initial_speed(), 10000)
def test_counters(self, mock_is_phy, mock_exec_command): mock_is_phy.return_value = False assert_equals(self.iface.counters, None) mock_is_phy.return_value = True mock_file = 'tests/test_netshowlib/ethtool_swp.txt' mock_exec_command.return_value = io.open(mock_file).read() _output = self.iface.counters.rx assert_equals(_output, {'unicast': 100, 'multicast': 300, 'errors': 10, 'broadcast': 200})
def test_check(mock_command): # Found linux OS # test with encoded string like in Python3 to ensure it gets decoded # properly mock_command.return_value = str.encode('Linux\n') assert_equals(provider_discovery.check(), 'linux') mock_command.assert_called_with('/bin/uname') # if for whatever reason provider check exec command fails mock_command.side_effect = ExecCommandException assert_equals(provider_discovery.check(), None)
def test_license_not_installed(mock_ansible_module, mock_install_license): instance = mock_ansible_module.return_value instance.params = {'src': 'blah'} run_command = MagicMock() run_command.return_value = (20, 'No license', None) instance.run_command = run_command cl_license.main() assert_equals(mock_install_license.call_count, 1) instance.exit_json.assert_called_with(msg='License installation completed', changed=True)
def test_port_list(mock_list_dir, mock_islink): values = { '/sys/class/net/bonding_masters': False, '/sys/class/net/eth1': True, '/sys/class/net/eth2': True, '/sys/class/net/tap1': True } mock_islink.side_effect = mod_args_generator(values) mock_list_dir.return_value = ['bonding_masters', 'eth1', 'eth2', 'tap1'] assert_equals(linux_iface.portname_list(), ['eth1', 'eth2', 'tap1'])
def test_license_already_installed(mock_ansible_module, mock_install_license): instance = mock_ansible_module.return_value instance.params = {'src': 'blah'} run_command = MagicMock() run_command.return_value = (0, 'license is there', None) instance.run_command = run_command cl_license.main() assert_equals(mock_install_license.call_count, 0) instance.exit_json.assert_called_with( msg='No change. License already installed', changed=False)