Skip to content
This repository was archived by the owner on Jun 2, 2019. It is now read-only.

updated provider mvn to use Puppet::Util::Execution.execute instead of P... #31

Merged
merged 1 commit into from
May 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/puppet/provider/maven/mvn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,25 @@ def download(dest, latest, offline = false)

begin
Timeout::timeout(timeout) do
output, status = Puppet::Util::SUIDManager.run_and_capture(command, user, group)
debug output if status.exitstatus == 0
debug "Exit status = #{status.exitstatus}"
output = Puppet::Util::Execution.execute(command, {:uid => user, :gid => group})

debug output if output.exitstatus == 0
debug "Exit status = #{output.exitstatus}"
end
rescue Timeout::Error
self.fail("Command timed out, increase timeout parameter if needed: #{command}")
end

if (status.exitstatus == 1) && (output == '')
self.fail("mvn returned #{status.exitstatus}: Is Maven installed?")
if (output.exitstatus == 1) && (output == '')
self.fail("mvn returned #{output.exitstatus}: Is Maven installed?")
end

# if we are offline, we check by this if the file is yet downloaded
if status.exitstatus != 0 && !offline
self.fail("#{command} returned #{status.exitstatus}: #{output}")
if output.exitstatus != 0 && !offline
self.fail("#{command} returned #{output.exitstatus}: #{output}")
end

status.exitstatus == 0
output.exitstatus == 0
end

def destroy
Expand Down
61 changes: 30 additions & 31 deletions spec/unit/puppet/provider/maven/mvn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@

context 'and an updated snapshot' do
before do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture) { |command|
expect(Puppet::Util::Execution).to receive(:execute) { |command|
command[0] =~ /-Ddest=([^\s]+)/
File.open($1, 'w') do |f|
f.write 'bar'
end
}.and_return ['', OpenStruct.new({exitstatus: 0})]
}.and_return OpenStruct.new({exitstatus: 0})
end

it { should equal :present }
end

context 'and a current snapshot' do
before do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture) { |command|
expect(Puppet::Util::Execution).to receive(:execute) { |command|
command[0] =~ /-Ddest=([^\s]+)/
File.open($1, 'w') do |f|
f.write 'foo'
end
}.and_return ['', OpenStruct.new({exitstatus: 0})]
}.and_return OpenStruct.new({exitstatus: 0})
end

it { should equal :latest }
Expand Down Expand Up @@ -119,9 +119,9 @@
subject do
command_line = nil

expect(Puppet::Util::SUIDManager).to receive(:run_and_capture) { |command|
expect(Puppet::Util::Execution).to receive(:execute) { |command|
command_line = command[0]
}.and_return [nil, exitstatus]
}.and_return exitstatus
provider_class.new(type.new({ path: path }.merge params)).ensure = ensure_param
command_line
end
Expand Down Expand Up @@ -197,48 +197,47 @@
let(:path) { '/tmp/blah.txt' }

context 'when mvn returns 1' do
let(:exitstatus) { OpenStruct.new exitstatus: 1 }
#let(:exitstatus) { OpenStruct.new exitstatus: 1 }

context 'with no output' do
let(:output) { '' }

let(:output) { Puppet::Util::Execution::ProcessOutput.new '', 1 }
example do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.and_return [output, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.and_return output
expect { subject }.to raise_error Puppet::Error, /^mvn returned 1: Is Maven installed\?/
end
end

context 'with output "busted!"' do
let(:output) { 'busted!' }
let(:output) { Puppet::Util::Execution::ProcessOutput. new 'busted!', 1 }

example do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.and_return [output, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.and_return output
expect { subject }.to raise_error Puppet::Error, /returned 1: busted\!/
end
end
end

it 'should default to root user' do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.with(anything(), 'root', anything())
.and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.with(anything(), hash_including(:uid => 'root'))
.and_return exitstatus

subject
end

it 'should default to root group' do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.with(anything(), anything(), 'root')
.and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.with(anything(), hash_including(:gid => 'root'))
.and_return exitstatus

subject
end

it 'should use no timeout' do
expect(Timeout).to receive(:timeout).with(0).and_call_original
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture).and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute).and_return exitstatus

subject
end
Expand All @@ -247,9 +246,9 @@
subject do
command_line = nil

expect(Puppet::Util::SUIDManager).to receive(:run_and_capture) { |command|
expect(Puppet::Util::Execution).to receive(:execute) { |command|
command_line = command[0]
}.and_return [nil, exitstatus]
}.and_return exitstatus
provider_class.new(type.new({ path: path }.merge params)).ensure = :present
command_line
end
Expand Down Expand Up @@ -435,13 +434,13 @@

it 'should use the given timeout' do
expect(Timeout).to receive(:timeout).with(1).and_call_original
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture).and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute).and_return exitstatus

subject
end

it 'should timeout if mvn takes too long' do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture) do
expect(Puppet::Util::Execution).to receive(:execute) do
sleep 2
end

Expand All @@ -453,9 +452,9 @@
let(:params) { { user: 'user_test' } }

it 'should use the given user' do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.with(anything(), 'user_test', anything())
.and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.with(anything(), hash_including(:uid => 'user_test'))
.and_return exitstatus

subject
end
Expand All @@ -465,9 +464,9 @@
let(:params) { { group: 'group_test' } }

it 'should use the given group' do
expect(Puppet::Util::SUIDManager).to receive(:run_and_capture)
.with(anything(), anything(), 'group_test')
.and_return [nil, exitstatus]
expect(Puppet::Util::Execution).to receive(:execute)
.with(anything(), hash_including(:gid => 'group_test'))
.and_return exitstatus

subject
end
Expand Down