From ef0206f8535f1d990b0373b8160e7b156d3de96d Mon Sep 17 00:00:00 2001 From: armab Date: Tue, 16 Oct 2018 20:13:15 +0100 Subject: [PATCH 1/5] Fx st2 key load to not fail on empty yaml/json files --- st2client/st2client/commands/keyvalue.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/st2client/st2client/commands/keyvalue.py b/st2client/st2client/commands/keyvalue.py index aba5f32db9..f5fff96814 100644 --- a/st2client/st2client/commands/keyvalue.py +++ b/st2client/st2client/commands/keyvalue.py @@ -294,12 +294,16 @@ def run(self, args, **kwargs): # load the data (JSON/YAML) from the file kvps = resource.load_meta_file(file_path) + instances = [] + # bail out if file was empty + if not kvps: + return instances + # if the data is not a list (ie. it's a single entry) # then make it a list so our process loop is generic if not isinstance(kvps, list): kvps = [kvps] - instances = [] for item in kvps: # parse required KeyValuePair properties name = item['name'] From 0223857848d98e3d84815fe5828c6eda3f4dabea Mon Sep 17 00:00:00 2001 From: armab Date: Tue, 16 Oct 2018 20:19:20 +0100 Subject: [PATCH 2/5] Add Unit test for an empty K/V load file case --- st2client/tests/unit/test_keyvalue.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/st2client/tests/unit/test_keyvalue.py b/st2client/tests/unit/test_keyvalue.py index b7233536ea..ab31c63d9f 100644 --- a/st2client/tests/unit/test_keyvalue.py +++ b/st2client/tests/unit/test_keyvalue.py @@ -333,3 +333,16 @@ def test_load_keyvalue_bad_file_extension(self): finally: os.close(fd) os.unlink(path) + + def test_load_keyvalue_empty_file(self): + """ + Loading K/V from an empty file shouldn't throw an error + """ + fd, path = tempfile.mkstemp(suffix='.yaml') + try: + args = ['key', 'load', path] + retcode = self.shell.run(args) + self.assertEqual(retcode, 0) + finally: + os.close(fd) + os.unlink(path) From 85c4aeca9e3fb151553af9543339c903985cbe3e Mon Sep 17 00:00:00 2001 From: armab Date: Tue, 16 Oct 2018 21:07:47 +0100 Subject: [PATCH 3/5] Improve 'st2 key load' test case by checking stdout output --- st2client/tests/unit/test_keyvalue.py | 1 + 1 file changed, 1 insertion(+) diff --git a/st2client/tests/unit/test_keyvalue.py b/st2client/tests/unit/test_keyvalue.py index ab31c63d9f..bd235852d1 100644 --- a/st2client/tests/unit/test_keyvalue.py +++ b/st2client/tests/unit/test_keyvalue.py @@ -342,6 +342,7 @@ def test_load_keyvalue_empty_file(self): try: args = ['key', 'load', path] retcode = self.shell.run(args) + self.assertTrue('No matching items found' in self.stdout.getvalue()) self.assertEqual(retcode, 0) finally: os.close(fd) From bfd2ff895e08bd225e18870b95956fa43d52589c Mon Sep 17 00:00:00 2001 From: armab Date: Tue, 16 Oct 2018 21:08:23 +0100 Subject: [PATCH 4/5] Add a Changelog entry for #4393 bug --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0fd49a3eca..9deeeec027 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -33,6 +33,7 @@ Fixed is checked out). (improvement) #4366 * st2 login now exits with non zero exit code when login fails due to invalid credentials. (improvement) #4338 +* Fix ``st2 key load`` that errors when importing an empty file #4393 2.9.0 - September 16, 2018 -------------------------- From f148484f3677df95d7ed22a3885b9fbd0b837bc0 Mon Sep 17 00:00:00 2001 From: armab Date: Tue, 16 Oct 2018 21:30:59 +0100 Subject: [PATCH 5/5] Use of assertIn instead of assertTrue in Unit test case --- st2client/tests/unit/test_keyvalue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2client/tests/unit/test_keyvalue.py b/st2client/tests/unit/test_keyvalue.py index bd235852d1..4ebcba1d8b 100644 --- a/st2client/tests/unit/test_keyvalue.py +++ b/st2client/tests/unit/test_keyvalue.py @@ -342,7 +342,7 @@ def test_load_keyvalue_empty_file(self): try: args = ['key', 'load', path] retcode = self.shell.run(args) - self.assertTrue('No matching items found' in self.stdout.getvalue()) + self.assertIn('No matching items found', self.stdout.getvalue()) self.assertEqual(retcode, 0) finally: os.close(fd)