Skip to content

Commit b3482ab

Browse files
Merge pull request #2970 from craigcomstock/ENT-12538/master
Fixed issue with yum package module regarding packages with epoch not validating
2 parents b719d69 + ce1fac4 commit b3482ab

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

.github/workflows/valgrind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
ref: ${{steps.together.outputs.core || github.base_ref || github.ref}}
2323
submodules: recursive
2424
- name: Install dependencies
25-
run: sudo apt-get update -y && sudo apt-get install -y libssl-dev libpam0g-dev liblmdb-dev byacc curl libyaml-dev valgrind
25+
run: sudo apt-get update -y && sudo apt-get install -y libssl-dev libpam0g-dev liblmdb-dev byacc curl libyaml-dev valgrind librsync-dev
2626
# - name: Install CFEngine with cf-remote
2727
# run: |
2828
# pip3 install cf-remote

modules/packages/vendored/yum.mustache

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ import subprocess
99
import re
1010

1111

12-
rpm_cmd = os.environ.get('CFENGINE_TEST_RPM_CMD', "/bin/rpm")
12+
rpm_cmd = os.environ.get("CFENGINE_TEST_RPM_CMD", "/bin/rpm")
1313
rpm_quiet_option = ["--quiet"]
14-
rpm_output_format = "Name=%{name}\nVersion=%{version}-%{release}\nArchitecture=%{arch}\n"
14+
rpm_output_format = (
15+
"Name=%{name}\nVersion=%{epoch}:%{version}-%{release}\nArchitecture=%{arch}\n"
16+
)
1517

16-
yum_cmd = os.environ.get('CFENGINE_TEST_YUM_CMD', "/usr/bin/yum")
18+
yum_cmd = os.environ.get("CFENGINE_TEST_YUM_CMD", "/usr/bin/yum")
1719
yum_options = ["--quiet", "-y"]
1820

19-
NULLFILE = open(os.devnull, 'w')
21+
NULLFILE = open(os.devnull, "w")
2022

2123

2224
redirection_is_broken_cached = -1
2325

26+
2427
def redirection_is_broken():
2528
# Older versions of Python have a bug where it is impossible to redirect
2629
# stderr using subprocess, and any attempt at redirecting *anything*, not
@@ -41,7 +44,12 @@ def redirection_is_broken():
4144

4245

4346
def subprocess_Popen(cmd, stdout=None, stderr=None):
44-
if not redirection_is_broken() or (stdout is None and stderr is None) or stdout == subprocess.PIPE or stderr == subprocess.PIPE:
47+
if (
48+
not redirection_is_broken()
49+
or (stdout is None and stderr is None)
50+
or stdout == subprocess.PIPE
51+
or stderr == subprocess.PIPE
52+
):
4553
return subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
4654

4755
old_stdout_fd = -1
@@ -87,7 +95,19 @@ def get_package_data():
8795
# Absolute file.
8896
sys.stdout.write("PackageType=file\n")
8997
sys.stdout.flush()
90-
return subprocess_call([rpm_cmd, "--qf", rpm_output_format, "-qp", pkg_string])
98+
process = subprocess_Popen(
99+
[rpm_cmd, "--qf", rpm_output_format, "-qp", pkg_string],
100+
stdout=subprocess.PIPE,
101+
)
102+
(stdoutdata, _) = process.communicate()
103+
104+
if process.returncode != 0:
105+
return process.returncode
106+
107+
for line in stdoutdata.decode("utf-8").splitlines():
108+
sys.stdout.write(line.replace("(none):", "") + "\n")
109+
110+
return 0
91111
elif re.search("[:,]", pkg_string):
92112
# Contains an illegal symbol.
93113
sys.stdout.write(line + "ErrorMessage: Package string with illegal format\n")
@@ -102,15 +122,26 @@ def list_installed():
102122
# Ignore everything.
103123
sys.stdin.readlines()
104124

105-
return subprocess_call([rpm_cmd, "-qa", "--qf", rpm_output_format])
125+
process = subprocess_Popen(
126+
[rpm_cmd, "-qa", "--qf", rpm_output_format], stdout=subprocess.PIPE
127+
)
128+
(stdoutdata, _) = process.communicate()
129+
130+
if process.returncode != 0:
131+
return process.returncode
132+
133+
for line in stdoutdata.decode("utf-8").splitlines():
134+
sys.stdout.write(line.replace("(none):", "") + "\n")
135+
136+
return 0
106137

107138

108139
def list_updates(online):
109140
global yum_options
110141
for line in sys.stdin:
111142
line = line.strip()
112143
if line.startswith("options="):
113-
option = line[len("options="):]
144+
option = line[len("options=") :]
114145
if option.startswith("-"):
115146
yum_options.append(option)
116147
elif option.startswith("enablerepo=") or option.startswith("disablerepo="):
@@ -120,7 +151,9 @@ def list_updates(online):
120151
if not online:
121152
online_flag = ["-C"]
122153

123-
process = subprocess_Popen([yum_cmd] + yum_options + online_flag + ["check-update"], stdout=subprocess.PIPE)
154+
process = subprocess_Popen(
155+
[yum_cmd] + yum_options + online_flag + ["check-update"], stdout=subprocess.PIPE
156+
)
124157
(stdoutdata, _) = process.communicate()
125158
# analyze return code from `yum check-update`:
126159
# 0 means no updates
@@ -129,7 +162,9 @@ def list_updates(online):
129162
if process.returncode == 1 and not online:
130163
# If we get an error when listing local updates, try again using the
131164
# online method, so that the cache is generated
132-
process = subprocess_Popen([yum_cmd] + yum_options + ["check-update"], stdout=subprocess.PIPE)
165+
process = subprocess_Popen(
166+
[yum_cmd] + yum_options + ["check-update"], stdout=subprocess.PIPE
167+
)
133168
(stdoutdata, _) = process.communicate()
134169
if process.returncode != 100:
135170
# either there were no updates or error happened
@@ -152,7 +187,9 @@ def list_updates(online):
152187
continue
153188

154189
lastline = ""
155-
match = re.match(r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$", line)
190+
match = re.match(
191+
r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$", line
192+
)
156193
if match is not None:
157194
sys.stdout.write("Name=" + match.group("name") + "\n")
158195
sys.stdout.write("Version=" + match.group("version") + "\n")
@@ -174,8 +211,9 @@ def one_package_argument(name, arch, version, is_yum_install):
174211
archs.append(arch)
175212

176213
if is_yum_install:
177-
process = subprocess_Popen([rpm_cmd, "--qf", "%{arch}\n",
178-
"-q", name], stdout=subprocess.PIPE)
214+
process = subprocess_Popen(
215+
[rpm_cmd, "--qf", "%{arch}\n", "-q", name], stdout=subprocess.PIPE
216+
)
179217
existing_archs = [line.decode("utf-8").rstrip() for line in process.stdout]
180218
process.wait()
181219
if process.returncode == 0 and existing_archs:
@@ -218,21 +256,23 @@ def package_arguments_builder(is_yum_install):
218256
name = ""
219257
version = ""
220258
arch = ""
221-
single_cmd_args = [] # List of arguments
222-
multi_cmd_args = [] # List of lists of arguments
259+
single_cmd_args = [] # List of arguments
260+
multi_cmd_args = [] # List of lists of arguments
223261
old_name = ""
224262
for line in sys.stdin:
225263
line = line.strip()
226264
if line.startswith("options="):
227-
option = line[len("options="):]
265+
option = line[len("options=") :]
228266
if option.startswith("-"):
229267
yum_options.append(option)
230268
elif option.startswith("enablerepo=") or option.startswith("disablerepo="):
231269
yum_options.append("--" + option)
232270
if line.startswith("Name="):
233271
if name:
234272
# Each new "Name=" triggers a new entry.
235-
single_list, multi_list = one_package_argument(name, arch, version, is_yum_install)
273+
single_list, multi_list = one_package_argument(
274+
name, arch, version, is_yum_install
275+
)
236276
single_cmd_args += single_list
237277
if name == old_name:
238278
# Packages that differ only by architecture should be
@@ -255,7 +295,9 @@ def package_arguments_builder(is_yum_install):
255295
arch = line.split("=", 1)[1].rstrip()
256296

257297
if name:
258-
single_list, multi_list = one_package_argument(name, arch, version, is_yum_install)
298+
single_list, multi_list = one_package_argument(
299+
name, arch, version, is_yum_install
300+
)
259301
single_cmd_args += single_list
260302
if name == old_name:
261303
# Packages that differ only by architecture should be
@@ -436,4 +478,5 @@ def main():
436478
sys.stderr.write("Invalid operation\n")
437479
return 2
438480

481+
439482
sys.exit(main())

tests/unit/test_package_module_yum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ assert check("list-updates-local", [], 18,
151151
assert check("list-installed", [], 6,
152152
["Name=firefox\nVersion=24.5.0-1.el5.centos\nArchitecture=i386",
153153
"Name=yum\nVersion=3.2.29-43.el6_5\nArchitecture=noarch"],
154-
4, ["rpm -qa --qf Name=%{name}\nVersion=%{version}-%{release}\nArchitecture=%{arch}\n"])
154+
4, ["rpm -qa --qf Name=%{name}\nVersion=%{epoch}:%{version}-%{release}\nArchitecture=%{arch}\n"])
155155

156156
assert check("get-package-data", ["File=/path/to/pkg"], 4,
157157
["PackageType=file\nName=file_pkg\nVersion=10.0\nArchitecture=x86_64"],
158-
4, ["rpm --qf Name=%{name}\nVersion=%{version}-%{release}\nArchitecture=%{arch}\n -qp /path/to/pkg"])
158+
4, ["rpm --qf Name=%{name}\nVersion=%{epoch}:%{version}-%{release}\nArchitecture=%{arch}\n -qp /path/to/pkg"])
159159
assert check("get-package-data", ["File=repo_pkg"], 2,
160160
["PackageType=repo\nName=repo_pkg"],
161161
0, [])

0 commit comments

Comments
 (0)