From b78e2575e04b8735e3e4557d97dcd866838b223c Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Mon, 26 Jun 2017 19:34:34 +0530 Subject: [PATCH] Added --rmdir flag to workspace remove * tempest workspace remove --name command just remove the workspace entry from ~/.tempest/workspace.yaml and it does not deletes the workspace. * by adding tempest workspace remove --name --rmdir removes the workspace as well as entry. * renamed remove_workspace to remove_workspace_entry Change-Id: I4528a23ca4933fdb7a3168f8dc99bbf0497ae5cc --- ...ete-directory-feature-74d6d157a5a05561.yaml | 5 +++++ tempest/cmd/workspace.py | 18 +++++++++++++++--- tempest/tests/cmd/test_workspace.py | 18 +++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/tempest-workspace-delete-directory-feature-74d6d157a5a05561.yaml diff --git a/releasenotes/notes/tempest-workspace-delete-directory-feature-74d6d157a5a05561.yaml b/releasenotes/notes/tempest-workspace-delete-directory-feature-74d6d157a5a05561.yaml new file mode 100644 index 0000000000..ec21098fda --- /dev/null +++ b/releasenotes/notes/tempest-workspace-delete-directory-feature-74d6d157a5a05561.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Added tempest workspace remove --name --rmdir + feature to delete the workspace directory as well as entry. diff --git a/tempest/cmd/workspace.py b/tempest/cmd/workspace.py index 96d23008db..8166b4f8ca 100644 --- a/tempest/cmd/workspace.py +++ b/tempest/cmd/workspace.py @@ -40,6 +40,8 @@ remove ------ Deletes the entry for a given tempest workspace --name +--rmdir Deletes the given tempest workspace directory + General Options =============== @@ -49,6 +51,7 @@ General Options """ import os +import shutil import sys from cliff import command @@ -102,11 +105,16 @@ class WorkspaceManager(object): sys.exit(1) @lockutils.synchronized('workspaces', external=True) - def remove_workspace(self, name): + def remove_workspace_entry(self, name): self._populate() self._name_exists(name) - self.workspaces.pop(name) + workspace_path = self.workspaces.pop(name) self._write_file() + return workspace_path + + @lockutils.synchronized('workspaces', external=True) + def remove_workspace_directory(self, workspace_path): + shutil.rmtree(workspace_path) @lockutils.synchronized('workspaces', external=True) def list_workspaces(self): @@ -226,12 +234,16 @@ class TempestWorkspaceRemove(command.Command): parser = super(TempestWorkspaceRemove, self).get_parser(prog_name) add_global_arguments(parser) parser.add_argument('--name', required=True) + parser.add_argument('--rmdir', action='store_true', + help='Deletes the given workspace directory') return parser def take_action(self, parsed_args): self.manager = WorkspaceManager(parsed_args.workspace_path) - self.manager.remove_workspace(parsed_args.name) + workspace_path = self.manager.remove_workspace_entry(parsed_args.name) + if parsed_args.rmdir: + self.manager.remove_workspace_directory(workspace_path) sys.exit(0) diff --git a/tempest/tests/cmd/test_workspace.py b/tempest/tests/cmd/test_workspace.py index dc6c0c8850..a1c8c53330 100644 --- a/tempest/tests/cmd/test_workspace.py +++ b/tempest/tests/cmd/test_workspace.py @@ -80,13 +80,20 @@ class TestTempestWorkspace(TestTempestWorkspaceBase): self.assertEqual( self.workspace_manager.get_workspace(self.name), new_path) - def test_run_workspace_remove(self): + def test_run_workspace_remove_entry(self): cmd = ['tempest', 'workspace', 'remove', '--workspace-path', self.store_file, '--name', self.name] self._run_cmd_gets_return_code(cmd, 0) self.assertIsNone(self.workspace_manager.get_workspace(self.name)) + def test_run_workspace_remove_directory(self): + cmd = ['tempest', 'workspace', 'remove', + '--workspace-path', self.store_file, + '--name', self.name, '--rmdir'] + self._run_cmd_gets_return_code(cmd, 0) + self.assertIsNone(self.workspace_manager.get_workspace(self.name)) + class TestTempestWorkspaceManager(TestTempestWorkspaceBase): def setUp(self): @@ -117,8 +124,13 @@ class TestTempestWorkspaceManager(TestTempestWorkspaceBase): self.assertEqual( self.workspace_manager.get_workspace(self.name), new_path) - def test_workspace_manager_remove(self): - self.workspace_manager.remove_workspace(self.name) + def test_workspace_manager_remove_entry(self): + self.workspace_manager.remove_workspace_entry(self.name) + self.assertIsNone(self.workspace_manager.get_workspace(self.name)) + + def test_workspace_manager_remove_directory(self): + path = self.workspace_manager.remove_workspace_entry(self.name) + self.workspace_manager.remove_workspace_directory(path) self.assertIsNone(self.workspace_manager.get_workspace(self.name)) def test_path_expansion(self):