Merge pull request #5816 from inspec/vasundhara/allow-core-resource-inheritance

CFINSPEC-15 Allows inheritance of core resource into the custom resource.
This commit is contained in:
Clinton Wolfe 2022-02-01 16:11:44 -05:00 committed by GitHub
commit cd4f0b59a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 103 additions and 2 deletions

View file

@ -30,6 +30,8 @@ module Inspec
c3 = Class.new do
include Inspec::DSL::RequireOverride
include Inspec::Resources
def initialize(require_loader)
@require_loader = require_loader
@inspec_binding = nil

View file

@ -68,6 +68,7 @@ module Inspec
end
def reload_dsl
@resource_registry.merge!(Inspec::Resource.new_registry)
@control_eval_context = nil
end

10
test/fixtures/files/node.json vendored Normal file
View file

@ -0,0 +1,10 @@
{
"name" : "hello",
"meta" : {
"creator" : "John Doe"
},
"array": [
"zero",
"one"
]
}

View file

@ -0,0 +1,3 @@
# Example InSpec Profile
This example shows the implementation of an InSpec profile.

View file

@ -0,0 +1,23 @@
# copyright: 2018, The Authors
# title "sample section"
# # you can also use plain tests
# describe file("/tmp") do
# it { should be_directory }
# end
# # you add controls here
# control "tmp-1.0" do # A unique ID for this control
# impact 0.7 # The criticality, if this control fails.
# title "Create /tmp directory" # A human-readable title
# desc "An optional description..."
# describe file("/tmp") do # The actual test
# it { should be_directory }
# end
# end
describe node do
its("name") { should eq "hello" }
its(['meta','creator']) { should eq 'John Doe' }
end

View file

@ -0,0 +1,10 @@
name: custom-resource-inheritance
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
supports:
platform: os

View file

@ -0,0 +1,13 @@
require "inspec/resources/json"
class NodeAttributes < JsonConfig
name 'node'
def initialize
super('./test/fixtures/files/node.json')
end
def to_s
"Node Json"
end
end

View file

@ -1,4 +1,5 @@
require "functional/helper"
require "helpers/mock_loader"
describe "inspec exec" do
parallelize_me!
@ -163,7 +164,6 @@ Test Summary: 0 successful, 0 failures, 0 skipped
it "executes a specs-only profile" do
inspec("exec " + File.join(profile_path, "spec_only") + " --no-create-lockfile")
_(stdout).must_include "Target: local://"
_(stdout).must_include "working"
_(stdout).must_include "✔ is expected to eq \"working\""
@ -521,6 +521,25 @@ Test Summary: 0 successful, 0 failures, 0 skipped
end
end
describe "with a profile that inherits core resource into custom reosuce" do
let(:out) { inspec("exec " + File.join(profile_path, "custom-resource-inheritance") + " --no-create-lockfile") }
it "executes the custom resoruc without error" do
_(stdout).must_equal "
Profile: InSpec Profile (custom-resource-inheritance)
Version: 0.1.0
Target: local://
Node Json
name is expected to eq \"hello\"
[\"meta\", \"creator\"] is expected to eq \"John Doe\"
Test Summary: 2 successful, 0 failures, 0 skipped
"
_(stderr).must_equal ""
assert_exit_code 0, out
end
end
describe "given a profile with controls and anonymous describe blocks" do
let(:out) { inspec("exec " + example_control + " --no-create-lockfile") }

View file

@ -1,5 +1,6 @@
require "helper"
require "inspec/library_eval_context"
require "inspec/resources/ini"
describe Inspec::LibraryEvalContext do
let(:resource_content) do
@ -17,6 +18,19 @@ describe Inspec::LibraryEvalContext do
EOF
end
let(:resource_content2) do
<<~EOF
class AnotherResource < IniConfig
name 'another_resource'
desc 'Another Resource description'
example 'see README'
def version
'2.0'
end
end
EOF
end
let(:registry) { Inspec::Resource.new_registry }
let(:eval_context) { Inspec::LibraryEvalContext.create(registry, nil) }
@ -35,7 +49,6 @@ describe Inspec::LibraryEvalContext do
_(old_default_registry.keys.sort).wont_include "my_test_resource"
eval_context.instance_eval(resource_content)
_(old_default_registry.keys.sort).must_equal Inspec::Resource.default_registry.keys.sort
_(old_default_registry).must_equal Inspec::Resource.default_registry
end
@ -43,4 +56,11 @@ describe Inspec::LibraryEvalContext do
it "provides an inspec context for requiring local files" do
_(eval_context.__inspec_binding).must_be_kind_of Binding
end
it "adds the resource to our registry" do
_(registry.keys).wont_include "another_resource"
eval_context.instance_eval(resource_content2)
old_default_registry = Inspec::Resource.default_registry.dup
_(old_default_registry.keys.sort).must_equal Inspec::Resource.default_registry.keys.sort
end
end