From 41a6311f6b3ed2cc94919d042aed51c46cdf3e2f Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 22 Sep 2021 19:34:44 -0700 Subject: [PATCH] add streaming parser tests --- .../centos-7.7/vmstat-a-streaming.json | 1 + .../centos-7.7/vmstat-at-5-10-streaming.json | 1 + .../centos-7.7/vmstat-awt-streaming.json | 1 + .../centos-7.7/vmstat-d-streaming.json | 1 + .../centos-7.7/vmstat-dt-streaming.json | 1 + .../fixtures/centos-7.7/vmstat-streaming.json | 1 + .../centos-7.7/vmstat-w-streaming.json | 1 + tests/test_ping_s.py | 2 +- tests/test_vmstat_s.py | 124 ++++++++++++++++++ 9 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/centos-7.7/vmstat-a-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-at-5-10-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-awt-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-d-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-dt-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-streaming.json create mode 100644 tests/fixtures/centos-7.7/vmstat-w-streaming.json create mode 100644 tests/test_vmstat_s.py diff --git a/tests/fixtures/centos-7.7/vmstat-a-streaming.json b/tests/fixtures/centos-7.7/vmstat-a-streaming.json new file mode 100644 index 00000000..253e0143 --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-a-streaming.json @@ -0,0 +1 @@ +[{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794696,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312736,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":null,"timezone":null}] diff --git a/tests/fixtures/centos-7.7/vmstat-at-5-10-streaming.json b/tests/fixtures/centos-7.7/vmstat-at-5-10-streaming.json new file mode 100644 index 00000000..4b6e874c --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-at-5-10-streaming.json @@ -0,0 +1 @@ +[{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794716,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312724,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":"2021-09-16 20:33:13","timezone":"PDT","epoch":1631849593,"epoch_utc":null},{"runnable_procs":0,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794716,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312744,"swap_in":0,"swap_out":0,"blocks_in":0,"blocks_out":0,"interrupts":41,"context_switches":84,"user_time":0,"system_time":0,"idle_time":100,"io_wait_time":0,"stolen_time":0,"timestamp":"2021-09-16 20:33:18","timezone":"PDT","epoch":1631849598,"epoch_utc":null},{"runnable_procs":0,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794716,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312752,"swap_in":0,"swap_out":0,"blocks_in":0,"blocks_out":0,"interrupts":41,"context_switches":84,"user_time":0,"system_time":0,"idle_time":100,"io_wait_time":0,"stolen_time":0,"timestamp":"2021-09-16 20:33:23","timezone":"PDT","epoch":1631849603,"epoch_utc":null},{"runnable_procs":0,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794716,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312752,"swap_in":0,"swap_out":0,"blocks_in":0,"blocks_out":0,"interrupts":44,"context_switches":87,"user_time":0,"system_time":0,"idle_time":100,"io_wait_time":0,"stolen_time":0,"timestamp":"2021-09-16 20:33:28","timezone":"PDT","epoch":1631849608,"epoch_utc":null}] diff --git a/tests/fixtures/centos-7.7/vmstat-awt-streaming.json b/tests/fixtures/centos-7.7/vmstat-awt-streaming.json new file mode 100644 index 00000000..084da286 --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-awt-streaming.json @@ -0,0 +1 @@ +[{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794864,"buffer_mem":null,"cache_mem":null,"inactive_mem":295968,"active_mem":312724,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":"2021-09-16 20:31:30","timezone":"PDT","epoch":1631849490,"epoch_utc":null}] diff --git a/tests/fixtures/centos-7.7/vmstat-d-streaming.json b/tests/fixtures/centos-7.7/vmstat-d-streaming.json new file mode 100644 index 00000000..23b80020 --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-d-streaming.json @@ -0,0 +1 @@ +[{"disk":"sda","total_reads":15257,"merged_reads":100,"sectors_read":841035,"reading_ms":8395,"total_writes":50851,"merged_writes":5502,"sectors_written":1648657,"writing_ms":146540,"current_io":0,"io_seconds":44,"timestamp":null,"timezone":null},{"disk":"sr0","total_reads":0,"merged_reads":0,"sectors_read":0,"reading_ms":0,"total_writes":0,"merged_writes":0,"sectors_written":0,"writing_ms":0,"current_io":0,"io_seconds":0,"timestamp":null,"timezone":null},{"disk":"dm-0","total_reads":14910,"merged_reads":0,"sectors_read":775075,"reading_ms":8350,"total_writes":56343,"merged_writes":0,"sectors_written":1644520,"writing_ms":180880,"current_io":0,"io_seconds":44,"timestamp":null,"timezone":null},{"disk":"dm-1","total_reads":88,"merged_reads":0,"sectors_read":4408,"reading_ms":13,"total_writes":0,"merged_writes":0,"sectors_written":0,"writing_ms":0,"current_io":0,"io_seconds":0,"timestamp":null,"timezone":null}] diff --git a/tests/fixtures/centos-7.7/vmstat-dt-streaming.json b/tests/fixtures/centos-7.7/vmstat-dt-streaming.json new file mode 100644 index 00000000..5097e9d4 --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-dt-streaming.json @@ -0,0 +1 @@ +[{"disk":"sda","total_reads":15257,"merged_reads":100,"sectors_read":841035,"reading_ms":8395,"total_writes":50851,"merged_writes":5502,"sectors_written":1648657,"writing_ms":146540,"current_io":0,"io_seconds":44,"timestamp":"2021-09-16 20:32:28","timezone":"UTC","epoch":1631849548,"epoch_utc":1631824348},{"disk":"sr0","total_reads":0,"merged_reads":0,"sectors_read":0,"reading_ms":0,"total_writes":0,"merged_writes":0,"sectors_written":0,"writing_ms":0,"current_io":0,"io_seconds":0,"timestamp":"2021-09-16 20:32:28","timezone":"UTC","epoch":1631849548,"epoch_utc":1631824348},{"disk":"dm-0","total_reads":14910,"merged_reads":0,"sectors_read":775075,"reading_ms":8350,"total_writes":56343,"merged_writes":0,"sectors_written":1644520,"writing_ms":180880,"current_io":0,"io_seconds":44,"timestamp":"2021-09-16 20:32:28","timezone":"UTC","epoch":1631849548,"epoch_utc":1631824348},{"disk":"dm-1","total_reads":88,"merged_reads":0,"sectors_read":4408,"reading_ms":13,"total_writes":0,"merged_writes":0,"sectors_written":0,"writing_ms":0,"current_io":0,"io_seconds":0,"timestamp":"2021-09-16 20:32:28","timezone":"UTC","epoch":1631849548,"epoch_utc":1631824348}] diff --git a/tests/fixtures/centos-7.7/vmstat-streaming.json b/tests/fixtures/centos-7.7/vmstat-streaming.json new file mode 100644 index 00000000..d858c964 --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-streaming.json @@ -0,0 +1 @@ +[{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794468,"buffer_mem":2108,"cache_mem":741208,"inactive_mem":null,"active_mem":null,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":null,"timezone":null}] diff --git a/tests/fixtures/centos-7.7/vmstat-w-streaming.json b/tests/fixtures/centos-7.7/vmstat-w-streaming.json new file mode 100644 index 00000000..c2d8c02b --- /dev/null +++ b/tests/fixtures/centos-7.7/vmstat-w-streaming.json @@ -0,0 +1 @@ +[{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794484,"buffer_mem":2108,"cache_mem":741208,"inactive_mem":null,"active_mem":null,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":null,"timezone":null}] diff --git a/tests/test_ping_s.py b/tests/test_ping_s.py index 11eedd36..6fdd8bfd 100644 --- a/tests/test_ping_s.py +++ b/tests/test_ping_s.py @@ -410,7 +410,7 @@ class MyTests(unittest.TestCase): Test 'ping' with quiet option and error """ data_in = 'not ping' - expected = json.loads('[{"_meta":{"success":false,"error":"Could not detect ping OS","line":"not ping"}}]') + expected = json.loads('[{"_meta":{"success":false,"error":"ParseError: Could not detect ping OS","line":"not ping"}}]') self.assertEqual(list(jc.parsers.ping_s.parse(data_in.splitlines(), quiet=True)), expected) def test_ping_ip_O_centos_7_7(self): diff --git a/tests/test_vmstat_s.py b/tests/test_vmstat_s.py new file mode 100644 index 00000000..3b0365ec --- /dev/null +++ b/tests/test_vmstat_s.py @@ -0,0 +1,124 @@ +import os +import sys +import time +import json +import unittest +from jc.exceptions import ParseError +import jc.parsers.vmstat_s + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + +# Set the timezone on POSIX systems. Need to manually set for Windows tests +if not sys.platform.startswith('win32'): + os.environ['TZ'] = 'America/Los_Angeles' + time.tzset() + + +# To create streaming output use: +# $ cat vmstat.out | jc --ls-s | jello -c > vmstat-streaming.json + + +class MyTests(unittest.TestCase): + + def setUp(self): + # input + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-a.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_a = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-w.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_w = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-at-5-10.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_at_5_10 = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-awt.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_awt = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-d.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_d = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-dt.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_dt = f.read() + + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-a-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_a_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-w-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_w_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-at-5-10-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_at_5_10_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-awt-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_awt_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-d-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_d_streaming_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/vmstat-dt-streaming.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_vmstat_dt_streaming_json = json.loads(f.read()) + + + def test_vmstat_nodata(self): + """ + Test 'vmstat' with no data + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse('')), []) + + def test_vmstat_unparsable(self): + data = 'unparsable data' + g = jc.parsers.vmstat_s.parse(data.splitlines()) + with self.assertRaises(KeyError): + list(g) + + def test_vmstat_centos_7_7(self): + """ + Test 'vmstat' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat.splitlines())), self.centos_7_7_vmstat_streaming_json) + + def test_vmstat_a_centos_7_7(self): + """ + Test 'vmstat -a' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_a.splitlines())), self.centos_7_7_vmstat_a_streaming_json) + + def test_vmstat_w_centos_7_7(self): + """ + Test 'vmstat -w' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_w.splitlines())), self.centos_7_7_vmstat_w_streaming_json) + + def test_vmstat_at_5_10_centos_7_7(self): + """ + Test 'vmstat -at 5 10' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_at_5_10.splitlines())), self.centos_7_7_vmstat_at_5_10_streaming_json) + + def test_vmstat_awt_centos_7_7(self): + """ + Test 'vmstat -awt' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_awt.splitlines())), self.centos_7_7_vmstat_awt_streaming_json) + + def test_vmstat_d_centos_7_7(self): + """ + Test 'vmstat -d' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_d.splitlines())), self.centos_7_7_vmstat_d_streaming_json) + + def test_vmstat_dt_centos_7_7(self): + """ + Test 'vmstat -dt' on Centos 7.7 + """ + self.assertEqual(list(jc.parsers.vmstat_s.parse(self.centos_7_7_vmstat_dt.splitlines())), self.centos_7_7_vmstat_dt_streaming_json) + +if __name__ == '__main__': + unittest.main()