Skip to content

Commit cac38c6

Browse files
committed
Address comments, add generated common_versions.sh
1 parent 15a1132 commit cac38c6

File tree

2 files changed

+151
-88
lines changed

2 files changed

+151
-88
lines changed

test/bin/common_versions.sh

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,66 +39,37 @@ get_vrel_from_rhsm() {
3939
echo ""
4040
}
4141

42-
# The current release minor version (e.g. '17' for '4.17') affects
43-
# the definition of previous and fake next versions.
4442
export MINOR_VERSION=20
4543
export PREVIOUS_MINOR_VERSION=$(( "${MINOR_VERSION}" - 1 ))
4644
export YMINUS2_MINOR_VERSION=$(( "${MINOR_VERSION}" - 2 ))
4745
export FAKE_NEXT_MINOR_VERSION=$(( "${MINOR_VERSION}" + 1 ))
4846

49-
# For a main branch, the current release repository usually comes from
50-
# the OpenShift mirror site, either 'ocp-dev-preview' in the beginning of the
51-
# development cycle or 'ocp' when release candidates are built regularly.
52-
#
53-
# For a release branch, the current release repository should come from the
54-
# official 'rhocp' stream.
55-
CURRENT_RELEASE_REPO="" # "https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/microshift/ocp/latest-4.20/el9/os"
56-
CURRENT_RELEASE_VERSION="" # "$(get_vrel_from_beta "${CURRENT_RELEASE_REPO}")"
47+
CURRENT_RELEASE_REPO=""
48+
CURRENT_RELEASE_VERSION=""
5749
export CURRENT_RELEASE_REPO
5850
export CURRENT_RELEASE_VERSION
5951

60-
# The previous release repository value should either point to the OpenShift
61-
# mirror URL or the 'rhocp' repository name.
62-
#
63-
# For a main branch, the previous release repository may come from the
64-
# official 'rhocp' stream or the OpenShift mirror. It is necessary to use the
65-
# release candidate repository from the OpenShift mirror after a branch is
66-
# created, but the previous release has not been made public yet.
67-
#
68-
# For a release branch, the previous release repository should come from the
69-
# official 'rhocp' stream.
7052
PREVIOUS_RELEASE_REPO="https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/microshift/ocp/latest-4.19/el9/os"
7153
PREVIOUS_RELEASE_VERSION="$(get_vrel_from_beta "${PREVIOUS_RELEASE_REPO}")"
7254
export PREVIOUS_RELEASE_REPO
7355
export PREVIOUS_RELEASE_VERSION
7456

75-
# The y-2 release repository value should always contain the 'rhocp' repository name.
7657
YMINUS2_RELEASE_REPO="rhocp-4.18-for-rhel-9-$(uname -m)-rpms"
7758
YMINUS2_RELEASE_VERSION="$(get_vrel_from_rhsm "${YMINUS2_RELEASE_REPO}")"
7859
export YMINUS2_RELEASE_REPO
7960
export YMINUS2_RELEASE_VERSION
8061

81-
# Define either a release version or the OpenShift mirror beta repository URL.
82-
# If the release version is defined, the repository should be deduced from the
83-
# CURRENT_RELEASE_REPO setting.
8462
RHOCP_MINOR_Y=""
85-
RHOCP_MINOR_Y_BETA="https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/dependencies/rpms/4.20-el9-beta/"
63+
RHOCP_MINOR_Y_BETA="https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/dependencies/rpms/4.20-el9-beta"
8664
export RHOCP_MINOR_Y
8765
export RHOCP_MINOR_Y_BETA
8866

89-
# Define a release version and/or the OpenShift mirror beta repository URL.
90-
# If the release version is defined, the repository should be deduced from the
91-
# PREVIOUS_RELEASE_REPO setting.
92-
# Beta repository URL needs to be set for CentOS images as they don't have access to the RHOCP.
9367
RHOCP_MINOR_Y1=""
94-
RHOCP_MINOR_Y1_BETA="https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/dependencies/rpms/4.19-el9-beta/"
68+
RHOCP_MINOR_Y1_BETA="https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/dependencies/rpms/4.19-el9-beta"
9569
export RHOCP_MINOR_Y1
9670
export RHOCP_MINOR_Y1_BETA
9771

