Skip to content

Commit 39461d5

Browse files
authored
Merge pull request #8623 from joshcooper/mask_systemd_service
(PUP-10974) Allow masking of nonexistent systemd services
2 parents 8daf434 + e42922b commit 39461d5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/puppet/provider/service/systemd.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,15 @@ def enable
164164
end
165165

166166
def mask
167-
self.disable
167+
disable if exist?
168168
systemctl_change_enable(:mask)
169169
end
170170

171+
def exist?
172+
result = execute([command(:systemctl), 'cat', '--', @resource[:name]], :failonfail => false)
173+
result.exitstatus == 0
174+
end
175+
171176
def unmask
172177
systemctl_change_enable(:unmask)
173178
end

spec/unit/provider/service/systemd_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,25 @@
357357
describe "#mask" do
358358
it "should run systemctl to disable and mask a service" do
359359
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
360+
expect(provider).to receive(:execute).
361+
with(['/bin/systemctl','cat', '--', 'sshd.service'], :failonfail => false).
362+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/sshd.service\n...", 0))
360363
# :disable is the only call in the provider that uses a symbol instead of
361364
# a string.
362365
# This should be made consistent in the future and all tests updated.
363366
expect(provider).to receive(:systemctl).with(:disable, '--', 'sshd.service')
364367
expect(provider).to receive(:systemctl).with(:mask, '--', 'sshd.service')
365368
provider.mask
366369
end
370+
371+
it "masks a service that doesn't exist" do
372+
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'doesnotexist.service'))
373+
expect(provider).to receive(:execute).
374+
with(['/bin/systemctl','cat', '--', 'doesnotexist.service'], :failonfail => false).
375+
and_return(Puppet::Util::Execution::ProcessOutput.new("No files found for doesnotexist.service.\n", 1))
376+
expect(provider).to receive(:systemctl).with(:mask, '--', 'doesnotexist.service')
377+
provider.mask
378+
end
367379
end
368380

369381
# Note: systemd provider does not care about hasstatus or a custom status

0 commit comments

Comments
 (0)