add inspec source reader

This commit is contained in:
Dominik Richter 2016-02-21 13:26:31 +01:00 committed by Stephan Renatus
parent 125ee53041
commit f023d02bbb
7 changed files with 99 additions and 3 deletions

View file

@ -27,3 +27,5 @@ module Inspec
Inspec::Plugins::SourceReader
end
end
require 'source_readers/inspec'

View 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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1 @@
# Library resource

View 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