def test_sysctl_d_conf(): r = sysctl.SysctlDConfEtc(context_wrap(SYSCTL_CONF_TEST)) assert keys_in(['kernel.domainname', 'kernel.modprobe'], r) assert r['kernel.domainname'] == 'example.com' assert r['kernel.modprobe'] == '/sbin/mod probe' assert 'kernel.domainname.invalid' not in r r = sysctl.SysctlDConfUsr(context_wrap(SYSCTL_CONF_TEST)) assert keys_in(['kernel.domainname', 'kernel.modprobe'], r) assert r['kernel.domainname'] == 'example.com' assert r['kernel.modprobe'] == '/sbin/mod probe' assert 'kernel.domainname.invalid' not in r
def test_ip_data_Link(): link_info = ip.IpLinkInfo(context_wrap(IP_S_LINK)) link_info_all = ip.IpLinkInfo(context_wrap(IP_S_LINK_ALL)) link_info_all_2 = ip.IpLinkInfo(context_wrap(IP_S_LINK_ALL_2)) link_info_all_3 = ip.IpLinkInfo(context_wrap(IP_S_LINK_ALL_3)) if_list_all_3 = link_info_all_3.active assert sorted(if_list_all_3) == sorted(['lo', 'eth0_1', 'eth0_2', 'vxlan_sys_4789', 'gre1']) eth0_1 = link_info_all_3["eth0_1"] assert eth0_1["mac"] == "00:90:fa:8d:36:1e" assert eth0_1["rx_packets"] == 15139642734 assert eth0_1["rx_bytes"] == 16809815066893 if_list_all_2 = link_info_all_2.active if_list_all = link_info_all.active if_list = link_info.active assert len(if_list) == 4 assert keys_in(["lo", "enp0s3", "enp0s8", "enp0s9"], if_list) assert keys_in(['ppp0', 'lo', 'tun0', 'enp0s25', 'vnet0', 'virbr0'], if_list_all) assert keys_in(['lo', 'eno1', 'eno2', 'ovs-system', 'br-ex', 'vxlan_sys_4789'], if_list_all_2) assert sorted(link_info.active) == sorted(['lo', 'enp0s3', 'enp0s8', 'enp0s9']) lo = link_info["lo"] assert lo["mac"] == "00:00:00:00:00:00" assert lo["flags"] == ["LOOPBACK", "UP", "LOWER_UP"] assert lo["type"] == "loopback" assert lo["mtu"] == 65536 assert lo["rx_packets"] == 98 assert lo["tx_packets"] == 10 assert lo["index"] == 2 enp0s3 = link_info["enp0s3"] assert enp0s3["mac"] == "08:00:27:4a:c5:ef" assert enp0s3["flags"] == ["BROADCAST", "MULTICAST", "UP", "LOWER_UP"] assert enp0s3["type"] == "ether" assert enp0s3["mtu"] == 1500 assert enp0s3["rx_packets"] == 2244 assert enp0s3["tx_packets"] == 1407 assert enp0s3["index"] == 1 enp0s25 = link_info_all["enp0s25"] assert enp0s25["mac"] == "1c:75:08:a5:7e:25" assert enp0s25["flags"] == ["BROADCAST", "MULTICAST", "UP", "LOWER_UP"] assert enp0s25["type"] == "ether" assert enp0s25["mtu"] == 1500 assert enp0s25["rx_packets"] == 1492476 assert enp0s25["tx_packets"] == 969514 assert enp0s25["index"] == 2 vxlan_sys_4789 = link_info_all_2["vxlan_sys_4789"] assert sorted(vxlan_sys_4789["vxlan"]) == sorted(['vxlan', 'id', '0', 'srcport', '0', '0', 'dstport', '4789', 'nolearning', 'ageing', '300', 'noudpcsum', 'noudp6zerocsumtx', 'udp6zerocsumrx', 'external']) ovs = link_info_all_2["ovs-system"] assert sorted(ovs["openvswitch"]) == sorted(['openvswitch', 'addrgenmode', 'eui64', 'numtxqueues', '1', 'numrxqueues', '1', 'gso_max_size', '65536', 'gso_max_segs', '65535'])
def test_ifcfg(): context = context_wrap(IFCFG_TEST) context.path = CONTEXT_PATH r = IfCFG(context) assert keys_in(["iface", "TYPE", "BOOTPROTO", "IPV4_FAILURE_FATAL", "NAME"], r) assert not keys_in(["ONBOOT"], r) assert r["TYPE"] == "Ethernet" assert r["BOOTPROTO"] == "dhcp" assert r["IPV4_FAILURE_FATAL"] == "no" assert r["NAME"] == "enp0s25" assert r["iface"] == "enp0s25" assert r.ifname == r['iface'] assert r.bonding_mode is None
def test_ps_auxww_from_auxcww(): # test with input from `ps auxcww` p = ps.PsAuxww(context_wrap(PsAuxcww_TEST)) d = p.data assert all('COMMAND' in row for row in d) assert keys_in([ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND" ], d[0]) assert d[0] == { '%MEM': '0.0', 'TTY': '?', 'VSZ': '19356', 'PID': '1', '%CPU': '0.0', 'START': 'May31', 'COMMAND': 'init', 'COMMAND_NAME': 'init', 'USER': '******', 'STAT': 'Ss', 'TIME': '0:01', 'RSS': '1544', 'ARGS': '', } assert d[2]["COMMAND"] == 'irqbalance' assert d[-2]["COMMAND"] == 'qemu-kvm' assert p.fuzzy_match('irqbal') assert p.number_occurences("bash") == 3 assert p.number_occurences("qemu-kvm") != 2 assert 'dhclient' in p assert 'sshd' not in p assert not p.fuzzy_match("sshd") assert p.cpu_usage('dhclient') == '0.0' assert p.cpu_usage('java') is None assert p.users('bash') == {'user1': ['20160'], 'user2': ['20161', '20164']}
def test_ip_data_Link(): link_info = IpLinkInfo(context_wrap(IP_S_LINK)) if_list = link_info.active assert len(if_list) == 4 assert keys_in(["lo", "enp0s3", "enp0s8", "enp0s9"], if_list) assert sorted(link_info.active) == sorted( ['lo', 'enp0s3', 'enp0s8', 'enp0s9']) lo = link_info["lo"] assert lo["mac"] == "00:00:00:00:00:00" assert lo["flags"] == ["LOOPBACK", "UP", "LOWER_UP"] assert lo["type"] == "loopback" assert lo["mtu"] == 65536 assert lo["rx_packets"] == 98 assert lo["tx_packets"] == 100 assert lo["index"] == 2 enp0s3 = link_info["enp0s3"] assert enp0s3["mac"] == "08:00:27:4a:c5:ef" assert enp0s3["flags"] == ["BROADCAST", "MULTICAST", "UP", "LOWER_UP"] assert enp0s3["type"] == "ether" assert enp0s3["mtu"] == 1500 assert enp0s3["rx_packets"] == 2244 assert enp0s3["tx_packets"] == 1407 assert enp0s3["index"] == 1
def test_ps_auxcww_from_auxwwwm(): p = ps.PsAuxcww(context_wrap(PS_AUXWWWM)) d = p.data assert all('COMMAND' in row for row in d) assert keys_in([ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND" ], d[0]) assert d[0] == { 'USER': '******', 'PID': '1503', '%CPU': '0.0', '%MEM': '0.0', 'VSZ': '11932', 'RSS': '6152', 'TTY': '?', 'STAT': 'Ss', 'START': '05:22', 'TIME': '0:00', 'COMMAND': '/usr/sbin/smartd -n -q never --capabilities', 'COMMAND_NAME': 'smartd', 'ARGS': '-n -q never --capabilities', 'threads': 1, } assert '/usr/sbin/chronyd' in p assert 'sshd' not in p assert 'kthread' not in p assert not p.fuzzy_match("sshd") assert p.number_occurences("abrtd") == 1 assert p.number_occurences("sshd") != 2 assert p.fuzzy_match('chronyd') assert p.pid_info["1514"]['threads'] == 5 assert p.pid_info["1621"]['threads'] == 1
def test_ps_auxww_from_auxwww(): # test with input from `ps auxwww` p = ps.PsAuxww(context_wrap(PS_AUXWWW)) d = p.data assert all('COMMAND' in row for row in d) assert keys_in([ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND" ], d[0]) assert d[0] == { 'USER': '******', 'PID': '1', '%CPU': '0.0', '%MEM': '0.0', 'VSZ': '21452', 'RSS': '1536', 'TTY': '?', 'STAT': 'Ss', 'START': 'Mar09', 'TIME': '0:01', 'COMMAND': '/sbin/init', 'COMMAND_NAME': 'init', 'ARGS': '', } assert p.fuzzy_match('kthread') assert '-bash' in p assert '/sbin/init' in p assert 'sshd' not in p assert 'kthread' not in p assert not p.fuzzy_match("sshd") assert p.number_occurences("python") == 1 assert p.number_occurences("sshd") != 2 assert p.fuzzy_match('python') assert p.search() == [] assert p.search(COMMAND__contains='rsyslogd') == [p.data[3]] assert p.search(USER='******', COMMAND__contains='kthread') == [p.data[1]] assert p.search(TTY='tty1') == [p.data[7], p.data[8]] assert p.search(STAT__contains='Z') == []
def test_ps_auxww_from_aux(): # test with input from `ps aux` p = ps.PsAuxww(context_wrap(PsAux_TEST)) d = p.data assert all('COMMAND' in row for row in d) assert keys_in([ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND" ], d[0]) assert d[0] == { '%MEM': '0.0', 'TTY': '?', 'VSZ': '19356', 'PID': '1', '%CPU': '0.0', 'START': 'May31', 'COMMAND': '/sbin/init', 'COMMAND_NAME': 'init', 'USER': '******', 'STAT': 'Ss', 'TIME': '0:01', 'RSS': '1544', 'ARGS': '' } assert p.fuzzy_match('irqbal') assert 'bash' in p assert '/sbin/init' in p assert 'sshd' not in p assert 'kondemand' not in p assert not p.fuzzy_match("sshd") assert p.number_occurences("systemd") != 2 assert p.number_occurences("bash") == 1 assert p.fuzzy_match('kondemand') assert p.search() == [] assert p.search(COMMAND__contains='java') == [p.data[6]] assert p.search(USER='******', COMMAND__contains='kondemand') == [p.data[1]] assert p.search(TTY='pts/3') == [p.data[3]] assert p.search(STAT__contains='Z') == []
def test_ps_ef_from_ef(): # test with input from `ps -ef` p = ps.PsEf(context_wrap(PsEf_TEST)) d = p.data assert all('CMD' in row for row in d) assert keys_in([ "UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD" ], d[0]) assert d[0] == { 'UID': 'root', 'TTY': '?', 'PID': '1', 'PPID': '0', 'TIME': '00:00:06', 'STIME': '03:53', 'C': '0', 'CMD': '/usr/lib/systemd/systemd --system --deserialize 15', 'COMMAND_NAME': 'systemd', 'ARGS': '--system --deserialize 15', } assert d[4]["CMD"] == '/usr/libexec/docker/rhel-push-plugin' assert d[-5] == { 'TTY': '?', 'ARGS': '', 'UID': 'root', 'CMD': '/usr/libexec/docker/rhel-push-plugin', 'PID': '1995', 'C': '0', 'STIME': '03:54', 'TIME': '00:02:06', 'PPID': '1', 'COMMAND_NAME': 'rhel-push-plugin' } assert p.fuzzy_match('kthreadd') assert p.number_occurences("systemd") != 2 assert p.number_occurences("openshift") == 2 assert p.parent_pid("111435") == ["111434", "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"] assert '[kthreadd]' in p assert 'sshd' not in p assert not p.fuzzy_match("sshd") assert p.running_pids() == ['1', '2', '1803', '1969', '1995', '2078', '7201', '111434', '111435'] assert p.users("nginx: worker process") == {'nginx': ['111435']}
def test_ifcfg_master(): context = context_wrap(IFCFG_TEST_MASTER) context.path = CONTEXT_PATH_DEVICE r = IfCFG(context) assert keys_in(["DEVICE", "iface", "ONBOOT", "BOOTPROTO", "MASTER"], r) assert r["MASTER"] == 'bond0'
def test_ifcfg_space_v2(): context = context_wrap(IFCFG_TEST_SPACE_V2) context.path = CONTEXT_PATH_DEVICE r = IfCFG(context) assert keys_in(["DEVICE", "iface", "ONBOOT", "BOOTPROTO", "IPV4_FAILURE_FATAL"], r) assert r["DEVICE"] == '\"\"badName2\" \"'
def test_ip_addr(): d = ip.IpAddr(context_wrap(IP_ADDR_TEST)) assert len(d) == 6 assert keys_in(["lo", "eth7", "tunl0", "tunl1", "bond1.57", "ip.tun2"], d) assert sorted(d.active) == sorted(['lo', 'eth7', 'bond1.57', 'ip.tun2']) lo = d["lo"] assert len(lo) == 2 assert lo["mac"] == "00:00:00:00:00:00" assert lo["flags"] == ["LOOPBACK", "UP", "LOWER_UP"] assert lo["type"] == "loopback" assert lo["mac"] == "00:00:00:00:00:00" assert lo["mtu"] == 16436 assert lo.addrs(4) == ["127.0.0.1"] assert lo.addrs(6) == ["::1"] eth7 = d["eth7"] assert len(eth7) == 0 assert eth7["mac"] == "00:11:3f:e2:f5:9f" assert eth7["flags"] == [ "NO-CARRIER", "BROADCAST", "MULTICAST", "SLAVE", "UP" ] assert eth7["master"] == "bond1" assert eth7["qlen"] == 1000 assert eth7["mtu"] == 1500 assert len(eth7["addr"]) == 0 tunl0 = d["tunl0"] assert tunl0["type"] == "ipip" tunl1 = d["tunl1"] assert tunl1["type"] == "[65534]" assert tunl1.addrs() == ["172.30.0.2"] # P2P links use the remote IP bond1Addr = d["bond1.57"] assert len(bond1Addr) == 3 assert bond1Addr.addrs(4) == ["10.192.4.171"] assert bond1Addr.addrs(6) == [ "fe80::211:3fff:fee2:f59e", "2001::211:3fff:fee2:f59e" ] tun2 = d["ip.tun2"] assert tun2["mtu"] == 80 assert tun2["type"] == "ipip" assert tun2["peer"] == "10.188.61.108" assert tun2["peer_ip"] == "10.192.4.203" assert tun2.addrs(4) == ["192.168.122.6"] # Tests of __cmp__ assert eth7 != tunl0 assert tunl0 != tunl1 assert tunl1 == d['tunl1'] # Tests of __iter__ and __getitem__ for iface in d: assert iface is not None
def test_ps_auxww_from_auxww(): # test with input from `ps auxww` p = ps.PsAuxww(context_wrap(PsAuxww_TEST)) d = p.data assert all('COMMAND' in row for row in d) assert keys_in([ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND" ], d[0]) assert d[0] == { '%MEM': '0.0', 'TTY': '?', 'VSZ': '193892', 'PID': '1', '%CPU': '0.0', 'START': 'Oct23', 'USER': '******', 'STAT': 'Ss', 'TIME': '1:40', 'RSS': '5000', 'COMMAND': '/usr/lib/systemd/systemd --switched-root --system --deserialize 22', 'COMMAND_NAME': 'systemd', 'ARGS': '--switched-root --system --deserialize 22', } assert d[2]["COMMAND"] == '/usr/lib/systemd/systemd-journald' assert d[-2] == { 'USER': '******', 'PID': '2713', '%CPU': '2.4', '%MEM': '19.2', 'VSZ': '12508144', 'RSS': '3077224', 'TTY': '?', 'STAT': 'Sl', 'START': 'Nov21', 'TIME': '533:33', 'COMMAND': '/usr/lib64/firefox/firefox', 'COMMAND_NAME': 'firefox', 'ARGS': '', } assert p.fuzzy_match('kthreadd') assert '[kthreadd]' in p assert 'sshd' not in p assert not p.fuzzy_match("sshd") assert p.number_occurences("systemd") == 2 assert p.number_occurences("openshift") != 2 assert p.cpu_usage('/usr/lib64/firefox/firefox') == '2.4' assert p.cpu_usage('firefox') is None assert p.cpu_usage('dhclient') is None assert p.cpu_usage('java') is None assert p.running_pids() == [ '1', '2', '781', '1111', '1112', '2713', '8257' ] # Test __iter__ assert 13718356 == sum(int(proc['VSZ']) for proc in p)
def test_ifcfg_space_v1(): context = context_wrap(IFCFG_TEST_SPACE_V1) context.path = CONTEXT_PATH_DEVICE r = IfCFG(context) assert keys_in([ "DEVICE", "iface", "ONBOOT", "BOOTPROTO", "IPV4_FAILURE_FATAL", "raw_device_value" ], r) assert r["DEVICE"] != '\'"badName1" \'' assert r["raw_device_value"] == '\'"badName1" \'' assert r["DEVICE"] == 'badName1'
def test_ip_data_Link(): link_info = ip.IpLinkInfo(context_wrap(IP_S_LINK)) link_info_all = ip.IpLinkInfo(context_wrap(IP_S_LINK_ALL)) if_list_all = link_info_all.active if_list = link_info.active assert len(if_list) == 4 assert keys_in(["lo", "enp0s3", "enp0s8", "enp0s9"], if_list) assert keys_in(['ppp0', 'lo', 'tun0', 'enp0s25', 'vnet0', 'virbr0'], if_list_all) assert sorted(link_info.active) == sorted( ['lo', 'enp0s3', 'enp0s8', 'enp0s9']) lo = link_info["lo"] assert lo["mac"] == "00:00:00:00:00:00" assert lo["flags"] == ["LOOPBACK", "UP", "LOWER_UP"] assert lo["type"] == "loopback" assert lo["mtu"] == 65536 assert lo["rx_packets"] == 98 assert lo["tx_packets"] == 10 assert lo["index"] == 2 enp0s3 = link_info["enp0s3"] assert enp0s3["mac"] == "08:00:27:4a:c5:ef" assert enp0s3["flags"] == ["BROADCAST", "MULTICAST", "UP", "LOWER_UP"] assert enp0s3["type"] == "ether" assert enp0s3["mtu"] == 1500 assert enp0s3["rx_packets"] == 2244 assert enp0s3["tx_packets"] == 1407 assert enp0s3["index"] == 1 enp0s25 = link_info_all["enp0s25"] assert enp0s25["mac"] == "1c:75:08:a5:7e:25" assert enp0s25["flags"] == ["BROADCAST", "MULTICAST", "UP", "LOWER_UP"] assert enp0s25["type"] == "ether" assert enp0s25["mtu"] == 1500 assert enp0s25["rx_packets"] == 1492476 assert enp0s25["tx_packets"] == 969514 assert enp0s25["index"] == 2
def test_get_ethtool_c(): context = context_wrap(TEST_ETHTOOL_C) context.path = TEST_ETHTOOL_C_PATH result = ethtool.CoalescingInfo(context) assert keys_in(["adaptive-rx", "adaptive-tx", "pkt-rate-high", "tx-usecs-irq", "tx-frame-low", "tx-usecs-high", "tx-frame-high"], result.data) assert result.ifname == "eth2" assert not result.adaptive_rx assert not result.adaptive_tx assert result.pkt_rate_high == 10 assert result.tx_usecs_irq == 0 assert result.tx_frame_low == 25 assert result.tx_usecs_high == 0 assert result.tx_frame_high == 0
def test_ethtool_g(): context = context_wrap(TEST_ETHTOOL_G) context.path = TEST_ETHTOOL_G_PATH result = ethtool.Ring(context) assert keys_in(["max", "current"], result.data) assert result.ifname == "eth2" assert result.data['max'].rx == 2047 assert result.data['max'].rx_mini == 0 assert result.data['max'].rx_jumbo == 0 assert result.data['max'].tx == 511 assert result.max.rx == 2047 assert result.max.rx_mini == 0 assert result.max.rx_jumbo == 0 assert result.max.tx == 511 assert result.current.rx == 200 assert result.current.rx_mini == 0 assert result.current.rx_jumbo == 0 assert result.current.tx == 511
def test_lssap_netweaver(): sap = lssap.Lssap(context_wrap(Lssap_nw_TEST)) data = sap.data assert keys_in( ["SID", "Nr", "Instance", "SAPLOCALHOST", "Version", "DIR_EXECUTABLE"], data[0]) assert data[0] == { "SID": "HA2", "Nr": "16", "Instance": "D16", "SAPLOCALHOST": "lu0417", "Version": "749, patch 10, changelist 1698137", "DIR_EXECUTABLE": "/usr/sap/HA2/D16/exe" } assert data[2]["Instance"] == "D50" assert data[-1]["SAPLOCALHOST"] == "lu0417" assert sap.is_netweaver() is True assert sap.is_hana() is False assert sap.is_ascs() is False
def test_sysctl(): r = sysctl.Sysctl(context_wrap(SYSCTL_TEST)) assert keys_in(["a", "b", "c"], r.data) assert r.data["a"] == "1" assert r.data["b"] == "2" assert r.data["c"] == "include an = sign"
def test_sysctl_conf(): r = sysctl.SysctlConf(context_wrap(SYSCTL_CONF_TEST)) assert keys_in(['kernel.domainname', 'kernel.modprobe'], r.data) assert r.data['kernel.domainname'] == 'example.com' assert r.data['kernel.modprobe'] == '/sbin/mod probe'