Skip to content

Commit da413eb

Browse files
authored
Merge pull request #9175 from cthorn42/bug_fix/main/error_when_resource_not_found
SystemD and Windows resources when not resource found will now return error
2 parents 324cf2a + 4f6a2ea commit da413eb

File tree

8 files changed

+76
-12
lines changed

8 files changed

+76
-12
lines changed

lib/puppet/application/resource.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,11 @@ def find_or_save_resources(type, name, params)
236236
resource = Puppet::Resource.new( type, name, :parameters => params )
237237

238238
# save returns [resource that was saved, transaction log from applying the resource]
239-
save_result = Puppet::Resource.indirection.save(resource, key)
240-
[ save_result.first ]
239+
save_result, report = Puppet::Resource.indirection.save(resource, key)
240+
status = report.resource_statuses[resource.ref]
241+
raise "Failed to manage resource #{resource.ref}" if status&.failed?
242+
243+
[ save_result ]
241244
end
242245
else
243246
if type == "file"

lib/puppet/provider/service/systemd.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ def daemon_reload?
163163
end
164164
end
165165

166+
# override base#status
167+
def status
168+
if exist?
169+
status = service_command(:status, false)
170+
if status.exitstatus == 0
171+
return :running
172+
else
173+
return :stopped
174+
end
175+
else
176+
return :absent
177+
end
178+
end
179+
166180
def enable
167181
self.unmask
168182
systemctl_change_enable(:enable)

lib/puppet/provider/service/windows.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def stop
9090
end
9191

9292
def status
93-
return :stopped unless Puppet::Util::Windows::Service.exists?(@resource[:name])
93+
return :absent unless Puppet::Util::Windows::Service.exists?(@resource[:name])
9494

9595
current_state = Puppet::Util::Windows::Service.service_state(@resource[:name])
9696
state = case current_state

lib/puppet/type/service.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ def insync?(current)
110110
provider.start
111111
end
112112

113+
newvalue(:absent)
114+
115+
validate do |val|
116+
fail "Managing absent on a service is not supported" if val.to_s == 'absent'
117+
end
118+
113119
aliasvalue(:false, :stopped)
114120
aliasvalue(:true, :running)
115121

spec/unit/application/resource_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,19 @@
118118
@resource_app.main
119119
end
120120

121+
before :each do
122+
allow(@res).to receive(:ref).and_return("type/name")
123+
end
124+
121125
it "should add given parameters to the object" do
122126
allow(@resource_app.command_line).to receive(:args).and_return(['type','name','param=temp'])
123127

124128
expect(Puppet::Resource.indirection).to receive(:save).with(@res, 'type/name').and_return([@res, @report])
125129
expect(Puppet::Resource).to receive(:new).with('type', 'name', {:parameters => {'param' => 'temp'}}).and_return(@res)
126130

131+
resource_status = instance_double('Puppet::Resource::Status')
132+
allow(@report).to receive(:resource_statuses).and_return({'type/name' => resource_status})
133+
allow(resource_status).to receive(:failed?).and_return(false)
127134
@resource_app.main
128135
end
129136
end
@@ -140,11 +147,13 @@ def exists?
140147
true
141148
end
142149

150+
def string=(value)
151+
end
152+
143153
def string
144154
Puppet::Util::Execution::ProcessOutput.new('test', 0)
145155
end
146156
end
147-
148157
it "should not emit puppet class tags when printing yaml when strict mode is off" do
149158
Puppet[:strict] = :warning
150159

