From 935b7e76a9101e0b3a481ccc6a201ca511260315 Mon Sep 17 00:00:00 2001 From: John Trowbridge Date: Wed, 9 May 2018 19:03:42 +0000 Subject: [PATCH] Add method for getting dlrn_hash from release and hash_name This method will parse the full repo hash from the delorean.repo file in a similar way to what we currently do via bash in the release files. In the future, it may be better to just get this data directly via the DLRN API. Unittests included which assert the validity of the regex used to parse the delorean.repo file, as well as the format of the repo URL. Change-Id: Ic7177d0ad1fcec5635d16d360b425f3b2737f668 --- .../emit_releases_file/emit_releases_file.py | 28 +++++++++++++++ .../emit_releases_file/test_get_dlrn_hash.py | 34 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 scripts/emit_releases_file/test_get_dlrn_hash.py diff --git a/scripts/emit_releases_file/emit_releases_file.py b/scripts/emit_releases_file/emit_releases_file.py index 699719dbe..38a3e4d0b 100644 --- a/scripts/emit_releases_file/emit_releases_file.py +++ b/scripts/emit_releases_file/emit_releases_file.py @@ -1,4 +1,6 @@ import logging +import re +import requests RELEASES = ['newton', 'ocata', 'pike', 'queens', 'master'] LONG_TERM_SUPPORT_RELEASES = ['queens'] @@ -10,6 +12,32 @@ def get_relative_release(release, relative_idx): return RELEASES[absolute_idx] +def get_dlrn_hash(release, hash_name, retries=10): + full_hash_pattern = re.compile('[a-z,0-9]{40}_[a-z,0-9]{8}') + repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' + % (release, hash_name)) + for retry_num in range(retries): + repo_file = None + # Timeout if initial connection is longer than default + # TCP packet retransmission window (3 secs), or if the + # sending of the data takes more than 27 seconds. + try: + repo_file = requests.get(repo_url, timeout=(3.05, 27)) + except Exception as e: + # TODO(trown): Handle exceptions + pass + else: + if repo_file is not None and repo_file.ok: + break + + if repo_file is None or not repo_file.ok: + raise RuntimeError("Failed to retrieve repo file from {} after " + "{} retries".format(repo_url, retries)) + + full_hash = full_hash_pattern.findall(repo_file.content) + return full_hash[0] + + def compose_releases_dictionary(stable_release, featureset): if stable_release not in RELEASES: diff --git a/scripts/emit_releases_file/test_get_dlrn_hash.py b/scripts/emit_releases_file/test_get_dlrn_hash.py new file mode 100644 index 000000000..e19855d3b --- /dev/null +++ b/scripts/emit_releases_file/test_get_dlrn_hash.py @@ -0,0 +1,34 @@ +from emit_releases_file import get_dlrn_hash + +import mock +import pytest + +@mock.patch('requests.get') +def test_get_dlrn_hash(mock_get): + mock_response = mock.Mock() + mock_response.content = ('[delorean]\nname=delorean-openstack-nova-81c23c04' + '7e8e0fc03b54164921f49fdb4103202c\nbaseurl=https:/' + '/trunk.rdoproject.org/centos7/81/c2/81c23c047e8e0' + 'fc03b54164921f49fdb4103202c_b333f915\nenabled=1\n' + 'gpgcheck=0\npriority=1') + release = 'master' + hash_name = 'current-tripleo' + repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' + % (release, hash_name)) + mock_get.return_value = mock_response + assert (get_dlrn_hash(release, hash_name) == + '81c23c047e8e0fc03b54164921f49fdb4103202c_b333f915') + mock_get.assert_called_once_with(repo_url, timeout=(3.05, 27)) + + +@mock.patch('requests.get') +def test_null_response_raises_runtimeerror(mock_get): + release = 'master' + hash_name = 'current-tripleo' + repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' % + (release, hash_name)) + mock_get.return_value = None + with pytest.raises(RuntimeError): + get_dlrn_hash(release, hash_name) + mock_get.assert_called(repo_url, timeout=(3.05, 27)) + assert (10 == mock_get.call_count)