98-
# Define a release version as it is not expected to use the OpenShift mirror
99-
# for the y-2 release.
10072
export RHOCP_MINOR_Y2=18
10173

102-
# The version of Sonobuoy package used in CNCF tests.
103-
# See https://github.com/vmware-tanzu/sonobuoy/releases.
10474
export CNCF_SONOBUOY_VERSION=v0.57.3
75+

test/bin/generate_common_versions.py

Lines changed: 146 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,85 @@
22
"""
33
The generate_common_versions.py generates all variables for the common_versions.sh script and prints them to stdout.
44
"""
5+
56
import requests
67
import subprocess
78
import os
89
import sys
910
import argparse
11+
import logging
1012

1113
ARCH = os.uname().machine
1214

15+
# The version of Sonobuoy package used in CNCF tests.
16+
# See https://github.com/vmware-tanzu/sonobuoy/releases.
1317
CNCF_SONOBUOY_VERSION = "v0.57.3"
1418

19+
logging.basicConfig(
20+
level=logging.INFO,
21+
format='%(levelname)s: %(message)s',
22+
stream=sys.stderr
23+
)
24+
1525

1626
def get_candidate_repo_url(minor, dev_preview=False):
1727
"""
18-
:param minor: the minor version, e.g. 19 for 4.19
19-
:param dev_preview: if True, returns the engineering candidate repo, otherwise returns the release candidate repo
28+
Get the URL of the engineering or release candidate repository.
29+
30+
Args:
31+
minor (int): The minor version, e.g., 19 for version 4.19.
32+
dev_preview (bool): If True, return the engineering candidate repo URL;
33+
otherwise, return the release candidate repo URL.
2034
21-
``get_candidate_repo_url`` returns the URL of the EC/RC repository for the specified minor version.
35+
Returns:
36+
str: The URL of the candidate repository.
2237
"""
2338
return f"https://mirror.openshift.com/pub/openshift-v4/{ARCH}/microshift/ocp{'-dev-preview' if dev_preview else ''}/latest-4.{minor}/el9/os"
2439

2540

