mirror of
https://github.com/inspec/inspec
synced 2024-11-22 20:53:11 +00:00
Adds unit test for opa_api and opa_cli resource
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
parent
e6d0277593
commit
f0ca02a1c5
7 changed files with 69 additions and 2 deletions
|
@ -11,7 +11,7 @@ module Inspec::Resources
|
|||
def initialize(opts={})
|
||||
@url = opts[:url] || nil
|
||||
@data = opts[:data] || nil
|
||||
fail_resource "OPA url and data are mandatory." if @url.nil? || @data.nil?
|
||||
fail_resource "OPA url and data are mandatory." if @url.nil? || @url.empty? || @data.nil? || @data.empty?
|
||||
@content = load_result
|
||||
super(@content)
|
||||
end
|
||||
|
|
|
@ -13,7 +13,9 @@ module Inspec::Resources
|
|||
@policy = opts[:policy] || nil
|
||||
@data = opts[:data] || nil
|
||||
@query = opts[:query] || nil
|
||||
fail_resource "OPA policy, data and query are mandatory." if @policy.nil? || @data.nil? || @query.nil?
|
||||
if (@policy.nil? || @policy.empty?) || (@data.nil? || @data.empty?) || (@query.nil? || @query.empty?)
|
||||
fail_resource "OPA policy, data and query are mandatory."
|
||||
end
|
||||
@content = load_result
|
||||
super(@content)
|
||||
end
|
||||
|
|
1
test/fixtures/cmd/opa-api-result
vendored
Normal file
1
test/fixtures/cmd/opa-api-result
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{"result":["ci","busybox"]}
|
16
test/fixtures/cmd/opa-result
vendored
Normal file
16
test/fixtures/cmd/opa-result
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"result": [
|
||||
{
|
||||
"expressions": [
|
||||
{
|
||||
"value": false,
|
||||
"text": "data.example.allow",
|
||||
"location": {
|
||||
"row": 1,
|
||||
"col": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -566,6 +566,8 @@ class MockLoader
|
|||
"semodule -lfull" => cmd.call("semodule-lfull"),
|
||||
"semanage boolean -l -n" => cmd.call("semanage-boolean"),
|
||||
"Get-ChildItem -Path \"C:\\Program Files\\MongoDB\\Server\" -Name" => cmd.call("mongodb-version"),
|
||||
"opa eval -i 'input.json' -d 'example.rego' 'data.example.allow'" => cmd.call("opa-result"),
|
||||
"curl -X POST localhost:8181/v1/data/example/violation -d @v1-data-input.json -H 'Content-Type: application/json'" => cmd.call("opa-api-result")
|
||||
}
|
||||
|
||||
if @platform && (@platform[:name] == "windows" || @platform[:name] == "freebsd")
|
||||
|
|
23
test/unit/resources/opa_api_test.rb
Normal file
23
test/unit/resources/opa_api_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require "helper"
|
||||
require "inspec/resource"
|
||||
require "inspec/resources/opa_api"
|
||||
|
||||
describe "Inspec::Resources::OpaApi" do
|
||||
it "verify opa api query result parsing" do
|
||||
resource = load_resource("opa_api", url: "localhost:8181/v1/data/example/violation", data: "v1-data-input.json")
|
||||
_(resource.params["result"]).must_equal ["ci", "busybox"]
|
||||
_(resource.params["result"]).must_include "ci"
|
||||
end
|
||||
|
||||
it "fails when url or data is nil." do
|
||||
resource = load_resource("opa_api")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "OPA url and data are mandatory."
|
||||
end
|
||||
|
||||
it "fails when url or data is empty." do
|
||||
resource = load_resource("opa_api", url: "", data: "")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "OPA url and data are mandatory."
|
||||
end
|
||||
end
|
23
test/unit/resources/opa_cli_test.rb
Normal file
23
test/unit/resources/opa_cli_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require "helper"
|
||||
require "inspec/resource"
|
||||
require "inspec/resources/opa_cli"
|
||||
|
||||
describe "Inspec::Resources::OpaCli" do
|
||||
it "verify opa eval query result parsing" do
|
||||
resource = load_resource("opa_cli", policy: "example.rego", data: "input.json", query: "data.example.allow")
|
||||
_(resource.params["result"][0]["expressions"][0]["value"]).must_equal false
|
||||
_(resource.allow).must_equal false
|
||||
end
|
||||
|
||||
it "fails when policy, data or query is nil." do
|
||||
resource = load_resource("opa_cli")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "OPA policy, data and query are mandatory."
|
||||
end
|
||||
|
||||
it "fails when empty string passed for options policy, data or query." do
|
||||
resource = load_resource("opa_cli", policy: "", data: "", query: "")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "OPA policy, data and query are mandatory."
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue