Skip to content
Browse files

Switch to RSpec, add some specs

  • Loading branch information...
1 parent a2b8836 commit 890dd84ab27109a53675db429cfcfba9fbcc009d @bmarini committed
View
11 Rakefile
@@ -1,10 +1,9 @@
require "bundler/gem_tasks"
-require 'rake/testtask'
+require "rspec/core/rake_task"
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'spec'
- test.pattern = 'spec/**/*_spec.rb'
- test.verbose = true
+RSpec::Core::RakeTask.new do |t|
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
+ t.pattern = 'spec/**/*_spec.rb'
end
-task :default => :test
+task :default => :spec
View
3 health_inspector.gemspec
@@ -18,8 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_development_dependency "minitest"
- s.add_development_dependency "mocha"
+ s.add_development_dependency "rspec"
s.add_runtime_dependency "thor"
s.add_runtime_dependency "chef", "~> 10.14"
View
1 lib/health_inspector.rb
@@ -13,7 +13,6 @@
require "health_inspector/cli"
require 'chef/rest'
require 'chef/checksum_cache'
-require "json"
module HealthInspector
end
View
9 lib/health_inspector/checklists/base.rb
@@ -27,6 +27,14 @@ def initialize(context)
@context = context
end
+ # Subclasses should collect all items from the server and the local repo,
+ # and for each item pair, yield an object that contains a reference to
+ # the server item, and the local repo item. A reference can be nil if it does
+ # not exist in one of the locations.
+ def each_item
+ raise NotImplementedError, "You must implement this method in a subclass"
+ end
+
def run
banner "Inspecting #{self.class.title}"
@@ -75,6 +83,7 @@ def diff(original, other)
memo
end
end
+
end
def chef_rest
View
4 lib/health_inspector/checklists/cookbooks.rb
@@ -4,11 +4,11 @@ module Checklists
class Cookbooks < Base
add_check "local copy exists" do
- failure( "exists on chef server but not locally" ) if item.path.nil?
+ failure( "exists on server but not locally" ) if item.path.nil?
end
add_check "server copy exists" do
- failure( "exists locally but not on chef server" ) if item.server_version.nil?
+ failure( "exists locally but not on server" ) if item.server_version.nil?
end
add_check "versions" do
View
2 lib/health_inspector/checklists/data_bags.rb
@@ -24,7 +24,7 @@ def each_item
item = DataBag.new.tap do |data_bag|
data_bag.name = name
data_bag.exists_on_server = data_bags_on_server.include?(name)
- data_bag.exists_locally = data_bags_in_repo.include?(name)
+ data_bag.exists_locally = data_bags_in_repo.include?(name)
end
yield item
View
0 spec/chef-repo/data_bags/.gitkeep
No changes.
View
26 spec/cookbook_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe "HealthInspector::Checklists::Cookbooks" do
+ subject do
+ HealthInspector::Checklists::Cookbooks.new(health_inspector_context)
+ end
+
+ # :name, :path, :server_version, :local_version, :bad_files
+ let(:item) { HealthInspector::Checklists::Cookbooks::Cookbook }
+
+ it "should detect if a cookbook does not exist locally" do
+ obj = item.new("ruby", nil, "0.0.1", nil, [])
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists on server but not locally"
+ end
+
+ it "should detect if a cookbook does not exist on server" do
+ obj = item.new("ruby", "cookbooks/ruby", nil, "0.0.1", [])
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists locally but not on server"
+ end
+end
View
34 spec/data_bag_item_spec.rb
@@ -5,12 +5,36 @@
HealthInspector::Checklists::DataBagItems.new(health_inspector_context)
end
+ let(:item) { HealthInspector::Checklists::DataBagItems::DataBagItem }
+
it "should detect if a data bag item does not exist locally" do
- item = HealthInspector::Checklists::DataBagItems::DataBagItem.new(
- :name => "apps", :server => ["apps/app1"], :local => nil
- )
+ obj = item.new("apps", {"foo" => "bar"}, nil)
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists on server but not locally"
+ end
+
+ it "should detect if a data bag item does not exist on server" do
+ obj = item.new("apps", nil, {"foo" => "bar"})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists locally but not on server"
+ end
+
+ it "should detect if a data bag item is different" do
+ obj = item.new("apps", {"foo" => "bar"}, {"foo" => "baz"})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == {"foo" => {"server" => "bar", "local" => "baz"}}
+ end
+
+ it "should detect if a data bag item is the same" do
+ obj = item.new("apps", {"foo" => "bar"}, {"foo" => "bar"})
- failures = subject.run_checks(item)
- failures.first.include?("exists on server but not locally").must_equal true
+ failures = subject.run_checks(obj)
+ failures.should be_empty
end
end
View
25 spec/data_bag_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe "HealthInspector::Checklists::DataBags" do
+ subject do
+ HealthInspector::Checklists::DataBags.new(health_inspector_context)
+ end
+
+ let(:item) { HealthInspector::Checklists::DataBags::DataBag }
+
+ it "should detect if a data bag does not exist locally" do
+ obj = item.new("users", true, false)
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists on server but not locally"
+ end
+
+ it "should detect if a data bag does not exist on server" do
+ obj = item.new("users", false, true)
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists locally but not on server"
+ end
+end
View
41 spec/environment_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+describe "HealthInspector::Checklists::Environments" do
+ subject do
+ HealthInspector::Checklists::Environments.new(health_inspector_context)
+ end
+
+ let(:item) { HealthInspector::Checklists::Environments::Environment }
+
+ it "should detect if an environment does not exist locally" do
+ obj = item.new("production", {}, nil)
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists on server but not locally"
+ end
+
+ it "should detect if an environment does not exist on server" do
+ obj = item.new("production", nil, {})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists locally but not on server"
+ end
+
+ it "should detect if an environment is different" do
+ obj = item.new("production", {"foo" => "bar"}, {"foo" => "baz"})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == {"foo"=>{"server"=>"bar", "local"=>"baz"}}
+ end
+
+ it "should detect if an environment is the same" do
+ obj = item.new("production", {}, {})
+
+ failures = subject.run_checks(obj)
+ failures.should be_empty
+ end
+
+end
View
41 spec/role_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+describe "HealthInspector::Checklists::Roles" do
+ subject do
+ HealthInspector::Checklists::Roles.new(health_inspector_context)
+ end
+
+ let(:item) { HealthInspector::Checklists::Roles::Role }
+
+ it "should detect if an role does not exist locally" do
+ obj = item.new("app-server", {}, nil)
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists on server but not locally"
+ end
+
+ it "should detect if an role does not exist on server" do
+ obj = item.new("app-server", nil, {})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == "exists locally but not on server"
+ end
+
+ it "should detect if an role is different" do
+ obj = item.new("app-server", {"foo" => "bar"}, {"foo" => "baz"})
+
+ failures = subject.run_checks(obj)
+ failures.should_not be_empty
+ failures.first.should == {"foo"=>{"server"=>"bar", "local"=>"baz"}}
+ end
+
+ it "should detect if an role is the same" do
+ obj = item.new("app-server", {}, {})
+
+ failures = subject.run_checks(obj)
+ failures.should be_empty
+ end
+
+end
View
6 spec/spec_helper.rb
@@ -1,7 +1,5 @@
require 'rubygems'
require 'bundler/setup'
-require 'minitest/autorun'
-require 'mocha'
require 'health_inspector'
module HealthInspector::SpecHelpers
@@ -16,6 +14,6 @@ def health_inspector_context
end
end
-class MiniTest::Unit::TestCase
- include HealthInspector::SpecHelpers
+RSpec.configure do |c|
+ c.include HealthInspector::SpecHelpers
end

0 comments on commit 890dd84

Please sign in to comment.
Something went wrong with that request. Please try again.