Commit 46a1a205 authored by John E. Vincent's avatar John E. Vincent

finished Host API tests

parent e2edd467
......@@ -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
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)
......
......@@ -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
......
......@@ -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}"
......
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment