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)