Exemple #1
0
def test_init(monkeypatch):
    # no auth parameters
    temp_cfg = dict(LH_CFG)
    temp_cfg["ssh_pass"] = None
    lh = GenericLinuxHost(temp_cfg, OPTS)
    assert not hasattr(lh, "ssh")

    # only pkey
    temp_cfg["ssh_pkey"] = "pkey"
    lh = GenericLinuxHost(temp_cfg, OPTS)
    assert hasattr(lh, "ssh")
    assert lh.ssh_pkey == temp_cfg["ssh_pkey"]

    # only pkey file
    temp_cfg["ssh_pkey"] = None
    temp_cfg["ssh_pkey_file"] = "pkey_file"
    lh = GenericLinuxHost(temp_cfg, OPTS)
    assert hasattr(lh, "ssh")
    assert lh.ssh_pkey_file == temp_cfg["ssh_pkey_file"]

    # default ssh_port
    assert hasattr(lh, "ssh_port")
    assert lh.ssh_port == 22

    # ssh_port from config
    temp_cfg["ssh_port"] = 8080
    lh = GenericLinuxHost(temp_cfg, OPTS)
    assert hasattr(lh, "ssh_port")
    assert lh.ssh_port == 8080
Exemple #2
0
def lh(monkeypatch, patch_clissh):
    cmd_list = []

    def mockreturn(self, command):
        cmd_list.append(command)
        return "", ""

    def m_get_table_ports(self, ports=None, all_params=False, ip_addr=False):
        return STR_TABLR

    def m_generate_port_name(self, port):
        return port

    def m_cli_set(self, commands, timeout=None, split_lines=True, expected_rcs=frozenset({0}),
                  multicall_treshold=0):
        for command in commands:
            cmd_list.append(command[0])
        return [[CmdStatus("", stderr="", rc=0)]]

    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn)

    monkeypatch.setattr(LinuxHostBash, 'cli_set', m_cli_set)

    lh = GenericLinuxHost(LH_CFG, OPTS)
    lh.produced_cmd_list = cmd_list

    monkeypatch.setattr(LinuxHostBash, 'get_table_ports', m_get_table_ports)
    monkeypatch.setattr(LinuxHostBash, 'generate_port_name', m_generate_port_name)
    monkeypatch.setattr(lh.ssh, 'shell_read', lambda timeout=1, interval=1: "")
    return lh
Exemple #3
0
def test_brctl_14():
    """Verify that brctl function with mac parameter return exception when bridge name is not set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    with pytest.raises(Exception) as excepinfo:
        lh.brctl("macs", ["xe1"])
    result = "Bridge name is not set."
    assert result == str(excepinfo.value)
Exemple #4
0
def test_routes_4():
    """Verify that routes function return exception when length of nexthop and ports lists is not equal.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    with pytest.raises(Exception) as excepinfo:
        lh.routes("up", ['default'], None, ["xe1", "xe2"], ["1.1.1.1"], None)
    result = "The lengths of the lists nexthop and ports is not equal."
    assert result == str(excepinfo.value)
Exemple #5
0
def test_routes_3():
    """Verify that routes function return exception when length of nexthop and netwrk lists is not equal.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    with pytest.raises(Exception) as excepinfo:
        lh.routes("up", ['10.10.10.10'], None, ["xe1"], ["20.20.20.20", "30.30.36.2"], None)
    result = "The lengths of the lists nexthop and netwrk is not equal."
    assert result == str(excepinfo.value)
Exemple #6
0
def test_ifconfig_4():
    """Verify that ifconfig command return exception when stats parameter without ports is given.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    with pytest.raises(Exception) as excepinfo:
        lh.ifconfig("stats")
    expect_result = "Ports table is empty"
    assert str(excepinfo.value) == expect_result
