diff options
author | Stephen Delano <stephen@opscode.com> | 2014-02-26 10:51:05 -0800 |
---|---|---|
committer | Stephen Delano <stephen@opscode.com> | 2014-02-26 10:51:05 -0800 |
commit | ddd26d20135600e0f7cde074a6c3f9e2c7c26dcf (patch) | |
tree | 79368cf9316ee31f97f302d8cca99f458f657d36 | |
parent | 0454ad7471d7992a40021c26ade123da329a73e6 (diff) | |
parent | e20e6507105a68d6d2d0ec4d636a6166297f692b (diff) | |
download | chef-pl-master.tar.gz |
Merge pull request #1284 from opscode/sd/couchdb-create-speeduppl-master
CouchDB Creation Speedup
-rw-r--r-- | chef/lib/chef/couchdb.rb | 15 | ||||
-rw-r--r-- | chef/spec/unit/couchdb_spec.rb | 59 |
2 files changed, 55 insertions, 19 deletions
diff --git a/chef/lib/chef/couchdb.rb b/chef/lib/chef/couchdb.rb index e7c6249aa9..44eca9bd04 100644 --- a/chef/lib/chef/couchdb.rb +++ b/chef/lib/chef/couchdb.rb @@ -69,10 +69,19 @@ class Chef end def create_db(check_for_existing=true) - @database_list = @rest.get_rest("_all_dbs") - if !check_for_existing || !@database_list.any? { |db| db == couchdb_database } - response = @rest.put_rest(couchdb_database, Hash.new) + database_exists = false + + if check_for_existing + database_list = @rest.get_rest("_all_dbs") + if database_list.any? { |db| db == couchdb_database } + database_exists = true + end end + + if !database_exists + @rest.put_rest(couchdb_database, Hash.new) + end + couchdb_database end diff --git a/chef/spec/unit/couchdb_spec.rb b/chef/spec/unit/couchdb_spec.rb index 0d4d48353a..501f504d01 100644 --- a/chef/spec/unit/couchdb_spec.rb +++ b/chef/spec/unit/couchdb_spec.rb @@ -48,26 +48,53 @@ describe Chef::CouchDB do @couchdb.stub!(:create_design_document).and_return(true) end - it "should get a list of current databases" do - @rest.should_receive(:get_rest).and_return(["chef"]) - @couchdb.create_db - end + context "with check_for_existing = true" do + it "should get a list of current databases" do + @rest.should_receive(:get_rest) { ["chef"] } + @couchdb.create_db + end - it "should create the chef database if it does not exist" do - @rest.stub!(:get_rest).and_return([]) - @rest.should_receive(:put_rest).with("chef", {}).and_return(true) - @couchdb.create_db - end + it "should create the chef database if it does not exist" do + @rest.stub!(:get_rest) { [] } + @rest.should_receive(:put_rest).with("chef", {}) { true } + @couchdb.create_db + end - it "should not create the chef database if it does exist" do - @rest.stub!(:get_rest).and_return(["chef"]) - @rest.should_not_receive(:put_rest) - @couchdb.create_db + it "should not create the chef database if it does exist" do + @rest.stub!(:get_rest) { ["chef"] } + @rest.should_not_receive(:put_rest) + @couchdb.create_db + end + + it "should return 'chef'" do + @rest.should_receive(:get_rest).with("_all_dbs") { ["chef"] } + @couchdb.create_db.should eql("chef") + end end - it "should return 'chef'" do - @rest.should_receive(:get_rest).with("_all_dbs").and_return(%w{chef}) - @couchdb.create_db.should eql("chef") + context "with check_for_existing = false" do + it "should not get a list of current databases" do + @rest.should_not_receive(:get_rest) + @rest.should_receive(:put_rest).with("chef", {}) { true } + @couchdb.create_db(false) + end + + it "should create the database if does not exist" do + @rest.stub!(:get_rest) { [] } + @rest.should_receive(:put_rest).with("chef", {}) { true } + @couchdb.create_db(false) + end + + it "should create the database if it exists" do + @rest.stub!(:get_rest) { ["chef"] } + @rest.should_receive(:put_rest).with("chef", {}) { true } + @couchdb.create_db(false) + end + + it "should return 'chef'" do + @rest.stub!(:put_rest) { true } + @couchdb.create_db(false).should eql("chef") + end end end |