Skip to content

Commit 2f1d002

Browse files
committed
(PUP-10949) Fix systemctl list-unit-files parsing
Some time ago the output of systemctl list-unit-files changed to include an additional column. This probably happened within the last year or so, as CentOS 8 has the old output, whereas Fedora 32 and Ubuntu 20.04 has the new output. To satisfy both cases, extend the regex that parses the output to allow for an additional column, while maintaining the same constraints.
1 parent 6272076 commit 2f1d002

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/puppet/provider/service/systemd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
def self.instances
3131
i = []
3232
output = systemctl('list-unit-files', '--type', 'service', '--full', '--all', '--no-pager')
33-
output.scan(/^(\S+)\s+(disabled|enabled|masked|indirect|bad|static)\s*$/i).each do |m|
33+
output.scan(/^(\S+)\s+(disabled|enabled|masked|indirect|bad|static)\s*([^-]\S+)?\s*$/i).each do |m|
3434
Puppet.debug("#{m[0]} marked as bad by `systemctl`. It is recommended to be further checked.") if m[1] == "bad"
3535
i << new(:name => m[0])
3636
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
UNIT FILE STATE VENDOR PRESET
2+
arp-ethers.service disabled disabled
3+
auditd.service enabled enabled
4+
dbus.service enabled disabled
5+
udev.service enabled-runtime disabled
6+
umountfs.service linked-runtime disabled
7+
umountnfs.service masked disabled
8+
umountroot.service masked-runtime disabled
9+
urandom.service indirect enabled

spec/unit/provider/service/systemd_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@
200200
})
201201
end
202202

203+
it "correctly parses services when list-unit-files has an additional column" do
204+
expect(provider_class).to receive(:systemctl).with('list-unit-files', '--type', 'service', '--full', '--all', '--no-pager').and_return(File.read(my_fixture('list_unit_files_services_vendor_preset')))
205+
expect(provider_class.instances.map(&:name)).to match_array(%w{
206+
arp-ethers.service
207+
auditd.service
208+
dbus.service
209+
umountnfs.service
210+
urandom.service
211+
})
212+
end
213+
203214
it "should print a debug message when a service with the state `bad` is found" do
204215
expect(provider_class).to receive(:systemctl).with('list-unit-files', '--type', 'service', '--full', '--all', '--no-pager').and_return(File.read(my_fixture('list_unit_files_services')))
205216
expect(Puppet).to receive(:debug).with("apparmor.service marked as bad by `systemctl`. It is recommended to be further checked.")

0 commit comments

Comments
 (0)