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
This commit is contained in:
parent
6f84110751
commit
935b7e76a9
|
@ -1,4 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
import requests
|
||||||
|
|
||||||
RELEASES = ['newton', 'ocata', 'pike', 'queens', 'master']
|
RELEASES = ['newton', 'ocata', 'pike', 'queens', 'master']
|
||||||
LONG_TERM_SUPPORT_RELEASES = ['queens']
|
LONG_TERM_SUPPORT_RELEASES = ['queens']
|
||||||
|
@ -10,6 +12,32 @@ def get_relative_release(release, relative_idx):
|
||||||
return RELEASES[absolute_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):
|
def compose_releases_dictionary(stable_release, featureset):
|
||||||
|
|
||||||
if stable_release not in RELEASES:
|
if stable_release not in RELEASES:
|
||||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue