puppet-catalog-test 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f53f78fc8023e2f26117a42efe335a2a28d7001
4
+ data.tar.gz: e7e7f8458131b14e1ce871a678d5c599a56e6edd
5
+ SHA512:
6
+ metadata.gz: bb3d70830479daf9a712bb61587bd31ff264e4931777ecbfa95341932d63622abcf0dc216e0564ba31ea88f78a10078f6c78b60d1d186f532ebf86b7bc2867a3
7
+ data.tar.gz: 06c3830c8c6711c7602b80b069f4d866c1e9e8539635142d2b0754d40468733b18517a5043d2c32194c0d375af08bab3f1c804224cb050082619e09b389e89af
data/README.md CHANGED
@@ -8,6 +8,10 @@ puppet-catalog-test is a tool for capturing and testing warnings and errors the
8
8
 
9
9
  For a more detailed insight into the puppet-compiler, you can take a look at <http://www.masterzen.fr/2012/03/17/puppet-internals-the-compiler/>.
10
10
 
11
+ ## Supported Ruby + Puppet Versions
12
+
13
+ Supported versions of Ruby <> Puppet combinations are listed in the [support matrix](SUPPORTED_VERSIONS.md). Combinations not listed might work, but aren't tested yet.
14
+
11
15
  ## Installation
12
16
 
13
17
  gem install puppet-catalog-test
@@ -22,6 +26,7 @@ USAGE: puppet-catalog-test [options]
22
26
  -i, --include-pattern PATTERN Include only test cases that match pattern
23
27
  -e, --exclude-pattern PATTERN Exclude test cases that match pattern
24
28
  -s, --scenario FILE Scenario definition to use
29
+ -f, --fact KEY=VALUE Add custom fact
25
30
  -v, --verbose Verbose
26
31
  -x, --xml Use xml report
27
32
  -h, --help Show this message
@@ -75,6 +80,8 @@ namespace :catalog do
75
80
 
76
81
  t.include_pattern = ENV["include"]
77
82
  t.exclude_pattern = ENV["exclude"]
83
+
84
+ t.verbose = true
78
85
  end
79
86
  end
80
87
  ```
@@ -91,6 +98,8 @@ In the case above no facts weren't defined so puppet-catalog-test falls back to
91
98
  }
92
99
  ```
93
100
 
101
+ **NOTE:** Working examples are bundled within [test/cases](test/cases).
102
+
94
103
  ### Testing all catalogs with custom facts
95
104
 
96
105
  It is also possible to define a custom set of facts. In this case the fallback facts listed in previous example won't be used.
@@ -198,6 +207,8 @@ namespace :catalog do
198
207
  PuppetCatalogTest::RakeTask.new(:all) do |t|
199
208
  t.module_paths = [File.join("modules")]
200
209
  t.manifest_path = File.join("scripts", "site.pp")
210
+
211
+ # crucial option for hiera integration
201
212
  t.config_dir = File.join("data") # expects hiera.yaml to be included in directory
202
213
 
203
214
  t.reporter = PuppetCatalogTest::JunitXmlReporter.new("puppet-vagrant-playground", "reports/puppet_catalog.xml")
data/Rakefile CHANGED
@@ -14,4 +14,56 @@ Rake::TestTask.new do |t|
14
14
  t.verbose = true
15
15
  end
16
16
 
17
- task :default => :test
17
+ task :test_integration do
18
+ base_dir = Dir.pwd
19
+ all_tests_green = true
20
+
21
+ Dir["test/**/Rakefile"].each do |rf|
22
+ supposed_to_fail = rf.include?("failing")
23
+ Dir.chdir rf.split("/")[0..-2].join("/")
24
+
25
+ ["catalog:scenarios", "catalog:all"].each do |tc|
26
+ tc_name = "#{rf} / #{tc}"
27
+ puts " * Running scenario: #{tc_name} ]"
28
+
29
+ exit_code = -1
30
+ captured_output = ""
31
+
32
+ if FileTest.exist?("hiera.yaml.erb")
33
+ template = ERB.new(File.read("hiera.yaml.erb"))
34
+ working_directory = File.join(File.dirname(__FILE__), File.dirname(rf))
35
+
36
+ File.open("hiera.yaml", "w") do |fp|
37
+ fp.puts template.result(binding)
38
+ end
39
+ end
40
+
41
+ IO.popen("bundle exec rake #{tc}") do |io|
42
+ while (line = io.gets)
43
+ captured_output << line
44
+ end
45
+
46
+ io.close
47
+ exit_code = $?
48
+ end
49
+
50
+ if (supposed_to_fail && exit_code != 0) || (!supposed_to_fail && exit_code == 0)
51
+ puts " WORKED (supposed_to_fail = #{supposed_to_fail})"
52
+ else
53
+ all_tests_green = false
54
+ puts "\tScenario: #{tc_name} FAILED (supposed_to_fail = #{supposed_to_fail})"
55
+ puts ">>>>>>>>>>>>>"
56
+ puts captured_output
57
+ puts "<<<<<<<<<<<<<"
58
+ end
59
+ end
60
+
61
+ Dir.chdir base_dir
62
+ end
63
+
64
+ if !all_tests_green
65
+ fail
66
+ end
67
+ end
68
+
69
+ task :default => [:test, :test_integration]
@@ -7,6 +7,7 @@ require "puppet-catalog-test"
7
7
  require "optparse"
8
8
 
9
9
  options = {}
10
+ options[:custom_facts] = {}
10
11
 
11
12
  opt_parser = OptionParser.new do |opts|
12
13
  opts.banner = "USAGE: #{File.basename($0)} [options]"
@@ -35,8 +36,14 @@ opt_parser = OptionParser.new do |opts|
35
36
  options[:scenario] = arg
36
37
  end
37
38
 
39
+ opts.on("-f", "--fact KEY=VALUE", "Add custom fact") do |arg|
40
+ k = arg.split("=").first
41
+ v = arg.split("=").last
42
+ options[:custom_facts][k] = v
43
+ end
44
+
38
45
  opts.on("-v", "--verbose", "Verbose") do |arg|
39
- options[:verbose] = 1
46
+ options[:verbose] = true
40
47
  end
41
48
 
42
49
  opts.on("-x", "--xml", "Use xml report") do |arg|
