diff --git a/changelog.txt b/changelog.txt index b3f3a89b..fc21a943 100644 --- a/changelog.txt +++ b/changelog.txt @@ -15,6 +15,8 @@ jc changelog - Update ntpq parser to fix error on parsing empty data - Update ps parser to fix error on parsing empty data - Update route parser to fix error on parsing empty data +- Update systemctl parser to fix error on parsing empty data +- Update systemctl_lj parser to fix error on parsing empty data - Add tests to all parsers for no data condition - Update ss parser to fix integer fields diff --git a/jc/parsers/systemctl.py b/jc/parsers/systemctl.py index 3f6f7074..ff434b29 100644 --- a/jc/parsers/systemctl.py +++ b/jc/parsers/systemctl.py @@ -40,7 +40,7 @@ import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'systemctl command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -99,24 +99,27 @@ def parse(data, raw=False, quiet=False): linedata = data.splitlines() # Clear any blank lines linedata = list(filter(None, linedata)) - # clean up non-ascii characters, if any - cleandata = [] - for entry in linedata: - cleandata.append(entry.encode('ascii', errors='ignore').decode()) - - header_text = cleandata[0] - header_list = header_text.lower().split() - raw_output = [] - for entry in cleandata[1:]: - if 'LOAD = ' in entry: - break + if linedata: + # clean up non-ascii characters, if any + cleandata = [] + for entry in linedata: + cleandata.append(entry.encode('ascii', errors='ignore').decode()) - else: - entry_list = entry.rstrip().split(maxsplit=4) - output_line = dict(zip(header_list, entry_list)) - raw_output.append(output_line) + header_text = cleandata[0] + header_list = header_text.lower().split() + + raw_output = [] + + for entry in cleandata[1:]: + if 'LOAD = ' in entry: + break + + else: + entry_list = entry.rstrip().split(maxsplit=4) + output_line = dict(zip(header_list, entry_list)) + raw_output.append(output_line) if raw: return raw_output diff --git a/jc/parsers/systemctl_lj.py b/jc/parsers/systemctl_lj.py index ee3d0ba9..acc5d174 100644 --- a/jc/parsers/systemctl_lj.py +++ b/jc/parsers/systemctl_lj.py @@ -59,7 +59,7 @@ import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'systemctl list-jobs command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -126,24 +126,27 @@ def parse(data, raw=False, quiet=False): # Clear any blank lines linedata = list(filter(None, linedata)) # clean up non-ascii characters, if any - cleandata = [] - for entry in linedata: - cleandata.append(entry.encode('ascii', errors='ignore').decode()) - - header_text = cleandata[0] - header_text = header_text.lower() - header_list = header_text.split() - raw_output = [] - for entry in cleandata[1:]: - if 'No jobs running.' in entry or 'jobs listed.' in entry: - break + if linedata: + cleandata = [] + for entry in linedata: + cleandata.append(entry.encode('ascii', errors='ignore').decode()) - else: - entry_list = entry.split(maxsplit=4) - output_line = dict(zip(header_list, entry_list)) - raw_output.append(output_line) + header_text = cleandata[0] + header_text = header_text.lower() + header_list = header_text.split() + + raw_output = [] + + for entry in cleandata[1:]: + if 'No jobs running.' in entry or 'jobs listed.' in entry: + break + + else: + entry_list = entry.split(maxsplit=4) + output_line = dict(zip(header_list, entry_list)) + raw_output.append(output_line) if raw: return raw_output diff --git a/tests/test_stat.py b/tests/test_stat.py index a9eb8bc7..212f9eec 100644 --- a/tests/test_stat.py +++ b/tests/test_stat.py @@ -35,6 +35,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/stat.json'), 'r', encoding='utf-8') as f: self.freebsd12_stat_json = json.loads(f.read()) + def test_stat_nodata(self): + """ + Test 'stat' with no data + """ + self.assertEqual(jc.parsers.stat.parse('', quiet=True), []) + def test_stat_centos_7_7(self): """ Test 'stat /bin/*' on Centos 7.7 diff --git a/tests/test_systemctl.py b/tests/test_systemctl.py index 1da9bad8..70516c4f 100644 --- a/tests/test_systemctl.py +++ b/tests/test_systemctl.py @@ -23,6 +23,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/systemctl.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_systemctl_json = json.loads(f.read()) + def test_systemctl_nodata(self): + """ + Test 'systemctl' with no data + """ + self.assertEqual(jc.parsers.systemctl.parse('', quiet=True), []) + def test_systemctl_centos_7_7(self): """ Test 'systemctl -a' on Centos 7.7 diff --git a/tests/test_systemctl_lj.py b/tests/test_systemctl_lj.py index bdc108b0..4c82f6cc 100644 --- a/tests/test_systemctl_lj.py +++ b/tests/test_systemctl_lj.py @@ -17,6 +17,12 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/systemctl-lj.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_systemctl_lj_json = json.loads(f.read()) + def test_systemctl_lj_nodata(self): + """ + Test 'systemctl -a list-jobs' with no data + """ + self.assertEqual(jc.parsers.systemctl_lj.parse('', quiet=True), []) + def test_systemctl_lj_ubuntu_18_4(self): """ Test 'systemctl -a list-jobs' on Ubuntu 18.4