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

Instance Method Summary collapse

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.



35
36
37
38
39
40
41
42
43
# File 'lib/inspec/dsl.rb', line 35

def self.execute_rule(r, profile_id)
  checks = r.instance_variable_get(:@checks)
  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

.get_spec_files_for_profile(id) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/inspec/dsl.rb', line 115

def self.get_spec_files_for_profile(id)
  base_path = '/etc/inspec/tests'
  path = File.join(base_path, id)
  # find all files to be included
  files = []
  if File.directory? path
    # include all library paths, if they exist
    libdir = File.join(path, 'lib')
    if File.directory? libdir and !$LOAD_PATH.include?(libdir)
      $LOAD_PATH.unshift(libdir)
    end
    files = Dir[File.join(path, 'spec', '*_spec.rb')]
  end
  files
end

.load_spec_file_for_profile(profile_id, file, rule_registry, only_ifs) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/inspec/dsl.rb', line 63

def self.load_spec_file_for_profile(profile_id, file, rule_registry, only_ifs)
  raw = File.read(file)
  # TODO: error-handling

  ctx = Inspec::ProfileContext.new(profile_id, rule_registry, only_ifs)
  ctx.instance_eval(raw, file, 1)
end

.load_spec_files_for_profile(bind_context, profile_id, include_all, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/inspec/dsl.rb', line 71

def self.load_spec_files_for_profile(bind_context, profile_id, include_all, &block)
  # get all spec files
  files = get_spec_files_for_profile profile_id
  # load all rules from spec files
  rule_registry = {}
  # TODO: handling of only_ifs
  only_ifs = []
  files.each do |file|
    load_spec_file_for_profile(profile_id, file, rule_registry, only_ifs)
  end

  # interpret the block and create a set of rules from it
  block_registry = {}
  if block_given?
    ctx = Inspec::ProfileContext.new(profile_id, block_registry, only_ifs)
    ctx.instance_eval(&block)
  end

  # if all rules are not included, select only the ones
  # that were defined in the block
  unless include_all
    remove = rule_registry.keys - block_registry.keys
    remove.each { |key| rule_registry.delete(key) }
  end

  # merge the rules in the block_registry (adjustments) with
  # the rules in the rule_registry (included)
  block_registry.each do |id, r|
    org = rule_registry[id]
    if org.nil?
      # TODO: print error because we write alter a rule that doesn't exist
    elsif r.nil?
      rule_registry.delete(id)
    else
      merge_rules(org, r)
    end
  end

  # finally register all combined rules
  rule_registry.each do |_id, rule|
    bind_context.__register_rule rule
  end
end

.merge_rules(dst, src) ⇒ Object

merge two rules completely; all defined fields from src will be overwritten in dst



47
48
49
# File 'lib/inspec/dsl.rb', line 47

def self.merge_rules(dst, src)
  InspecBaseRule.merge dst, src
end

.rule_from_check(m, a, b) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inspec/dsl.rb', line 19

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



54
55
56
57
58
59
60
61
# File 'lib/inspec/dsl.rb', line 54

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



12
13
14
# File 'lib/inspec/dsl.rb', line 12

def include_controls(id, &block)
  ::Inspec::DSL.load_spec_files_for_profile self, id, true, &block
end

#require_controls(id, &block) ⇒ Object Also known as: require_rules



8
9
10
# File 'lib/inspec/dsl.rb', line 8

def require_controls(id, &block)
  ::Inspec::DSL.load_spec_files_for_profile self, id, false, &block
end