From 46a1a20522c2da3eb18500dfdf2b73a46ba09bef Mon Sep 17 00:00:00 2001 From: "John E. Vincent" <lusis.org+github.com@gmail.com> Date: Wed, 26 Jan 2011 23:56:38 -0500 Subject: [PATCH] finished Host API tests --- Gemfile | 2 +- Gemfile.lock | 7 ---- lib/models.rb | 12 ++---- noah.rb | 3 +- spec/noahapp_host_spec.rb | 79 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 84 insertions(+), 19 deletions(-) diff --git a/Gemfile b/Gemfile index f462dfc..9b6eed8 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,7 @@ platforms :mri do end platforms :jruby do gem "json-jruby", "= 1.4.6", :require => "json" - gem "kirk", :git => "git://github.com/strobecorp/kirk.git" +# gem "kirk", :git => "git://github.com/strobecorp/kirk.git" gem "warbler", "= 1.2.1" gem "jruby-openssl", "= 0.7.3" end diff --git a/Gemfile.lock b/Gemfile.lock index bede712..f3e27bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,3 @@ -GIT - remote: git://github.com/strobecorp/kirk.git - revision: 561ae9cf0108a9bda675c8d0fd07cfba3f670ee8 - specs: - kirk (0.1.0.beta.1-java) - GEM remote: http://rubygems.org/ specs: @@ -73,7 +67,6 @@ DEPENDENCIES haml (= 3.0.25) jruby-openssl (= 0.7.3) json-jruby (= 1.4.6) - kirk! ohm (= 0.1.3) ohm-contrib (= 0.1.0) rack-test (= 0.5.7) diff --git a/lib/models.rb b/lib/models.rb index f67351d..8e0418e 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -39,10 +39,8 @@ class Host < Ohm::Model host.status = opts[:status] if host.valid? host.save - host - else - raise host.errors end + host rescue Exception => e e.message end @@ -93,10 +91,8 @@ class Service < Ohm::Model service.status = opts[:status] if service.valid? service.save - service - else - raise service.errors end + service rescue Exception => e e.message end @@ -180,10 +176,8 @@ class Application < Ohm::Model find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first) if app.valid? app.save - app - else - raise app.errors end + app rescue Exception => e e.message end diff --git a/noah.rb b/noah.rb index 6430d93..5c4864f 100644 --- a/noah.rb +++ b/noah.rb @@ -58,6 +58,7 @@ class NoahApp < Sinatra::Base end error do + content_type "application/json" erb :'500' end @@ -95,7 +96,7 @@ class NoahApp < Sinatra::Base data = JSON.parse(request.body.read) data.keys.sort == required_params.sort ? (host = Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters") if host.valid? - r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}"} + r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?} r.to_json else raise "#{host.errors}" diff --git a/spec/noahapp_host_spec.rb b/spec/noahapp_host_spec.rb index 6e14877..5850ee4 100644 --- a/spec/noahapp_host_spec.rb +++ b/spec/noahapp_host_spec.rb @@ -34,5 +34,82 @@ describe "NoahApp Host API", :reset_redis => false, :populate_sample_data => tru service["name"].should == "noah" service["status"].should == "up" service["host"].should == "localhost" - end + end + + it "PUT /h/:hostname - new host" do + host_data = {:name => "host99.domain.com", :status => "down"}.to_json + put '/h/host99.domain.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["result"].should == "success" + response["id"].nil?.should == false + response["name"].should == "host99.domain.com" + response["status"].should == "down" + response["new_record"].should == true + end + + it "PUT /h/:hostname - existing host" do + sleep 3 + host_data = {:name => "localhost", :status => "pending"}.to_json + put '/h/host99.domain.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["new_record"].should == false + end + + it "PUT /h/:hostname - missing name parameter" do + host_data = {:status => "pending"}.to_json + put '/h/host100.domain.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["result"].should == "failure" + response["error_message"].should == "Missing Parameters" + end + + it "PUT /h/:hostname - missing status parameter" do + host_data = {:name => "host100.domain.com"}.to_json + put '/h/host100.domain.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["result"].should == "failure" + response["error_message"].should == "Missing Parameters" + end + + it "PUT /h/:hostname - invalid status parameter" do + host_data = {:name => "host100.domain.com", :status => "fscked"}.to_json + put '/h/host100.domain.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["result"].should == "failure" + response["error_message"].should == "[[:status, :not_member]]" + end + + it "DELETE /h/:hostname - existing host" do + h = Host.find(:name => "localhost").first + svc_size = h.services.size + host_data = {:name => "localhost"} + delete '/h/localhost', host_data, "CONTENT_TYPE" => "application/json" + last_response.should be_ok + last_response.headers["Content-Type"].should == "application/json" + response = JSON.parse(last_response.body) + response["result"].should == "success" + response["id"].should == h.id + response["name"].should == "localhost" + response["service_count"].should == svc_size.to_s + end + + it "DELETE /h/:hostname - invalid host" do + host_data = {:name => "invalid_host.asdf.com"} + delete '/h/invalid_host.asdf.com', host_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + last_response.status.should == 404 + response = JSON.parse(last_response.body) + response["result"].should == "failure" + response["error_message"].should == "Resource not found" + end end -- 2.22.0