Module: Inspec::DSL
- Defined in:
- lib/inspec/dsl.rb
Overview
copyright: 2015, Dominik Richter license: All rights reserved author: Dominik Richter author: Christoph Hartmann
Class Method Summary collapse
-
.execute_rule(r, profile_id) ⇒ Object
Register a given rule with RSpec and let it run.
- .filter_included_controls(context, opts, &block) ⇒ Object
- .get_reference_profile(id, opts) ⇒ Object
- .load_profile_context(id, profile, opts) ⇒ Object
- .load_spec_files_for_profile(bind_context, opts, &block) ⇒ Object
-
.merge_rules(dst, src) ⇒ Object
merge two rules completely; all defined fields from src will be overwritten in dst.
- .rule_from_check(m, a, b) ⇒ Object
-
.set_rspec_ids(obj, id) ⇒ Object
Attach an ID attribute to the metadata of all examples TODO: remove this once IDs are in rspec-core.
Instance Method Summary collapse
- #include_controls(id, &block) ⇒ Object (also: #include_rules)
- #require_controls(id, &block) ⇒ Object (also: #require_rules)
Class Method Details
.execute_rule(r, profile_id) ⇒ Object
Register a given rule with RSpec and let it run. This happens after everything else is merged in.
37 38 39 40 41 42 43 44 45 |
# File 'lib/inspec/dsl.rb', line 37 def self.execute_rule(r, profile_id) checks = ::Inspec::Rule.prepare_checks(r) fid = InspecBaseRule.full_id(r, profile_id) checks.each do |m, a, b| # check if the resource is skippable and skipped cres = rule_from_check(m, a, b) set_rspec_ids(cres, fid) if m == 'describe' end end |
.filter_included_controls(context, opts, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/inspec/dsl.rb', line 84 def self.filter_included_controls(context, opts, &block) mock = Inspec::Backend.create({ backend: 'mock' }) include_ctx = Inspec::ProfileContext.new(opts[:profile_id], mock, opts[:conf]) include_ctx.load(block) if block_given? # remove all rules that were not registered context.rules.keys.each do |id| unless include_ctx.rules[id] context.rules[id] = nil end end end |
.get_reference_profile(id, opts) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/inspec/dsl.rb', line 96 def self.get_reference_profile(id, opts) profiles_path = opts['profiles_path'] || fail('You must supply a --profiles-path to inherit from other profiles.') abs_path = File.(profiles_path.to_s) unless File.directory? abs_path fail("Cannot find profiles path #{abs_path}") end id_path = File.join(abs_path, id) unless File.directory? id_path fail("Cannot find referenced profile #{id} in #{id_path}") end id_path end |
.load_profile_context(id, profile, opts) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/inspec/dsl.rb', line 112 def self.load_profile_context(id, profile, opts) ctx = Inspec::ProfileContext.new(id, opts[:backend], opts[:conf]) profile.libraries.each do |path, content| ctx.load(content.to_s, path, 1) ctx.reload_dsl end profile.tests.each do |path, content| ctx.load(content.to_s, path, 1) end ctx end |
.load_spec_files_for_profile(bind_context, opts, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/inspec/dsl.rb', line 65 def self.load_spec_files_for_profile(bind_context, opts, &block) # get all spec files target = get_reference_profile(opts[:profile_id], opts[:conf]) profile = Inspec::Profile.for_target(target, opts) context = load_profile_context(opts[:profile_id], profile, opts) # if we don't want all the rules, then just make 1 pass to get all rule_IDs # that we want to keep from the original filter_included_controls(context, opts, &block) if !opts[:include_all] # interpret the block and skip/modify as required context.load(block) if block_given? # finally register all combined rules context.rules.values.each do |control| bind_context.register_control(control) end end |
.merge_rules(dst, src) ⇒ Object
merge two rules completely; all defined fields from src will be overwritten in dst
49 50 51 |
# File 'lib/inspec/dsl.rb', line 49 def self.merge_rules(dst, src) InspecBaseRule.merge dst, src end |
.rule_from_check(m, a, b) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/inspec/dsl.rb', line 21 def self.rule_from_check(m, a, b) if a.is_a?(Array) && !a.empty? && a[0].respond_to?(:resource_skipped) && !a[0].resource_skipped.nil? ::Inspec::Rule.__send__(m, *a) do it a[0].resource_skipped end else # execute the method ::Inspec::Rule.__send__(m, *a, &b) end end |
.set_rspec_ids(obj, id) ⇒ Object
Attach an ID attribute to the metadata of all examples TODO: remove this once IDs are in rspec-core
56 57 58 59 60 61 62 63 |
# File 'lib/inspec/dsl.rb', line 56 def self.set_rspec_ids(obj, id) obj.examples.each {|ex| ex.[:id] = id } obj.children.each {|c| set_rspec_ids(c, id) } end |
Instance Method Details
#include_controls(id, &block) ⇒ Object Also known as: include_rules
13 14 15 16 |
# File 'lib/inspec/dsl.rb', line 13 def include_controls(id, &block) opts = { profile_id: id, include_all: true, backend: @backend, conf: @conf } ::Inspec::DSL.load_spec_files_for_profile(self, opts, &block) end |
#require_controls(id, &block) ⇒ Object Also known as: require_rules
8 9 10 11 |
# File 'lib/inspec/dsl.rb', line 8 def require_controls(id, &block) opts = { profile_id: id, include_all: false, backend: @backend, conf: @conf } ::Inspec::DSL.load_spec_files_for_profile(self, opts, &block) end |