mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
add inspec source reader
This commit is contained in:
parent
125ee53041
commit
f023d02bbb
7 changed files with 99 additions and 3 deletions
|
@ -27,3 +27,5 @@ module Inspec
|
|||
Inspec::Plugins::SourceReader
|
||||
end
|
||||
end
|
||||
|
||||
require 'source_readers/inspec'
|
||||
|
|
55
lib/source_readers/inspec.rb
Normal file
55
lib/source_readers/inspec.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
require 'inspec/fetcher'
|
||||
require 'inspec/metadata'
|
||||
|
||||
module SourceReaders
|
||||
class InspecReader < Inspec.source_reader(1)
|
||||
name 'inspec'
|
||||
priority 10
|
||||
|
||||
def self.resolve(target)
|
||||
return new(target, 'inspec.yml') if target.files.include?('inspec.yml')
|
||||
# TODO: deprecated for 1.0.0 release
|
||||
if target.files.include?('metadata.rb') &&
|
||||
(
|
||||
target.files.include?('controls') ||
|
||||
target.files.include?('test')
|
||||
)
|
||||
return new(target, 'metadata.rb')
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
attr_reader :metadata, :tests, :libraries
|
||||
|
||||
def initialize(target, metadata_source)
|
||||
@target = target
|
||||
@metadata = Inspec::Metadata.from_ref(
|
||||
metadata_source,
|
||||
@target.read(metadata_source),
|
||||
nil)
|
||||
|
||||
@tests = load_tests
|
||||
@libraries = load_libs
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_tests
|
||||
tests = @target.files.find_all do |path|
|
||||
path.start_with?('controls', 'test') && path.end_with?('.rb')
|
||||
end
|
||||
Hash[tests.map { |x| [x, @target.read(x)] }]
|
||||
end
|
||||
|
||||
def load_libs
|
||||
tests = @target.files.find_all do |path|
|
||||
path.start_with?('libraries') && path.end_with?('.rb')
|
||||
end
|
||||
Hash[tests.map { |x| [x, @target.read(x)] }]
|
||||
end
|
||||
end
|
||||
end
|
|
@ -21,7 +21,7 @@ describe Fetchers::Tar do
|
|||
end
|
||||
|
||||
it 'must contain all files' do
|
||||
_(res.files).must_equal ["inspec.yml", "controls", "controls/filesystem_spec.rb"]
|
||||
_(res.files).must_equal ["inspec.yml", "libraries", "libraries/testlib.rb", "controls", "controls/filesystem_spec.rb"]
|
||||
end
|
||||
|
||||
it 'must not read if the file isnt included' do
|
||||
|
|
|
@ -32,7 +32,7 @@ describe Fetchers::Url do
|
|||
end
|
||||
|
||||
it 'must contain all files' do
|
||||
_(res.files).must_equal ["inspec.yml", "controls", "controls/filesystem_spec.rb"]
|
||||
_(res.files).must_equal ["inspec.yml", "libraries", "libraries/testlib.rb", "controls", "controls/filesystem_spec.rb"]
|
||||
end
|
||||
|
||||
it 'must not read if the file isnt included' do
|
||||
|
|
|
@ -21,7 +21,7 @@ describe Fetchers::Zip do
|
|||
end
|
||||
|
||||
it 'must contain all files' do
|
||||
_(res.files).must_equal ["inspec.yml", "controls", "controls/filesystem_spec.rb"]
|
||||
_(res.files).must_equal ["inspec.yml", "libraries", "libraries/testlib.rb", "controls", "controls/filesystem_spec.rb"]
|
||||
end
|
||||
|
||||
it 'must not read if the file isnt included' do
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
# Library resource
|
38
test/unit/source_readers/inspec_test.rb
Normal file
38
test/unit/source_readers/inspec_test.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
require 'helper'
|
||||
|
||||
describe SourceReaders::InspecReader do
|
||||
let(:reader) { SourceReaders::InspecReader }
|
||||
|
||||
it 'registers with the source readers registry' do
|
||||
reg = Inspec::SourceReader.registry
|
||||
_(reg['inspec']).must_equal reader
|
||||
end
|
||||
|
||||
describe 'with a valid profile' do
|
||||
let(:mock_file) { mock_file = MockLoader.profile_tgz('complete-profile') }
|
||||
let(:target) { Inspec::Fetcher.resolve(mock_file) }
|
||||
let(:res) { Inspec::SourceReader.resolve(target) }
|
||||
|
||||
it 'resolves the target to inspec' do
|
||||
_(res).must_be_kind_of reader
|
||||
end
|
||||
|
||||
it 'retrieves metadata' do
|
||||
_(res.metadata.params[:name]).must_equal 'complete'
|
||||
end
|
||||
|
||||
it 'retrieves all files' do
|
||||
_(res.tests.keys).must_equal %w{controls/filesystem_spec.rb}
|
||||
_(res.tests.values[0]).must_match /^control 'test01' do$/
|
||||
end
|
||||
|
||||
it 'retrieves all libraries' do
|
||||
_(res.libraries.keys).must_equal %w{libraries/testlib.rb}
|
||||
_(res.libraries.values[0]).must_match /^# Library resource$/
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue