Skip to content

Commit 4f6a2ea

Browse files
committed
(PE-36365) Puppet resource service should error if service is not present
Previously if a service was not present on a system and a user tried to manage that service with `puppet resource service`, and that service wasn't there an error message would appear on the screen but the puppet command would return a 0 exit code. This commit is meant to bubble up those error messages so they will be reflected on the command line properly. First pass will be to update the Windows and Systemd providers. blank
1 parent f754cf7 commit 4f6a2ea

File tree

7 files changed

+56
-10
lines changed

7 files changed

+56
-10
lines changed

lib/puppet/application/resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def find_or_save_resources(type, name, params)
237237
save_result, report = Puppet::Resource.indirection.save(resource, key)
238238
status = report.resource_statuses[resource.ref]
239239
raise "Failed to manage resource #{resource.ref}" if status&.failed?
240+
240241
[ save_result ]
241242
end
242243
else

lib/puppet/provider/service/windows.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def stop
9292
end
9393

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

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

lib/puppet/type/service.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def insync?(current)
111111
end
112112

113113
newvalue(:absent)
114+
115+
validate do |val|
116+
fail "Managing absent on a service is not supported" if val.to_s == 'absent'
117+
end
114118

115119
aliasvalue(:false, :stopped)
116120
aliasvalue(:true, :running)

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)