Merge "Improve error reporting when workspace not registered"
diff --git a/tempest/cmd/run.py b/tempest/cmd/run.py
index 236953c..5fa8b74 100644
--- a/tempest/cmd/run.py
+++ b/tempest/cmd/run.py
@@ -135,6 +135,12 @@
workspace_mgr = workspace.WorkspaceManager(
parsed_args.workspace_path)
path = workspace_mgr.get_workspace(parsed_args.workspace)
+ if not path:
+ sys.exit(
+ "The %r workspace isn't registered in "
+ "%r. Use 'tempest init' to "
+ "register the workspace." %
+ (parsed_args.workspace, workspace_mgr.path))
os.chdir(path)
# NOTE(mtreinish): tempest init should create a .testrepository dir
# but since workspaces can be imported let's sanity check and
diff --git a/tempest/cmd/workspace.py b/tempest/cmd/workspace.py
index b36cf4e..3c58648 100644
--- a/tempest/cmd/workspace.py
+++ b/tempest/cmd/workspace.py
@@ -72,7 +72,10 @@
@lockutils.synchronized('workspaces', external=True)
def get_workspace(self, name):
- """Returns the workspace that has the given name"""
+ """Returns the workspace that has the given name
+
+ If the workspace isn't registered then `None` is returned.
+ """
self._populate()
return self.workspaces.get(name)
diff --git a/tempest/tests/cmd/test_run.py b/tempest/tests/cmd/test_run.py
index 772391f..7ac347d 100644
--- a/tempest/tests/cmd/test_run.py
+++ b/tempest/tests/cmd/test_run.py
@@ -18,6 +18,7 @@
import subprocess
import tempfile
+import fixtures
import mock
from tempest.cmd import run
@@ -122,3 +123,32 @@
# too.
subprocess.call(['git', 'init'], stderr=DEVNULL)
self.assertRunExit(['tempest', 'run'], 1)
+
+
+class TestTakeAction(base.TestCase):
+ def test_workspace_not_registered(self):
+ class Exception_(Exception):
+ pass
+
+ m_exit = self.useFixture(fixtures.MockPatch('sys.exit')).mock
+ # sys.exit must not continue (or exit)
+ m_exit.side_effect = Exception_
+
+ workspace = self.getUniqueString()
+
+ tempest_run = run.TempestRun(app=mock.Mock(), app_args=mock.Mock())
+ parsed_args = mock.Mock()
+ parsed_args.config_file = []
+
+ # Override $HOME so that empty workspace gets created in temp dir.
+ self.useFixture(fixtures.TempHomeDir())
+
+ # Force use of the temporary home directory.
+ parsed_args.workspace_path = None
+
+ # Simulate --workspace argument.
+ parsed_args.workspace = workspace
+
+ self.assertRaises(Exception_, tempest_run.take_action, parsed_args)
+ exit_msg = m_exit.call_args[0][0]
+ self.assertIn(workspace, exit_msg)