Use cliff.lister for tempest workspace command

This commit makes tempest workspace command use cliff.lister to show
various formats. And this commit splits the original class -
TempestWorkspace into the subcommand classes to use the cliff.lister
class. With this splitting, we can get the subcommand helps as a side
effect.

Change-Id: I07c69c467743a2f132c99d992773a53bda3bec7b
This commit is contained in:
Masayuki Igawa 2016-07-25 12:28:26 +09:00 committed by Masayuki Igawa
parent 6455980e71
commit 00effc933f
3 changed files with 106 additions and 77 deletions

View File

@ -39,7 +39,11 @@ tempest.cm =
cleanup = tempest.cmd.cleanup:TempestCleanup
list-plugins = tempest.cmd.list_plugins:TempestListPlugins
verify-config = tempest.cmd.verify_tempest_config:TempestVerifyConfig
workspace = tempest.cmd.workspace:TempestWorkspace
workspace_register = tempest.cmd.workspace:TempestWorkspaceRegister
workspace_rename = tempest.cmd.workspace:TempestWorkspaceRename
workspace_move = tempest.cmd.workspace:TempestWorkspaceMove
workspace_remove = tempest.cmd.workspace:TempestWorkspaceRemove
workspace_list = tempest.cmd.workspace:TempestWorkspaceList
run = tempest.cmd.run:TempestRun
oslo.config.opts =
tempest.config = tempest.config:list_opts

View File

@ -52,8 +52,8 @@ import os
import sys
from cliff import command
from cliff import lister
from oslo_concurrency import lockutils
import prettytable
import yaml
from tempest import config
@ -154,76 +154,97 @@ class WorkspaceManager(object):
self.workspaces = yaml.safe_load(f) or {}
class TempestWorkspace(command.Command):
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
if getattr(parsed_args, 'register', None):
self.manager.register_new_workspace(
parsed_args.name, parsed_args.path)
elif getattr(parsed_args, 'rename', None):
self.manager.rename_workspace(
parsed_args.old_name, parsed_args.new_name)
elif getattr(parsed_args, 'move', None):
self.manager.move_workspace(
parsed_args.name, parsed_args.path)
elif getattr(parsed_args, 'remove', None):
self.manager.remove_workspace(
parsed_args.name)
else:
self._print_workspaces()
sys.exit(0)
def add_global_arguments(parser):
parser.add_argument(
'--workspace-path', required=False, default=None,
help="The path to the workspace file, the default is "
"~/.tempest/workspace.yaml")
return parser
class TempestWorkspaceRegister(command.Command):
def get_description(self):
return 'Tempest workspace actions'
return ('Registers a new tempest workspace via a given '
'--name and --path')
def get_parser(self, prog_name):
parser = super(TempestWorkspace, self).get_parser(prog_name)
parser.add_argument(
'--workspace-path', required=False, default=None,
help="The path to the workspace file, the default is "
"~/.tempest/workspace.yaml")
subparsers = parser.add_subparsers()
list_parser = subparsers.add_parser(
'list', help='Outputs the name and path of all known tempest '
'workspaces')
list_parser.set_defaults(list=True)
register_parser = subparsers.add_parser(
'register', help='Registers a new tempest workspace via a given '
'--name and --path')
register_parser.add_argument('--name', required=True)
register_parser.add_argument('--path', required=True)
register_parser.set_defaults(register=True)
update_parser = subparsers.add_parser(
'rename', help='Renames a tempest workspace from --old-name to '
'--new-name')
update_parser.add_argument('--old-name', required=True)
update_parser.add_argument('--new-name', required=True)
update_parser.set_defaults(rename=True)
move_parser = subparsers.add_parser(
'move', help='Changes the path of a given tempest workspace '
'--name to --path')
move_parser.add_argument('--name', required=True)
move_parser.add_argument('--path', required=True)
move_parser.set_defaults(move=True)
remove_parser = subparsers.add_parser(
'remove', help='Deletes the entry for a given tempest workspace '
'--name')
remove_parser.add_argument('--name', required=True)
remove_parser.set_defaults(remove=True)
parser = super(TempestWorkspaceRegister, self).get_parser(prog_name)
add_global_arguments(parser)
parser.add_argument('--name', required=True)
parser.add_argument('--path', required=True)
return parser
def _print_workspaces(self):
output = prettytable.PrettyTable(["Name", "Path"])
if self.manager.list_workspaces() is not None:
for name, path in self.manager.list_workspaces().items():
output.add_row([name, path])
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
self.manager.register_new_workspace(parsed_args.name, parsed_args.path)
sys.exit(0)
print(output)
class TempestWorkspaceRename(command.Command):
def get_description(self):
return 'Renames a tempest workspace from --old-name to --new-name'
def get_parser(self, prog_name):
parser = super(TempestWorkspaceRename, self).get_parser(prog_name)
add_global_arguments(parser)
parser.add_argument('--old-name', required=True)
parser.add_argument('--new-name', required=True)
return parser
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
self.manager.rename_workspace(
parsed_args.old_name, parsed_args.new_name)
sys.exit(0)
class TempestWorkspaceMove(command.Command):
def get_description(self):
return 'Changes the path of a given tempest workspace --name to --path'
def get_parser(self, prog_name):
parser = super(TempestWorkspaceMove, self).get_parser(prog_name)
add_global_arguments(parser)
parser.add_argument('--name', required=True)
parser.add_argument('--path', required=True)
return parser
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
self.manager.move_workspace(parsed_args.name, parsed_args.path)
sys.exit(0)
class TempestWorkspaceRemove(command.Command):
def get_description(self):
return 'Deletes the entry for a given tempest workspace --name'
def get_parser(self, prog_name):
parser = super(TempestWorkspaceRemove, self).get_parser(prog_name)
add_global_arguments(parser)
parser.add_argument('--name', required=True)
return parser
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
self.manager.remove_workspace(parsed_args.name)
sys.exit(0)
class TempestWorkspaceList(lister.Lister):
def get_description(self):
return 'Outputs the name and path of all known tempest workspaces'
def get_parser(self, prog_name):
parser = super(TempestWorkspaceList, self).get_parser(prog_name)
add_global_arguments(parser)
return parser
def take_action(self, parsed_args):
self.manager = WorkspaceManager(parsed_args.workspace_path)
return (("Name", "Path"),
((n, p) for n, p in self.manager.list_workspaces().items()))

