def test_add_mgmt_iface_2(monkeypatch): """Verify that add_mgmt_iface function return exception when mgmt iface can't be deleted. """ output_exept = "Failed to create management iface for HOST.\n" def mockreturn(command): return "", "", "" lh = IpNetworkNamespace(LH_CFG, OPTS) with pytest.raises(Exception) as excepinfo: lh.add_mgmt_iface() assert str(excepinfo.value) == output_exept
def test_check_mgmt_bridge_2(monkeypatch): """Verify that check_mgmt_bridge check that mgmt bridge is already created. """ cmd_list = [] def mockreturn(command): cmd_list.append(command) return "", "", "" lh = IpNetworkNamespace(LH_CFG, OPTS) lh.mgmt_br = "mgmt111" monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) output_result = lh.check_mgmt_bridge() assert not output_result
def test_del_mgmt_iface_1(monkeypatch): """Verify that del_mgmt_iface function return correct set of commands and exceptions when mgmt interface can not be deleted. """ comm_expect = ['ip link delete veth19'] cmd_list = [] def mockreturn(command): cmd_list.append(command) rc = "0" return "", "", rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) lh.del_mgmt_iface() assert comm_expect[0] == cmd_list[0]
def test_start_1(monkeypatch): """Verify that start function return exception when namespace is already created. """ output_result = "Namespace is already created" def mockreturn(command): so = "HOST" return so, "", "" lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) with pytest.raises(Exception) as excepinfo: lh.start() print(str(excepinfo.value)) assert str(excepinfo.value) == output_result
def test_check_mgmt_bridge_1(monkeypatch): """Verify that check_mgmt_bridge check that mgmt bridge is already created. """ comm_expect = ['ifconfig mgmt111'] cmd_list = [] def mockreturn(command): cmd_list.append(command) so = "mgmt111 created" return so, "", "" lh = IpNetworkNamespace(LH_CFG, OPTS) lh.mgmt_br = "mgmt111" monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) output_result = lh.check_mgmt_bridge() assert output_result assert comm_expect[0] == cmd_list[0]
def test_add_mgmt_bridge_1(monkeypatch): """Verify that add_mgmt_bridge function generate correct set of commands to add mgmt bridge. """ comm_expect = ['brctl addbr mbrlocalhos254', 'ifconfig mbrlocalhos254 localhos.254 up', "ifconfig mbrlocalhos254"] cmd_list = [] def mockreturn_native_cmd(command): cmd_list.append(command) if comm_expect[0] in cmd_list: so, rc = True, "0" return so, "", rc else: rc = "0" return "", "", rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn_native_cmd) lh.add_mgmt_bridge() assert set(comm_expect) == set(cmd_list)
def test_del_mgmt_iface_2(monkeypatch): """Verify that del_mgmt_iface function return correct set of commands and exceptions when mgmt interface can not be deleted. """ comm_expect = ['ip link delete veth19'] output_expect = "Failed to delete management iface for HOST.\n" cmd_list = [] def mockreturn(command): cmd_list.append(command) rc = 5 return "", "", rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) with pytest.raises(Exception) as excepinfo: lh.del_mgmt_iface() assert comm_expect[0] == cmd_list[0] print(str(excepinfo.value)) print(output_expect) assert output_expect == str(excepinfo.value)
def test_add_mgmt_bridge_2(monkeypatch): """Verify that add_mgmt_bridge function return exception when managment bridge can not be created. """ output_result = "Failed to create management bridge for Network namespaces.\n" + "Stdout: , Stderr: Error" def mockreturn_mgmt_br(command): so = False return so def mockreturn_native_cmd(command): se = "Error" rc = "5" return "", se, rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(IpNetworkNamespace, 'check_mgmt_bridge', mockreturn_mgmt_br) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn_native_cmd) with pytest.raises(Exception) as excepinfo: lh.add_mgmt_bridge() assert str(excepinfo.value) == output_result
def test_add_mgmt_iface_1(monkeypatch): """Verify that mgmt interface return correct set of commands for adding mgmt interface. """ comm_expect = ['ip link add veth19 type veth peer name veth19 netns HOST', 'brctl addif mbrlocalhos254 veth19', 'ifconfig veth19 up', 'ifconfig veth19 localhost up'] cmd_list = [] def mockreturn_native_cmd(command): cmd_list.append(command) rc = "0" return "", "", rc def mockreturn_exec_cmd(command): cmd_list.append(command) return CmdStatus("", "", 0) lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn_native_cmd) monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn_exec_cmd) lh.add_mgmt_iface() assert comm_expect == comm_expect
def test_del_mgmt_bridge_1(monkeypatch): """Verify that del_mgmt_bridge function return correct set of commands to delete mgmt bridge. """ comm_expect = ['ifconfig mbrlocalhos254 down', 'brctl delbr mbrlocalhos254'] cmd_list = [] def mockreturn_mgmt_br(command): so = True return so def mockreturn_native_cmd(command): cmd_list.append(command) rc = "0" return "", "", rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(IpNetworkNamespace, 'check_mgmt_bridge', mockreturn_mgmt_br) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn_native_cmd) lh.del_mgmt_bridge() print(cmd_list) assert comm_expect[0] == cmd_list[0]
def test_start_2(monkeypatch): """Verify that start function exception when network namespace was not created. """ result_output = "Cannot create network namespace. Return code = 5" def mockreturn(command): so = "HOST_2" print("dssd") print(command) if command == "ip netns add HOST": rc = "5" else: rc = "" return so, "", rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn) with pytest.raises(Exception) as excepinfo: lh.start() print(str(excepinfo.value)) assert result_output == str(excepinfo.value)
def test_del_mgmt_bridge_2(monkeypatch): """Verify that del_mgmt_bridge function return exception when management bridge can not be deleted. """ output_result = "Failed to delete management bridge for Network namespaces.\n" + "Stdout: , Stderr: Error" cmd_list = [] def mockreturn_mgmt_br(command): so = True return so def mockreturn_native_cmd(command): cmd_list.append(command) rc = "5" se = "Error" return "", se, rc lh = IpNetworkNamespace(LH_CFG, OPTS) monkeypatch.setattr(IpNetworkNamespace, 'check_mgmt_bridge', mockreturn_mgmt_br) monkeypatch.setattr(lh.ssh, 'native_cmd', mockreturn_native_cmd) with pytest.raises(Exception) as excepinfo: lh.del_mgmt_bridge() print(str(excepinfo.value)) assert str(excepinfo.value) == output_result