Browse Source

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
John Trowbridge 11 months ago
parent
commit
935b7e76a9

+ 28
- 0
scripts/emit_releases_file/emit_releases_file.py View File

@@ -1,4 +1,6 @@
1 1
 import logging
2
+import re
3
+import requests
2 4
 
3 5
 RELEASES = ['newton', 'ocata', 'pike', 'queens', 'master']
4 6
 LONG_TERM_SUPPORT_RELEASES = ['queens']
@@ -10,6 +12,32 @@ def get_relative_release(release, relative_idx):
10 12
     return RELEASES[absolute_idx]
11 13
 
12 14
 
15
+def get_dlrn_hash(release, hash_name, retries=10):
16
+    full_hash_pattern = re.compile('[a-z,0-9]{40}_[a-z,0-9]{8}')
17
+    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo'
18
+                % (release, hash_name))
19
+    for retry_num in range(retries):
20
+        repo_file = None
21
+        # Timeout if initial connection is longer than default
22
+        # TCP packet retransmission window (3 secs), or if the
23
+        # sending of the data takes more than 27 seconds.
24
+        try:
25
+            repo_file = requests.get(repo_url, timeout=(3.05, 27))
26
+        except Exception as e:
27
+            # TODO(trown): Handle exceptions
28
+            pass
29
+        else:
30
+            if repo_file is not None and repo_file.ok:
31
+                break
32
+
33
+    if repo_file is None or not repo_file.ok:
34
+        raise RuntimeError("Failed to retrieve repo file from {} after "
35
+                           "{} retries".format(repo_url, retries))
36
+
37
+    full_hash = full_hash_pattern.findall(repo_file.content)
38
+    return full_hash[0]
39
+
40
+
13 41
 def compose_releases_dictionary(stable_release, featureset):
14 42
 
15 43
     if stable_release not in RELEASES:

+ 34
- 0
scripts/emit_releases_file/test_get_dlrn_hash.py View File

@@ -0,0 +1,34 @@
1
+from emit_releases_file import get_dlrn_hash
2
+
3
+import mock
4
+import pytest
5
+
6
+@mock.patch('requests.get')
7
+def test_get_dlrn_hash(mock_get):
8
+    mock_response = mock.Mock()
9
+    mock_response.content = ('[delorean]\nname=delorean-openstack-nova-81c23c04'
10
+                             '7e8e0fc03b54164921f49fdb4103202c\nbaseurl=https:/'
11
+                             '/trunk.rdoproject.org/centos7/81/c2/81c23c047e8e0'
12
+                             'fc03b54164921f49fdb4103202c_b333f915\nenabled=1\n'
13
+                             'gpgcheck=0\npriority=1')
14
+    release = 'master'
15
+    hash_name = 'current-tripleo'
16
+    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo'
17
+                % (release, hash_name))
18
+    mock_get.return_value = mock_response
19
+    assert (get_dlrn_hash(release, hash_name) ==
20
+            '81c23c047e8e0fc03b54164921f49fdb4103202c_b333f915')
21
+    mock_get.assert_called_once_with(repo_url, timeout=(3.05, 27))
22
+
23
+
24
+@mock.patch('requests.get')
25
+def test_null_response_raises_runtimeerror(mock_get):
26
+    release = 'master'
27
+    hash_name = 'current-tripleo'
28
+    repo_url = ('https://trunk.rdoproject.org/centos7-%s/%s/delorean.repo' %
29
+                (release, hash_name))
30
+    mock_get.return_value = None
31
+    with pytest.raises(RuntimeError):
32
+        get_dlrn_hash(release, hash_name)
33
+    mock_get.assert_called(repo_url, timeout=(3.05, 27))
34
+    assert (10 == mock_get.call_count)

Loading…
Cancel
Save