26-
def get_dependencies_repo_url(minor, prev=None):
41+
def get_dependencies_repo_url(minor, steps_back=0):
2742
"""
28-
:param minor: the minor version, e.g. 19. for 4.19
29-
:param prev: specifies how many previous minor versions to try if current is unavailable
43+
Get the URL of the beta repository for the specified minor version.
3044
31-
``get_dependencies_repo_url`` returns the URL of the beta repository for the specified
32-
minor version. If the repo for the wanted version does not exist or it does not provide
33-
the necessary packages, it looks for previous releases, for up to `prev` previous minors.
34-
If `prev` is not specified, it only checks the current minor.
35-
"""
45+
This function constructs and checks the beta repository URL for the given
46+
minor version (e.g., 4.19). If the repository does not exist or does not
47+
provide the required packages, it searches previous minor versions—up to
48+
`steps_back` times—until a valid repository is found.
3649
37-
count = prev if prev is not None else 0
50+
Args:
51+
minor (int): The minor version, e.g., 19 for version 4.19.
52+
steps_back (int, optional): How many previous minor versions to try
53+
if the current version is unavailable. Defaults to 0.
3854
39-
print(f"Getting beta repository for 4.{minor}, max. {count} previous minors", file=sys.stderr)
55+
Returns:
56+
str or None: The URL of a valid beta repository, or None if none are found.
57+
"""
58+
logging.info(f"Getting beta repository for 4.{minor}, max. {steps_back} previous minors")
4059

41-
for i in range(minor, minor-count-1, -1):
60+
for i in range(minor, minor-steps_back-1, -1):
4261
url = f"https://mirror.openshift.com/pub/openshift-v4/{ARCH}/dependencies/rpms/4.{i}-el9-beta"
43-
if mirror_exists(url) and provides_pkg(url, "cri-o"):
44-
print(f"Beta repository found for 4.{i}", file=sys.stderr)
62+
if mirror_exists(url) and repo_provides_pkg(url, "cri-o"):
63+
logging.info(f"Beta repository found for 4.{i}")
4564
return url
46-
print(f"Beta repository for 4.{i} not found{', retrying' if i>minor-count else ''}")
47-
65+
logging.info(f"Beta repository for 4.{i} not found{', trying older minor' if i>minor-steps_back else ''}")
4866
return None
4967

5068

51-
def provides_pkg(repo, pkg):
69+
def repo_provides_pkg(repo, pkg):
5270
"""
53-
:param repo: the repository to check
54-
:param pkg: the package to look for
71+
Check if the repository provides the specified package.
72+
73+
Args:
74+
repo (str): The repository URL or name to check.
75+
pkg (str): The name of the package to look for.
5576
56-
``provides_pkg`` checks if the repository provides the package specified by `pkg`.
77+
Returns:
78+
bool: True if the package is available in the repository, False otherwise.
5779
"""
5880
args = ['dnf', 'repoquery', pkg, '--queryformat', '%{version}-%{release}']
5981

6082
if repo.startswith("https"):
61-
temp = f"this,{repo}"
62-
args += ['--disablerepo', '*', '--repofrompath', temp]
83+
args += ['--disablerepo', '*', '--repofrompath', f"this,{repo}"]
6384
else:
6485
args += ['--repo', repo]
6586

@@ -72,9 +93,13 @@ def provides_pkg(repo, pkg):
7293

7394
def mirror_exists(repo_url):
7495
"""
75-
:param repo_url: the URL of a repository
96+
Check if a URL points to a valid repository.
7697
77-
``mirror_exists`` checks if a URL points to a valid repository.
98+
Args:
99+
repo_url (str): The URL of the repository to check.
100+
101+
Returns:
102+
bool: True if the repository exists and is accessible, False otherwise.
78103
"""
79104
url = repo_url + "/repodata/repomd.xml"
80105
r = requests.get(url)
@@ -86,56 +111,75 @@ def mirror_exists(repo_url):
86111

87112
def get_subscription_repo_name_if_exists(minor):
88113
"""
89-
:param minor: the minor version, e.g. 19 for 4.19
114+
Get the name of the subscription repository for the specified minor version.
115+
116+
If the repository provides the microshift package, return the repository name;
117+
otherwise, return None.
90118
91-
``get_subscription_repo_name_if_exists`` returns the name of the subscription repository
92-
for the specified minor version if the repository provides the microshift package,
93-
otherwise returns None.
119+
Args:
120+
minor (int): The minor version, e.g., 19 for 4.19.
121+
122+
Returns:
123+
str or None: The name of the subscription repository, or None if not available.
94124
"""
95125
repo = f"rhocp-4.{minor}-for-rhel-9-{ARCH}-rpms"
96126

97-
if provides_pkg(repo, "microshift"):
127+
if repo_provides_pkg(repo, "microshift"):
98128
return repo
99129
else:
100130
return None
101131

102132

103133
def get_microshift_repo(minor):
104134
"""
105-
:param minor: the minor version, e.g. 19 for 4.19
135+
Get the repository for the specified minor version.
106136
107-
``get_microshift_repo`` returns the repository for the specified minor version.
108-
It looks for the 'rhocp' stream, release candidate and engineering candidate,
109-
in that order, and checks if they provide the microshift package. If none of
110-
these repositories are available, returns empty string.
111-
"""
137+
This function searches for a repository that provides the microshift package
138+
for the given minor version. It checks the 'rhocp' stream, then release
139+
candidate (RC), and finally engineering candidate (EC) repositories—in that
140+
order. If none are available, it returns an empty string.
141+
142+
Args:
143+
minor (int): The minor version, e.g., 19 for 4.19.
112144
145+
Returns:
146+
str: The repository name or URL if found, otherwise an empty string.
147+
"""
113148
repo = get_subscription_repo_name_if_exists(minor)
114149
if repo is not None:
115-
print(f"Found subscription repository for 4.{minor}", file=sys.stderr)
150+
logging.info(f"Found subscription repository for 4.{minor}")
116151
return repo
117152

