bats-chef-handler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Joe Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ @TODO
@@ -0,0 +1,69 @@
1
+ class Chef
2
+ class Handler
3
+ class BatsHandler < Chef::Handler
4
+ include Chef::Mixin::ShellOut
5
+
6
+ def report
7
+ # do not run tests if chef failed
8
+ return if failed?
9
+
10
+ Chef::Log.debug "BatsHandler#report called"
11
+ Chef::Log.debug "found tests: #{test_file_paths}"
12
+
13
+ test_failures = []
14
+
15
+ Dir.glob(test_file_paths).each do |test_path|
16
+ Chef::Log.info "Running test: #{test_path}"
17
+ cmd = shell_out("bats #{test_path}", :live_stream => STDOUT)
18
+ if cmd.exitstatus == 0
19
+ Chef::Log.debug "#{test_path}: test passed"
20
+ else
21
+ Chef::Log.debug "#{test_path}: test failed"
22
+ test_failures << test_path
23
+ end
24
+ end
25
+
26
+ if test_failures.size > 0
27
+ ::Chef::Client.when_run_completes_successfully do |run_status|
28
+ error_msg = "BATS tests failed with #{test_failures.size} failures"
29
+ raise error_msg
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_file_paths
35
+ used_recipe_names.map do |recipe_name|
36
+ cookbook_name, recipe_short_name = ::Chef::Recipe.parse_recipe_name(recipe_name)
37
+ base_path = ::Chef::Config[:cookbook_path]
38
+
39
+ cookbook_paths = lookup_cookbook(base_path, cookbook_name)
40
+ Chef::Log.debug "cookbook_paths: #{cookbook_paths}"
41
+
42
+ # TODO: also support test-kitchen's default path somehow? eg: tests/integration/<test_suite_name>/bats/<test_name>.bats
43
+ cookbook_paths.map do |path|
44
+ file_test_pattern = "%s/tests/%s_test.bats" % [path, recipe_short_name]
45
+ dir_test_pattern = "%s/tests/%s/*.bats" % [path, recipe_short_name]
46
+
47
+ [file_test_pattern, dir_test_pattern]
48
+ end.flatten
49
+ end.flatten
50
+ end
51
+
52
+ def used_recipe_names
53
+ if recipes = run_status.node.run_state[:seen_recipes]
54
+ recipes.keys
55
+ else
56
+ # chef 11 - see http://docs.opscode.com/breaking_changes_chef_11.html#node-run-state-replaced
57
+ run_status.run_context.loaded_recipes
58
+ end
59
+ end
60
+
61
+ def lookup_cookbook(path, name)
62
+ path_expr = Array(path).join(',')
63
+
64
+ Dir.glob("{%s}/%s" % [path_expr, name])
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,7 @@
1
+ class Chef
2
+ class Handler
3
+ class BatsHandler < Chef::Handler
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bats-chef-handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joe Miller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: '10.14'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: '10.14'
30
+ description: Run BATS tests at the end of a chef run
31
+ email:
32
+ - joeym@joeym.net
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - LICENSE
38
+ - README.md
39
+ - lib/chef/handler/bats_handler_version.rb
40
+ - lib/chef/handler/bats_handler.rb
41
+ homepage: https://github.com/joemiller/bats-chef-handler
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.25
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: BATS tests handler
65
+ test_files: []