2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/json"
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::JSON" do
|
|
|
|
describe "when loading a valid json" do
|
2019-05-31 21:59:06 +00:00
|
|
|
let(:resource) { load_resource("json", "policyfile.lock.json") }
|
2015-10-27 14:37:01 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "gets params as a hashmap" do
|
2015-10-27 14:37:01 +00:00
|
|
|
_(resource.params).must_be_kind_of Hash
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves nil if a param is missing" do
|
|
|
|
_(resource.params["missing"]).must_be_nil
|
2015-10-27 14:37:01 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves params by name" do
|
|
|
|
_(resource.send("name")).must_equal "demo"
|
2015-10-27 14:37:01 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves an array by name" do
|
|
|
|
_(resource.send("run_list")).must_equal %w{a b}
|
2015-10-27 14:37:01 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt resolve dot-notation names" do
|
|
|
|
_(resource.send("x.y.z")).must_be_nil
|
2015-10-27 14:37:01 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt resolve symbol-notation names" do
|
2015-10-27 14:37:01 +00:00
|
|
|
_(resource.send(:'x.y.z')).must_be_nil
|
|
|
|
end
|
2018-04-26 15:54:16 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "is enumerability matches its data" do
|
|
|
|
enum = load_resource("json", content: '["a","b"]')
|
|
|
|
not_enum = load_resource("json", content: "525600")
|
2018-04-26 15:54:16 +00:00
|
|
|
|
|
|
|
_(enum.respond_to?(:each)).must_equal true
|
|
|
|
_(not_enum.respond_to?(:each)).must_equal false
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
end
|
2017-11-29 21:31:06 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when loading a nonexistent file" do
|
|
|
|
let(:resource) { load_resource("json", "nonexistent.json") }
|
2016-12-15 13:00:56 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "produces an error" do
|
2018-03-22 12:25:45 +00:00
|
|
|
_(resource.resource_exception_message).must_equal "Can't find file: nonexistent.json"
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
2017-11-29 21:31:06 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "still provides an empty hash for params" do
|
2017-11-29 21:31:06 +00:00
|
|
|
_(resource.params).must_equal({})
|
|
|
|
end
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#load_raw_from_file" do
|
|
|
|
let(:path) { "/path/to/file.txt" }
|
2017-11-27 16:13:02 +00:00
|
|
|
let(:resource) { Inspec::Resources::JsonConfig.allocate }
|
|
|
|
let(:inspec) { mock }
|
|
|
|
let(:file) { mock }
|
|
|
|
|
|
|
|
before do
|
|
|
|
resource.stubs(:inspec).returns(inspec)
|
|
|
|
inspec.expects(:file).with(path).returns(file)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an exception when the file does not exist" do
|
2017-11-27 16:13:02 +00:00
|
|
|
file.expects(:file?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(proc { resource.send(:load_raw_from_file, path) }).must_raise Inspec::Exceptions::ResourceSkipped
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an exception if the file content is nil" do
|
2017-11-27 16:13:02 +00:00
|
|
|
file.expects(:file?).returns(true)
|
|
|
|
file.expects(:content).returns(nil)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(proc { resource.send(:load_raw_from_file, path) }).must_raise Inspec::Exceptions::ResourceSkipped
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an exception if the file content is empty" do
|
2017-11-27 16:13:02 +00:00
|
|
|
file.expects(:file?).returns(true)
|
2019-06-11 22:24:35 +00:00
|
|
|
file.expects(:content).at_least_once.returns("")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(proc { resource.send(:load_raw_from_file, path) }).must_raise Inspec::Exceptions::ResourceSkipped
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the file content" do
|
2017-11-27 16:13:02 +00:00
|
|
|
file.expects(:file?).returns(true)
|
2019-06-11 22:24:35 +00:00
|
|
|
file.expects(:content).at_least_once.returns("json goes here")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource.send(:load_raw_from_file, path)).must_equal "json goes here"
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
describe ":command" do
|
|
|
|
# CASES:
|
|
|
|
#
|
|
|
|
# stdout:good, stderr:empty
|
|
|
|
# stdout:bad, stderr:empty
|
|
|
|
# stdout:empty, stderr:msg
|
|
|
|
# stdout:empty, stderr:empty
|
2017-11-27 16:13:02 +00:00
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
def run_json_cmd(cmd)
|
2021-03-30 02:13:11 +00:00
|
|
|
Inspec::Config.cached["command_timeout"] = 3600 # Reset to default
|
2019-10-05 00:06:21 +00:00
|
|
|
quick_resource("json", :linux, command: cmd)
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
# TODO: push up
|
|
|
|
def assert_resource_skipped(res, msg)
|
|
|
|
assert_predicate res, :resource_skipped?
|
|
|
|
refute_predicate res, :resource_failed?
|
|
|
|
|
|
|
|
assert_includes res.resource_exception_message, msg
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
# TODO: push up
|
|
|
|
def assert_resource_failed(res, msg)
|
|
|
|
refute_predicate res, :resource_skipped?
|
|
|
|
assert_predicate res, :resource_failed?
|
|
|
|
|
|
|
|
assert_includes res.resource_exception_message, msg
|
2019-02-28 08:55:58 +00:00
|
|
|
end
|
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
it "good stdout, empty stderr" do
|
2019-11-23 00:16:16 +00:00
|
|
|
resource = run_json_cmd %(#{Gem.ruby} -rjson -e "h={'result'=>true}; puts h.to_json")
|
2019-09-10 00:38:46 +00:00
|
|
|
|
|
|
|
assert_equal %({"result":true}), resource.raw_content.chomp
|
|
|
|
assert_equal({ "result" => true }, resource.params)
|
2017-11-27 16:13:02 +00:00
|
|
|
end
|
|
|
|
|
2019-09-10 00:38:46 +00:00
|
|
|
it "bad stdout, empty stderr" do
|
|
|
|
resource = run_json_cmd "echo 'not-valid-json'"
|
|
|
|
|
|
|
|
assert_resource_failed resource, "unexpected token at 'not-valid-json"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "empty stdout, message in stderr" do
|
2019-09-10 20:06:26 +00:00
|
|
|
skip "Bug #4465: does not work on windows w/ transport==local" if windows?
|
|
|
|
|
|
|
|
resource = run_json_cmd %{abort 'bad args'"}
|
2019-09-10 00:38:46 +00:00
|
|
|
|
|
|
|
assert_resource_failed resource, "No JSON output, STDERR:"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "empty stdout, empty stderr" do
|
2019-11-23 00:16:16 +00:00
|
|
|
resource = run_json_cmd %{#{Gem.ruby} -e "exit 1"}
|
2019-09-10 00:38:46 +00:00
|
|
|
|
|
|
|
assert_resource_failed resource, "No JSON output, STDERR was empty"
|
2016-12-15 13:00:56 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
end
|