@@ -70,7 +77,7 @@ filter.exclude_pattern = options[:exclude_pattern] if options.has_key?(:exclude_
70
77
  if options[:scenario]
71
78
  pct.load_scenario_yaml(options[:scenario], filter)
72
79
  else
73
- pct.load_all(filter)
80
+ pct.load_all(filter, options[:custom_facts])
74
81
  end
75
82
 
76
83
  pct.run_tests!
@@ -0,0 +1,49 @@
1
+ require "puppet"
2
+
3
+ module PuppetCatalogTest
4
+ class BasePuppetAdapter
5
+ def initialize(config)
6
+ manifest_path = config[:manifest_path]
7
+ module_paths = config[:module_paths]
8
+ config_dir = config[:config_dir]
9
+ hiera_config = config[:hiera_config]
10
+ verbose = config[:verbose]
11
+
12
+ raise ArgumentError, "[ERROR] manifest_path must be specified" if !manifest_path
13
+ raise ArgumentError, "[ERROR] manifest_path (#{manifest_path}) does not exist" if !FileTest.exist?(manifest_path)
14
+
15
+ raise ArgumentError, "[ERROR] module_path must be specified" if !module_paths
16
+ module_paths.each do |mp|
17
+ raise ArgumentError, "[ERROR] module_path (#{mp}) does not exist" if !FileTest.directory?(mp)
18
+ end
19
+
20
+ if config_dir
21
+ Puppet.settings.handlearg("--confdir", config_dir)
22
+ end
23
+
24
+ if verbose
25
+ Puppet::Util::Log.newdestination(:console)
26
+ Puppet::Util::Log.level = :debug
27
+ end
28
+
29
+ Puppet.settings.handlearg("--config", ".")
30
+ Puppet.settings.handlearg("--manifest", manifest_path)
31
+
32
+ module_path = module_paths.join(":")
33
+
34
+ Puppet.settings.handlearg("--modulepath", module_path)
35
+
36
+ if hiera_config
37
+ raise ArgumentError, "[ERROR] hiera_config (#{hiera_config}) does not exist" if !FileTest.exist?(hiera_config)
38
+ Puppet.settings[:hiera_config] = hiera_config
39
+ end
40
+ end
41
+
42
+ def parser; end
43
+ def compile(node); end
44
+ def create_node(hostname, facts); end
45
+ def version; end
46
+ def prepare; end
47
+ def cleanup; end
48
+ end
49
+ end
@@ -0,0 +1,51 @@
1
+ require "puppet"
2
+
3
+ module PuppetCatalogTest
4
+ class Puppet3xAdapter < BasePuppetAdapter
5
+ def initialize(config)
6
+ super(config)
7
+
8
+ require 'puppet/test/test_helper'
9
+
10
+ # works 3.7.x
11
+ if version.start_with?("3.7.")
12
+ Puppet::Test::TestHelper.initialize
13
+ end
14
+
15
+ Puppet::Node::Environment.new.modules_by_path.each do |_, mod|
16
+ mod.entries.each do |entry|
17
+ ldir = entry.plugin_directory
18
+ $LOAD_PATH << ldir unless $LOAD_PATH.include?(ldir)
19
+ end
20
+ end
21
+
22
+ Puppet.parse_config
23
+ end
24
+
25
+ def nodes
26
+ parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new)
27
+ parser.known_resource_types.nodes.keys
28
+ end
29
+
30
+ def compile(node)
31
+ begin
32
+ Puppet::Test::TestHelper.before_each_test
33
+ Puppet::Parser::Compiler.compile(node)
34
+ rescue => e
35
+ raise e
36
+ ensure
37
+ Puppet::Test::TestHelper.after_each_test
38
+ end
39
+ end
40
+
41
+ def create_node(hostname, facts)
42
+ node = Puppet::Node.new(hostname)
43
+ node.merge(facts)
44
+ node
45
+ end
46
+
47
+ def version
48
+ Puppet::PUPPETVERSION
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,45 @@
1
+ require "puppet"
2
+
3
+ module PuppetCatalogTest
4
+ class Puppet4xAdapter < BasePuppetAdapter
5
+ def initialize(config)
6
+ super(config)
7
+
8
+ @env = Puppet.lookup(:current_environment).
9
+ override_with(:manifest => config[:manifest_path]).
10
+ override_with(:modulepath => config[:module_paths])
11
+
12
+ @env.each_plugin_directory do |dir|
13
+ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
14
+ end
15
+
16
+ require 'puppet/test/test_helper'
17
+
18
+ Puppet::Test::TestHelper.initialize
19
+ Puppet::Test::TestHelper.before_all_tests
20
+ end
21
+
22
+ def version
23
+ Puppet.version
24
+ end
25
+
26
+ def nodes
27
+ @env.known_resource_types.nodes.keys
28
+ end
29
+
30
+ def create_node(hostname, facts)
31
+ Puppet::Node.new(hostname, :facts => Puppet::Node::Facts.new("facts", facts))
32
+ end
33
+
34
+ def compile(node)
35
+ begin
36
+ Puppet::Test::TestHelper.before_each_test
37
+ Puppet::Parser::Compiler.compile(node)
38
+ rescue => e
39
+ raise e
40
+ ensure
41
+ Puppet::Test::TestHelper.after_each_test
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ require "puppet/version"
2
+ require "puppet-catalog-test/puppet_adapter/base_puppet_adapter"
3
+
4
+ require "puppet-catalog-test/puppet_adapter/puppet_3x_adapter"
5
+ require "puppet-catalog-test/puppet_adapter/puppet_4x_adapter"
6
+
7
+ module PuppetCatalogTest
8
+ class PuppetAdapterFactory
9
+ def self.create_adapter(config)
10
+ if Puppet.version.start_with?("3.")
11
+ return Puppet3xAdapter.new(config)
12
+ elsif Puppet.version.start_with?("4.")
13
+ return Puppet4xAdapter.new(config)
14
+ end
15
+
16
+ raise RuntimeException, "Unsupported Puppet version detected (#{Puppet.version})"
17
+ end
18
+ end
19
+ end
@@ -13,6 +13,7 @@ module PuppetCatalogTest
13
13
  attr_accessor :exclude_pattern
14
14
  attr_accessor :facts
15
15
  attr_accessor :reporter
16
+ attr_accessor :verbose
16
17
 
17
18
  def initialize(name, &task_block)
18
19
  desc "Compile all puppet catalogs" unless ::Rake.application.last_comment
@@ -27,9 +28,14 @@ module PuppetCatalogTest
27
28
  puppet_config = {
28
29
  :manifest_path => @manifest_path,
29
30
  :module_paths => @module_paths,
30
- :config_dir => @config_dir
31
+ :config_dir => @config_dir,
32
+ :verbose => @verbose
31
33
  }
32
34
 
35
+ if @config_dir
36
+ puppet_config[:hiera_config] = File.join(@config_dir, "hiera.yaml")
37
+ end
38
+
33
39
  pct = TestRunner.new(puppet_config)
34
40
 
35
41
  @filter = Filter.new
@@ -1,9 +1,9 @@
1
1
  require "yaml"
2
- require "puppet"
3
2
  require "parallel"
4
3
 
5
4
  require "puppet-catalog-test/test_case"
6
5
  require "puppet-catalog-test/stdout_reporter"
6
+ require "puppet-catalog-test/puppet_adapter_factory"
7
7
 
8
8
  module PuppetCatalogTest
9
9
  class TestRunner
@@ -21,6 +21,7 @@ module PuppetCatalogTest
21
21
  @test_cases = []
22
22
  @exit_on_fail = true
23
23
  @out = stdout_target
24
+ @puppet_adapter = PuppetAdapterFactory.create_adapter(puppet_config)
24
25
 
25
26
  if puppet_config[:xml]
26
27
  require 'puppet-catalog-test/junit_xml_reporter'
@@ -30,44 +31,6 @@ module PuppetCatalogTest
30
31
  end
31
32
 
32
33
  @total_duration = nil
33
-
34
- manifest_path = puppet_config[:manifest_path]
35
- module_paths = puppet_config[:module_paths]
36
- config_dir = puppet_config[:config_dir]
37
- hiera_config = puppet_config[:hiera_config]
38
- verbose = puppet_config[:verbose]
39
-
40
- raise ArgumentError, "[ERROR] manifest_path must be specified" if !manifest_path
41
- raise ArgumentError, "[ERROR] manifest_path (#{manifest_path}) does not exist" if !FileTest.exist?(manifest_path)
42
-
43
- raise ArgumentError, "[ERROR] module_path must be specified" if !module_paths
44
- module_paths.each do |mp|
45
- raise ArgumentError, "[ERROR] module_path (#{mp}) does not exist" if !FileTest.directory?(mp)
46
- end
47
-
48
- if config_dir
49
- Puppet.settings.handlearg("--confdir", config_dir)
50
- end
51
-
52
- if verbose == 1
53
- Puppet::Util::Log.newdestination(:console)
54
- Puppet::Util::Log.level = :debug
55
- end
56
-
57
- Puppet.settings.handlearg("--config", ".")
58
- Puppet.settings.handlearg("--config", ".")
59
- Puppet.settings.handlearg("--manifest", manifest_path)
60
-
61
- module_path = module_paths.join(":")
62
-
63
- Puppet.settings.handlearg("--modulepath", module_path)
64
-
65
- if hiera_config
66
- raise ArgumentError, "[ERROR] hiera_config (#{hiera_config}) does not exist" if !FileTest.exist?(hiera_config)
67
- Puppet.settings[:hiera_config] = hiera_config
68
- end
69
-
70
- Puppet.parse_config
71
34
  end
72
35
 
73
36
  def load_scenario_yaml(yaml_file, filter = nil)
@@ -111,15 +74,13 @@ module PuppetCatalogTest
111
74
  hostname = node_fqdn.split('.').first
112
75
  facts['hostname'] = hostname
113
76
 
114
- node = Puppet::Node.new(hostname)
115
- node.merge(facts)
77
+ node = @puppet_adapter.create_node(hostname, facts)
116
78
 
117
- Puppet::Parser::Compiler.compile(node)
79
+ @puppet_adapter.compile(node)
118
80
  end
119
81
 
120
82
  def collect_puppet_nodes(filter)
121
- parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new)
122
- nodes = parser.environment.known_resource_types.nodes.keys
83
+ nodes = @puppet_adapter.nodes
123
84
 
124
85
  if filter.exclude_pattern
125
86
  nodes.delete_if { |node| node.match(filter.exclude_pattern) }
@@ -133,7 +94,7 @@ module PuppetCatalogTest
133
94
  end
134
95
 
135
96
  def run_tests!
136
- @out.puts "[INFO] Using puppet #{Puppet::PUPPETVERSION}"
97
+ @out.puts "[INFO] Using puppet #{@puppet_adapter.version}"
137
98
 
138
99
  run_start = Time.now
139
100
  proc_count = Parallel.processor_count
@@ -153,7 +114,11 @@ module PuppetCatalogTest
153
114
  @reporter.report_passed_test_case(tc)
154
115
  end
155
116
  rescue => error
156
- p error if $DEBUG
117
+ if $DEBUG
118
+ p error
119
+ puts e.backtrace
120
+ end
121
+
157
122
  tc.duration = Time.now - tc_start_time
158
123
  tc.error = error.message
159
124
  tc.passed = false
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'puppet-catalog-test'
3
- s.version = '0.3.2'
3
+ s.version = '0.4.0'
4
4
  s.homepage = 'https://github.com/invadersmustdie/puppet-catalog-test/'
5
5
  s.summary = 'Test all your puppet catalogs for compiler warnings and errors'
6
6
  s.description = 'Test all your puppet catalogs for compiler warnings and errors.'
@@ -23,4 +23,5 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.authors = ['Rene Lengwinat']
25
25
  s.email = 'rene.lengwinat@googlemail.com'
26
+ s.license = 'MIT'
26
27
  end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-catalog-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rene Lengwinat
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-11-27 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: puppet
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: parallel
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: builder
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: Test all your puppet catalogs for compiler warnings and errors.
@@ -66,40 +59,44 @@ executables:
66
59
  extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
- - bin/puppet-catalog-test
70
62
  - LICENSE
71
- - Rakefile
72
63
  - README.md
73
- - puppet-catalog-test.gemspec
64
+ - Rakefile
65
+ - bin/puppet-catalog-test
66
+ - lib/puppet-catalog-test.rb
74
67
  - lib/puppet-catalog-test/filter.rb
75
68
  - lib/puppet-catalog-test/junit_xml_reporter.rb
69
+ - lib/puppet-catalog-test/puppet_adapter/base_puppet_adapter.rb
70
+ - lib/puppet-catalog-test/puppet_adapter/puppet_3x_adapter.rb
71
+ - lib/puppet-catalog-test/puppet_adapter/puppet_4x_adapter.rb
72
+ - lib/puppet-catalog-test/puppet_adapter_factory.rb
76
73
  - lib/puppet-catalog-test/rake_task.rb
77
74
  - lib/puppet-catalog-test/stdout_reporter.rb
78
75
  - lib/puppet-catalog-test/test_case.rb
79
76
  - lib/puppet-catalog-test/test_runner.rb
80
- - lib/puppet-catalog-test.rb
77
+ - puppet-catalog-test.gemspec
81
78
  homepage: https://github.com/invadersmustdie/puppet-catalog-test/
82
- licenses: []
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
83
82
  post_install_message:
84
83
  rdoc_options: []
85
84
  require_paths:
86
85
  - lib
87
86
  required_ruby_version: !ruby/object:Gem::Requirement
88
- none: false
89
87
  requirements:
90
- - - ! '>='
88
+ - - ">="
91
89
  - !ruby/object:Gem::Version
92
90
  version: '0'
93
91
  required_rubygems_version: !ruby/object:Gem::Requirement
94
- none: false
95
92
  requirements:
96
- - - ! '>='
93
+ - - ">="
97
94
  - !ruby/object:Gem::Version
98
95
  version: '0'
99
96
  requirements: []
100
97
  rubyforge_project:
101
- rubygems_version: 1.8.23.2
98
+ rubygems_version: 2.4.5
102
99
  signing_key:
103
- specification_version: 3
100
+ specification_version: 4
104
101
  summary: Test all your puppet catalogs for compiler warnings and errors
105
102
  test_files: []