118153
rc = get_candidate_repo_url(minor, dev_preview=False)
119-
if mirror_exists(rc) and provides_pkg(rc, "microshift"):
120-
print(f"Found release candidate for 4.{minor}", file=sys.stderr)
154+
if mirror_exists(rc) and repo_provides_pkg(rc, "microshift"):
155+
logging.info(f"Found release candidate for 4.{minor}")
121156
return rc
122157

123158
ec = get_candidate_repo_url(minor, dev_preview=True)
124-
if mirror_exists(ec) and provides_pkg(ec, "microshift"):
125-
print(f"Found engineering candidate for 4.{minor}", file=sys.stderr)
159+
if mirror_exists(ec) and repo_provides_pkg(ec, "microshift"):
160+
logging.info(f"Found engineering candidate for 4.{minor}")
126161
return ec
127162

128-
print(f"No repository found for 4.{minor}", file=sys.stderr)
163+
logging.info(f"No repository found for 4.{minor}")
129164
return ""
130165

131166

132167
def get_release_version_string(repo, var_name):
133168
"""
134-
:param repo: the name or the URL of the repository
169+
Get a Bash string calling the appropriate `get_vrel_from_*` function.
170+
171+
This function returns a string suitable for use as a Bash variable assignment,
172+
calling either `get_vrel_from_rhsm` or `get_vrel_from_beta` depending on whether
173+
the `repo` parameter is a repository name or a URL. If `repo` is neither,
174+
it returns an empty string.
135175
136-
``get_release_version_string`` returns a string to be used as a bash variable,
137-
with a call to the right function depending on whether the `repo` param
138-
is a URL or not. if neither, returns empty double quotes.
176+
Args:
177+
repo (str): The name or URL of the repository.
178+
var_name (str): The name of the Bash variable to use in the command.
179+
180+
Returns:
181+
str: A Bash command string using the appropriate `get_vrel_from_*` function,
182+
or an empty string if the input is invalid.
139183
"""
140184
if repo.startswith("rhocp"):
141185
return f'$(get_vrel_from_rhsm "${{{var_name}}}")'
@@ -153,43 +197,91 @@ def get_release_version_string(repo, var_name):
153197
minor_version = args.minor
154198
previous_minor_version = minor_version - 1
155199
yminus2_minor_version = minor_version - 2
156-
fake_next_minor_version = minor_version + 1
157200

158201
# The current release repository comes from the 'rhocp' stream for release
159202
# branches, or the OpenShift mirror if only a RC or EC is available. It can
160203
# be empty, if no candidate for the current minor has been built yet.
204+
logging.info("Getting CURRENT_RELEASE_REPO")
161205
current_release_repo = get_microshift_repo(minor_version)
162206
current_release_version = get_release_version_string(current_release_repo, "CURRENT_RELEASE_REPO")
163207

164208
# The previous release repository value should either point to the OpenShift
165209
# mirror URL or the 'rhocp' repository name.
210+
logging.info("Getting PREVIOUS_RELEASE_REPO")
166211
previous_release_repo = get_microshift_repo(previous_minor_version)
167212
previous_release_version = get_release_version_string(previous_release_repo, "PREVIOUS_RELEASE_REPO")
168213

