Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit db0e668b237c6b81a87e4e8ce431e786045dd708 @calavera calavera committed Feb 28, 2012
Showing with 204 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +71 −0 README.md
  5. +2 −0 Rakefile
  6. +3 −0 Vagrantfile
  7. +1 −0 examples/cookbooks/foo/recipes/default.rb
  8. +3 −0 examples/dna.json
  9. +4 −0 examples/solo.rb
  10. +9 −0 examples/test/test_foo.rb
  11. +53 −0 lib/minitest/chef.rb
  12. +15 −0 minitest-chef-handler.gemspec
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'http://rubygems.org'
+
+# Specify your gem's dependencies in minitest-chef-handler.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+== minitest-chef-handler
+
+Copyright (c) 2012 David Calavera
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
+# Minitest Chef Handler
+
+Run minitest suites after your Chef recipes to check the status of your system.
+
+## Motivation
+
+Working at Engine Yard I have to maintain a quite complicated set of Chef recipes that we use to set up our customers' instances. I need to be sure that everytime someone modify those recipes, mostly myself, the provisioned services continue working as expected.
+
+There are other solutions that evaluate the configured node after the recipes
+are loaded without arriving to the converge phase, like [ChefSpec](https://github.com/acrmp/chefspec) or [rspec-chef](https://github.com/calavera/rspec-chef), but I needed something to write integration tests easily. I checked [chef-minitest](https://github.com/fujin/chef-minitest) but I'm still amazed by the ugly code that I have to write into the recipes to make it work.
+
+## Installation
+
+```
+$ gem install minitest-chef-handler
+```
+
+## Usage
+
+1. Add the report handler to your client.rb or solo.rb file:
+
+```ruby
+require 'minitest-chef-handler'
+
+report_handlers << MiniTest::Chef::Handler.new
+```
+
+2. Write your tests as normal MiniTest cases extending from MiniTest::Chef::TestCase:
+
+```ruby
+class TestNginx < MiniTest::Chef::TestUnit
+ def test_config_file_exist
+ assert File.exist?('/etc/nginx.conf')
+ end
+end
+```
+
+You still have access to Chef's `run_status`, `node` and `run_context` from your tests:
+
+```ruby
+class TestNginx < MiniTest::Chef::TestUnit
+ def test_config_file_exist
+ assert run_status.success?
+ end
+end
+```
+
+## Further configuration
+
+These are the options the handler accepts:
+
+* :path => where your test files are, './test/test_*.rb' by default
+* :filter => filter test names on pattern
+* :seed => set random seed
+* :verbose => show progress processing files.
+
+Example:
+
+```ruby
+handler = MiniTest::Chef::Handler.new({
+ :path => './cookbooks/test/*_test.rb',
+ :filter => 'foo',
+ :seed => srand,
+ :verbose => true})
+
+report_handlers << handler
+```
+
+## Copyright
+
+Copyright (c) 2012 David Calavera. See LICENSE for details.
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
+Vagrant::Config.run do |config|
+ config.vm.box = "lucid64"
+end
@@ -0,0 +1 @@
+file '/tmp/temporal_file'
@@ -0,0 +1,3 @@
+{
+ "run_list": [ "recipe[foo]" ]
+}
@@ -0,0 +1,4 @@
+require 'minitest-chef-handler'
+
+report_handlers << MiniTest::Chef::TestHandler.new
+cookbook_path File.expand_path('cookbooks', File.dirname(__FILE__))
@@ -0,0 +1,9 @@
+class FooTest < MiniTest::Chef::TestCase
+ def test_exist_file
+ assert File.exist?('/tmp/temporal_file')
+ end
+
+ def test_succeed
+ assert run_status.success?
+ end
+end
@@ -0,0 +1,53 @@
+require 'minitest/unit'
+
+module MiniTest
+ module Chef
+ class TestHandler < ::Chef::Handler
+ def initialize(options = {})
+ path = options.delete(:path) || './test/test_*.rb'
+ Dir.glob(path).each {|test_suite| require test_suite}
+
+ @options = options
+ end
+
+ def report
+ # do not run tests if chef failed
+ return if failed?
+
+ runner = Runner.new(run_status)
+ runner._run(miniunit_options)
+ end
+
+ private
+ def miniunit_options
+ options = []
+ options << ['-n', @options[:filter]] if @options[:filter]
+ options << "-v" if @options[:verbose]
+ options << ['-s', @options[:seed]] if @options[:seed]
+ options.flatten
+ end
+ end
+
+ class Runner < MiniTest::Unit
+ attr_reader :run_status
+
+ def initialize(run_status)
+ @run_status = run_status
+ super()
+ end
+ end
+
+ class TestCase < MiniTest::Unit::TestCase
+ attr_reader :run_status, :node, :run_context
+
+ def run(runner)
+ if runner.respond_to?(:run_status)
+ @run_status = runner.run_status
+ @node = @run_status.node
+ @run_context = @run_status.run_context
+ end
+ super(runner)
+ end
+ end
+ end
+end
@@ -0,0 +1,15 @@
+# -*- encoding: utf-8 -*-
+Gem::Specification.new do |gem|
+ gem.authors = ["David Calavera"]
+ gem.email = ["david.calavera@gmail.com"]
+ gem.description = %q{Run Minitest suites as Chef report handlers}
+ gem.summary = %q{Run Minitest suites as Chef report handlers}
+ gem.homepage = ""
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "minitest-chef-handler"
+ gem.require_paths = ["lib"]
+ gem.version = '0.1.0'
+end

0 comments on commit db0e668

Please sign in to comment.