mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
json resource: Add handling for command:
error
This provides the user with messages if `json(command: 'command_that_errors')` exits non-zero and/or has STDERR. Without this the resource will report "No output from command" when really the output is contained in STDERR. Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
parent
d1ecefc970
commit
0e1b3fa65a
2 changed files with 35 additions and 11 deletions
|
@ -93,10 +93,25 @@ module Inspec::Resources
|
|||
end
|
||||
|
||||
def load_raw_from_command(command)
|
||||
command_output = inspec.command(command).stdout
|
||||
raise Inspec::Exceptions::ResourceSkipped, "No output from command: #{command}" if command_output.nil? || command_output.empty?
|
||||
command_result = inspec.command(command)
|
||||
|
||||
command_output
|
||||
case command_result.exit_status
|
||||
when 0
|
||||
|
||||
# TODO: Handle case where command exits 0 but STDERR is found.
|
||||
|
||||
if command_result.stdout.empty?
|
||||
raise Inspec::Exceptions::ResourceSkipped, "No output from command: #{command}"
|
||||
end
|
||||
else
|
||||
if command_result.stderr.empty?
|
||||
raise Inspec::Exceptions::ResourceFailed, "Command exited non-zero but STDERR was empty"
|
||||
else
|
||||
raise Inspec::Exceptions::ResourceFailed, "Command exited non-zero. Error:\n #{command_result.stderr}"
|
||||
end
|
||||
end
|
||||
|
||||
command_result.stdout
|
||||
end
|
||||
|
||||
# for resources the subclass JsonConfig, this allows specification of the resource
|
||||
|
|
|
@ -86,7 +86,7 @@ describe "Inspec::Resources::JSON" do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#load_raw_from_file" do
|
||||
describe "#load_raw_from_command" do
|
||||
let(:cmd_str) { "curl localhost" }
|
||||
let(:resource) { Inspec::Resources::JsonConfig.allocate }
|
||||
let(:inspec) { mock }
|
||||
|
@ -97,18 +97,27 @@ describe "Inspec::Resources::JSON" do
|
|||
inspec.expects(:command).with(cmd_str).returns(command)
|
||||
end
|
||||
|
||||
it "raises an exception if command stdout is nil" do
|
||||
command.expects(:stdout).returns(nil)
|
||||
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceSkipped
|
||||
end
|
||||
|
||||
it "raises an exception if command stdout is empty" do
|
||||
it "skips the resource if command exits zero and stdout is empty" do
|
||||
command.expects(:exit_status).returns(0)
|
||||
command.expects(:stdout).returns("")
|
||||
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceSkipped
|
||||
end
|
||||
|
||||
it "fails the resource if the command exits non-zero and stderr is not empty" do
|
||||
command.expects(:exit_status).returns(1)
|
||||
command.expects(:stderr).returns("This is a fake stderr message").twice
|
||||
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceFailed
|
||||
end
|
||||
|
||||
it "fails the resource if the command exits non-zero and stderr is empty" do
|
||||
command.expects(:exit_status).returns(1)
|
||||
command.expects(:stderr).returns("")
|
||||
proc { resource.send(:load_raw_from_command, cmd_str) }.must_raise Inspec::Exceptions::ResourceFailed
|
||||
end
|
||||
|
||||
it "returns the command output" do
|
||||
command.expects(:stdout).returns("json goes here")
|
||||
command.expects(:exit_status).returns(0)
|
||||
command.expects(:stdout).returns("json goes here").twice
|
||||
resource.send(:load_raw_from_command, cmd_str).must_equal "json goes here"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue