Merge pull request #330 from chef/dr/metadata-ref

Verify profile metadata contents correctly
This commit is contained in:
Stephan Renatus 2015-12-23 09:28:51 +01:00
commit ce8786bfbc
5 changed files with 55 additions and 23 deletions

View file

@ -44,12 +44,12 @@ module Inspec
def valid? def valid?
is_valid = true is_valid = true
%w{ name title version summary }.each do |field| %w{ name version }.each do |field|
next unless params[field.to_sym].nil? next unless params[field.to_sym].nil?
@logger.error("Missing profile #{field} in metadata.rb") @logger.error("Missing profile #{field} in metadata.rb")
is_valid = false is_valid = false
end end
%w{ maintainer copyright }.each do |field| %w{ title summary maintainer copyright }.each do |field|
next unless params[field.to_sym].nil? next unless params[field.to_sym].nil?
@logger.warn("Missing profile #{field} in metadata.rb") @logger.warn("Missing profile #{field} in metadata.rb")
is_valid = false is_valid = false

View file

@ -6,7 +6,7 @@
require 'inspec/metadata' require 'inspec/metadata'
module Inspec module Inspec
class Profile # rubocop:disable Metrics/ClassLength class Profile
def self.from_path(path, options = nil) def self.from_path(path, options = nil)
opt = {} opt = {}
options.each { |k, v| opt[k.to_sym] = v } unless options.nil? options.each { |k, v| opt[k.to_sym] = v } unless options.nil?
@ -35,6 +35,7 @@ module Inspec
id: @profile_id, id: @profile_id,
backend: :mock, backend: :mock,
) )
@runner.add_tests([@path]) @runner.add_tests([@path])
@runner.rules.each do |id, rule| @runner.rules.each do |id, rule|
file = rule.instance_variable_get(:@__file) file = rule.instance_variable_get(:@__file)
@ -91,24 +92,13 @@ module Inspec
} }
@logger.info "Checking profile in #{@path}" @logger.info "Checking profile in #{@path}"
@logger.info 'Metadata OK.' if @metadata.valid?
if @params[:name].to_s.empty?
error.call('No profile name defined')
elsif !(@params[:name].to_s =~ %r{^\S+\/\S+$})
error.call('Profile name must be defined as: OWNER/ID')
end
warn.call('No version defined') if @params[:name].to_s.empty?
warn.call('No title defined') if @params[:name].to_s.empty?
warn.call('No maintainer defined') if @params[:name].to_s.empty?
warn.call('No supports defined') if @params[:name].empty?
@logger.info 'Metadata OK.' if no_warnings
no_warnings = true no_warnings = true
if @params[:name].empty? if @params[:rules].empty?
warn.call('No rules were found.') warn.call('No controls or tests were defined.')
else else
@logger.debug "Found #{@params[:name].length} rules." @logger.debug "Found #{@params[:rules].length} rules."
end end
# iterate over hash of groups # iterate over hash of groups

View file

@ -0,0 +1,7 @@
name 'name'
version '1.2.3'
maintainer 'bob'
title 'title'
copyright 'left'
summary 'nothing'
supports nil

View file

@ -4,11 +4,6 @@
require 'helper' require 'helper'
def load_profile(name)
pwd = File.dirname(__FILE__)
Inspec::Profile.from_path("#{pwd}/mock/profiles/#{name}")
end
describe Inspec::Profile do describe Inspec::Profile do
before { before {
# mock up the profile runner # mock up the profile runner
@ -23,6 +18,13 @@ describe Inspec::Profile do
end end
} }
let(:logger) { Minitest::Mock.new }
let(:home) { File.dirname(__FILE__) }
def load_profile(name, opts = {})
Inspec::Profile.from_path("#{home}/mock/profiles/#{name}", opts)
end
describe 'with empty profile' do describe 'with empty profile' do
let(:profile) { load_profile('empty') } let(:profile) { load_profile('empty') }
@ -46,4 +48,37 @@ describe Inspec::Profile do
profile.params[:rules].must_equal({}) profile.params[:rules].must_equal({})
end end
end end
describe 'when checking' do
describe 'an empty profile' do
let(:profile) { load_profile('empty', {logger: logger}) }
it 'prints loads of warnings' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/empty"]
logger.expect :error, nil, ['Missing profile name in metadata.rb']
logger.expect :error, nil, ['Missing profile version in metadata.rb']
logger.expect :warn, nil, ['Missing profile title in metadata.rb']
logger.expect :warn, nil, ['Missing profile summary in metadata.rb']
logger.expect :warn, nil, ['Missing profile maintainer in metadata.rb']
logger.expect :warn, nil, ['Missing profile copyright in metadata.rb']
logger.expect :warn, nil, ['No controls or tests were defined.']
profile.check
logger.verify
end
end
describe 'a complete metadata profile' do
let(:profile) { load_profile('complete-meta', {logger: logger}) }
it 'prints ok messages' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/complete-meta"]
logger.expect :info, nil, ['Metadata OK.']
logger.expect :warn, nil, ['No controls or tests were defined.']
profile.check
logger.verify
end
end
end
end end