From 69b22215697d0c9aad944d6c885fd9d3a269ab4f Mon Sep 17 00:00:00 2001 From: sujkang Date: Wed, 25 Nov 2020 01:34:22 -0800 Subject: [PATCH] unit test --- show/reboot_cause.py | 31 +++++++++++++++++-------------- tests/mock_tables/state_db.json | 4 ++-- tests/reboot_cause_test.py | 10 ++++++++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/show/reboot_cause.py b/show/reboot_cause.py index 3d2cafb46a..685a0c352d 100755 --- a/show/reboot_cause.py +++ b/show/reboot_cause.py @@ -47,22 +47,25 @@ def history(): prefix = REBOOT_CAUSE_TABLE_NAME + TABLE_NAME_SEPARATOR _hash = '{}{}'.format(prefix, '*') table_keys = db.keys(db.STATE_DB, _hash) - if table_keys is None: + if table_keys is not None: click.echo("Reboot-cause history is not yet available in StateDB") sys.exit(1) - table_keys.sort(reverse=True) + table_keys.sort(reverse=True) - table = [] - for tk in table_keys: - entry = db.get_all(db.STATE_DB, tk) - r = [] - r.append(tk.replace(prefix,"")) - r.append(entry['cause'] if 'cause' in entry else "") - r.append(entry['time'] if 'time' in entry else "") - r.append(entry['user'] if 'user' in entry else "") - r.append(entry['comment'] if 'comment' in entry else "") - table.append(r) + table = [] + for tk in table_keys: + entry = db.get_all(db.STATE_DB, tk) + r = [] + r.append(tk.replace(prefix,"")) + r.append(entry['cause'] if 'cause' in entry else "") + r.append(entry['time'] if 'time' in entry else "") + r.append(entry['user'] if 'user' in entry else "") + r.append(entry['comment'] if 'comment' in entry else "") + table.append(r) - header = ['Name', 'Cause', 'Time', 'User', 'Comment'] - click.echo(tabulate(table, header)) + header = ['Name', 'Cause', 'Time', 'User', 'Comment'] + click.echo(tabulate(table, header)) + else: + click.echo("Reboot-cause history is not yet available in StateDB") + sys.exit(1) diff --git a/tests/mock_tables/state_db.json b/tests/mock_tables/state_db.json index 0351777be9..3e297df3b9 100644 --- a/tests/mock_tables/state_db.json +++ b/tests/mock_tables/state_db.json @@ -277,13 +277,13 @@ "cause": "warm-reboot", "time": "Fri Oct 9 04:51:47 UTC 2020", "user": "admin", - "comment": "" + "comment": "N/A" }, "REBOOT_CAUSE|2020_10_09_02_33_06": { "cause": "reboot", "time": "Fri Oct 9 02:29:44 UTC 2020", "user": "admin", - "comment": "" + "comment": "N/A" }, "CHASSIS_TABLE|CHASSIS 1": { "module_num": "5" diff --git a/tests/reboot_cause_test.py b/tests/reboot_cause_test.py index 89f2c4ef6b..0eab069bee 100644 --- a/tests/reboot_cause_test.py +++ b/tests/reboot_cause_test.py @@ -3,6 +3,12 @@ import textwrap import mock from click.testing import CliRunner +from .mock_tables import dbconnector + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +sys.path.insert(0, modules_path) + import show.main as show @@ -52,8 +58,8 @@ def test_reboot_cause_history(self): expected_output = """\ Name Cause Time User Comment ------------------- ----------- ---------------------------- ------ --------- -2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin -2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin +2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin N/A +2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin N/A """ runner = CliRunner() result = runner.invoke(show.cli.commands["reboot-cause"].commands["history"], [])