Merge pull request #1026 from chef/ssd/attribute-fix-1

Pass attributes from command line into profile context
This commit is contained in:
Christoph Hartmann 2016-09-07 10:34:56 +02:00 committed by GitHub
commit bd64428458
4 changed files with 18 additions and 7 deletions

View file

@ -45,8 +45,8 @@ module Inspec
def_delegator :@source_reader, :metadata def_delegator :@source_reader, :metadata
# rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/AbcSize
def initialize(source_reader, options = nil) def initialize(source_reader, options = {})
@options = options || {} @options = options
@target = @options.delete(:target) @target = @options.delete(:target)
@logger = @options[:logger] || Logger.new(nil) @logger = @options[:logger] || Logger.new(nil)
@source_reader = source_reader @source_reader = source_reader
@ -57,7 +57,7 @@ module Inspec
@profile_id = @options[:id] @profile_id = @options[:id]
@backend = @options[:backend] || Inspec::Backend.create(options) @backend = @options[:backend] || Inspec::Backend.create(options)
Metadata.finalize(@source_reader.metadata, @profile_id) Metadata.finalize(@source_reader.metadata, @profile_id)
@runner_context = @options[:profile_context] || Inspec::ProfileContext.for_profile(self, @backend) @runner_context = @options[:profile_context] || Inspec::ProfileContext.for_profile(self, @backend, @options[:attributes])
end end
def name def name

View file

@ -12,8 +12,9 @@ require 'inspec/objects/attribute'
module Inspec module Inspec
class ProfileContext # rubocop:disable Metrics/ClassLength class ProfileContext # rubocop:disable Metrics/ClassLength
def self.for_profile(profile, backend) def self.for_profile(profile, backend, attributes)
new(profile.name, backend, { 'profile' => profile }) new(profile.name, backend, { 'profile' => profile,
'attributes' => attributes })
end end
attr_reader :attributes, :rules, :profile_id, :resource_registry attr_reader :attributes, :rules, :profile_id, :resource_registry

View file

@ -126,13 +126,16 @@ module Inspec
# @eturns [Inspec::ProfileContext] # @eturns [Inspec::ProfileContext]
# #
def add_target(target, _opts = []) def add_target(target, _opts = [])
profile = Inspec::Profile.for_target(target, backend: @backend, controls: @controls) profile = Inspec::Profile.for_target(target,
backend: @backend,
controls: @controls,
attributes: @conf[:attributes])
fail "Could not resolve #{target} to valid input." if profile.nil? fail "Could not resolve #{target} to valid input." if profile.nil?
@target_profiles << profile if supports_profile?(profile) @target_profiles << profile if supports_profile?(profile)
end end
# #
# This is used by inspec-shell and inspec-detect. This should # This is used by inspec-shell and inspec-detect. This should
# probably be cleaned up a bit. # probably be cleaned up a bit.
# #
# @params [Hash] Options # @params [Hash] Options

View file

@ -53,6 +53,13 @@ Summary: \e[32m0 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[0m
" "
end end
it "executes a profile and reads attributes" do
out = inspec("exec #{File.join(examples_path, 'profile-attribute')} --attrs #{File.join(examples_path, "profile-attribute.yml")}")
out.stderr.must_equal ''
out.exit_status.must_equal 0
out.stdout.force_encoding(Encoding::UTF_8).must_include "Summary: \e[32m2 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[0m"
end
it 'executes a specs-only profile' do it 'executes a specs-only profile' do
out = inspec('exec ' + File.join(profile_path, 'spec_only')) out = inspec('exec ' + File.join(profile_path, 'spec_only'))
out.stderr.must_equal '' out.stderr.must_equal ''