Merge pull request #6126 from inspec/vasundhara/group_2_resource_id

Adds resource_id to group 2  resources
This commit is contained in:
Clinton Wolfe 2022-06-08 16:14:45 -04:00 committed by GitHub
commit 6385ea9f74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 83 additions and 6 deletions

View file

@ -45,6 +45,10 @@ module Inspec::Resources
end
end
def resource_id
@package_name || "chocolatey_package"
end
def to_s
"Chocolatey package #{package_name}"
end

View file

@ -34,6 +34,10 @@ module Inspec::Resources
param
end
def resource_id
@conf_path || "chrony_conf"
end
def to_s
"chrony.conf"
end

View file

@ -93,6 +93,11 @@ module Inspec::Resources
res.exit_status.to_i == 0
end
# to_s method outputs the command which we are using here as UUID to identify resource and also it take cares of Redact output
def resource_id
to_s || "command"
end
def to_s
output = "Command: `#{@command}`"
# Redact output if the `redact_regex` option is passed

View file

@ -51,6 +51,10 @@ module Inspec::Resources
info[:version]
end
def resource_id
@package_name || "cpan"
end
def to_s
"Perl Module #{@package_name}"
end

View file

@ -57,6 +57,10 @@ module Inspec::Resources
info[:version]
end
def resource_id
@package_name || "cran"
end
def to_s
"R Module #{@package_name}"
end

View file

@ -38,6 +38,11 @@ module Inspec::Resources
@params.include?(rule)
end
def resource_id
user = @user || "current user"
"cron #{user}"
end
def to_s
if is_user_crontab?
"cron for user #{@user}"

View file

@ -19,7 +19,8 @@ module Inspec::Resources
def initialize(path, headers = true)
@headers = headers
super(path)
@path = path
super(@path)
end
# override the parse method from JsonConfig
@ -68,6 +69,10 @@ module Inspec::Resources
end
end
def resource_id
@path || "csv"
end
private
# used by JsonConfig to build up a full to_s method

View file

@ -76,6 +76,10 @@ module Inspec::Resources
@dh_params.params_ok?
end
def resource_id
@dh_params_path || "dh_params"
end
def to_s
"dh_params #{@dh_params_path}"
end

View file

@ -6,6 +6,7 @@ describe "Inspec::Resources::ChocoPkg" do
it "can parse output from `choco` when package is installed" do
pkg = { name: "git", installed: false, version: nil, type: "chocolatey" }
resource = MockLoader.new(:windows).load_resource("chocolatey_package", "git")
_(resource.resource_id).must_equal "git"
_(resource.installed?).must_equal pkg[:installed]
_(resource.version).must_be_nil
_(resource.info).must_equal pkg
@ -18,4 +19,9 @@ describe "Inspec::Resources::ChocoPkg" do
_(resource.version).must_equal pkg[:version]
_(resource.info).must_equal pkg
end
it "gets the resource_id for the current resource" do
resource = MockLoader.new(:windows).load_resource("chocolatey_package", "nssm")
_(resource.resource_id).must_equal "nssm"
end
end

View file

@ -15,4 +15,9 @@ describe "Inspec::Resources::ChronyConf" do
}
assert_nil resource.allow
end
it "gets resource_id for the current resource" do
resource = load_resource("chrony_conf")
_(resource.resource_id).must_equal "/etc/chrony.conf"
end
end

View file

@ -14,10 +14,12 @@ describe Inspec::Resources::Cmd do
end
it "runs a valid mocked command" do
_(resource("env").result).wont_be_nil
_(resource("env").stdout).must_include "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
resource = load_resource("command", "env")
_(resource.resource_id).must_equal "Command: `env`"
_(resource.result).wont_be_nil
_(resource.stdout).must_include "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n"
_(resource.stderr).must_equal ""
_(resource.exit_status).must_equal 0
end
it "exist? returns false on nil os name" do
@ -42,8 +44,10 @@ describe Inspec::Resources::Cmd do
it "redacts output if `redact_regex` is passed with capture groups" do
cmd = "command_with_password -p supersecret -d no_redact"
resource = load_resource("command", cmd, redact_regex: /(-p ).*( -d)/)
expected_to_s = "Command: `command_with_password -p REDACTED -d no_redact`"
_(resource(cmd, redact_regex: /(-p ).*( -d)/).to_s).must_equal(expected_to_s)
_(resource.resource_id).must_equal(expected_to_s)
_(resource.to_s).must_equal(expected_to_s)
end
it "redacts output if `redact_regex` is passed without a capture group" do

View file

@ -18,4 +18,8 @@ describe "Inspec::Resources::Cpan" do
_(resource.info[:type]).must_equal "cpan"
_(resource.info).must_equal pkg
end
it "gets the resource_id for the current resource" do
resource = load_resource("cpan", "DBD::Pg")
_(resource.resource_id).must_equal "DBD::Pg"
end
end

View file

@ -16,4 +16,8 @@ describe "Inspec::Resources::Cran" do
_(resource.info[:name]).must_equal "DoesNotExist"
_(resource.info[:type]).must_equal "cran"
end
it "gets resource_id for the current resource" do
resource = load_resource("cran", "DBI")
_(resource.resource_id).must_equal "DBI"
end
end

View file

@ -36,4 +36,15 @@ describe "Inspec::Resources::Cron" do
_(resource.resource_exception_message).must_include "Error while executing crontab -l -u testuser command:"
end
end
describe "resource_id" do
it "generates the resource_id for the current resouce if user is not specified" do
_(cron.resource_id).must_equal "cron current user"
end
it "generates the resource_id for the current resouce if user is specified" do
resource = load_resource("cron", { user: "testuser" })
_(resource.resource_id).must_equal "cron testuser"
end
end
end

View file

@ -9,6 +9,10 @@ describe "Inspec::Resources::CSV" do
{}
end
it "gets the resource_id for the current resource" do
_(resource.resource_id).must_equal "example.csv"
end
it "captures an array of params" do
_(resource.params).must_be_kind_of Array
end

View file

@ -71,6 +71,10 @@ describe "Inspec::Resources::DhParams" do
EOF
end
it "gets the resource_id for the current resource" do
_(resource_dh_params.resource_id).must_equal "dh_params.dh_pem"
end
it "parses the generator used for the Diffie-Hellman operation" do
_(resource_dh_params.send("generator")).must_equal 2
end