Skip to content

Commit a7b99d9

Browse files
committed
Merge branch 'master' into skeptomai/chef-174
2 parents 0abc33e + af73b22 commit a7b99d9

4 files changed

Lines changed: 104 additions & 15 deletions

File tree

chef-server/bin/chef-server

100644100755
File mode changed.

chef/lib/chef/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Config
6363
:remotefile_url => "http://localhost:4000",
6464
:search_url => "http://localhost:4000",
6565
:couchdb_database => "chef",
66+
:couchdb_version => nil,
6667
:openid_store_couchdb => false,
6768
:openid_cstore_couchdb => false,
6869
:openid_store_path => "/var/chef/openid/db",

chef/lib/chef/couchdb.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
class Chef
2626
class CouchDB
2727
include Chef::Mixin::ParamsValidate
28-
28+
2929
def initialize(url=nil)
3030
url ||= Chef::Config[:couchdb_url]
3131
@rest = Chef::REST.new(url)
32+
Chef::Config[:couchdb_version] ||= @rest.run_request(:GET, URI.parse(@rest.url + "/"), false, 10, false)["version"].gsub(/-.+/,"").to_f
3233
end
3334

3435
def create_db
@@ -119,9 +120,9 @@ def list(view, inflate=false)
119120
}
120121
)
121122
if inflate
122-
@rest.get_rest("#{Chef::Config[:couchdb_database]}/_view/#{view}/all")
123+
@rest.get_rest(view_uri(view, "all"))
123124
else
124-
@rest.get_rest("#{Chef::Config[:couchdb_database]}/_view/#{view}/all_id")
125+
@rest.get_rest(view_uri(view, "all_id"))
125126
end
126127
end
127128

@@ -145,9 +146,19 @@ def has_key?(obj_type, name)
145146
end
146147

147148
private
148-
def safe_name(name)
149-
name.gsub(/\./, "_")
149+
150+
def safe_name(name)
151+
name.gsub(/\./, "_")
152+
end
153+
154+
def view_uri(design, view)
155+
case Chef::Config[:couchdb_version]
156+
when 0.9
157+
"#{Chef::Config[:couchdb_database]}/_design/#{design}/_view/#{view}"
158+
when 0.8
159+
"#{Chef::Config[:couchdb_database]}/_view/#{design}/#{view}"
150160
end
161+
end
151162

152163
end
153164
end

chef/spec/unit/couchdb_spec.rb

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@
1919
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2020

2121
describe Chef::CouchDB, "new" do
22+
before do
23+
@mock_rest = mock("Chef::REST", :null_object => true)
24+
@mock_rest.stub!(:run_request).and_return({"couchdb" => "Welcome", "version" =>"0.9.0"})
25+
@mock_rest.stub!(:url).and_return("http://localhost:5984")
26+
Chef::REST.stub!(:new).and_return(@mock_rest)
27+
end
28+
2229
it "should create a new Chef::REST object from the default url" do
2330
Chef::Config[:couchdb_url] = "http://monkey"
24-
Chef::REST.should_receive(:new).with("http://monkey").and_return(true)
31+
Chef::REST.should_receive(:new).with("http://monkey")
2532
Chef::CouchDB.new
2633
end
2734

2835
it "should create a new Chef::REST object from a provided url" do
29-
Chef::REST.should_receive(:new).with("http://monkeypants").and_return(true)
36+
Chef::REST.should_receive(:new).with("http://monkeypants")
3037
Chef::CouchDB.new("http://monkeypants")
38+
end
39+
40+
it "should parse the CouchDB version number" do
41+
3142
end
3243
end
3344

@@ -185,18 +196,42 @@ def do_delete(rev=nil)
185196

