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
diff --git a/tempest/cmd/init.py b/tempest/cmd/init.py
index f577d9b..baa36a2 100644
--- a/tempest/cmd/init.py
+++ b/tempest/cmd/init.py
@@ -173,10 +173,10 @@
         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)
diff --git a/tempest/tests/cmd/test_tempest_init.py b/tempest/tests/cmd/test_tempest_init.py
index 2844371..79510be 100644
--- a/tempest/tests/cmd/test_tempest_init.py
+++ b/tempest/tests/cmd/test_tempest_init.py
@@ -137,3 +137,18 @@
         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)