spec/unit/provider/service/systemd_spec.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,52 @@
388388
# Note: systemd provider does not care about hasstatus or a custom status
389389
# command. I just assume that it does not make sense for systemd.
390390
describe "#status" do
391-
it "should return running if the command returns 0" do
392-
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
393-
expect(provider).to receive(:execute)
394-
.with(['/bin/systemctl','is-active', '--', 'sshd.service'], {:failonfail => false, :override_locale => false, :squelch => false, :combine => true})
395-
.and_return(Puppet::Util::Execution::ProcessOutput.new("active\n", 0))
391+
it 'when called on a service that does not exist returns absent' do
392+
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'doesnotexist.service'))
393+
expect(provider).to receive(:exist?).and_return(false)
394+
expect(provider.status).to eq(:absent)
395+
end
396+
397+
it 'when called on a service that does exist and is running returns running' do
398+
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'doesexist.service'))
399+
expect(provider).to receive(:execute).
400+
with(['/bin/systemctl','cat', '--', 'doesexist.service'], {:failonfail=>false}).
401+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/doesexist.service\n...", 0)).once
402+
expect(provider).to receive(:execute).
403+
with(['/bin/systemctl','is-active', '--', 'doesexist.service'], {:combine=>true, :failonfail=>false, :override_locale=>false, :squelch=>false}).
404+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/doesexist.service\n...", 0)).once
396405
expect(provider.status).to eq(:running)
397406
end
398407

408+
it 'when called on a service that does exist and is not running returns stopped' do
409+
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'doesexist.service'))
410+
expect(provider).to receive(:execute).
411+
with(['/bin/systemctl','cat', '--', 'doesexist.service'], {:failonfail=>false}).
412+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/doesexist.service\n...", 0)).once
413+
expect(provider).to receive(:execute).
414+
with(['/bin/systemctl','is-active', '--', 'doesexist.service'], {:combine=>true, :failonfail=>false, :override_locale=>false, :squelch=>false}).
415+
and_return(Puppet::Util::Execution::ProcessOutput.new("inactive\n", 3)).once
416+
expect(provider.status).to eq(:stopped)
417+
end
418+
399419
[-10,-1,3,10].each { |ec|
400420
it "should return stopped if the command returns something non-0" do
401421
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
422+
expect(provider).to receive(:execute).
423+
with(['/bin/systemctl','cat', '--', 'sshd.service'], {:failonfail=>false}).
424+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/sshd.service\n...", 0)).once
402425
expect(provider).to receive(:execute)
403-
.with(['/bin/systemctl','is-active', '--', 'sshd.service'], {:failonfail => false, :override_locale => false, :squelch => false, :combine => true})
426+
.with(['/bin/systemctl','is-active', '--', 'sshd.service'], {:failonfail => false, :override_locale => false, :squelch => false, :combine => true})
404427
.and_return(Puppet::Util::Execution::ProcessOutput.new("inactive\n", ec))
405428
expect(provider.status).to eq(:stopped)
406429
end
407430
}
408431

409432
it "should use the supplied status command if specified" do
410433
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :status => '/bin/foo'))
434+
expect(provider).to receive(:execute).
435+
with(['/bin/systemctl','cat', '--', 'sshd.service'], {:failonfail=>false}).
436+
and_return(Puppet::Util::Execution::ProcessOutput.new("# /lib/systemd/system/sshd.service\n...", 0)).once
411437
expect(provider).to receive(:execute)
412438
.with(['/bin/foo'], {:failonfail => false, :override_locale => false, :squelch => false, :combine => true})
413439
.and_return(process_output)

spec/unit/provider/service/windows_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
end
8282

8383
describe "#status" do
84-
it "should report a nonexistent service as stopped" do
84+
it "should report a nonexistent service as absent" do
8585
allow(service_util).to receive(:exists?).with(resource[:name]).and_return(false)
8686

87-
expect(provider.status).to eql(:stopped)
87+
expect(provider.status).to eql(:absent)
8888
end
8989

9090
it "should report service as stopped when status cannot be retrieved" do

spec/unit/type/service_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def safely_load_service_type
6767
expect(svc.should(:ensure)).to eq(:stopped)
6868
end
6969

70+
describe 'the absent property' do
71+
it 'should fail validate if it is a service' do
72+
expect { Puppet::Type.type(:service).new(:name => "service_name", :ensure => :absent) }.to raise_error(Puppet::Error, /Managing absent on a service is not supported/)
73+
end
74+
end
75+
7076
describe "the enable property" do
7177
before :each do
7278
allow(@provider.class).to receive(:supports_parameter?).and_return(true)

0 commit comments

Comments
 (0)