From 293a02872925f61727c5e09c18e562786eafb171 Mon Sep 17 00:00:00 2001 From: Andrew Schoen Date: Thu, 3 Sep 2015 10:12:12 -0500 Subject: [PATCH] GitbuilderProject: fetch sha1 from gitbuilder if not in the config If the sha1 is not provided in the job config, it needs to be fetched from gitbuilder. See: http://tracker.ceph.com/issues/12934 Signed-off-by: Andrew Schoen --- teuthology/packaging.py | 36 ++++++++++++++++++++++++++++++- teuthology/test/test_packaging.py | 33 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/teuthology/packaging.py b/teuthology/packaging.py index b06bd28fa3..f8ef92c7b5 100644 --- a/teuthology/packaging.py +++ b/teuthology/packaging.py @@ -437,7 +437,6 @@ class GitbuilderProject(object): # avoiding circular imports from teuthology.suite import get_install_task_flavor self.flavor = get_install_task_flavor(self.job_config) - self.sha1 = self.job_config.get("sha1") if remote and ctx: self._init_from_remote() @@ -477,6 +476,21 @@ class GitbuilderProject(object): "debian", ) else "rpm" + @property + def sha1(self): + """ + Performs a call to gitbuilder to retrieve the sha1 if not provided in + the job_config. The returned value is cached so that this call only + happens once. + + :returns: The sha1 of the project as a string. + """ + if not hasattr(self, "_sha1"): + self._sha1 = self.job_config.get('sha1') + if not self._sha1: + self._sha1 = self._get_package_sha1() + return self._sha1 + @property def version(self): """ @@ -691,3 +705,23 @@ class GitbuilderProject(object): version = version.split('-')[0] log.info("Found version: {0}".format(version)) return version + + def _get_package_sha1(self): + """ + Look for, and parse, a file called 'sha1' in base_url. + """ + url = "{0}/sha1".format(self.base_url) + log.info("Looking for package sha1: {0}".format(url)) + resp = requests.get(url) + sha1 = None + if not resp.ok: + # TODO: maybe we should have this retry a few times? + log.error( + 'Package sha1 was not there (got HTTP code %s)...', + resp.status_code, + ) + else: + sha1 = resp.text.strip() + log.info("Found sha1: {0}".format(sha1)) + + return sha1 diff --git a/teuthology/test/test_packaging.py b/teuthology/test/test_packaging.py index 2fc9e4e418..780dbafcaa 100644 --- a/teuthology/test/test_packaging.py +++ b/teuthology/test/test_packaging.py @@ -364,6 +364,39 @@ class TestGitbuilderProject(object): gp = packaging.GitbuilderProject("ceph", {}, ctx=ctx, remote=rem) assert not gp.version + @patch("teuthology.packaging.config") + @patch("teuthology.packaging._get_config_value_for_remote") + @patch("requests.get") + def test_get_package_sha1_fetched_found(self, m_get, m_get_config_value, + m_config): + m_config.baseurl_template = 'http://{host}/{proj}-{pkg_type}-{dist}-{arch}-{flavor}/{uri}' + m_config.gitbuilder_host = "gitbuilder.ceph.com" + m_get_config_value.return_value = None + resp = Mock() + resp.ok = True + resp.text = "the_sha1" + m_get.return_value = resp + rem = self._get_remote() + ctx = dict(foo="bar") + gp = packaging.GitbuilderProject("ceph", {}, ctx=ctx, remote=rem) + assert gp.sha1 == "the_sha1" + + @patch("teuthology.packaging.config") + @patch("teuthology.packaging._get_config_value_for_remote") + @patch("requests.get") + def test_get_package_sha1_fetched_not_found(self, m_get, m_get_config_value, + m_config): + m_config.baseurl_template = 'http://{host}/{proj}-{pkg_type}-{dist}-{arch}-{flavor}/{uri}' + m_config.gitbuilder_host = "gitbuilder.ceph.com" + m_get_config_value.return_value = None + resp = Mock() + resp.ok = False + m_get.return_value = resp + rem = self._get_remote() + ctx = dict(foo="bar") + gp = packaging.GitbuilderProject("ceph", {}, ctx=ctx, remote=rem) + assert not gp.sha1 + GITBUILDER_DISTRO_MATRIX = [ ('rhel', '7.0', None, 'centos7'), ('centos', '6.5', None, 'centos6'), -- 2.39.5