169214
# The y-2 release repository value should either point to the OpenShift
170215
# mirror URL or the 'rhocp' repository name. It should always come from
171216
# the 'rhocp' stream.
217+
logging.info("Getting YMINUS2_RELEASE_REPO")
172218
yminus2_release_repo = get_microshift_repo(yminus2_minor_version)
173219
yminus2_release_version = get_release_version_string(yminus2_release_repo, "YMINUS2_RELEASE_REPO")
174220

175221
# The 'rhocp_minor_y' variable should be the minor version number, if the
176222
# current release is available through the 'rhocp' stream, otherwise empty.
177-
rhocp_minor_y = minor_version if get_subscription_repo_name_if_exists(minor_version) is not None else '""'
223+
rhocp_minor_y = minor_version if repo_provides_pkg(f"rhocp-4.{minor_version}-for-rhel-9-{ARCH}-rpms", "cri-o") else '""'
178224

179225
# The beta repository, containing dependencies, should point to the
180-
# OpenShift mirror URL. If the repository for current minor is not
226+
# OpenShift mirror URL. If the mirror for current minor is not
181227
# available yet, it should point to an older release.
228+
logging.info("Getting RHOCP_MINOR_Y_BETA")
182229
rhocp_minor_y_beta = get_dependencies_repo_url(minor_version, 3)
183230

184-
# The 'rhocp_minor_y1' should always be the y-1 minor version number.
185-
# The repository for y-1 release should always exist.
186-
rhocp_minor_y1 = previous_minor_version
231+
# The 'rhocp_minor_y' variable should be the previous minor version number, if
232+
# the previous release is available through the 'rhocp' stream, otherwise empty.
233+
rhocp_minor_y1 = previous_minor_version if repo_provides_pkg(f"rhocp-4.{previous_minor_version}-for-rhel-9-{ARCH}-rpms", "cri-o") else '""'
234+
235+
# The beta repository, containing dependencies, should point to the
236+
# OpenShift mirror URL. The mirror for previous release should always
237+
# be available.
238+
logging.info("Getting RHOCP_MINOR_Y1_BETA")
187239
rhocp_minor_y1_beta = get_dependencies_repo_url(previous_minor_version)
188240

189241
# The 'rhocp_minor_y2' should always be the y-2 minor version number.
190242
rhocp_minor_y2 = yminus2_minor_version
191243

192-
output = f"""
244+
output = f"""#!/bin/bash
245+
set -euo pipefail
246+
247+
if [[ "${{BASH_SOURCE[0]}}" == "${{0}}" ]]; then
248+
echo "This script must be sourced, not executed."
249+
exit 1
250+
fi
251+
252+
get_vrel_from_beta() {{
253+
local -r beta_repo="$1"
254+
local -r beta_vrel=$(\\
255+
dnf repoquery microshift \\
256+
--quiet \\
257+
--queryformat '%{{version}}-%{{release}}' \\
258+
--disablerepo '*' \\
259+
--repofrompath "this,${{beta_repo}}" \\
260+
--latest-limit 1 2>/dev/null \\
261+
)
262+
if [ -n "${{beta_vrel}}" ]; then
263+
echo "${{beta_vrel}}"
264+
return
265+
fi
266+
echo ""
267+
}}
268+
269+
get_vrel_from_rhsm() {{
270+
local -r rhsm_repo="$1"
271+
local -r rhsm_vrel=$(\\
272+
dnf repoquery microshift \\
273+
--quiet \\
274+
--queryformat '%{{version}}-%{{release}}' \\
275+
--repo "${{rhsm_repo}}" \\
276+
--latest-limit 1 2>/dev/null \\
277+
)
278+
if [ -n "${{rhsm_vrel}}" ]; then
279+
echo "${{rhsm_vrel}}"
280+
return
281+
fi
282+
echo ""
283+
}}
284+
193285
export MINOR_VERSION={minor_version}
194286
export PREVIOUS_MINOR_VERSION=$(( "${{MINOR_VERSION}}" - 1 ))
195287
export YMINUS2_MINOR_VERSION=$(( "${{MINOR_VERSION}}" - 2 ))

0 commit comments

Comments
 (0)