summaryrefslogtreecommitdiff
path: root/chef-server-api
diff options
context:
space:
mode:
authorMatthew Kent <mkent@magoazul.com>2012-01-08 01:07:46 -0800
committerBryan McLellan <btm@opscode.com>2012-04-03 11:33:31 -0700
commit1ce3fd4eff17da0a9aef2acb34b832896d06880a (patch)
tree837ee7aae77334db81c74578924317a0f683ba26 /chef-server-api
parented19441a21788a5a2f052cec20ea12a99eaef88e (diff)
downloadchef-1ce3fd4eff17da0a9aef2acb34b832896d06880a.tar.gz
CHEF-2781: We don't need to worry about the frozen state of a cookbook
if it's new. Add specs to validate some basic logic in the controller.
Diffstat (limited to 'chef-server-api')
-rw-r--r--chef-server-api/app/controllers/cookbooks.rb8
-rw-r--r--chef-server-api/spec/spec_helper.rb13
-rw-r--r--chef-server-api/spec/unit/cookbooks_controller_spec.rb44
3 files changed, 63 insertions, 2 deletions
diff --git a/chef-server-api/app/controllers/cookbooks.rb b/chef-server-api/app/controllers/cookbooks.rb
index 7b1ce8afdc..7d4993d60a 100644
--- a/chef-server-api/app/controllers/cookbooks.rb
+++ b/chef-server-api/app/controllers/cookbooks.rb
@@ -162,13 +162,17 @@ class Cookbooks < Application
begin
cookbook = Chef::CookbookVersion.cdb_load(cookbook_name, cookbook_version)
cookbook.manifest = params['inflated_object'].manifest
+ new_cookbook = false
rescue Chef::Exceptions::CouchDBNotFound => e
Chef::Log.debug("Cookbook #{cookbook_name} version #{cookbook_version} does not exist")
cookbook = params['inflated_object']
+ new_cookbook = true
end
- if cookbook.frozen_version? && params[:force].nil?
- raise Conflict, "The cookbook #{cookbook.name} at version #{cookbook.version} is frozen. Use the 'force' option to override."
+ unless new_cookbook
+ if cookbook.frozen_version? && params[:force].nil?
+ raise Conflict, "The cookbook #{cookbook.name} at version #{cookbook.version} is frozen. Use the 'force' option to override."
+ end
end
cookbook.freeze_version if params["inflated_object"].frozen_version?
diff --git a/chef-server-api/spec/spec_helper.rb b/chef-server-api/spec/spec_helper.rb
index 53654e46a9..6bc95f8e29 100644
--- a/chef-server-api/spec/spec_helper.rb
+++ b/chef-server-api/spec/spec_helper.rb
@@ -49,6 +49,19 @@ def post_json(path, post_body, env = {}, &block)
end
end
+def put_json(path, put_body, env = {}, &block)
+ request_json("PUT", path, {}, env) do |controller|
+ # Merb FakeRequest allows me no way to pass JSON across the
+ # FakeRequest/StringIO boundary, so we hack it here.
+ if put_body.is_a?(Hash)
+ controller.params.merge!(put_body)
+ else
+ controller.params['inflated_object'] = put_body
+ end
+ block.call if block
+ end
+end
+
# Make an HTTP call of <method>, assign the accept header to
# application/json, and return the JSON-parsed output.
#
diff --git a/chef-server-api/spec/unit/cookbooks_controller_spec.rb b/chef-server-api/spec/unit/cookbooks_controller_spec.rb
index b33a3444a8..f476e94d7f 100644
--- a/chef-server-api/spec/unit/cookbooks_controller_spec.rb
+++ b/chef-server-api/spec/unit/cookbooks_controller_spec.rb
@@ -98,4 +98,48 @@ describe "Cookbooks Controller" do
end
end
end
+
+ describe "when uploading a cookbook" do
+ before do
+ @cookbook = make_cookbook("cookbook1", "1.0.0")
+ end
+
+ describe "and the cookbook is new" do
+ it "should upload a standard cookbook" do
+ Chef::CookbookVersion.stub!(:cdb_load).and_raise(Chef::Exceptions::CouchDBNotFound)
+ @cookbook.should_receive(:cdb_save).and_return(true)
+ response = put_json("/cookbooks/#{@cookbook.name}/#{@cookbook.version}", @cookbook)
+ end
+
+ it "should upload a frozen cookbook" do
+ @cookbook.freeze_version
+ Chef::CookbookVersion.stub!(:cdb_load).and_raise(Chef::Exceptions::CouchDBNotFound)
+ @cookbook.should_receive(:cdb_save).and_return(true)
+ response = put_json("/#{@cookbook.save_url}", @cookbook)
+ end
+ end
+
+ describe "and the cookbook already exists" do
+ it "should overwrite the existing version" do
+ Chef::CookbookVersion.stub!(:cdb_load).and_return(@cookbook)
+ @cookbook.should_receive(:cdb_save).and_return(true)
+ response = put_json("/#{@cookbook.save_url}", @cookbook)
+ end
+
+ it "should not overwrite a frozen version" do
+ @cookbook.freeze_version
+ Chef::CookbookVersion.stub!(:cdb_load).and_return(@cookbook)
+ lambda do
+ put_json("/#{@cookbook.save_url}", @cookbook)
+ end.should raise_error(Merb::ControllerExceptions::Conflict, /The cookbook (\S+) at version (\S+) is frozen/)
+ end
+
+ it "should overwrite a frozen version if forced" do
+ @cookbook.freeze_version
+ Chef::CookbookVersion.stub!(:cdb_load).and_return(@cookbook)
+ @cookbook.should_receive(:cdb_save).and_return(true)
+ response = put_json("/#{@cookbook.force_save_url}", @cookbook)
+ end
+ end
+ end
end