View File

@ -47,23 +47,25 @@ class TestTempestWorkspace(TestTempestWorkspaceBase):
self.assertEqual(return_code, expected, msg)
def test_run_workspace_list(self):
cmd = ['tempest', 'workspace', '--workspace-path',
self.store_file, 'list']
cmd = ['tempest', 'workspace', 'list',
'--workspace-path', self.store_file]
self._run_cmd_gets_return_code(cmd, 0)
def test_run_workspace_register(self):
name = data_utils.rand_uuid()
path = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, path, ignore_errors=True)
cmd = ['tempest', 'workspace', '--workspace-path', self.store_file,
'register', '--name', name, '--path', path]
cmd = ['tempest', 'workspace', 'register',
'--workspace-path', self.store_file,
'--name', name, '--path', path]
self._run_cmd_gets_return_code(cmd, 0)
self.assertIsNotNone(self.workspace_manager.get_workspace(name))
def test_run_workspace_rename(self):
new_name = data_utils.rand_uuid()
cmd = ['tempest', 'workspace', '--workspace-path', self.store_file,
'rename', "--old-name", self.name, '--new-name', new_name]
cmd = ['tempest', 'workspace', 'rename',
'--workspace-path', self.store_file,
'--old-name', self.name, '--new-name', new_name]
self._run_cmd_gets_return_code(cmd, 0)
self.assertIsNone(self.workspace_manager.get_workspace(self.name))
self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))
@ -71,15 +73,17 @@ class TestTempestWorkspace(TestTempestWorkspaceBase):
def test_run_workspace_move(self):
new_path = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
cmd = ['tempest', 'workspace', '--workspace-path', self.store_file,
'move', '--name', self.name, '--path', new_path]
cmd = ['tempest', 'workspace', 'move',
'--workspace-path', self.store_file,
'--name', self.name, '--path', new_path]
self._run_cmd_gets_return_code(cmd, 0)
self.assertEqual(
self.workspace_manager.get_workspace(self.name), new_path)
def test_run_workspace_remove(self):
cmd = ['tempest', 'workspace', '--workspace-path', self.store_file,
'remove', '--name', self.name]
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))