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 -------------------------- 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'] diff --git a/st2client/tests/unit/test_keyvalue.py b/st2client/tests/unit/test_keyvalue.py index b7233536ea..4ebcba1d8b 100644 --- a/st2client/tests/unit/test_keyvalue.py +++ b/st2client/tests/unit/test_keyvalue.py @@ -333,3 +333,17 @@ 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.assertIn('No matching items found', self.stdout.getvalue()) + self.assertEqual(retcode, 0) + finally: + os.close(fd) + os.unlink(path)