2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/command"
|
2019-08-06 04:47:44 +00:00
|
|
|
require "inspec/resources/os"
|
2017-06-19 15:07:36 +00:00
|
|
|
|
|
|
|
describe Inspec::Resources::Cmd do
|
|
|
|
let(:x) { rand.to_s }
|
2018-07-16 12:20:57 +00:00
|
|
|
def resource(command, options = {} )
|
2019-06-11 22:24:35 +00:00
|
|
|
load_resource("command", command, options)
|
2017-06-19 15:07:36 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "prints as a bash command" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource(x).to_s).must_equal "Command: `#{x}`"
|
2017-06-19 15:07:36 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "runs a valid mocked command" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource("env").result).wont_be_nil
|
|
|
|
_(resource("env").stdout).must_equal "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n"
|
|
|
|
_(resource("env").stderr).must_equal ""
|
|
|
|
_(resource("env").exit_status).must_equal 0
|
2017-06-19 15:07:36 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "exist? returns false on nil os name" do
|
2017-12-05 13:21:31 +00:00
|
|
|
Inspec::Resources::OSResource.any_instance.stubs(:name).returns(nil)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource("test").exist?).must_equal false
|
2017-12-05 13:21:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "exist? returns false on mock os name" do
|
|
|
|
Inspec::Resources::OSResource.any_instance.stubs(:name).returns("mock")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource("test").exist?).must_equal false
|
2017-12-05 13:21:31 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises when called with nil as a command" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(proc { resource(nil).result }).must_raise StandardError
|
2017-06-19 15:07:36 +00:00
|
|
|
end
|
2018-07-16 12:20:57 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "fails the resource if `redact_regex` is not a regular expression" do
|
|
|
|
result = resource("env", redact_regex: "string")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(result.resource_failed?).must_equal true
|
|
|
|
_(result.resource_exception_message).must_match(/must be a regular expression/)
|
2018-07-16 12:20:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "redacts output if `redact_regex` is passed with caputure groups" do
|
|
|
|
cmd = "command_with_password -p supersecret -d no_redact"
|
|
|
|
expected_to_s = "Command: `command_with_password -p REDACTED -d no_redact`"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource(cmd, redact_regex: /(-p ).*( -d)/).to_s).must_equal(expected_to_s)
|
2018-07-16 12:20:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "redacts output if `redact_regex` is passed without a caputure group" do
|
|
|
|
cmd = "command_with_password -p supersecret -d no_redact"
|
|
|
|
expected_to_s = "Command: `command_with_password REDACTED no_redact`"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource(cmd, redact_regex: /-p .* -d/).to_s).must_equal(expected_to_s)
|
2018-07-16 12:20:57 +00:00
|
|
|
end
|
2017-06-19 15:07:36 +00:00
|
|
|
end
|