Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix chef_roles with no options #12

Merged
merged 2 commits into from over 1 year ago

2 participants

Matt Campbell Nathan L Smith
Matt Campbell

Allow chef_roles to be called without supplying any options and fix a test that was failing under 1.8.7.

Nathan L Smith smith merged commit a519833 into from October 09, 2012
Nathan L Smith smith closed this October 09, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
3  lib/capistrano/chef.rb
@@ -23,7 +23,7 @@ def self.search_chef_nodes(query = '*:*', arg = :ipaddress, limit = 1000)
23 23
         iface, family = arg.keys.first.to_s, arg.values.first.to_s
24 24
         Proc.new do |n|
25 25
           addresses = n["network"]["interfaces"][iface]["addresses"]
26  
-          addresses.select{|address, data| data["family"] == family }.keys.first
  26
+          addresses.select{|address, data| data["family"] == family }.to_a.first.first
27 27
         end
28 28
       when Symbol, String
29 29
         Proc.new{|n| n[arg.to_s]}
@@ -47,6 +47,7 @@ def self.load_into(configuration)
47 47
     configuration.set :capistrano_chef, self
48 48
     configuration.load do
49 49
       def chef_role(name, query = '*:*', options = {})
  50
+        options = {:attribute => :ipaddress, :limit => 1000}.merge(options)
50 51
         role name, *(capistrano_chef.search_chef_nodes(query, options.delete(:attribute), options.delete(:limit)) + [options])
51 52
       end
52 53
 
39  spec/capistrano/chef_spec.rb
@@ -64,7 +64,6 @@
64 64
     expect { described_class.to be_a Module }
65 65
   end
66 66
 
67  
-
68 67
   describe 'search_chef_nodes' do
69 68
     before(:each) do
70 69
       Chef::Knife.new.configure_chef
@@ -90,7 +89,7 @@
90 89
     # use Proc for more deep, complex attributes search.
91 90
     specify 'with Proc argument' do
92 91
       search_proc = Proc.new do |n|
93  
-        n["network"]["interfaces"]["eth1"]["addresses"].select{|address, data| data["family"] == "inet" }.keys.first
  92
+        n["network"]["interfaces"]["eth1"]["addresses"].select{|address, data| data["family"] == "inet" }.to_a.first.first
94 93
       end
95 94
       Capistrano::Chef.search_chef_nodes('*:*', search_proc).should eql ['192.168.77.101']
96 95
     end
@@ -121,13 +120,35 @@
121 120
     @configuration.fetch(:id).should === 'other_test'
122 121
   end
123 122
 
124  
-  specify 'chef_role' do
125  
-    Capistrano::Chef.stub!(:search_chef_nodes).and_return(['10.0.0.2'])
126  
-    @search = mock('Chef::Search::Query')
127  
-    @configuration.should respond_to :chef_role
  123
+  describe '#chef_role' do
  124
+    it 'add nodes to the role' do
  125
+      Capistrano::Chef.stub!(:search_chef_nodes).and_return(['10.0.0.2'])
  126
+      @search = mock('Chef::Search::Query')
  127
+      @configuration.should respond_to :chef_role
128 128
 
129  
-    @configuration.chef_role(:test)
130  
-    @configuration.roles.should have_key :test
131  
-    @configuration.roles[:test].to_a[0].host.should === '10.0.0.2'
  129
+      @configuration.chef_role(:test)
  130
+      @configuration.roles.should have_key :test
  131
+      @configuration.roles[:test].to_a[0].host.should === '10.0.0.2'
  132
+    end
  133
+
  134
+    it 'defaults to calling search with :ipaddress as the attribute and 1000 as the limit when giving a query' do
  135
+      query = "this is my chef query"
  136
+      Capistrano::Chef.should_receive(:search_chef_nodes).with(query, :ipaddress, 1000).and_return(['10.0.0.2'])
  137
+      @configuration.chef_role(:test, query)
  138
+    end
  139
+
  140
+    it 'allows you to specify the attribute used in the query' do
  141
+      query = "this is my chef query"
  142
+      attribute = :my_attr
  143
+      Capistrano::Chef.should_receive(:search_chef_nodes).with(query, attribute, 1000).and_return(['10.0.0.2'])
  144
+      @configuration.chef_role(:test, query, :attribute => attribute)
  145
+    end
  146
+
  147
+    it 'allows you to specify the limit used in the query' do
  148
+      query = "this is my chef query"
  149
+      limit = 55
  150
+      Capistrano::Chef.should_receive(:search_chef_nodes).with(query, :ipaddress, limit).and_return(['10.0.0.2'])
  151
+      @configuration.chef_role(:test, query, :limit => limit)
  152
+    end
132 153
   end
133 154
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.