From 864f37eb66316707c9311d70fd66a94406b40f00 Mon Sep 17 00:00:00 2001 From: Manik Bindlish Date: Thu, 6 Sep 2018 06:30:51 +0000 Subject: [PATCH] Handling invalid name of workspace register and rename. This PS will fix the invalid value handling. Error will be raised if no/blank value will be specified for workspace register name and workspace rename. Change-Id: Id8a3f496a8902bd7f0d66254ee8fa148675974e9 Closes-Bug: #1791007 --- .../notes/bug-1791007-328a8b9a43bfb157.yaml | 8 +++++++ tempest/cmd/workspace.py | 8 +++++++ tempest/tests/cmd/test_workspace.py | 23 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 releasenotes/notes/bug-1791007-328a8b9a43bfb157.yaml diff --git a/releasenotes/notes/bug-1791007-328a8b9a43bfb157.yaml b/releasenotes/notes/bug-1791007-328a8b9a43bfb157.yaml new file mode 100644 index 0000000000..a2e23fd224 --- /dev/null +++ b/releasenotes/notes/bug-1791007-328a8b9a43bfb157.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed bug #1791007. ``tempest workspace register`` and ``tempest workspace rename`` CLI will + error if None or empty string is passed in --name arguments. Earlier both CLI used to accept + the None or empty string as name which was confusing. + + diff --git a/tempest/cmd/workspace.py b/tempest/cmd/workspace.py index 929a584d62..d276bde43d 100644 --- a/tempest/cmd/workspace.py +++ b/tempest/cmd/workspace.py @@ -86,6 +86,7 @@ class WorkspaceManager(object): def rename_workspace(self, old_name, new_name): self._populate() self._name_exists(old_name) + self._invalid_name_check(new_name) self._workspace_name_exists(new_name) self.workspaces[new_name] = self.workspaces.pop(old_name) self._write_file() @@ -128,6 +129,12 @@ class WorkspaceManager(object): name)) sys.exit(1) + def _invalid_name_check(self, name): + if not name: + print("None or empty name is specified." + " Please specify correct name for workspace.") + sys.exit(1) + def _validate_path(self, path): if not os.path.exists(path): print("Path does not exist.") @@ -141,6 +148,7 @@ class WorkspaceManager(object): # This only happens when register is called from outside of init if not init: self._validate_path(path) + self._invalid_name_check(name) self._workspace_name_exists(name) self.workspaces[name] = path self._write_file() diff --git a/tempest/tests/cmd/test_workspace.py b/tempest/tests/cmd/test_workspace.py index 3ed8a10611..a256368265 100644 --- a/tempest/tests/cmd/test_workspace.py +++ b/tempest/tests/cmd/test_workspace.py @@ -122,6 +122,17 @@ class TestTempestWorkspaceManager(TestTempestWorkspaceBase): self.assertIsNone(self.workspace_manager.get_workspace(self.name)) self.assertIsNotNone(self.workspace_manager.get_workspace(new_name)) + def test_workspace_manager_rename_no_name_exist(self): + no_name = "" + with patch('sys.stdout', new_callable=StringIO) as mock_stdout: + ex = self.assertRaises(SystemExit, + self.workspace_manager.rename_workspace, + self.name, no_name) + self.assertEqual(1, ex.code) + self.assertEqual(mock_stdout.getvalue(), + "None or empty name is specified." + " Please specify correct name for workspace.\n") + def test_workspace_manager_move(self): new_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, new_path, ignore_errors=True) @@ -184,3 +195,15 @@ class TestTempestWorkspaceManager(TestTempestWorkspaceBase): self.assertEqual(1, len(listed)) self.assertIn(self.name, listed) self.assertEqual(self.path, listed.get(self.name)) + + def test_register_new_workspace_no_name(self): + no_name = "" + with patch('sys.stdout', new_callable=StringIO) as mock_stdout: + ex = self.assertRaises(SystemExit, + self.workspace_manager. + register_new_workspace, + no_name, self.path) + self.assertEqual(1, ex.code) + self.assertEqual(mock_stdout.getvalue(), + "None or empty name is specified." + " Please specify correct name for workspace.\n")