diff --git a/tests/test_ifconfig.py b/tests/test_ifconfig.py index aaa469de..f4caec41 100644 --- a/tests/test_ifconfig.py +++ b/tests/test_ifconfig.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.ifconfig @@ -8,69 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ifconfig.out'), 'r') as f: self.centos_7_7_ifconfig = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ifconfig.out'), 'r') as f: self.ubuntu_18_4_ifconfig = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ifconfig.json'), 'r') as f: + self.centos_7_7_ifconfig_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ifconfig.json'), 'r') as f: + self.ubuntu_18_4_ifconfig_json = json.loads(f.read()) + def test_ifconfig_centos_7_7(self): """ Test 'ifconfig' on Centos 7.7 """ - self.assertEqual(jc.parsers.ifconfig.parse(self.centos_7_7_ifconfig)[0], {'name': 'docker0', - 'flags': '4099', - 'state': 'UP,BROADCAST,MULTICAST', - 'mtu': '1500', - 'ipv4_addr': '172.17.0.1', - 'ipv4_mask': '255.255.0.0', - 'ipv4_bcast': '0.0.0.0', - 'mac_addr': '02:42:b1:9a:ea:02', - 'type': 'Ethernet', - 'rx_packets': '0', - 'rx_errors': '0', - 'rx_dropped': '0', - 'rx_overruns': '0', - 'rx_frame': '0', - 'tx_packets': '0', - 'tx_errors': '0', - 'tx_dropped': '0', - 'tx_overruns': '0', - 'tx_carrier': '0', - 'tx_collisions': '0', - 'ipv6_addr': None, - 'ipv6_mask': None, - 'ipv6_scope': None, - 'metric': None}) + self.assertEqual(jc.parsers.ifconfig.parse(self.centos_7_7_ifconfig, quiet=True), self.centos_7_7_ifconfig_json) def test_ifconfig_ubuntu_18_4(self): """ Test 'ifconfig' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ifconfig.parse(self.ubuntu_18_4_ifconfig)[1], {'name': 'lo', - 'flags': '73', - 'state': 'UP,LOOPBACK,RUNNING', - 'mtu': '65536', - 'ipv4_addr': '127.0.0.1', - 'ipv4_mask': '255.0.0.0', - 'ipv4_bcast': None, - 'ipv6_addr': '::1', - 'ipv6_mask': '128', - 'ipv6_scope': 'host', - 'mac_addr': None, - 'type': 'Local Loopback', - 'rx_packets': '825', - 'rx_errors': '0', - 'rx_dropped': '0', - 'rx_overruns': '0', - 'rx_frame': '0', - 'tx_packets': '825', - 'tx_errors': '0', - 'tx_dropped': '0', - 'tx_overruns': '0', - 'tx_carrier': '0', - 'tx_collisions': '0', - 'metric': None}) + self.assertEqual(jc.parsers.ifconfig.parse(self.ubuntu_18_4_ifconfig, quiet=True), self.ubuntu_18_4_ifconfig_json) if __name__ == '__main__': diff --git a/tests/test_iptables.py b/tests/test_iptables.py index 564214ae..5bc8c8d2 100644 --- a/tests/test_iptables.py +++ b/tests/test_iptables.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.iptables @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-filter.out'), 'r') as f: self.centos_7_7_iptables_filter = f.read() @@ -38,261 +40,96 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-raw.out'), 'r') as f: self.ubuntu_18_4_iptables_raw = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-filter.json'), 'r') as f: + self.centos_7_7_iptables_filter_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-filter.json'), 'r') as f: + self.ubuntu_18_4_iptables_filter_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-filter-nv.json'), 'r') as f: + self.centos_7_7_iptables_filter_nv_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-filter-nv.json'), 'r') as f: + self.ubuntu_18_4_iptables_filter_nv_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-mangle.json'), 'r') as f: + self.centos_7_7_iptables_mangle_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-mangle.json'), 'r') as f: + self.ubuntu_18_4_iptables_mangle_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-nat.json'), 'r') as f: + self.centos_7_7_iptables_nat_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-nat.json'), 'r') as f: + self.ubuntu_18_4_iptables_nat_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-raw.json'), 'r') as f: + self.centos_7_7_iptables_raw_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-raw.json'), 'r') as f: + self.ubuntu_18_4_iptables_raw_json = json.loads(f.read()) + def test_iptables_filter_centos_7_7(self): """ Test 'sudo iptables -L -t filter' on Centos 7.7 """ - self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_filter)[2], {'chain': 'OUTPUT', - 'rules': [{'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'OUTPUT_direct', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate ESTABLISHED'}, - {'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'tcp spt:ssh ctstate ESTABLISHED'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate ESTABLISHED'}, - {'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'tcp spt:ssh ctstate ESTABLISHED'}]}) + self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_filter, quiet=True), self.centos_7_7_iptables_filter_json) def test_iptables_filter_ubuntu_18_4(self): """ Test 'sudo iptables -L -t filter' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_filter)[0], {'chain': 'INPUT', - 'rules': [{'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate RELATED,ESTABLISHED'}, - {'target': 'DROP', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate INVALID'}, - {'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'source': '15.15.15.0/24', - 'destination': 'anywhere', - 'options': 'tcp dpt:ssh ctstate NEW,ESTABLISHED'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate RELATED,ESTABLISHED'}, - {'target': 'DROP', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate INVALID'}, - {'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'source': '15.15.15.0/24', - 'destination': 'anywhere', - 'options': 'tcp dpt:ssh ctstate NEW,ESTABLISHED'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate RELATED,ESTABLISHED'}, - {'target': 'DROP', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': 'ctstate INVALID'}, - {'target': 'DROP', - 'prot': 'all', - 'opt': '--', - 'source': '15.15.15.51', - 'destination': 'anywhere'}, - {'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'source': '15.15.15.0/24', - 'destination': 'anywhere', - 'options': 'tcp dpt:ssh ctstate NEW,ESTABLISHED'}]}) + self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_filter, quiet=True), self.ubuntu_18_4_iptables_filter_json) def test_iptables_filter_nv_centos_7_7(self): """ Test 'sudo iptables -nvL -t filter' on Centos 7.7 """ - self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_filter_nv)[4], {'chain': 'DOCKER-ISOLATION', - 'rules': [{'pkts': '0', - 'bytes': '0', - 'target': 'RETURN', - 'prot': 'all', - 'opt': '--', - 'in': '*', - 'out': '*', - 'source': '0.0.0.0/0', - 'destination': '0.0.0.0/0'}]}) + self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_filter_nv, quiet=True), self.centos_7_7_iptables_filter_nv_json) def test_iptables_filter_nv_ubuntu_18_4(self): """ Test 'sudo iptables -nvL -t filter' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_filter_nv)[0]['rules'][3], {'pkts': '0', - 'bytes': '0', - 'target': 'ACCEPT', - 'prot': 'tcp', - 'opt': '--', - 'in': '*', - 'out': '*', - 'source': '15.15.15.0/24', - 'destination': '0.0.0.0/0', - 'options': 'tcp dpt:22 ctstate NEW,ESTABLISHED'}) + self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_filter_nv, quiet=True), self.ubuntu_18_4_iptables_filter_nv_json) def test_iptables_mangle_centos_7_7(self): """ Test 'sudo iptables -L -t mangle' on Centos 7.7 """ - self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_mangle)[0], {'chain': 'PREROUTING', - 'rules': [{'target': 'PREROUTING_direct', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'PREROUTING_ZONES_SOURCE', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'PREROUTING_ZONES', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}]}) + self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_mangle, quiet=True), self.centos_7_7_iptables_mangle_json) def test_iptables_mangle_ubuntu_18_4(self): """ Test 'sudo iptables -L -t mangle' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_mangle), [{'chain': 'PREROUTING', - 'rules': []}, - {'chain': 'INPUT', - 'rules': []}, - {'chain': 'FORWARD', - 'rules': []}, - {'chain': 'OUTPUT', - 'rules': []}]) + self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_mangle, quiet=True), self.ubuntu_18_4_iptables_mangle_json) def test_iptables_nat_centos_7_7(self): """ Test 'sudo iptables -L -t nat' on Centos 7.7 """ - self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_nat)[3], {'chain': 'POSTROUTING', - 'rules': [{'target': 'MASQUERADE', - 'prot': 'all', - 'opt': '--', - 'source': '172.17.0.0/16', - 'destination': 'anywhere'}, - {'target': 'POSTROUTING_direct', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'POSTROUTING_ZONES_SOURCE', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}, - {'target': 'POSTROUTING_ZONES', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere'}]}) + self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_nat, quiet=True), self.centos_7_7_iptables_nat_json) def test_iptables_nat_ubuntu_18_4(self): """ Test 'sudo iptables -L -t nat' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_nat), [{'chain': 'PREROUTING', - 'rules': []}, - {'chain': 'INPUT', - 'rules': []}, - {'chain': 'OUTPUT', - 'rules': []}]) + self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_nat, quiet=True), self.ubuntu_18_4_iptables_nat_json) def test_iptables_raw_centos_7_7(self): """ Test 'sudo iptables -L -t raw' on Centos 7.7 """ - self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_raw)[3], {'chain': 'PREROUTING_ZONES', - 'rules': [{'target': 'PRE_public', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': '[goto] '}, - {'target': 'PRE_public', - 'prot': 'all', - 'opt': '--', - 'source': 'anywhere', - 'destination': 'anywhere', - 'options': '[goto] '}]}) + self.assertEqual(jc.parsers.iptables.parse(self.centos_7_7_iptables_raw, quiet=True), self.centos_7_7_iptables_raw_json) def test_iptables_raw_ubuntu_18_4(self): """ Test 'sudo iptables -L -t raw' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_raw), [{'chain': 'PREROUTING', - 'rules': []}]) + self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_raw, quiet=True), self.ubuntu_18_4_iptables_raw_json) if __name__ == '__main__': diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 1d748d8b..9d49a24e 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.jobs @@ -8,29 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/jobs.out'), 'r') as f: self.centos_7_7_jobs = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/jobs.out'), 'r') as f: self.ubuntu_18_4_jobs = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/jobs.json'), 'r') as f: + self.centos_7_7_jobs_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/jobs.json'), 'r') as f: + self.ubuntu_18_4_jobs_json = json.loads(f.read()) + def test_jobs_centos_7_7(self): """ Test 'jobs' on Centos 7.7 """ - self.assertEqual(jc.parsers.jobs.parse(self.centos_7_7_jobs)[3], {'job_number': '4', - 'history': 'current', - 'status': 'Running', - 'command': 'sleep 14 &'}) + self.assertEqual(jc.parsers.jobs.parse(self.centos_7_7_jobs, quiet=True), self.centos_7_7_jobs_json) def test_jobs_ubuntu_18_4(self): """ Test 'jobs' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.jobs.parse(self.ubuntu_18_4_jobs)[2], {'job_number': '3', - 'history': 'previous', - 'status': 'Running', - 'command': 'sleep 13 &'}) + self.assertEqual(jc.parsers.jobs.parse(self.ubuntu_18_4_jobs, quiet=True), self.ubuntu_18_4_jobs_json) if __name__ == '__main__': diff --git a/tests/test_ls.py b/tests/test_ls.py index e61b2ba8..89d05624 100644 --- a/tests/test_ls.py +++ b/tests/test_ls.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.ls @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls.out'), 'r') as f: self.centos_7_7_ls = f.read() @@ -26,66 +28,60 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alh.out'), 'r') as f: self.ubuntu_18_4_ls_alh = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls.json'), 'r') as f: + self.centos_7_7_ls_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls.json'), 'r') as f: + self.ubuntu_18_4_ls_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-al.json'), 'r') as f: + self.centos_7_7_ls_al_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-al.json'), 'r') as f: + self.ubuntu_18_4_ls_al_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alh.json'), 'r') as f: + self.centos_7_7_ls_alh_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alh.json'), 'r') as f: + self.ubuntu_18_4_ls_alh_json = json.loads(f.read()) + def test_ls_centos_7_7(self): """ Test plain 'ls /' on Centos 7.7 """ - self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls)[1]['filename'], 'boot') + self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls, quiet=True), self.centos_7_7_ls_json) def test_ls_ubuntu_18_4(self): """ Test plain 'ls /' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls)[1]['filename'], 'boot') + self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls, quiet=True), self.ubuntu_18_4_ls_json) def test_ls_al_centos_7_7(self): """ Test 'ls -al /' on Centos 7.7 """ - self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_al)[2], {'filename': 'bin', - 'link_to': 'usr/bin', - 'flags': 'lrwxrwxrwx.', - 'links': '1', - 'owner': 'root', - 'group': 'root', - 'size': '7', - 'date': 'Aug 15 10:53'}) + self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_al, quiet=True), self.centos_7_7_ls_al_json) def test_ls_al_ubuntu_18_4(self): """ Test 'ls -al /' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_al)[4], {'filename': 'cdrom', - 'flags': 'drwxr-xr-x', - 'links': '2', - 'owner': 'root', - 'group': 'root', - 'size': '4096', - 'date': 'Aug 12 17:21'}) + self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_al, quiet=True), self.ubuntu_18_4_ls_al_json) def test_ls_alh_centos_7_7(self): """ Test 'ls -alh /' on Centos 7.7 """ - self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_alh)[5], {'filename': 'etc', - 'flags': 'drwxr-xr-x.', - 'links': '78', - 'owner': 'root', - 'group': 'root', - 'size': '8.0K', - 'date': 'Oct 25 18:47'}) + self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_alh, quiet=True), self.centos_7_7_ls_alh_json) def test_ls_alh_ubuntu_18_4(self): """ Test 'ls -alh /' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_alh)[7], {'filename': 'home', - 'flags': 'drwxr-xr-x', - 'links': '3', - 'owner': 'root', - 'group': 'root', - 'size': '4.0K', - 'date': 'Aug 12 17:24'}) + self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_alh, quiet=True), self.ubuntu_18_4_ls_alh_json) if __name__ == '__main__': diff --git a/tests/test_lsblk.py b/tests/test_lsblk.py index 147c531c..5a1e4af3 100644 --- a/tests/test_lsblk.py +++ b/tests/test_lsblk.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.lsblk @@ -8,35 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsblk.out'), 'r') as f: self.centos_7_7_lsblk = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsblk.out'), 'r') as f: self.ubuntu_18_4_lsblk = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsblk.json'), 'r') as f: + self.centos_7_7_lsblk_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsblk.json'), 'r') as f: + self.ubuntu_18_4_lsblk_json = json.loads(f.read()) + def test_lsblk_centos_7_7(self): """ Test 'lsblk' on Centos 7.7 """ - self.assertEqual(jc.parsers.lsblk.parse(self.centos_7_7_lsblk)[4], {'name': 'centos-swap', - 'maj_min': '253:1', - 'rm': '0', - 'size': '2G', - 'ro': '0', - 'type': 'lvm', - 'mountpoint': '[SWAP]'}) + self.assertEqual(jc.parsers.lsblk.parse(self.centos_7_7_lsblk, quiet=True), self.centos_7_7_lsblk_json) def test_lsblk_ubuntu_18_4(self): """ Test 'lsblk' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.lsblk.parse(self.ubuntu_18_4_lsblk)[8], {'name': 'loop8', - 'maj_min': '7:8', - 'rm': '0', - 'size': '3.1M', - 'ro': '1', - 'type': 'loop', - 'mountpoint': '/snap/stress-ng/847'}) + self.assertEqual(jc.parsers.lsblk.parse(self.ubuntu_18_4_lsblk, quiet=True), self.ubuntu_18_4_lsblk_json) if __name__ == '__main__': diff --git a/tests/test_lsmod.py b/tests/test_lsmod.py index 2fff9853..ab6ab627 100644 --- a/tests/test_lsmod.py +++ b/tests/test_lsmod.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.lsmod @@ -8,32 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsmod.out'), 'r') as f: self.centos_7_7_lsmod = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsmod.out'), 'r') as f: self.ubuntu_18_4_lsmod = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsmod.json'), 'r') as f: + self.centos_7_7_lsmod_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsmod.json'), 'r') as f: + self.ubuntu_18_4_lsmod_json = json.loads(f.read()) + def test_lsmod_centos_7_7(self): """ Test 'lsmod' on Centos 7.7 """ - self.assertEqual(jc.parsers.lsmod.parse(self.centos_7_7_lsmod)[17], {'module': 'nf_nat_ipv6', - 'size': '14131', - 'used': '1', - 'by': ['ip6table_nat']}) + self.assertEqual(jc.parsers.lsmod.parse(self.centos_7_7_lsmod, quiet=True), self.centos_7_7_lsmod_json) def test_lsmod_ubuntu_18_4(self): """ Test 'lsmod' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.lsmod.parse(self.ubuntu_18_4_lsmod)[21], {'module': 'nf_conntrack', - 'size': '131072', - 'used': '4', - 'by': ['xt_conntrack', - 'nf_conntrack_ipv4', - 'nf_nat', - 'nf_nat_ipv4']}) + self.assertEqual(jc.parsers.lsmod.parse(self.ubuntu_18_4_lsmod, quiet=True), self.ubuntu_18_4_lsmod_json) if __name__ == '__main__': diff --git a/tests/test_lsof.py b/tests/test_lsof.py index b96a37b3..aa7419c6 100644 --- a/tests/test_lsof.py +++ b/tests/test_lsof.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.lsof @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsof.out'), 'r') as f: self.centos_7_7_lsof = f.read() @@ -20,65 +22,42 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsof-sudo.out'), 'r') as f: self.ubuntu_18_4_lsof_sudo = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsof.json'), 'r') as f: + self.centos_7_7_lsof_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsof.json'), 'r') as f: + self.ubuntu_18_4_lsof_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lsof-sudo.json'), 'r') as f: + self.centos_7_7_lsof_sudo_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lsof-sudo.json'), 'r') as f: + self.ubuntu_18_4_lsof_sudo_json = json.loads(f.read()) + def test_lsof_centos_7_7(self): """ Test 'lsof' on Centos 7.7 """ - self.assertEqual(jc.parsers.lsof.parse(self.centos_7_7_lsof)[155], {'command': 'scsi_eh_0', - 'pid': '291', - 'tid': None, - 'user': 'root', - 'fd': 'NOFD', - 'type': None, - 'device': None, - 'size_off': None, - 'node': None, - 'name': '/proc/291/fd (opendir: Permission denied)'}) + self.assertEqual(jc.parsers.lsof.parse(self.centos_7_7_lsof, quiet=True), self.centos_7_7_lsof_json) def test_lsof_ubuntu_18_4(self): """ Test 'lsof' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.lsof.parse(self.ubuntu_18_4_lsof)[341], {'command': 'scsi_tmf_', - 'pid': '246', - 'tid': None, - 'user': 'root', - 'fd': 'rtd', - 'type': 'unknown', - 'device': None, - 'size_off': None, - 'node': None, - 'name': '/proc/246/root (readlink: Permission denied)'}) + self.assertEqual(jc.parsers.lsof.parse(self.ubuntu_18_4_lsof, quiet=True), self.ubuntu_18_4_lsof_json) def test_lsof_sudo_centos_7_7(self): """ Test 'sudo lsof' on Centos 7.7 """ - self.assertEqual(jc.parsers.lsof.parse(self.centos_7_7_lsof_sudo)[500], {'command': 'dbus-daem', - 'pid': '778', - 'tid': None, - 'user': 'dbus', - 'fd': '6u', - 'type': 'netlink', - 'device': None, - 'size_off': '0t0', - 'node': '17854', - 'name': 'SELINUX'}) + self.assertEqual(jc.parsers.lsof.parse(self.centos_7_7_lsof_sudo, quiet=True), self.centos_7_7_lsof_sudo_json) def test_lsof_sudo_ubuntu_18_4(self): """ Test 'sudo lsof' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.lsof.parse(self.ubuntu_18_4_lsof_sudo)[782], {'command': 'vmtoolsd', - 'pid': '680', - 'tid': None, - 'user': 'root', - 'fd': '4u', - 'type': 'a_inode', - 'device': '0,13', - 'size_off': '0', - 'node': '10717', - 'name': '[eventfd]'}) + self.assertEqual(jc.parsers.lsof.parse(self.ubuntu_18_4_lsof_sudo, quiet=True), self.ubuntu_18_4_lsof_sudo_json) if __name__ == '__main__': diff --git a/tests/test_mount.py b/tests/test_mount.py index 0c8cdace..7fb10045 100644 --- a/tests/test_mount.py +++ b/tests/test_mount.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.mount @@ -8,40 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/mount.out'), 'r') as f: self.centos_7_7_mount = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/mount.out'), 'r') as f: self.ubuntu_18_4_mount = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/mount.json'), 'r') as f: + self.centos_7_7_mount_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/mount.json'), 'r') as f: + self.ubuntu_18_4_mount_json = json.loads(f.read()) + def test_mount_centos_7_7(self): """ Test 'mount' on Centos 7.7 """ - self.assertEqual(jc.parsers.mount.parse(self.centos_7_7_mount)[5], {'filesystem': 'devpts', - 'mount_point': '/dev/pts', - 'type': 'devpts', - 'access': ['rw', - 'nosuid', - 'noexec', - 'relatime', - 'seclabel', - 'gid=5', - 'mode=620', - 'ptmxmode=000']}) + self.assertEqual(jc.parsers.mount.parse(self.centos_7_7_mount, quiet=True), self.centos_7_7_mount_json) def test_mount_ubuntu_18_4(self): """ Test 'mount' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.mount.parse(self.ubuntu_18_4_mount)[9], {'filesystem': 'tmpfs', - 'mount_point': '/sys/fs/cgroup', - 'type': 'tmpfs', - 'access': ['ro', - 'nosuid', - 'nodev', - 'noexec', - 'mode=755']}) + self.assertEqual(jc.parsers.mount.parse(self.ubuntu_18_4_mount, quiet=True), self.ubuntu_18_4_mount_json) if __name__ == '__main__': diff --git a/tests/test_netstat.py b/tests/test_netstat.py index 53812a99..9bc7b50c 100644 --- a/tests/test_netstat.py +++ b/tests/test_netstat.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.netstat @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat.out'), 'r') as f: self.centos_7_7_netstat = f.read() @@ -32,120 +34,78 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.out'), 'r') as f: self.ubuntu_18_4_netstat_sudo_lnp = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat.json'), 'r') as f: + self.centos_7_7_netstat_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat.json'), 'r') as f: + self.ubuntu_18_4_netstat_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-l.json'), 'r') as f: + self.centos_7_7_netstat_l_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-l.json'), 'r') as f: + self.ubuntu_18_4_netstat_l_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-p.json'), 'r') as f: + self.centos_7_7_netstat_p_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-p.json'), 'r') as f: + self.ubuntu_18_4_netstat_p_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/netstat-sudo-lnp.json'), 'r') as f: + self.centos_7_7_netstat_sudo_lnp_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/netstat-sudo-lnp.json'), 'r') as f: + self.ubuntu_18_4_netstat_sudo_lnp_json = json.loads(f.read()) + def test_netstat_centos_7_7(self): """ Test 'netstat' on Centos 7.7 """ - self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat)[1], {'transport_protocol': 'tcp', - 'network_protocol': 'ipv4', - 'local_address': 'localhost.localdoma', - 'local_port': 'ssh', - 'foreign_address': '192.168.71.1', - 'foreign_port': '58727', - 'state': 'ESTABLISHED', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat, quiet=True), self.centos_7_7_netstat_json) def test_netstat_ubuntu_18_4(self): """ Test 'netstat' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat)[1], {'transport_protocol': 'tcp', - 'network_protocol': 'ipv4', - 'local_address': 'kbrazil-ubuntu', - 'local_port': '55656', - 'foreign_address': 'lb-192-30-253-113', - 'foreign_port': 'https', - 'state': 'ESTABLISHED', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat, quiet=True), self.ubuntu_18_4_netstat_json) def test_netstat_l_centos_7_7(self): """ Test 'netstat -l' on Centos 7.7 """ - self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_l)[3], {'transport_protocol': 'tcp', - 'network_protocol': 'ipv6', - 'local_address': '[::]', - 'local_port': 'ssh', - 'foreign_address': '[::]', - 'foreign_port': '*', - 'state': 'LISTEN', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_l, quiet=True), self.centos_7_7_netstat_l_json) def test_netstat_l_ubuntu_18_4(self): """ Test 'netstat -l' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_l)[4], {'transport_protocol': 'udp', - 'network_protocol': 'ipv4', - 'local_address': 'localhost', - 'local_port': 'domain', - 'foreign_address': '0.0.0.0', - 'foreign_port': '*', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_l, quiet=True), self.ubuntu_18_4_netstat_l_json) def test_netstat_p_centos_7_7(self): """ Test 'netstat -l' on Centos 7.7 """ - self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_p), [{'transport_protocol': 'tcp', - 'network_protocol': 'ipv4', - 'local_address': 'localhost.localdoma', - 'local_port': 'ssh', - 'foreign_address': '192.168.71.1', - 'foreign_port': '58727', - 'state': 'ESTABLISHED', - 'receive_q': '0', - 'send_q': '0'}]) + self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_p, quiet=True), self.centos_7_7_netstat_p_json) def test_netstat_p_ubuntu_18_4(self): """ Test 'netstat -l' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_p)[1], {'transport_protocol': 'tcp', - 'network_protocol': 'ipv4', - 'local_address': 'kbrazil-ubuntu', - 'local_port': '55656', - 'foreign_address': 'lb-192-30-253-113', - 'foreign_port': 'https', - 'state': 'ESTABLISHED', - 'pid': '23921', - 'program_name': 'git-remote-ht', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_p, quiet=True), self.ubuntu_18_4_netstat_p_json) def test_netstat_sudo_lnp_centos_7_7(self): """ Test 'sudo netstat -lnp' on Centos 7.7 """ - self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_sudo_lnp)[5], {'transport_protocol': 'udp', - 'network_protocol': 'ipv4', - 'local_address': '127.0.0.1', - 'local_port': '323', - 'foreign_address': '0.0.0.0', - 'foreign_port': '*', - 'pid': '795', - 'program_name': 'chronyd', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.centos_7_7_netstat_sudo_lnp, quiet=True), self.centos_7_7_netstat_sudo_lnp_json) def test_netstat_sudo_lnp_ubuntu_18_4(self): """ Test 'sudo netstat -lnp' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_sudo_lnp)[4], {'transport_protocol': 'udp', - 'network_protocol': 'ipv4', - 'local_address': '127.0.0.53', - 'local_port': '53', - 'foreign_address': '0.0.0.0', - 'foreign_port': '*', - 'pid': '885', - 'program_name': 'systemd-resolve', - 'receive_q': '0', - 'send_q': '0'}) + self.assertEqual(jc.parsers.netstat.parse(self.ubuntu_18_4_netstat_sudo_lnp, quiet=True), self.ubuntu_18_4_netstat_sudo_lnp_json) if __name__ == '__main__': diff --git a/tests/test_ps.py b/tests/test_ps.py index 4f39e1db..bb97bb2f 100644 --- a/tests/test_ps.py +++ b/tests/test_ps.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.ps @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ps-ef.out'), 'r') as f: self.centos_7_7_ps_ef = f.read() @@ -20,63 +22,42 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ps-axu.out'), 'r') as f: self.ubuntu_18_4_ps_axu = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ps-ef.json'), 'r') as f: + self.centos_7_7_ps_ef_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ps-ef.json'), 'r') as f: + self.ubuntu_18_4_ps_ef_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ps-axu.json'), 'r') as f: + self.centos_7_7_ps_axu_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ps-axu.json'), 'r') as f: + self.ubuntu_18_4_ps_axu_json = json.loads(f.read()) + def test_ps_ef_centos_7_7(self): """ Test 'ps -ef' on Centos 7.7 """ - self.assertEqual(jc.parsers.ps.parse(self.centos_7_7_ps_ef)[25], {'uid': 'root', - 'pid': '33', - 'ppid': '2', - 'c': '0', - 'stime': 'Oct25', - 'tty': '?', - 'time': '00:00:00', - 'cmd': '[crypto]'}) + self.assertEqual(jc.parsers.ps.parse(self.centos_7_7_ps_ef, quiet=True), self.centos_7_7_ps_ef_json) def test_ps_ef_ubuntu_18_4(self): """ Test 'ps -ef' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ps.parse(self.ubuntu_18_4_ps_ef)[30], {'uid': 'root', - 'pid': '36', - 'ppid': '2', - 'c': '0', - 'stime': 'Oct26', - 'tty': '?', - 'time': '00:00:00', - 'cmd': '[ecryptfs-kthrea]'}) + self.assertEqual(jc.parsers.ps.parse(self.ubuntu_18_4_ps_ef, quiet=True), self.ubuntu_18_4_ps_ef_json) def test_ps_axu_centos_7_7(self): """ Test 'ps axu' on Centos 7.7 """ - self.assertEqual(jc.parsers.ps.parse(self.centos_7_7_ps_axu)[13], {'user': 'root', - 'pid': '16', - 'cpu_percent': '0.0', - 'mem_percent': '0.0', - 'vsz': '0', - 'rss': '0', - 'tty': '?', - 'stat': 'S<', - 'start': 'Oct25', - 'time': '0:00', - 'command': '[writeback]'}) + self.assertEqual(jc.parsers.ps.parse(self.centos_7_7_ps_axu, quiet=True), self.centos_7_7_ps_axu_json) def test_ps_axu_ubuntu_18_4(self): """ Test 'ps axu' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.ps.parse(self.ubuntu_18_4_ps_axu)[40], {'user': 'root', - 'pid': '170', - 'cpu_percent': '0.0', - 'mem_percent': '0.0', - 'vsz': '0', - 'rss': '0', - 'tty': '?', - 'stat': 'I<', - 'start': 'Oct26', - 'time': '0:00', - 'command': '[mpt_poll_0]'}) + self.assertEqual(jc.parsers.ps.parse(self.ubuntu_18_4_ps_axu, quiet=True), self.ubuntu_18_4_ps_axu_json) if __name__ == '__main__': diff --git a/tests/test_route.py b/tests/test_route.py index 27fe4122..bad4b406 100644 --- a/tests/test_route.py +++ b/tests/test_route.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.route @@ -8,6 +9,7 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/route.out'), 'r') as f: self.centos_7_7_route = f.read() @@ -20,57 +22,42 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/route-vn.out'), 'r') as f: self.ubuntu_18_4_route_vn = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/route.json'), 'r') as f: + self.centos_7_7_route_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/route.json'), 'r') as f: + self.ubuntu_18_4_route_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/route-vn.json'), 'r') as f: + self.centos_7_7_route_vn_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/route-vn.json'), 'r') as f: + self.ubuntu_18_4_route_vn_json = json.loads(f.read()) + def test_route_centos_7_7(self): """ Test 'route' on Centos 7.7 """ - self.assertEqual(jc.parsers.route.parse(self.centos_7_7_route)[2], {'destination': '192.168.71.0', - 'gateway': '0.0.0.0', - 'genmask': '255.255.255.0', - 'flags': 'U', - 'metric': '100', - 'ref': '0', - 'use': '0', - 'iface': 'ens33'}) + self.assertEqual(jc.parsers.route.parse(self.centos_7_7_route, quiet=True), self.centos_7_7_route_json) def test_route_ubuntu_18_4(self): """ Test 'route' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.route.parse(self.ubuntu_18_4_route)[1], {'destination': '192.168.71.0', - 'gateway': '0.0.0.0', - 'genmask': '255.255.255.0', - 'flags': 'U', - 'metric': '0', - 'ref': '0', - 'use': '0', - 'iface': 'ens33'}) + self.assertEqual(jc.parsers.route.parse(self.ubuntu_18_4_route, quiet=True), self.ubuntu_18_4_route_json) def test_route_vn_centos_7_7(self): """ Test 'route -vn' on Centos 7.7 """ - self.assertEqual(jc.parsers.route.parse(self.centos_7_7_route_vn)[2], {'destination': '192.168.71.0', - 'gateway': '0.0.0.0', - 'genmask': '255.255.255.0', - 'flags': 'U', - 'metric': '100', - 'ref': '0', - 'use': '0', - 'iface': 'ens33'}) + self.assertEqual(jc.parsers.route.parse(self.centos_7_7_route_vn, quiet=True), self.centos_7_7_route_vn_json) def test_route_vn_ubuntu_18_4(self): """ Test 'route -vn' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.route.parse(self.ubuntu_18_4_route_vn)[2], {'destination': '192.168.71.2', - 'gateway': '0.0.0.0', - 'genmask': '255.255.255.255', - 'flags': 'UH', - 'metric': '100', - 'ref': '0', - 'use': '0', - 'iface': 'ens33'}) + self.assertEqual(jc.parsers.route.parse(self.ubuntu_18_4_route_vn, quiet=True), self.ubuntu_18_4_route_vn_json) if __name__ == '__main__': diff --git a/tests/test_uname.py b/tests/test_uname.py index 56375440..7cd67a3e 100644 --- a/tests/test_uname.py +++ b/tests/test_uname.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.uname @@ -8,37 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uname-a.out'), 'r') as f: self.centos_7_7_uname_a = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uname-a.out'), 'r') as f: self.ubuntu_18_4_uname_a = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uname-a.json'), 'r') as f: + self.centos_7_7_uname_a_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uname-a.json'), 'r') as f: + self.ubuntu_18_4_uname_a_json = json.loads(f.read()) + def test_uname_centos_7_7(self): """ Test 'uname -a' on Centos 7.7 """ - self.assertEqual(jc.parsers.uname.parse(self.centos_7_7_uname_a), {'kernel_name': 'Linux', - 'node_name': 'localhost.localdomain', - 'kernel_release': '3.10.0-1062.1.2.el7.x86_64', - 'operating_system': 'GNU/Linux', - 'hardware_platform': 'x86_64', - 'processor': 'x86_64', - 'machine': 'x86_64', - 'kernel_version': '#1 SMP Mon Sep 30 14:19:46 UTC 2019'}) + self.assertEqual(jc.parsers.uname.parse(self.centos_7_7_uname_a, quiet=True), self.centos_7_7_uname_a_json) def test_uname_ubuntu_18_4(self): """ Test 'uname -a' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.uname.parse(self.ubuntu_18_4_uname_a), {'kernel_name': 'Linux', - 'node_name': 'kbrazil-ubuntu', - 'kernel_release': '4.15.0-65-generic', - 'operating_system': 'GNU/Linux', - 'hardware_platform': 'x86_64', - 'processor': 'x86_64', - 'machine': 'x86_64', - 'kernel_version': '#74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019'}) + self.assertEqual(jc.parsers.uname.parse(self.ubuntu_18_4_uname_a, quiet=True), self.ubuntu_18_4_uname_a_json) if __name__ == '__main__': diff --git a/tests/test_uptime.py b/tests/test_uptime.py index 9d013736..e17ad139 100644 --- a/tests/test_uptime.py +++ b/tests/test_uptime.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.uptime @@ -8,33 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uptime.out'), 'r') as f: self.centos_7_7_uptime = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uptime.out'), 'r') as f: self.ubuntu_18_4_uptime = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uptime.json'), 'r') as f: + self.centos_7_7_uptime_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uptime.json'), 'r') as f: + self.ubuntu_18_4_uptime_json = json.loads(f.read()) + def test_uptime_centos_7_7(self): """ Test 'uptime' on Centos 7.7 """ - self.assertEqual(jc.parsers.uptime.parse(self.centos_7_7_uptime), {'time': '10:25:20', - 'uptime': '16:03', - 'users': '2', - 'load_1m': '0.00', - 'load_5m': '0.01', - 'load_15m': '0.05'}) + self.assertEqual(jc.parsers.uptime.parse(self.centos_7_7_uptime, quiet=True), self.centos_7_7_uptime_json) def test_uptime_ubuntu_18_4(self): """ Test 'uptime' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.uptime.parse(self.ubuntu_18_4_uptime), {'time': '19:43:06', - 'uptime': '2 days, 19:32', - 'users': '2', - 'load_1m': '0.00', - 'load_5m': '0.00', - 'load_15m': '0.00'}) + self.assertEqual(jc.parsers.uptime.parse(self.ubuntu_18_4_uptime, quiet=True), self.ubuntu_18_4_uptime_json) if __name__ == '__main__': diff --git a/tests/test_w.py b/tests/test_w.py index 50c39356..52d8b8f1 100644 --- a/tests/test_w.py +++ b/tests/test_w.py @@ -1,4 +1,5 @@ import os +import json import unittest import jc.parsers.w @@ -8,37 +9,31 @@ THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class MyTests(unittest.TestCase): def setUp(self): + # input with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/w.out'), 'r') as f: self.centos_7_7_w = f.read() with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/w.out'), 'r') as f: self.ubuntu_18_4_w = f.read() + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/w.json'), 'r') as f: + self.centos_7_7_w_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/w.json'), 'r') as f: + self.ubuntu_18_4_w_json = json.loads(f.read()) + def test_w_centos_7_7(self): """ Test 'w' on Centos 7.7 """ - self.assertEqual(jc.parsers.w.parse(self.centos_7_7_w)[1], {'user': 'kbrazil', - 'tty': 'pts/0', - 'from': '192.168.71.1', - 'login_at': '09:53', - 'idle': '8.00s', - 'jcpu': '0.10s', - 'pcpu': '0.00s', - 'what': 'w'}) + self.assertEqual(jc.parsers.w.parse(self.centos_7_7_w, quiet=True), self.centos_7_7_w_json) def test_w_ubuntu_18_4(self): """ Test 'w' on Ubuntu 18.4 """ - self.assertEqual(jc.parsers.w.parse(self.ubuntu_18_4_w)[1], {'user': 'kbrazil', - 'tty': 'pts/0', - 'from': '192.168.71.1', - 'login_at': 'Thu22', - 'idle': '10.00s', - 'jcpu': '0.17s', - 'pcpu': '0.00s', - 'what': 'w'}) + self.assertEqual(jc.parsers.w.parse(self.ubuntu_18_4_w, quiet=True), self.ubuntu_18_4_w_json) if __name__ == '__main__':