Merge "[Verify] Fix issue with verifier versions"

This commit is contained in:
Jenkins 2017-02-28 14:14:35 +00:00 committed by Gerrit Code Review
commit 3ba251fcd2
3 changed files with 108 additions and 43 deletions

View File

@ -113,9 +113,9 @@ class VerifyCommands(object):
help="Not to set the created verifier as the default " help="Not to set the created verifier as the default "
"verifier for future operations.") "verifier for future operations.")
@plugins.ensure_plugins_are_loaded @plugins.ensure_plugins_are_loaded
def create_verifier(self, api, name, vtype, namespace="", def create_verifier(self, api, name, vtype, namespace="", source=None,
source=None, version="master", system_wide=False, version=None, system_wide=False, extra=None,
extra=None, do_use=True): do_use=True):
"""Create a verifier.""" """Create a verifier."""
verifier_uuid = api.verifier.create( verifier_uuid = api.verifier.create(
name, vtype=vtype, namespace=namespace, source=source, name, vtype=vtype, namespace=namespace, source=source,

View File

@ -59,7 +59,7 @@ def configure(name, namespace="default", default_repo=None,
def decorator(plugin): def decorator(plugin):
plugin._configure(name, namespace) plugin._configure(name, namespace)
plugin._meta_set("default_repo", default_repo) plugin._meta_set("default_repo", default_repo)
plugin._meta_set("default_version", default_version or "master") plugin._meta_set("default_version", default_version)
plugin._meta_set("context", context or {}) plugin._meta_set("context", context or {})
return plugin return plugin
@ -104,12 +104,13 @@ class VerifierManager(plugin.Plugin):
doc += (" * *system_wide*: Whether or not to use the system-wide " doc += (" * *system_wide*: Whether or not to use the system-wide "
"environment for verifier instead of a virtual environment. " "environment for verifier instead of a virtual environment. "
"Defaults to False.\n" "Defaults to False.\n"
" * *version*: Branch, tag or commit ID to checkout before "
"verifier installation. Defaults to %(default_version)s\n"
" * *source*: Path or URL to the repo to clone verifier from." " * *source*: Path or URL to the repo to clone verifier from."
" Default to %(default_source)s" % { " Defaults to %(default_source)s\n"
"default_version": cls._meta_get("default_version"), " * *version*: Branch, tag or commit ID to checkout before "
"default_source": cls._meta_get("default_repo")}) "verifier installation. Defaults to '%(default_version)s'."
% {"default_source": cls._meta_get("default_repo"),
"default_version": cls._meta_get(
"default_version") or "master"})
return doc return doc
@ -192,17 +193,39 @@ class VerifierManager(plugin.Plugin):
if not URL_RE.match(source) and not os.path.exists(source): if not URL_RE.match(source) and not os.path.exists(source):
raise exceptions.RallyException("Source path '%s' is not valid." raise exceptions.RallyException("Source path '%s' is not valid."
% source) % source)
if logging.is_debug(): if logging.is_debug():
LOG.debug("Cloning verifier repo from %s into %s.", source, LOG.debug("Cloning verifier repo from %s into %s.", source,
self.repo_dir) self.repo_dir)
else: else:
LOG.info("Cloning verifier repo from %s.", source) LOG.info("Cloning verifier repo from %s.", source)
utils.check_output(["git", "clone", source, self.repo_dir])
version = self.verifier.version or self._meta_get("default_version") cmd = ["git", "clone", source, self.repo_dir]
if version and version != "master":
default_version = self._meta_get("default_version")
if default_version and default_version != "master":
cmd.extend(["-b", default_version])
utils.check_output(cmd)
version = self.verifier.version
if version:
LOG.info("Switching verifier repo to the '%s' version." % version) LOG.info("Switching verifier repo to the '%s' version." % version)
utils.check_output(["git", "checkout", version], cwd=self.repo_dir) utils.check_output(["git", "checkout", version], cwd=self.repo_dir)
else:
output = utils.check_output(["git", "describe", "--all"],
cwd=self.repo_dir).strip()
if output.startswith("heads/"): # it is a branch
version = output[6:]
else:
head = utils.check_output(["git", "rev-parse", "HEAD"],
cwd=self.repo_dir).strip()
if output.endswith(head[:7]): # it is a commit ID
version = head
else: # it is a tag
version = output
self.verifier.update_properties(version=version)
def install(self): def install(self):
"""Clone and install a verifier.""" """Clone and install a verifier."""

View File

@ -75,56 +75,98 @@ class VerifierManagerTestCase(test.TestCase):
@mock.patch("rally.verification.manager.os.path.exists", @mock.patch("rally.verification.manager.os.path.exists",
side_effect=[False, True]) side_effect=[False, True])
def test__clone(self, mock_exists): def test__clone(self, mock_exists):
verifier = mock.Mock(source=None) verifier = mock.Mock(version=None)
vmanager = FakeVerifier(verifier) vmanager = FakeVerifier(verifier)
verifier.version = None
vmanager._clone() # Check source validation
self.assertEqual(
[mock.call(["git", "clone", DEFAULT_REPO, vmanager.repo_dir]),
mock.call(["git", "checkout", DEFAULT_VERSION],
cwd=vmanager.repo_dir)],
self.check_output.call_args_list)
self.check_output.reset_mock()
verifier.version = "master"
verifier.source = "some_source" verifier.source = "some_source"
e = self.assertRaises(exceptions.RallyException, vmanager._clone) e = self.assertRaises(exceptions.RallyException, vmanager._clone)
self.assertEqual("Source path 'some_source' is not valid.", "%s" % e) self.assertEqual("Source path 'some_source' is not valid.", "%s" % e)
vmanager._clone() verifier.source = None
self.check_output.assert_called_once_with(
["git", "clone", verifier.source, vmanager.repo_dir])
# Version to switch repo is provided
verifier.version = "1.0.0"
vmanager._clone()
self.assertEqual(
[mock.call(["git", "clone", DEFAULT_REPO,
vmanager.repo_dir, "-b", DEFAULT_VERSION]),
mock.call(["git", "checkout", "1.0.0"], cwd=vmanager.repo_dir)],
self.check_output.call_args_list)
verifier.update_properties.assert_not_called()
# Version to switch repo is not provided
verifier.version = None
self.check_output.side_effect = [
"Output from cloning", "heads/master", "Output from cloning",
"0.1.0-72-g4a39bd4", "4a39bd4qwerty12345", "Output from cloning",
"2.0.0", "12345qwerty4a39bd4"]
# Case 1: verifier is switched to a branch
self.check_output.reset_mock()
verifier.update_properties.reset_mock()
vmanager._clone()
self.assertEqual(
[mock.call(["git", "clone", DEFAULT_REPO,
vmanager.repo_dir, "-b", DEFAULT_VERSION]),
mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir)],
self.check_output.call_args_list)
verifier.update_properties.assert_called_once_with(version="master")
# Case 2: verifier is switched to a commit ID
self.check_output.reset_mock()
verifier.update_properties.reset_mock()
vmanager._clone()
self.assertEqual(
[mock.call(["git", "clone", DEFAULT_REPO,
vmanager.repo_dir, "-b", DEFAULT_VERSION]),
mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir),
mock.call(["git", "rev-parse", "HEAD"], cwd=vmanager.repo_dir)],
self.check_output.call_args_list)
verifier.update_properties.assert_called_once_with(
version="4a39bd4qwerty12345")
# Case 3: verifier is switched to a tag
self.check_output.reset_mock()
verifier.update_properties.reset_mock()
vmanager._clone()
self.assertEqual(
[mock.call(["git", "clone", DEFAULT_REPO,
vmanager.repo_dir, "-b", DEFAULT_VERSION]),
mock.call(["git", "describe", "--all"], cwd=vmanager.repo_dir),
mock.call(["git", "rev-parse", "HEAD"], cwd=vmanager.repo_dir)],
self.check_output.call_args_list)
verifier.update_properties.assert_called_once_with(version="2.0.0")
@mock.patch("rally.verification.manager.VerifierManager.install_venv")
@mock.patch("rally.verification.manager.VerifierManager.check_system_wide")
@mock.patch("rally.verification.manager.VerifierManager._clone")
@mock.patch("rally.verification.utils.create_dir") @mock.patch("rally.verification.utils.create_dir")
def test_install(self, mock_create_dir): def test_install(self, mock_create_dir, mock__clone,
mock_check_system_wide, mock_install_venv):
verifier = mock.Mock() verifier = mock.Mock()
vmanager = FakeVerifier(verifier) vmanager = FakeVerifier(verifier)
vmanager._clone = mock.Mock()
vmanager.check_system_wide = mock.Mock()
vmanager.install_venv = mock.Mock()
# venv case # venv case
verifier.system_wide = False verifier.system_wide = False
vmanager.install() vmanager.install()
vmanager._clone.assert_called_once_with() mock__clone.assert_called_once_with()
self.assertFalse(vmanager.check_system_wide.called) self.assertFalse(mock_check_system_wide.called)
vmanager.install_venv.assert_called_once_with() mock_install_venv.assert_called_once_with()
# system-wide case # system-wide case
vmanager._clone.reset_mock() mock__clone.reset_mock()
vmanager.check_system_wide.reset_mock() mock_check_system_wide.reset_mock()
vmanager.install_venv.reset_mock() mock_install_venv.reset_mock()
verifier.system_wide = True verifier.system_wide = True
vmanager.install() vmanager.install()
vmanager._clone.assert_called_once_with() mock__clone.assert_called_once_with()
vmanager.check_system_wide.assert_called_once_with() mock_check_system_wide.assert_called_once_with()
self.assertFalse(vmanager.install_venv.called) self.assertFalse(mock_install_venv.called)
@mock.patch("rally.verification.manager.shutil.rmtree") @mock.patch("rally.verification.manager.shutil.rmtree")
@mock.patch("rally.verification.manager.os.path.exists", return_value=True) @mock.patch("rally.verification.manager.os.path.exists", return_value=True)
@ -331,8 +373,8 @@ class VerifierManagerTestCase(test.TestCase):
" * *system_wide*: Whether or not to use the system-wide " " * *system_wide*: Whether or not to use the system-wide "
"environment for verifier instead of a virtual environment. " "environment for verifier instead of a virtual environment. "
"Defaults to False.\n" "Defaults to False.\n"
" * *version*: Branch, tag or commit ID to checkout before "
"verifier installation. Defaults to 3.14159\n"
" * *source*: Path or URL to the repo to clone verifier from. " " * *source*: Path or URL to the repo to clone verifier from. "
"Default to https://git.example.com", "Defaults to https://git.example.com\n"
" * *version*: Branch, tag or commit ID to checkout before "
"verifier installation. Defaults to '%s'." % DEFAULT_VERSION,
FakeVerifier._get_doc()) FakeVerifier._get_doc())