Improve error reporting when workspace not registered

When the user ran

 tempest run --workspace <whatever>

and the workspace wasn't in .tempest/workspace.yaml the eror message
reported was

 CRITICAL tempest [-] TypeError: coercing to Unicode: need string or buffer, NoneType found.

Rather than report this error, a message is printed saying that the
workspace isn't registered.

Change-Id: Ic539ee2fab83401904fcaec2c3fbf1cd41e4a6e3
This commit is contained in:
Brant Knudson 2016-10-13 12:51:49 -05:00
parent 93e6a03700
commit 6a090f4896
3 changed files with 40 additions and 1 deletions

View File

@ -135,6 +135,12 @@ class TempestRun(command.Command):
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

View File

@ -72,7 +72,10 @@ class WorkspaceManager(object):
@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)

View File

@ -18,6 +18,7 @@ import shutil
import subprocess
import tempfile
import fixtures
import mock
from tempest.cmd import run
@ -122,3 +123,32 @@ class TestRunReturnCode(base.TestCase):
# 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)