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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

  
  if puppet_config[:xml]
	 require 'puppet-catalog-test/junit_xml_reporter'
     @reporter = PuppetCatalogTest::JunitXmlReporter.new("puppet-catalog-test", "puppet_catalogs.xml")
  else
     @reporter = StdoutReporter.new(stdout_target)
  end

  @total_duration = nil

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

  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

  if verbose == 1
      Puppet::Util::Log.newdestination(:console)
      Puppet::Util::Log.level = :debug
  end

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

  module_path = module_paths.join(":")

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

  if hiera_config
    raise ArgumentError, "[ERROR] hiera_config  (#{hiera_config}) does not exist" if !FileTest.exist?(hiera_config)
    Puppet.settings[:hiera_config] = hiera_config
  end

  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



103
104
105
106
107
108
109
# File 'lib/puppet-catalog-test/test_runner.rb', line 103

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet-catalog-test/test_runner.rb', line 121

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



111
112
113
114
115
116
117
118
119
# File 'lib/puppet-catalog-test/test_runner.rb', line 111

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



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

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet-catalog-test/test_runner.rb', line 74

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



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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/puppet-catalog-test/test_runner.rb', line 136

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