186197
describe Chef::CouchDB, "list" do
187198
before(:each) do
188-
@mock_rest = mock("Chef::REST", :null_object => true)
199+
@mock_rest = mock("Chef::REST", :null_object => true, :url => "http://monkeypants")
189200
Chef::REST.stub!(:new).and_return(@mock_rest)
201+
@couch = Chef::CouchDB.new("http://monkeypants")
202+
Chef::Config.stub!(:[]).with(:couchdb_database).and_return("chef")
190203
end
191204

192-
it "should get the view for all objects if inflate is true" do
193-
@mock_rest.should_receive(:get_rest).with("chef/_view/node/all").and_return(true)
194-
Chef::CouchDB.new.list("node", true)
205+
describe "on couchdb 0.8" do
206+
before do
207+
Chef::Config.stub!(:[]).with(:couchdb_version).and_return(0.8)
208+
end
209+
210+
it "should get the view for all objects if inflate is true" do
211+
@mock_rest.should_receive(:get_rest).with("chef/_view/node/all").and_return(true)
212+
@couch.list("node", true)
213+
end
214+
215+
it "should get the view for just the object id's if inflate is false" do
216+
@mock_rest.should_receive(:get_rest).with("chef/_view/node/all_id").and_return(true)
217+
@couch.list("node", false)
218+
end
195219
end
196-
197-
it "should get the view for just the object id's if inflate is false" do
198-
@mock_rest.should_receive(:get_rest).with("chef/_view/node/all_id").and_return(true)
199-
Chef::CouchDB.new.list("node", false)
220+
221+
describe "on couchdb 0.9" do
222+
before do
223+
Chef::Config.stub!(:[]).with(:couchdb_version).and_return(0.9)
224+
end
225+
226+
it "should get the view for all objects if inflate is true" do
227+
@mock_rest.should_receive(:get_rest).with("chef/_design/node/_view/all").and_return(true)
228+
@couch.list("node", true)
229+
end
230+
231+
it "should get the view for just the object id's if inflate is false" do
232+
@mock_rest.should_receive(:get_rest).with("chef/_design/node/_view/all_id").and_return(true)
233+
@couch.list("node", false)
234+
end
200235
end
201236
end
202237

@@ -216,3 +251,45 @@ def do_delete(rev=nil)
216251
Chef::CouchDB.new.has_key?("node", "bob").should eql(false)
217252
end
218253
end
254+
255+
describe Chef::CouchDB, "safe_name" do
256+
before do
257+
@couchdb = mock("Chef::CouchDB", :null_object => true)
258+
Chef::CouchDB.stub!(:new).and_return(@couchdb)
259+
end
260+
261+
it "should convert the name to a safe name" do
262+
@couchdb.should_receive(:safe_name).with("asdf.lol.com").and_return("asdf_lol_com")
263+
@couchdb.safe_name("asdf.lol.com")
264+
end
265+
end
266+
267+
describe Chef::CouchDB, "view_uri" do
268+
before do
269+
@couchdb = mock("Chef::CouchDB", :null_object => true)
270+
Chef::CouchDB.stub!(:new).and_return(@couchdb)
271+
Chef::Config.stub!(:[]).with(:couchdb_database).and_return("chef")
272+
end
273+
274+
describe "on couchdb 0.8" do
275+
before do
276+
Chef::Config.stub!(:[]).with(:couchdb_version).and_return(0.8)
277+
end
278+
279+
it "should output an appropriately formed view URI" do
280+
@couchdb.should_receive(:view_uri).with("nodes", "all").and_return("chef/_view/nodes/all")
281+
@couchdb.view_uri("nodes", "all")
282+
end
283+
end
284+
285+
describe "on couchdb 0.9" do
286+
before do
287+
Chef::Config.stub!(:[]).with(:couchdb_version).and_return(0.9)
288+
end
289+
290+
it "should output an appropriately formed view URI" do
291+
@couchdb.should_receive(:view_uri).with("nodes", "all").and_return("chef/_design/nodes/_view/all")
292+
@couchdb.view_uri("nodes", "all")
293+
end
294+
end
295+
end

0 commit comments

Comments
 (0)