Skip to content

Commit c59f276

Browse files
authored
Merge pull request #8621 from luchihoratiu/PUP-3631
(PUP-3631) Fix regular expressions in the `nim` package provider
2 parents 7e22e62 + 50a18b6 commit c59f276

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

lib/puppet/provider/package/nim.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,25 @@ def install(useversion = true)
154154
# I spent a lot of time trying to figure out a solution that didn't
155155
# require parsing the `nimclient -o showres` output and was unable to
156156
# do so.
157-
self::HEADER_LINE_REGEX = /^([^\s]+)\s+[^@]+@@(I|R):(\1)\s+[^\s]+$/
158-
self::PACKAGE_LINE_REGEX = /^.*@@(I|R):(.*)$/
159-
self::RPM_PACKAGE_REGEX = /^(.*)-(.*-\d+) \2$/
157+
self::HEADER_LINE_REGEX = /^([^\s]+)\s+[^@]+@@(I|R|S):(\1)\s+[^\s]+$/
158+
self::PACKAGE_LINE_REGEX = /^.*@@(I|R|S):(.*)$/
159+
self::RPM_PACKAGE_REGEX = /^(.*)-(.*-\d+\w*) \2$/
160160
self::INSTALLP_PACKAGE_REGEX = /^(.*) (.*)$/
161161

162162
# Here is some sample output that shows what the above regexes will be up
163163
# against:
164-
# FOR AN INSTALLP PACKAGE:
164+
# FOR AN INSTALLP(bff) PACKAGE:
165165
#
166166
# mypackage.foo ALL @@I:mypackage.foo _all_filesets
167-
# @ 1.2.3.1 MyPackage Runtime Environment @@I:mypackage.foo 1.2.3.1
168167
# + 1.2.3.4 MyPackage Runtime Environment @@I:mypackage.foo 1.2.3.4
169168
# + 1.2.3.8 MyPackage Runtime Environment @@I:mypackage.foo 1.2.3.8
170169
#
170+
# FOR AN INSTALLP(bff) PACKAGE with security update:
171+
#
172+
# bos.net ALL @@S:bos.net _all_filesets
173+
# + 7.2.0.1 TCP/IP ntp Applications @@S:bos.net.tcp.ntp 7.2.0.1
174+
# + 7.2.0.2 TCP/IP ntp Applications @@S:bos.net.tcp.ntp 7.2.0.2
175+
#
171176
# FOR AN RPM PACKAGE:
172177
#
173178
# mypackage.foo ALL @@R:mypackage.foo _all_filesets
@@ -243,7 +248,7 @@ def parse_showres_package_line(line)
243248
package_string = match.captures[1]
244249

245250
case package_type_flag
246-
when "I"
251+
when "I","S"
247252
parse_installp_package_string(package_string)
248253
when "R"
249254
parse_rpm_package_string(package_string)

spec/unit/provider/package/nim_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,27 @@
191191
expect(versions[version]).to eq(:rpm)
192192
end
193193
end
194+
195+
it "should be able to parse RPM package listings with letters in version" do
196+
showres_output = <<END
197+
cairo ALL @@R:cairo _all_filesets
198+
@@R:cairo-1.14.6-2waixX11 1.14.6-2waixX11
199+
END
200+
packages = subject.send(:parse_showres_output, showres_output)
201+
expect(Set.new(packages.keys)).to eq(Set.new(['cairo']))
202+
versions = packages['cairo']
203+
expect(versions.has_key?('1.14.6-2waixX11')).to eq(true)
204+
expect(versions['1.14.6-2waixX11']).to eq(:rpm)
205+
end
206+
207+
it "should raise error when parsing invalid RPM package listings" do
208+
showres_output = <<END
209+
cairo ALL @@R:cairo _all_filesets
210+
@@R:cairo-invalid_version invalid_version
211+
END
212+
expect{ subject.send(:parse_showres_output, showres_output) }.to raise_error(Puppet::Error,
213+
/Unable to parse output from nimclient showres: package string does not match expected rpm package string format/)
214+
end
194215
end
195216

196217
context "#determine_latest_version" do
@@ -220,6 +241,27 @@
220241
it "should return :installp for installp/bff packages" do
221242
expect(subject.send(:determine_package_type, bff_showres_output, 'mypackage.foo', '1.2.3.4')).to eq(:installp)
222243
end
244+
245+
it "should return :installp for security updates" do
246+
nimclient_showres_output = <<END
247+
bos.net ALL @@S:bos.net _all_filesets
248+
+ 7.2.0.1 TCP/IP ntp Applications @@S:bos.net.tcp.ntp 7.2.0.1
249+
+ 7.2.0.2 TCP/IP ntp Applications @@S:bos.net.tcp.ntp 7.2.0.2
250+
251+
END
252+
expect(subject.send(:determine_package_type, nimclient_showres_output, 'bos.net.tcp.ntp', '7.2.0.2')).to eq(:installp)
253+
end
254+
255+
it "should raise error when invalid header format is given" do
256+
nimclient_showres_output = <<END
257+
bos.net ALL @@INVALID_TYPE:bos.net _all_filesets
258+
+ 7.2.0.1 TCP/IP ntp Applications @@INVALID_TYPE:bos.net.tcp.ntp 7.2.0.1
259+
+ 7.2.0.2 TCP/IP ntp Applications @@INVALID_TYPE:bos.net.tcp.ntp 7.2.0.2
260+
261+
END
262+
expect{ subject.send(:determine_package_type, nimclient_showres_output, 'bos.net.tcp.ntp', '7.2.0.2') }.to raise_error(
263+
Puppet::Error, /Unable to parse output from nimclient showres: line does not match expected package header format/)
264+
end
223265
end
224266
end
225267
end

0 commit comments

Comments
 (0)