Class: PuppetCatalogTest::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-catalog-test/test_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puppet_config, stdout_target = $stdout) ⇒ TestRunner

Returns a new instance of TestRunner.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet-catalog-test/test_runner.rb', line 16

def initialize(puppet_config, stdout_target = $stdout)
  if !puppet_config
    raise ArgumentError, "No puppet_config hash supplied"
  end

  @test_cases = []
  @exit_on_fail = true
  @out = stdout_target

  @reporter = StdoutReporter.new(stdout_target)

  @total_duration = nil

  manifest_path = puppet_config[:manifest_path]
  module_paths = puppet_config[:module_paths]
  config_dir = puppet_config[:config_dir]

  raise ArgumentError, "[ERROR] manifest_path must be specified" if !manifest_path
  raise ArgumentError, "[ERROR] manifest_path (#{manifest_path}) does not exist" if !FileTest.exist?(manifest_path)

  raise ArgumentError, "[ERROR] module_path must be specified" if !module_paths
  module_paths.each do |mp|
    raise ArgumentError, "[ERROR] module_path (#{mp}) does not exist" if !FileTest.directory?(mp)
  end

  if config_dir
    Puppet.settings.handlearg("--confdir", config_dir)
  end

  Puppet.settings.handlearg("--config", ".")
  Puppet.settings.handlearg("--manifest", manifest_path)

  module_path = module_paths.join(":")

  Puppet.settings.handlearg("--modulepath", module_path)

  Puppet.parse_config
end

Instance Attribute Details

#exit_on_failObject

Returns the value of attribute exit_on_fail.



10
11
12
# File 'lib/puppet-catalog-test/test_runner.rb', line 10

def exit_on_fail
  @exit_on_fail
end

#reporterObject

Returns the value of attribute reporter.



11
12
13
# File 'lib/puppet-catalog-test/test_runner.rb', line 11

def reporter
  @reporter
end

#test_casesObject (readonly)

Returns the value of attribute test_cases.



13
14
15
# File 'lib/puppet-catalog-test/test_runner.rb', line 13

def test_cases
  @test_cases
end

#total_durationObject (readonly)

Returns the value of attribute total_duration.



14
15
16
# File 'lib/puppet-catalog-test/test_runner.rb', line 14

def total_duration
  @total_duration
end

Instance Method Details

#add_test_case(tc_name, facts) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/puppet-catalog-test/test_runner.rb', line 84

def add_test_case(tc_name, facts)
  tc = TestCase.new
  tc.name = tc_name
  tc.facts = facts

  @test_cases << tc
end

#collect_puppet_nodes(filter) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puppet-catalog-test/test_runner.rb', line 102

def collect_puppet_nodes(filter)
  parser = Puppet::Parser::Parser.new("environment")
  nodes = parser.environment.known_resource_types.nodes.keys

  if filter.exclude_pattern
    nodes.delete_if { |node| node.match(filter.exclude_pattern) }
  end

  if filter.include_pattern
    nodes.delete_if { |node| !node.match(filter.include_pattern) }
  end

  nodes
end

#compile_catalog(node_fqdn, facts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/puppet-catalog-test/test_runner.rb', line 92

def compile_catalog(node_fqdn, facts = {})
  hostname = node_fqdn.split('.').first
  facts['hostname'] = hostname

  node = Puppet::Node.new(hostname)
  node.merge(facts)

  Puppet::Parser::Compiler.compile(node)
end

#load_all(filter = Filter.new, facts = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet-catalog-test/test_runner.rb', line 70

def load_all(filter = Filter.new, facts = {})
  nodes = collect_puppet_nodes(filter)

  nodes.each do |n|
    node_facts = facts.dup

    if !node_facts.has_key?('fqdn')
      node_facts['fqdn'] = n
    end

    add_test_case(n, node_facts)
  end
end

#load_scenario_yaml(yaml_file, filter = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet-catalog-test/test_runner.rb', line 55

def load_scenario_yaml(yaml_file, filter = nil)
  scenarios = YAML.load_file(yaml_file)

  scenarios.each do |tc_name, facts|
    next if tc_name =~ /^__/

    if filter
      next if filter.exclude_pattern && tc_name.match(filter.exclude_pattern)
      next if filter.include_pattern && !tc_name.match(filter.include_pattern)
    end

    add_test_case(tc_name, facts)
  end
end

#run_tests!Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/puppet-catalog-test/test_runner.rb', line 117

def run_tests!
  @out.puts "[INFO] Using puppet #{Puppet::PUPPETVERSION}"

  run_start = Time.now
  proc_count = Parallel.processor_count

  processed_test_cases = Parallel.map(@test_cases, :in_processes => proc_count) do |tc|
    begin
      tc_start_time = Time.now

      if tc.facts['fqdn'].nil?
        raise "fact 'fqdn' must be defined"
      else
        compile_catalog(tc.facts['fqdn'], tc.facts)
        tc.duration = Time.now - tc_start_time

        tc.passed = true

        @reporter.report_passed_test_case(tc)
      end
    rescue => error
      p error if $DEBUG
      tc.duration = Time.now - tc_start_time
      tc.error = error.message
      tc.passed = false

      @reporter.report_failed_test_case(tc)
    end

    tc
  end

  @test_cases = processed_test_cases

  @total_duration = Time.now - run_start

  @reporter.summarize(self)

  if test_cases.any? { |tc| tc.passed == false }
    exit 1 if @exit_on_fail
    return false
  end

  true
end