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

more tests. break out configuration to file

parent 42f35f04
......@@ -2,9 +2,13 @@
(make sure redis is running)
## Setup
Edit `bin/setup` and config/db.rb if you want to point to a different db on your local redis install
Edit `config/db.yml` if you want to point to a different db on your local redis install.
_bin/setup_
* `RACK_ENV` is honored. production listens on 9291, development 9292, test 9293.
* `rake sample` populates the development database
* `rake spec` populates the test database
_rake sample_
Creating Host entry for 'localhost'
Create Service entry for redis
......@@ -18,9 +22,10 @@ _bin/setup_
## Run it
_rackup config.ru_ or _ruby noah.rb_
[2011-01-17 08:00:30] INFO WEBrick 1.3.1
[2011-01-17 08:00:30] INFO ruby 1.9.2 (2010-12-25) [x86_64-linux]
[2011-01-17 08:00:30] INFO WEBrick::HTTPServer#start: pid=15349 port=9292
== Sinatra/1.1.2 has taken the stage on 9292 for development with backup from Thin
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
## Example links
[Noah Start Page](http://localhost:9292/)
......
......@@ -10,6 +10,83 @@ require 'rake'
require 'rspec/core'
require 'rspec/core/rake_task'
desc "Populate database with sample dataset"
task :sample do |t|
require 'ohm'
begin
require 'yajl'
rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), 'lib','models')
config_file = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read
Ohm::connect(:url => "redis://#{config_file["development"]["host"]}:#{config_file["development"]["port"]}/#{config_file["development"]["db"]}")
Ohm::redis.flushdb
puts "Creating Host entry for 'localhost'"
h = 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)
h.services << s
end
end
puts "Creating Application entry for 'noah'"
a = 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)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
end
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")
if h.save
%w[http https smtp mysql].each do |service|
s = Service.create(:name => service, :status => "pending", :host => h)
h.services << s
end
end
end
puts "Creating sample entries - Application and Configuration"
my_yaml = <<EOY
development:
database: development_database
adapter: mysql
username: dev_user
password: dev_password
EOY
my_json = <<EOJ
{
"id":"hostname",
"data":"localhost"
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
end
puts "Sample data populated!"
end
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
#!/usr/bin/env ruby
require 'ohm'
begin
require 'yajl'
rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), '../config/db')
require File.join(File.dirname(__FILE__), '../lib/models')
# Flush the DB
Ohm.redis.flushdb
# Add an entry for my localhost
puts "Creating Host entry for 'localhost'"
h = 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)
h.services << s
end
end
puts "Creating Application entry for 'noah'"
a = 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)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
end
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")
if h.save
%w[http https smtp mysql].each do |service|
s = Service.create(:name => service, :status => "pending", :host => h)
h.services << s
end
end
end
puts "Creating sample entries - Application and Configuration"
my_yaml = <<EOY
development:
database: development_database
adapter: mysql
username: dev_user
password: dev_password
EOY
my_json = <<EOJ
{
"id":"hostname",
"data":"localhost"
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
end
puts "Setup successful!"
Ohm.connect(:url => 'redis://localhost:6379/0')
production:
host: localhost
port: 6379
db: 0
development:
host: localhost
port: 6379
db: 1
test:
host: localhost
port: 6379
db: 2
#!/usr/bin/env ruby
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/namespace'
require 'ohm'
begin
require 'yajl'
......@@ -9,30 +8,40 @@ rescue LoadError
require 'json'
end
require 'haml'
require 'yaml'
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'lib/models')
require File.join(File.dirname(__FILE__), 'lib/helpers')
@db_settings = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read
class NoahApp < Sinatra::Base
register Sinatra::Namespace
helpers Sinatra::NoahHelpers
config_file = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read
db = config_file["#{environment}"]
Ohm.connect(:url => "redis://#{db["host"]}:#{db["port"]}/#{db["db"]}")
configure(:development) do
register Sinatra::Reloader
also_reload "models.rb"
also_reload "helpers.rb"
end
configure do
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :server, %w[thin mongrel webrick]
set :port, 9292
set :port, 9291
set :logging, true
set :raise_errors, false
set :show_exceptions, false
end
configure(:development) do
require 'sinatra/reloader'
register Sinatra::Reloader
also_reload "models.rb"
also_reload "helpers.rb"
set :port, 9292
end
configure(:test) do
set :port, 9294
end
get '/' do
content_type "text/html"
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Host", :reset_redis => true do
describe "Noah Host Model", :reset_redis => true do
it "should create a new Host with no services" do
hostname = "host1.domain.com"
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "NoahApp Host API", :reset_redis => false, :populate_sample_data => true do
it "GET /h" do
get '/h'
last_response.should be_ok
last_response.headers["Content-Type"].should == "application/json"
end
it "GET /h/localhost" do
get '/h/localhost'
last_response.should be_ok
last_response.headers["Content-Type"].should == "application/json"
host = JSON.parse(last_response.body)
services = host["services"]
host["name"].should == "localhost"
host["status"].should == "up"
services.size.should == 2
services.first["name"].should == "redis"
services.first["status"].should == "up"
services.first["host"].should == "localhost"
services.last["name"].should == "noah"
services.last["status"].should == "up"
services.last["host"].should == "localhost"
end
it "GET /h/localhost/noah" do
get '/h/localhost/noah'
last_response.should be_ok
last_response.headers["Content-Type"].should == "application/json"
service = JSON.parse(last_response.body)
service["name"].should == "noah"
service["status"].should == "up"
service["host"].should == "localhost"
end
end
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Service", :reset_redis => true do
describe "Noah Service Model", :reset_redis => true do
it "should create a new Service" do
servicename = "myservice"
......
......@@ -5,14 +5,66 @@ begin
rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), '..', 'config','db')
ENV['RACK_ENV'] = 'test'
config_file = YAML::load File.new(File.join(File.dirname(__FILE__), '..', 'config','db.yml')).read
Ohm::connect(:url => "redis://#{config_file["test"]["host"]}:#{config_file["test"]["port"]}/#{config_file["test"]["db"]}")
require File.join(File.dirname(__FILE__), '..', 'lib', 'models')
require File.join(File.dirname(__FILE__), '..', 'noah')
require 'rspec'
require 'rack/test'
RSpec.configure do |config|
config.color_enabled = true
config.formatter = "documentation"
config.before(:each, :reset_redis => true) { Ohm::redis.flushdb }
config.after(:each, :reset_redis => true) {Ohm::redis.flushdb }
config.before(:all, :populate_sample_data => true) do
Ohm::redis.flushdb
h = Host.create(:name => 'localhost', :status => "up")
if h.save
%w[redis noah].each do |service|
s = Service.create(:name => service, :status => "up", :host => h)
h.services << s
end
end
a = 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)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
end
my_yaml = <<EOY
development:
database: development_database
adapter: mysql
username: dev_user
password: dev_password
EOY
my_json = <<EOJ
{
"id":"hostname",
"data":"localhost"
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
end
end
config.include Rack::Test::Methods
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