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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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
  @puppet_adapter = PuppetAdapterFactory.create_adapter(puppet_config)

  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
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



65
66
67
68
69
70
71
# File 'lib/puppet-catalog-test/test_runner.rb', line 65

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



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet-catalog-test/test_runner.rb', line 82

def collect_puppet_nodes(filter)
  nodes = @puppet_adapter.nodes

  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



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

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

  node = @puppet_adapter.create_node(hostname, facts)

  @puppet_adapter.compile(node)
end

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet-catalog-test/test_runner.rb', line 51

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet-catalog-test/test_runner.rb', line 36

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/puppet-catalog-test/test_runner.rb', line 96

def run_tests!
  @out.puts "[INFO] Using puppet #{@puppet_adapter.version}"

  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
      if $DEBUG
        p error
        puts e.backtrace
      end

      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