Fix tempest init inconsistency when it fails

This commit fixes an inconsistent behavior when tempest init fails.
For example::

  $ tempest init /foobar
  ERROR tempest [-] [Errno 13] Permission denied: '/foobar'
  $ tempest init /foobar
  A workspace already exists with name: foobar.
  $ tempest workspace list
  +------+------+
  | Name | Path |
  +------+------+
  +------+------+

The error message indicates that the workspace already exists but we
cannot see it with the tempest workspace list command. I think users
might be confused with this behavior.

And this commit also fixes --show-global-config-dir,-s option behavior
as a side effect. The original behavior was registering a current
directory as a workspace when the option was specified. However, I
think users don't expect the behavior.

Change-Id: I04ed102a4ad3c3aa678128f0004eb0ba09a05ea7
This commit is contained in:
Masayuki Igawa 2016-09-12 17:12:35 +09:00 committed by Masayuki Igawa
parent 094026d1c1
commit f8026418b1
2 changed files with 17 additions and 2 deletions

View File

@ -173,10 +173,10 @@ class TempestInit(command.Command):
workspace_manager = workspace.WorkspaceManager(
parsed_args.workspace_path)
name = parsed_args.name or parsed_args.dir.split(os.path.sep)[-1]
workspace_manager.register_new_workspace(
name, parsed_args.dir, init=True)
config_dir = parsed_args.config_dir or get_tempest_default_config_dir()
if parsed_args.show_global_dir:
print("Global config dir is located at: %s" % config_dir)
sys.exit(0)
self.create_working_dir(parsed_args.dir, config_dir)
workspace_manager.register_new_workspace(
name, parsed_args.dir, init=True)

View File

@ -137,3 +137,18 @@ class TestTempestInit(base.TestCase):
self.assertTrue(os.path.isfile(fake_file_moved))
self.assertTrue(os.path.isfile(local_conf_file))
self.assertTrue(os.path.isfile(local_testr_conf))
def test_take_action_fails(self):
class ParsedArgs(object):
workspace_dir = self.useFixture(fixtures.TempDir()).path
workspace_path = os.path.join(workspace_dir, 'workspace.yaml')
name = 'test'
dir_base = self.useFixture(fixtures.TempDir()).path
dir = os.path.join(dir_base, 'foo', 'bar')
config_dir = self.useFixture(fixtures.TempDir()).path
show_global_dir = False
pa = ParsedArgs()
init_cmd = init.TempestInit(None, None)
self.assertRaises(OSError, init_cmd.take_action, pa)
# one more trying should be a same error not "workspace already exists"
self.assertRaises(OSError, init_cmd.take_action, pa)