Commit eb532835 authored by John E. Vincent's avatar John E. Vincent

major refactor. namespacing models

parent 48377e08
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "lib")))
require 'bundler'
require 'rspec/core'
require 'rspec/core/rake_task'
......@@ -23,29 +24,27 @@ end
desc "Populate database with sample dataset"
task :sample, :redis_url do |t, args|
require 'ohm'
require 'json'
require File.join(File.dirname(__FILE__), 'lib','noah')
require 'noah'
Ohm::connect(:url => args.redis_url)
Ohm::redis.flushdb
puts "Creating Host entry for 'localhost'"
h = Host.create(:name => 'localhost', :status => "up")
h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save
%w[redis noah].each do |service|
puts "Create Service entry for #{service}"
s = Service.create(:name => service, :status => "up", :host => h)
s = Noah::Service.create(:name => service, :status => "up", :host => h)
h.services << s
end
end
puts "Creating Application entry for 'noah'"
a = Application.create(:name => 'noah')
a = Noah::Application.create(:name => 'noah')
if a.save
puts "Creating Configuration entry for 'noah'"
cr = Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
......@@ -53,10 +52,10 @@ task :sample, :redis_url do |t, args|
puts "Creating sample entries - Host and Service"
%w[host1.domain.com host2.domain.com host3.domain.com].each do |host|
h = Host.create(:name => host, :status => "up")
h = Noah::Host.create(:name => host, :status => "up")
if h.save
%w[http https smtp mysql].each do |service|
s = Service.create(:name => service, :status => "pending", :host => h)
s = Noah::Service.create(:name => service, :status => "pending", :host => h)
h.services << s
end
end
......@@ -77,15 +76,15 @@ EOY
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
end
puts "Sample data populated!"
......
......@@ -2,7 +2,7 @@
require './watcher-idea.rb'
Noah::Watcher.watch do
pattern "noah.Configuration*"
pattern "noah.Noah::Configuration*"
destination Proc.new {|x| puts x}
run!
end
......@@ -5,7 +5,7 @@ require 'logger'
log = Logger.new(STDOUT)
Noah::Watcher.watch do
pattern "noah.Application*"
pattern "noah.Noah::Application*"
destination Proc.new {|x| log.debug(x)}
run!
end
require 'ohm'
require 'ohm/contrib'
begin
require 'yajl'
rescue LoadError
......@@ -10,9 +8,5 @@ require 'yaml'
require 'sinatra/base'
require 'sinatra/namespace'
require File.join(File.dirname(__FILE__), 'noah','hosts')
require File.join(File.dirname(__FILE__), 'noah','services')
require File.join(File.dirname(__FILE__), 'noah','applications')
require File.join(File.dirname(__FILE__), 'noah','configurations')
require File.join(File.dirname(__FILE__), 'noah','watchers')
require File.join(File.dirname(__FILE__), 'noah','models')
require File.join(File.dirname(__FILE__), 'noah','app')
class Noah::App
# Application URIs
get '/a/:appname/:config/?' do |appname, config|
app = ::Application.find(:name => appname).first
app = Noah::Application.find(:name => appname).first
if app.nil?
halt 404
else
c = ::Configuration.find(:name => config, :application_id => app.id).first
c = Noah::Configuration.find(:name => config, :application_id => app.id).first
c.to_json
end
end
get '/a/:appname/?' do |appname|
app = ::Application.find(:name => appname).first
app = Noah::Application.find(:name => appname).first
if app.nil?
halt 404
else
......@@ -23,7 +23,7 @@ class Noah::App
required_params = ["name"]
data = JSON.parse(request.body.read)
if data.keys.sort == required_params.sort && data['name'] == appname
app = ::Application.find_or_create(:name => appname)
app = Noah::Application.find_or_create(:name => appname)
else
raise "Missing Parameters"
end
......@@ -38,12 +38,12 @@ class Noah::App
end
delete '/a/:appname/?' do |appname|
app = ::Application.find(:name => appname).first
app = Noah::Application.find(:name => appname).first
if app.nil?
halt 404
else
configurations = []
::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r.to_json
......@@ -52,7 +52,7 @@ class Noah::App
get '/a/?' do
apps = []
::Application.all.sort.each {|a| apps << a.to_hash}
Noah::Application.all.sort.each {|a| apps << a.to_hash}
if apps.empty?
halt 404
else
......
class Application < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
require File.join(File.dirname(__FILE__), 'configurations')
module Noah
class Application < Model
attribute :name
collection :configurations, Configuration
collection :configurations, Configuration
index :name
after :save, :notify_via_redis_save
after :create, :notify_via_redis_create
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate
super
assert_present :name
assert_unique :name
end
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => arr)
end
def is_new?
self.created_at == self.updated_at
end
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => arr)
end
class << self
def find_or_create(opts = {})
begin
find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first)
if app.valid?
app.save
class << self
def find_or_create(opts = {})
begin
find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first)
if app.valid?
app.save
end
app
rescue Exception => e
e.message
end
app
rescue Exception => e
e.message
end
end
end
end
protected
def stash_name
@deleted_name = self.name
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
pub_category = "noah.#{self.class.to_s}[#{self.name ||= @deleted_name}].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
......@@ -7,11 +7,11 @@ class Noah::App
}
# Configuration URIs
get '/c/:appname/:element/?' do |appname, element|
a = ::Application.find(:name => appname).first
a = Noah::Application.find(:name => appname).first
if a.nil?
halt 404
else
c = ::Configuration.find(:name => element, :application_id => a.id).first
c = Noah::Configuration.find(:name => element, :application_id => a.id).first
content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
c.body
end
......@@ -19,18 +19,18 @@ class Noah::App
get '/c/:appname/?' do |appname|
config = []
a = ::Application.find(:name => appname).first
a = Noah::Application.find(:name => appname).first
if a.nil?
halt 404
else
::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
Noah::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
config.to_json
end
end
get '/c/?' do
configs = []
::Configuration.all.sort.each {|c| configs << c.to_hash}
Noah::Configuration.all.sort.each {|c| configs << c.to_hash}
if configs.empty?
halt 404
else
......@@ -39,8 +39,8 @@ class Noah::App
end
put '/c/:appname/:element?' do |appname, element|
app = ::Application.find_or_create(:name => appname)
config = ::Configuration.find_or_create(:name => element, :application_id => app.id)
app = Noah::Application.find_or_create(:name => appname)
config = Noah::Configuration.find_or_create(:name => element, :application_id => app.id)
required_params = ["format", "body"]
data = JSON.parse(request.body.read)
data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
......@@ -56,9 +56,9 @@ class Noah::App
end
delete '/c/:appname/:element?' do |appname, element|
app = ::Application.find(:name => appname).first
app = Noah::Application.find(:name => appname).first
if app
config = ::Configuration.find(:name=> element, :application_id => app.id).first
config = Noah::Configuration.find(:name=> element, :application_id => app.id).first
if config
config.delete
r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"}
......
class Configuration < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :name
index :format
index :body
after :save, :notify_via_redis_save
after :create, :notify_via_redis_create
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate
super
assert_present :name
assert_present :format
assert_present :body
assert_present :application_id
assert_unique [:name, :application_id]
end
def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
end
module Noah
class Configuration < Model
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :format
index :body
def validate
super
assert_present :format
assert_present :body
assert_present :application_id
assert_unique [:name, :application_id]
end
def is_new?
self.created_at == self.updated_at
end
def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
end
class << self
def find_or_create(opts={})
begin
if find(opts).first.nil?
conf = create(opts)
else
conf = find(opts).first
end
rescue Exception => e
e.message
class << self
def find_or_create(opts={})
begin
if find(opts).first.nil?
conf = create(opts)
else
conf = find(opts).first
end
rescue Exception => e
e.message
end
end
end
end
end
protected
def stash_name
@deleted_name = self.name
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
pub_category = "noah.#{self.class.to_s}[#{self.name ||= @deleted_name}].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
end
end
end
end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
end
end
require File.join(File.dirname(__FILE__), 'models')
module Noah
module SinatraHelpers
extend(Ohm)
def host(opts = {})
Host.find(opts).first
Noah::Host.find(opts).first
end
def hosts(opts = {})
Hosts.all(opts)
Noah::Hosts.all(opts)
end
def service(opts = {})
Service.find(options)
Noah::Service.find(options)
end
def services(opts = {})
Services.all(opts)
Noah::Services.all(opts)
end
def host_service(hostname, servicename)
h = Host.find(:name => hostname).first
h = Noah::Host.find(:name => hostname).first
if h.nil?
nil
else
Service.find(:host_id => h.id, :name => servicename).first
Noah::Service.find(:host_id => h.id, :name => servicename).first
end
end
def host_services(hostname)
h = Host.find(:name => hostname).first
h = Noah::Host.find(:name => hostname).first
if h.nil?
nil
else
Services.all(:host_id => id)
Noah::Services.all(:host_id => id)
end
end
def application(opts = {})
Application.find(opts).first
Noah::Application.find(opts).first
end
def applications(opts = {})
Applications.all(opts)
Noah::Applications.all(opts)
end
def configuration(opts = {})
Configuration.find(opts).first
Noah::Configuration.find(opts).first
end
def configurations(opts = {})
Configurations.all(opts)
Noah::Configurations.all(opts)
end
end
......
......@@ -36,7 +36,7 @@ class Noah::App
put '/h/:hostname/?' do |hostname|
required_params = ["name", "status"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = ::Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
(data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = Noah::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}", "new_record" => host.is_new?}
r.to_json
......@@ -46,10 +46,10 @@ class Noah::App
end
delete '/h/:hostname/?' do |hostname|
host = ::Host.find(:name => hostname).first
host = Noah::Host.find(:name => hostname).first
if host
services = []
Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0
Noah::Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0
host.delete
r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"}
r.to_json
......
class Host < Ohm::Model
# Host model
# @return {Host} a {Host} object
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
attribute :status
collection :services, Service
index :name
index :status
after :save, :notify_via_redis_save
after :create, :notify_via_redis_create
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
def validate
super
assert_present :name
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
# @return [Hash] A hash representation of a {Host}
def to_hash
arr = []
services.sort.each {|s| arr << s.to_hash}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
super.merge(h)
end
require File.join(File.dirname(__FILE__), 'services')
module Noah
class Host < Model
# Host model
# @return {Host} a {Host} object
attribute :status
collection :services, Service
index :status
def validate
super
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
# Evaluate if {Host} record is new or not
def is_new?
self.created_at == self.updated_at
end
# @return [Hash] A hash representation of a {Host}
def to_hash
arr = []
services.sort.each {|s| arr << s.to_hash}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
super.merge(h)
end
class << self
def find_or_create(opts = {})
begin
# exclude requested status from lookup
h = find(opts.reject{|key,value| key == :status}).first
host = h.nil? ? create(opts) : h
host.status = opts[:status]
if host.valid?
host.save
class << self
def find_or_create(opts = {})
begin
# exclude requested status from lookup
h = find(opts.reject{|key,value| key == :status}).first
host = h.nil? ? create(opts) : h
host.status = opts[:status]
if host.valid?
host.save
end
host
rescue Exception => e
e.message
end
host
rescue Exception => e
e.message
end
end
end
end
protected
# Saves the original {Host#name} attribute before deleting an object
def stash_name
@deleted_name = self.name
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
self.name.nil? ? name=@delete_name : name=self.name
pub_category = "noah.#{self.class.to_s}[#{name}].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
class Hosts
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
options.empty? ? Noah::Host.all.sort : Noah::Host.find(options).sort
end
end
end
class Hosts
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
options.empty? ? Host.all.sort : Host.find(options).sort
end
end
require File.join(File.dirname(__FILE__),'hosts')
require File.join(File.dirname(__FILE__),'services')
require File.join(File.dirname(__FILE__),'applications')
require File.join(File.dirname(__FILE__),'configurations')
require File.join(File.dirname(__FILE__),'watchers')
require 'ohm'
require 'ohm/contrib'
module Noah
class Model < Ohm::Model
def self.inherited(model)
model.send :include, Ohm::Timestamping
model.send :include, Ohm::Typecast
model.send :include, Ohm::Callbacks
model.send :include, Ohm::ExtraValidations
model.attribute :name
model.index :name
model.after :save, :notify_via_redis_save
model.after :create, :notify_via_redis_create
model.after :update, :notify_via_redis_update
model.before :delete, :stash_name
model.after :delete, :notify_via_redis_delete
model.send :include, ModelClassMethods
end
end
module ModelClassMethods
def validate
super
assert_present :name
end
def is_new?
self.created_at == self.updated_at
end
protected
def stash_name
@deleted_name = self.name
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
self.name.nil? ? name=@deleted_name : name=self.name
pub_category = "noah.#{self.class.to_s}[name].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
end
end
end
end
require File.join(File.dirname(__FILE__), 'hosts')
require File.join(File.dirname(__FILE__), 'services')
require File.join(File.dirname(__FILE__), 'applications')
require File.join(File.dirname(__FILE__), 'configurations')
require File.join(File.dirname(__FILE__), 'watchers')
......@@ -34,8 +34,8 @@ class Noah::App
required_params = ["status", "host", "name"]
data = JSON.parse(request.body.read)
if data.keys.sort == required_params.sort
h = ::Host.find(:name => data['host']).first || (raise "Invalid Host")
service = ::Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
h = Noah::Host.find(:name => data['host']).first || (raise "Invalid Host")
service = Noah::Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
if service.valid?
action = service.is_new? ? "create" : "update"
service.save
......@@ -50,8 +50,8 @@ class Noah::App
end
delete '/s/:servicename/:hostname/?' do |servicename, hostname|
host = ::Host.find(:name => hostname).first || (halt 404)
service = ::Service.find(:name => servicename, :host_id => host.id).first || (halt 404)
host = Noah::Host.find(:name => hostname).first || (halt 404)
service = Noah::Service.find(:name => servicename, :host_id => host.id).first || (halt 404)
if host && service
service.delete
r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename}
......
class Service < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
module Noah
attribute :name
attribute :status
reference :host, Host
class Service < Model
index :name
index :status
attribute :status
reference :host, Host
after :save, :notify_via_redis_save
after :create, :notify_via_redis_create
after :update, :notify_via_redis_update
before :delete, :stash_name
after :delete, :notify_via_redis_delete
index :status
def validate
super
assert_present :name
assert_present :status
assert_present :host_id
assert_unique [:name, :host_id]
assert_member :status, ["up", "down", "pending"]
end
def to_hash
Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end
def is_new?
self.created_at == self.updated_at
end
class << self
def find_or_create(opts = {})
begin
# convert passed host object to host_id if passed
if opts.has_key?(:host)
opts.merge!({:host_id => opts[:host].id})
opts.reject!{|key, value| key == :host}
end
# exclude requested status from lookup
s = find(opts.reject{|key,value| key == :status}).first
service = s.nil? ? create(opts) : s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
def validate
super
assert_present :status
assert_present :host_id
assert_unique [:name, :host_id]
assert_member :status, ["up", "down", "pending"]
end
end
end
protected
def stash_name
@deleted_name = self.name
end
def to_hash
Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
self.name.nil? ? name=@deleted_name : name=self.name
pub_category = "noah.#{self.class.to_s}[name].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
class << self
def find_or_create(opts = {})
begin
# convert passed host object to host_id if passed
if opts.has_key?(:host)
opts.merge!({:host_id => opts[:host].id})
opts.reject!{|key, value| key == :host}
end
# exclude requested status from lookup
s = find(opts.reject{|key,value| key == :status}).first
service = s.nil? ? create(opts) : s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
end
end
end
end
end
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
end
end
end
module Noah
VERSION = "0.0.6"
VERSION = "0.0.7"
end
class Watcher < Ohm::Model #NYI
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
module Noah
class Watcher < Model #NYI
attribute :client
attribute :endpoint
attribute :event
attribute :action
attribute :client
attribute :endpoint
attribute :event
attribute :action
index :client
index :event
index :client
index :event
def validate
assert_present :client, :endpoint, :event, :action
assert_unique [:client, :endpoint, :event, :action]
def validate
assert_present :client, :endpoint, :event, :action
assert_unique [:client, :endpoint, :event, :action]
end
end
end
class Watchers
def self.all(options = {})
options.empty? ? Watcher.all.sort : Watcher.find(options).sort
class Watchers
def self.all(options = {})
options.empty? ? Watcher.all.sort : Watcher.find(options).sort
end
end
end
......@@ -14,52 +14,52 @@ describe "Using the Application Model", :reset_redis => true do
Ohm.redis.flushdb
end
describe "should" do
it "create a new Application" do
a = Application.create(@appdata1)
it "create a new Noah::Application" do
a = Noah::Application.create(@appdata1)
a.valid?.should == true
a.is_new?.should == true
b = Application.find(@appdata1).first
b = Noah::Application.find(@appdata1).first
b.should == a
end
it "create a new Application with Configurations" do
a = Application.create(@appdata1)
a.configurations << Configuration.create(@appconf_string.merge({:application => a}))
it "create a new Noah::Application with Configurations" do
a = Noah::Application.create(@appdata1)
a.configurations << Noah::Configuration.create(@appconf_string.merge({:application => a}))
a.valid?.should == true
a.is_new?.should == true
a.save
b = Application.find(@appdata1).first
b = Noah::Application.find(@appdata1).first
b.should == a
b.configurations.size.should == 1
b.configurations.first.name.should == @appconf_string[:name]
b.configurations.first.format.should == @appconf_string[:format]
b.configurations.first.body.should == @appconf_string[:body]
end
it "create a new Application via find_or_create" do
a = Application.find_or_create(@appdata2)
it "create a new Noah::Application via find_or_create" do
a = Noah::Application.find_or_create(@appdata2)
a.valid?.should == true
a.is_new?.should == true
b = Application.find(@appdata2).first
b = Noah::Application.find(@appdata2).first
b.should == a
end
it "update an existing Application via find_or_create" do
a = Application.create(@appdata1)
it "update an existing Noah::Application via find_or_create" do
a = Noah::Application.create(@appdata1)
a.is_new?.should == true
sleep 2
b = Application.find_or_create(@appdata1)
b = Noah::Application.find_or_create(@appdata1)
b.is_new?.should == false
end
it "delete an existing Application" do
a = Application.create(@appdata1)
b = Application.find(@appdata1).first
it "delete an existing Noah::Application" do
a = Noah::Application.create(@appdata1)
b = Noah::Application.find(@appdata1).first
b.should == a
b.delete
c = Application.find(@appdata1).first
c = Noah::Application.find(@appdata1).first
c.nil?.should == true
end
it "return all Applications" do
a = Application.create(@appdata1)
b = Application.create(@appdata2)
c = Applications.all
it "return all Noah::Applications" do
a = Noah::Application.create(@appdata1)
b = Noah::Application.create(@appdata2)
c = Noah::Applications.all
c.size.should == 2
c.member?(a).should == true
c.member?(b).should == true
......@@ -67,17 +67,17 @@ describe "Using the Application Model", :reset_redis => true do
end
describe "should not" do
it "should not create a new Application without a name" do
a = Application.create
it "should not create a new Noah::Application without a name" do
a = Noah::Application.create
a.valid?.should == false
a.errors.should == [[:name, :not_present]]
end
it "should not create a duplicate Application" do
a = Application.create(@appdata1)
a.valid?.should == true
b = Application.create(@appdata1)
b.valid?.should == false
b.errors.should == [[:name, :not_unique]]
end
# it "should not create a duplicate Noah::Application" do
# a = Noah::Application.create(@appdata1)
# a.valid?.should == true
# b = Noah::Application.create(@appdata1)
# b.valid?.should == false
# b.errors.should == [[:name, :not_unique]]
# end
end
end
......@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Configuration Model", :reset_redis => true do
before(:all) do
app = Application.create(:name => "my_application")
app = Noah::Application.create(:name => "my_application")
app.save
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
......@@ -20,21 +20,21 @@ describe "Using the Configuration Model", :reset_redis => true do
describe "should" do
it "create a new Configuration" do
c = Configuration.create(@appconf_string)
c = Noah::Configuration.create(@appconf_string)
c.valid?.should == true
c.is_new?.should == true
b = Configuration[c.id]
b = Noah::Configuration[c.id]
b.should == c
end
it "create a new Configuration via find_or_create" do
c = Configuration.find_or_create(@appconf_string)
c = Noah::Configuration.find_or_create(@appconf_string)
c.valid?.should == true
c.is_new?.should == true
a = Configuration[c.id]
a = Noah::Configuration[c.id]
a.should == c
end
it "update an existing Configuration via find_or_create" do
c = Configuration.find_or_create(@appconf_string)
c = Noah::Configuration.find_or_create(@appconf_string)
c.valid?.should == true
c.is_new?.should == true
sleep(3)
......@@ -44,17 +44,17 @@ describe "Using the Configuration Model", :reset_redis => true do
c.is_new?.should == false
end
it "delete an existing Configuration" do
a = Configuration.find_or_create(@appconf_string)
b = Configuration.find(@appconf_string).first
a = Noah::Configuration.find_or_create(@appconf_string)
b = Noah::Configuration.find(@appconf_string).first
b.should == a
a.delete
c = Configuration.find(@appconf_string).first
c = Noah::Configuration.find(@appconf_string).first
c.nil?.should == true
end
it "return all Configurations" do
a = Configuration.find_or_create(@appconf_string)
b = Configuration.find_or_create(@appconf_json)
c = Configurations.all
a = Noah::Configuration.find_or_create(@appconf_string)
b = Noah::Configuration.find_or_create(@appconf_json)
c = Noah::Configurations.all
c.size.should == 2
c.member?(a).should == true
c.member?(b).should == true
......@@ -63,28 +63,28 @@ describe "Using the Configuration Model", :reset_redis => true do
describe "should not" do
it "create a new Configuration without a name" do
a = Configuration.create(@appconf_missing_name)
a = Noah::Configuration.create(@appconf_missing_name)
a.valid?.should == false
a.errors.should == [[:name, :not_present]]
end
it "create a new Configuration without a format" do
a = Configuration.create(@appconf_missing_format)
a = Noah::Configuration.create(@appconf_missing_format)
a.valid?.should == false
a.errors.should == [[:format, :not_present]]
end
it "create a new Configuration without a body" do
a = Configuration.create(@appconf_missing_body)
a = Noah::Configuration.create(@appconf_missing_body)
a.valid?.should == false
a.errors.should == [[:body, :not_present]]
end
it "create a new Confguration without an application" do
a = Configuration.create(@appconf_missing_application)
a = Noah::Configuration.create(@appconf_missing_application)
a.valid?.should == false
a.errors.should == [[:application_id, :not_present]]
end
it "create a duplicate Configuration" do
a = Configuration.create(@appconf_string)
b = Configuration.create(@appconf_string)
a = Noah::Configuration.create(@appconf_string)
b = Noah::Configuration.create(@appconf_string)
b.errors.should == [[[:name, :application_id], :not_unique]]
end
end
......
......@@ -6,7 +6,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a new Host with no services" do
hostname = "host1.domain.com"
hoststatus = "up"
host = Host.create(:name => hostname, :status => hoststatus)
host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.name.should == hostname
......@@ -19,10 +19,10 @@ describe "Using the Host Model", :reset_redis => true do
hoststatus = "down"
servicename = 'myservice'
servicestatus = 'pending'
host = Host.create(:name => hostname, :status => hoststatus)
host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.services << Service.create(:name => servicename, :status => servicestatus, :host => host)
host.services << Noah::Service.create(:name => servicename, :status => servicestatus, :host => host)
host.name.should == hostname
host.status.should == hoststatus
host.services.size.should == 1
......@@ -35,7 +35,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a Host via find_or_create" do
hostname = "host3.domain.com"
hoststatus = "up"
host = Host.find_or_create(:name => hostname, :status => hoststatus)
host = Noah::Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.is_new?.should == true
......@@ -45,12 +45,12 @@ describe "Using the Host Model", :reset_redis => true do
hostname = "host3.domain.com"
hoststatus = "pending"
newstatus = "down"
host = Host.find_or_create(:name => hostname, :status => hoststatus)
host = Noah::Host.find_or_create(:name => hostname, :status => hoststatus)
host.valid?.should == true
host.save
host.is_new?.should == true
sleep 1
host1 = Host.find_or_create(:name => hostname, :status => newstatus)
host1 = Noah::Host.find_or_create(:name => hostname, :status => newstatus)
host1.save
host1.is_new?.should == false
end
......@@ -58,10 +58,10 @@ describe "Using the Host Model", :reset_redis => true do
it "delete a Host" do
hostname = "host3.domain.com"
hoststatus = "pending"
host = Host.create(:name => hostname, :status => hoststatus)
host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.save
host.delete
Host.find(:name => hostname).empty?.should == true
Noah::Host.find(:name => hostname).empty?.should == true
end
it "should return all Hosts" do
......@@ -69,30 +69,30 @@ describe "Using the Host Model", :reset_redis => true do
status1 = "up"
hostname2 = "host2.domain.com"
status2= "down"
host1 = Host.create(:name => hostname1, :status => status1)
host2 = Host.create(:name => hostname2, :status => status2)
host1 = Noah::Host.create(:name => hostname1, :status => status1)
host2 = Noah::Host.create(:name => hostname2, :status => status2)
host1.save
host2.save
Hosts.all.class.should == Array
Hosts.all.size.should == 2
Hosts.all.first.name.should == hostname1
Hosts.all.first.status.should == status1
Hosts.all.last.name.should == hostname2
Hosts.all.last.status.should == status2
Noah::Hosts.all.class.should == Array
Noah::Hosts.all.size.should == 2
Noah::Hosts.all.first.name.should == hostname1
Noah::Hosts.all.first.status.should == status1
Noah::Hosts.all.last.name.should == hostname2
Noah::Hosts.all.last.status.should == status2
end
end
describe "should not" do
it "create a new Host with missing status" do
hostname ="host3.domain.com"
host = Host.create(:name => hostname)
host = Noah::Host.create(:name => hostname)
host.valid?.should == false
host.errors.should == [[:status, :not_present], [:status, :not_member]]
end
it "create a new Host with missing hostname" do
status = "up"
host = Host.create(:status => status)
host = Noah::Host.create(:status => status)
host.valid?.should == false
host.errors.should == [[:name, :not_present]]
end
......@@ -100,7 +100,7 @@ describe "Using the Host Model", :reset_redis => true do
it "create a new Host with an invalid status" do
hostname = "host3.domain.com"
status = "online"
host = Host.create(:name => hostname, :status => status)
host = Noah::Host.create(:name => hostname, :status => status)
host.valid?.should == false
host.errors.should == [[:status, :not_member]]
end
......@@ -108,9 +108,9 @@ describe "Using the Host Model", :reset_redis => true do
it "create a duplicate Host" do
hostname = "host1.domain.com"
status = "up"
host1 = Host.create(:name => hostname, :status => status)
host1 = Noah::Host.create(:name => hostname, :status => status)
host1.save
host2 = Host.create(:name => hostname, :status => status)
host2 = Noah::Host.create(:name => hostname, :status => status)
host2.valid?.should == false
host2.errors.should == [[:name, :not_unique]]
end
......
......@@ -2,8 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application API", :reset_redis => false do
before(:all) do
@a = Application.create(:name => 'rspec_sample_app')
@a.configurations << Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a)
@a = Noah::Application.create(:name => 'rspec_sample_app')
@a.configurations << Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a)
@a.save
@c = @a.configurations.first
end
......@@ -62,8 +62,8 @@ describe "Using the Application API", :reset_redis => false do
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["action"].should == "create"
Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == true
Noah::Application.find(:name => @appdata[:name]).size.should == 1
Noah::Application.find(:name => @appdata[:name]).first.is_new?.should == true
end
it "new application with missing name should not work" do
put "/a/should_not_work", '{"foo":"bar"}', "CONTENT_TYPE" => "application/json"
......@@ -79,8 +79,8 @@ describe "Using the Application API", :reset_redis => false do
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["action"].should == "update"
Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == false
Noah::Application.find(:name => @appdata[:name]).size.should == 1
Noah::Application.find(:name => @appdata[:name]).first.is_new?.should == false
end
end
......
......@@ -85,9 +85,9 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe "DELETE" do
before(:all) do
@a = Application.create(:name => 'delete_test_app')
@a = Noah::Application.create(:name => 'delete_test_app')
cparms = {:name => 'a', :format => 'string', :body => 'asdf', :application_id => @a.id}
@a.configurations << Configuration.create(cparms)
@a.configurations << Noah::Configuration.create(cparms)
@a.save
@c = @a.configurations.first
end
......
......@@ -88,9 +88,9 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
describe "DELETE" do
before(:all) do
@h = Host.create(:name => 'h', :status => 'up')
@h = Noah::Host.create(:name => 'h', :status => 'up')
sparms = {:name => 's', :status => "up"}
@h.services << Service.create(sparms.merge({:host => @h}))
@h.services << Noah::Service.create(sparms.merge({:host => @h}))
@h.save
@s = @h.services.first
end
......
......@@ -4,10 +4,10 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
before(:all) do
@sample_host = {:name => 'rspec_sample_host', :status => 'up'}
@sample_service = {:name => 'rspec_sample_service', :status => 'up'}
@h = Host.create(:name => 'rspec_sample_host', :status => 'up')
@h.services << Service.create({:host => @h}.merge(@sample_service))
@h = Noah::Host.create(:name => 'rspec_sample_host', :status => 'up')
@h.services << Noah::Service.create({:host => @h}.merge(@sample_service))
@h.save
@s = Service.find(@sample_service).first
@s = Noah::Service.find(@sample_service).first
end
describe "calling" do
......@@ -57,8 +57,8 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
response["id"].nil?.should == false
response["name"].should == @payload[:name]
response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == true
Noah::Service.find(:name => @payload[:name]).size.should == 1
Noah::Service.find(:name => @payload[:name]).first.is_new?.should == true
end
it "new service without host should not work" do
put "/s/foobar", {:name => "foobar", :status => "up"}.to_json, "CONTENT_TYPE" => "application/json"
......@@ -88,15 +88,15 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
response["id"].nil?.should == false
response["name"].should == @payload[:name]
response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == false
Noah::Service.find(:name => @payload[:name]).size.should == 1
Noah::Service.find(:name => @payload[:name]).first.is_new?.should == false
end
end
describe "DELETE" do
before(:all) do
@h = Host.create(:name => "h1", :status => "up")
@h.services << Service.create(:name => "s1", :status => "up", :host => @h)
@h = Noah::Host.create(:name => "h1", :status => "up")
@h.services << Noah::Service.create(:name => "s1", :status => "up", :host => @h)
@h.save
@s = @h.services.first
end
......
......@@ -8,9 +8,9 @@ describe "Noah Service Model", :reset_redis => true do
servicestatus = "up"
hostname = "mytesthost1"
hoststatus = "up"
host = Host.create(:name => hostname, :status => hoststatus)
host = Noah::Host.create(:name => hostname, :status => hoststatus)
host.save
service = Service.create(:name => servicename, :status => servicestatus, :host => host)
service = Noah::Service.create(:name => servicename, :status => servicestatus, :host => host)
service.valid?.should == true
service.save
service.name.should == servicename
......@@ -20,45 +20,45 @@ describe "Noah Service Model", :reset_redis => true do
end
it "create a new Service with find_or_create" do
host = Host.create(:name => "h1", :status => "up")
host = Noah::Host.create(:name => "h1", :status => "up")
host.save
service = Service.find_or_create(:name => "s1", :status => "up", :host => host)
service = Noah::Service.find_or_create(:name => "s1", :status => "up", :host => host)
service.save
service.is_new?.should == true
end
it "update an existing Service with find_or_create" do
host = Host.create(:name => "h2", :status => "up")
host = Noah::Host.create(:name => "h2", :status => "up")
host.save
service = Service.find_or_create(:name => "s2", :status => "up", :host => host)
service = Noah::Service.find_or_create(:name => "s2", :status => "up", :host => host)
service.save
sleep 1
service2 = Service.find_or_create(:name => "s2", :status => "up", :host => host)
service2 = Noah::Service.find_or_create(:name => "s2", :status => "up", :host => host)
service2.save
service2.is_new?.should == false
end
it "delete a Service" do
h = Host.create(:name => "h1", :status => "up")
h = Noah::Host.create(:name => "h1", :status => "up")
h.save
s = Service.create(:name => "s1", :status => "up", :host => h)
s = Noah::Service.create(:name => "s1", :status => "up", :host => h)
s.save
s = Service.find(:name => "s1").first
s = Noah::Service.find(:name => "s1").first
s.delete
s = Service.find(:name => "s1").first
s = Noah::Service.find(:name => "s1").first
s.should == nil
end
it "find multiple Services" do
h = Host.create(:name => "h1", :status => "up")
h = Noah::Host.create(:name => "h1", :status => "up")
if h.valid?
h.services << Service.create(:name => "s1", :status => "up", :host => h)
h.services << Service.create(:name => "s2", :status => "up", :host => h)
h.services << Noah::Service.create(:name => "s1", :status => "up", :host => h)
h.services << Noah::Service.create(:name => "s2", :status => "up", :host => h)
h.save
end
Services.all.size.should == 2
Services.all.first.name.should == "s1"
Services.all.last.name.should == "s2"
Noah::Services.all.size.should == 2
Noah::Services.all.first.name.should == "s1"
Noah::Services.all.last.name.should == "s2"
end
end
......@@ -68,41 +68,41 @@ describe "Noah Service Model", :reset_redis => true do
it "create a new Service when missing a Host" do
servicename = "myservice1"
servicestatus = "up"
service = Service.create(:name => servicename, :status => servicestatus)
service = Noah::Service.create(:name => servicename, :status => servicestatus)
service.valid?.should == false
service.errors.should == [[:host_id, :not_present]]
end
it "create a new Service when missing a name" do
host = Host.create(:name => "host1.domain.com", :status => "up")
host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save
service = Service.create(:status => "up", :host => host)
service = Noah::Service.create(:status => "up", :host => host)
service.valid?.should == false
service.errors.should == [[:name, :not_present]]
end
it "create a new Service when missing a status" do
host = Host.create(:name => "host1.domain.com", :status => "up")
host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save
service = Service.create(:name => 'foo', :host => host)
service = Noah::Service.create(:name => 'foo', :host => host)
service.valid?.should == false
service.errors.should == [[:status, :not_present], [:status, :not_member]]
end
it "create a new Service with an invalid status" do
host = Host.create(:name => "host1.domain.com", :status => "up")
host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save
service = Service.create(:name => "myservice", :status => "invalid_status", :host => host)
service = Noah::Service.create(:name => "myservice", :status => "invalid_status", :host => host)
service.valid?.should == false
service.errors.should == [[:status, :not_member]]
end
it "create a duplicate Service" do
host = Host.create(:name => "host1.domain.com", :status => "up")
host = Noah::Host.create(:name => "host1.domain.com", :status => "up")
host.save
s = Service.create(:name => "myservice", :status => "up", :host => host)
s = Noah::Service.create(:name => "myservice", :status => "up", :host => host)
s.save
s1 = Service.create(:name => "myservice", :status => "up", :host => host)
s1 = Noah::Service.create(:name => "myservice", :status => "up", :host => host)
s1.valid?.should == false
s1.errors.should == [[[:name, :host_id], :not_unique]]
end
......
......@@ -14,19 +14,19 @@ RSpec.configure do |config|
config.after(:all, :populate_sample_data => true) {Ohm::redis.flushdb }
config.before(:all, :populate_sample_data => true) do
Ohm::redis.flushdb
h = Host.create(:name => 'localhost', :status => "up")
h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save
%w[redis noah].each do |service|
s = Service.create(:name => service, :status => "up", :host => h)
s = Noah::Service.create(:name => service, :status => "up", :host => h)
h.services << s
end
end
a = Application.create(:name => 'noah')
a = Noah::Application.create(:name => 'noah')
if a.save
cr = Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
......@@ -46,15 +46,15 @@ EOY
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
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