Exemple #7
0
def test_brctl_9():
    """Verify that brctl command return exception when br name is not set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "del"
    with pytest.raises(Exception) as excepinfo:
        lh.brctl(mode)
    result = "Bridge name is not set."
    assert result == str(excepinfo.value)
Exemple #8
0
def test_brctl_6():
    """Verify brctl command with delif parameter return exception when ports are not given.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "delif"
    with pytest.raises(Exception) as excepinfo:
        lh.brctl(mode, None, "br0")
    result = "Bridge name or port is not set."
    assert result == str(excepinfo.value)
Exemple #9
0
def test_brctl_5():
    """Verify brctl command return exception when bridge name is not given as parameter.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "delif"
    with pytest.raises(Exception) as excepinfo:
        lh.brctl(mode)
    result = "Bridge name or port is not set."
    assert result == str(excepinfo.value)
Exemple #10
0
def test_brctl_4(monkeypatch):
    """Verify cfg parameter in brctl return exception when bridge name is not given.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "cfg"
    with pytest.raises(Exception) as excepinfo:
        lh.brctl(mode)
    result = "Bridge name is not set."
    assert result == str(excepinfo.value)
Exemple #11
0
def test_ipforward_2():
    """Verify that ipforward command return exception when incorrect version value is set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    inc_ver = "-8"
    with pytest.raises(Exception) as exepinfo:
        lh.ipforward(inc_ver)
    result = "Incorrect version value: %s. Allowed values is: ['-4', '-6']" % (inc_ver, )
    assert str(exepinfo.value) == result
Exemple #12
0
def test_routes_2():
    """Verify that routes command return exception when incorrect mode value is set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "upf"
    with pytest.raises(Exception) as excepinfo:
        lh.routes(mode)
    result = "Unknown mode parameter value - %s" % mode
    assert str(excepinfo.value) == result
Exemple #13
0
def test_ifconfig_5():
    """Verify that ifconfig command return exception when improper mode is set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "upfg"
    with pytest.raises(Exception) as excepinfo:
        lh.ifconfig(mode)
    expect_result = "Unknown mode parameter value - %s" % (mode, )
    assert excepinfo.value.parameter == expect_result
Exemple #14
0
def test_brctl_10():
    """Verify brctl command return exception when incorrect mode is set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "upfg"
    with pytest.raises(Exception) as excepinfo:
        lh.brctl(mode)
    result = "Unknown mode for brctl method."
    assert result == str(excepinfo.value)
