Permalink
Browse files

add tests and refactor

  • Loading branch information...
1 parent fa6a732 commit 805af60994a1d9de0eac6b74276f11707c62eaf4 @smith smith committed Mar 26, 2012
Showing with 94 additions and 10 deletions.
  1. +2 −0 .rspec
  2. +7 −1 Gemfile
  3. +9 −0 Guardfile
  4. +7 −0 Rakefile
  5. +21 −9 lib/capistrano/chef.rb
  6. +37 −0 spec/capistrano/chef_spec.rb
  7. +11 −0 spec/spec_helper.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
8 Gemfile
@@ -1,4 +1,10 @@
source "http://rubygems.org"
-# Specify your gem's dependencies in capistrano-chef.gemspec
+group :test do
+ gem 'rake'
+ gem 'rspec'
+ gem 'capistrano-spec'
+ gem 'guard-rspec'
+end
+
gemspec
View
9 Guardfile
@@ -0,0 +1,9 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
+
View
7 Rakefile
@@ -1,2 +1,9 @@
require 'bundler'
+require 'rspec/core/rake_task'
Bundler::GemHelper.install_tasks
+
+desc 'Default: run specs.'
+task :default => :spec
+
+desc 'Run specs'
+RSpec::Core::RakeTask.new
View
30 lib/capistrano/chef.rb
@@ -1,17 +1,29 @@
+require 'capistrano'
require 'chef/knife'
require 'chef/search/query'
-Capistrano::Configuration.instance.load do
- Chef::Knife.new.configure_chef
-
- # Define a role for capistrano, but instead of a list of addresses, use a chef
- # query to search nodes.
- def chef_role(name, query = "*:*", options = {})
+module Capistrano::Chef
+ # Do a search on the Chef server and return an attary of the requested
+ # matching attributes
+ def self.search_chef_nodes(query = '*:*', options = {})
# TODO: This can only get a node's top-level attributes. Make it get nested
# ones.
attr = options.delete(:attribute) || :ipaddress
- nodes = Chef::Search::Query.new.search(:node, query)[0].map {|n| n[attr] }
- role name, *nodes, options
- nodes
+ Chef::Search::Query.new.search(:node, query)[0].map {|n| n[attr] }
+ end
+
+ # Load into Capistrano
+ def self.load_into(configuration)
+ Chef::Knife.new.configure_chef
+ configuration.set :capistrano_chef, self
+ configuration.load do
+ def chef_role(name, query = '*:*', options = {})
+ role name, *capistrano_chef.search_chef_nodes(query), options
+ end
+ end
end
end
+
+if Capistrano::Configuration.instance
+ Capistrano::Chef.load_into(Capistrano::Confiruation.instance)
+end
View
37 spec/capistrano/chef_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+require 'capistrano/chef'
+
+describe Capistrano::Chef do
+ before do
+ # Stub knife config
+ @knife = mock('Chef::Knife')
+ Chef::Knife.stub!(:new).and_return(@knife)
+ @knife.stub!(:configure_chef)
+
+ # Load into capistrano configuration
+ @configuration = Capistrano::Configuration.new
+ Capistrano::Chef.load_into(@configuration)
+ end
+
+ it 'should be a module' do
+ expect { described_class.to be_a Module }
+ end
+
+ specify 'search_chef_nodes' do
+ Chef::Knife.new.configure_chef
+ @search = mock('Chef::Search::Query')
+ Chef::Search::Query.stub!(:new).and_return(@search)
+ @search.stub!(:search).and_return([[{ :ipaddress => '10.0.0.2' }], 0, 1])
+ Capistrano::Chef.search_chef_nodes('*:*').should eql ['10.0.0.2']
+ end
+
+ specify 'chef_role' do
+ Capistrano::Chef.stub!(:search_chef_nodes).and_return(['10.0.0.2'])
+ @search = mock('Chef::Search::Query')
+ @configuration.should respond_to :chef_role
+
+ @configuration.chef_role(:test)
+ @configuration.roles.should have_key :test
+ @configuration.roles[:test].to_a[0].host.should === '10.0.0.2'
+ end
+end
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end

0 comments on commit 805af60

Please sign in to comment.