Exemple #15
0
def test_ethtool_1():
    """Verify ethtool function return exception when improper mode is set.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    inc_mode = "genericc"
    with pytest.raises(Exception) as exepinfo:
        lh.ethtool("xe1", inc_mode)
    result = "Incorrect mode=%s" % inc_mode
    assert str(exepinfo.value) == result
Exemple #16
0
def test_ifconfig_2():
    """Verify that ifconfig command return exception when lengths of ipaddr, ip6addr, mac parameters is not correct.

    """
    lh = GenericLinuxHost(LH_CFG, OPTS)
    commands = [('up', ['xe1'], ['1.1.1.1', "2.2.2.2"], None, None, "ipaddr"), ('up', ['xe1'], None, ['2002::', '3000::'], None, "ip6addr"),
                ('up', ['xe1'], None, None, ["00:12:12:12:23:56", "12:23:45:05:12:10", "10:11:02:12:12:13"], "mac")]
    for mode, port, ip, ip6, mac, name in commands:
        with pytest.raises(Exception) as excepinfo:
            lh.ifconfig(mode, port, ip, ip6, mac)
            result = "The lengths of the %s and ports lists are not equal." % name
            assert str(excepinfo.value) == result
Exemple #17
0
def test_enable_8021q_1(monkeypatch):
    """Verify that enable_802q_1 function return exception when 802.1q is not supported by current os.

    """
    def mockreturn(command):
        return CmdStatus("", "", 0)
    # monkeypatch.setattr(CLISSHNetNS, 'exec_command', mockreturn)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn)
    with pytest.raises(Exception) as excepinfo:
        lh.enable_8021q()
    result = "Current OS doesn't support 802.1q."
    assert result == str(excepinfo.value)
Exemple #18
0
def test_vconf_4(monkeypatch):
    """Verify that vconfig function return exception when mode is incorrect.

    """
    def mockreturn_8021q(self):
        pass
    monkeypatch.setattr(GenericLinuxHost, 'enable_8021q', mockreturn_8021q)

    lh = GenericLinuxHost(LH_CFG, OPTS)
    mode = "upfg"
    with pytest.raises(Exception) as excepinfo:
        lh.vconfig(mode, "xe1", 3)
    result = "Incorrect mode=%s" % mode
    assert result == str(excepinfo.value)
Exemple #19
0
def test_brctl_1(monkeypatch):
    """Verify that brctl command return correct set of commands when add parameter with stp_cfg  is defined.

    """
    expected_commands = ["ifconfig -s -a | grep ^lhbr | awk '{print $1}'", 'sudo brctl addbr lhbr1', 'sudo brctl addif lhbr1 xe1',
                         'sudo brctl stp lhbr1 on', 'sudo brctl setbridgeprio lhbr1 1000', 'sudo ifconfig lhbr1 up']
    cmd_list = []

    def mockreturn(command):
        cmd_list.append(command)
        return CmdStatus("", "", 0)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn)
    lh.brctl("add", ["xe1"], "br0", "on", {"bprio": 1000})
    for comm, value in enumerate(expected_commands):
        assert expected_commands[comm] == cmd_list[comm]
Exemple #20
0
def test_enable_8021q_2(monkeypatch):
    """Verify that enable_802q_1 function return correct set of commands when 8021q is already loaded.

    """
    comm_expected = ['modprobe -l | grep 8021q', 'lsmod | grep ^8021q']
    cmd_list = []

    def mockreturn(command):
        cmd_list.append(command)
        so = "8021q"
        return CmdStatus(so, "", 0)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn)
    lh.enable_8021q()
    for comm, value in enumerate(comm_expected):
        assert cmd_list[comm] == comm_expected[comm]
Exemple #21
0
def test_getmac_1(monkeypatch):
    """Verify that getmac function return interface mac address.

    """
    result_expected = "94:de:80:b0:25:f8"
    cmd_list = []

    def mockreturn(self, command):
        cmd_list.append(command)
        result = "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 \n \
                  link/ether 94:de:80:b0:25:f8 brd ff:ff:ff:ff:ff:ff"
        return CmdStatus(result, "", 0)
    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn)
    lh2 = GenericLinuxHost(LH_CFG, OPTS)
    result_received = lh2.getmac("xe1")
    assert result_expected == result_received
Exemple #22
0
def test_brctl_12(monkeypatch):
    """Verify that brctl function return dictionary when "macs" parameter is set.

    """
    result_expected = {'1': [{'ageing timer': '119.25', 'no mac addr': '00:10:4b:b6:c6:e4', 'is local?': 'no'}],
                       '4': [{'ageing timer': '0.00', 'no mac addr': '08:00:09:fc:d2:11', 'is local?': 'yes'}]}
    cmd_list = []

    def mockreturn(self, command):
        cmd_list.append(command)
        result = "port \t no mac addr \t is local? \t ageing timer \n 1 \t 00:10:4b:b6:c6:e4 \t no \t 119.25 \n 4 \t 08:00:09:fc:d2:11 \t yes \t 0.00 \n"
        return CmdStatus(result, "", 0)
    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn)
    lh2 = GenericLinuxHost(LH_CFG, OPTS)
    result_received = lh2.brctl("macs", None, "br0")
    assert result_received == result_expected
Exemple #23
0
def test_enable_8021q_3(monkeypatch):
    """Verify that enable_802q_1 function return exception if 8021q can not be loaded.

    """
    cmd_list = []

    def mockreturn(command):
        cmd_list.append(command)
        so = "8021q"
        if command == "lsmod | grep ^8021q":
            so = ""
        return CmdStatus(so, "", 0)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn)
    with pytest.raises(Exception) as excepinfo:
        lh.enable_8021q()
    result = "Fail to load 8021q:\n8021q"
    assert str(excepinfo.value) == result
Exemple #24
0
def test_brctl_2(monkeypatch):
    """Verify that brctl command return correct set of commands when add command is used.

    """
    expected_brname = "lhbr1"
    expected_commands = ["ifconfig -s -a | grep ^lhbr | awk '{print $1}'", 'sudo brctl addbr lhbr1', 'sudo brctl addif lhbr1 xe1', 'sudo brctl stp lhbr1 on',
                         'sudo ifconfig lhbr1 up']
    cmd_list = []

    def mockreturn(command):
        cmd_list.append(command)
        return CmdStatus("", "", 0)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    monkeypatch.setattr(lh.ssh, 'exec_command', mockreturn)
    brname = lh.brctl("add", ["xe1"], "br0", "on")
    assert brname == expected_brname
    for comm, value in enumerate(expected_commands):
        assert expected_commands[comm] == cmd_list[comm]
Exemple #25
0
def test_vconf_2(monkeypatch):
    """Verify vconf function return correct command when add parameter is set.

    """
    commands_expected = ['vconfig add xe1 3']

    def mockreturn_8021q(self):
        pass
    monkeypatch.setattr(GenericLinuxHost, 'enable_8021q', mockreturn_8021q)

    cmd_list = []

    def mockreturn_exec_cmd(self, command):
        cmd_list.append(command)
        return "", ""
    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn_exec_cmd)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    lh.vconfig("add", "xe1", 3)
    assert cmd_list[0] == commands_expected[0]
Exemple #26
0
def test_brctl_11(monkeypatch):
    """Verify that brctl function return proper dictionary when 'stpstat' parameter is set.

    """
    result_expected = {'': {}, 'proxy1': {'designated root': '01f4.001b2189ac4c', 'state': 'forwarding', 'port id': '0000', 'path cost': '2',
                       'port number': '0'}, 'proxy0': {'port number': '2'}, 'lhbr2': {'max age': '20.00', 'designated root': '05dc.4aa38155aa0f',
                       'bridge max age': '4', 'max age 20.00': 'bridge max age', 'bridge id': '05dc.4aa38155aa0f', 'path cost': '4', 'root port': '1'}}
    cmd_list = []

    def mockreturn(self, command):
        cmd_list.append(command)
        result = "lhbr2 \nbridge id \t 05dc.4aa38155aa0f \ndesignated root \t 05dc.4aa38155aa0f \nroot port \t1 \t path cost \t4 \nmax age \t 20.00 \t \
                bridge max age \t 4 \n  max age 20.00 \t bridge max age \t 20.00 \n\nproxy0 (2) \n\n port id \t 0000 \t state \t forwarding \
                \ndesignated root \t 01f4.001b2189ac4c \t path cost \t 2 \n\nproxy1 (0) \n port id \t 0000 \t state \t forwarding \n designated root \
                \t 01f4.001b2189ac4c \t path cost \t 2 \n\n"
        return CmdStatus(result, "", 0)
    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn)
    lh2 = GenericLinuxHost(LH_CFG, OPTS)
    result_received = lh2.brctl("stpstat", None, "br0")
    assert result_expected == result_received
Exemple #27
0
def test_vconf_3(monkeypatch):
    """Verify vconf function return exception after creating vlan which is already exist.

    """
    expected_result = "Port xe1 already in 3 vlan"

    def mockreturn_8021q(self):
        pass
    monkeypatch.setattr(GenericLinuxHost, 'enable_8021q', mockreturn_8021q)

    cmd_list = []

    def mockreturn_exec_cmd(self, command):
        cmd_list.append(command)
        return "", ""
    monkeypatch.setattr(GenericLinuxHost, 'exec_cmd', mockreturn_exec_cmd)
    lh = GenericLinuxHost(LH_CFG, OPTS)
    with pytest.raises(Exception) as excepinfo:
        lh.vlans = {'xe1': [3]}
        lh.vconfig("add", "xe1", 3)
    assert str(excepinfo.value) == expected_result