Multi-server integration tests are now possible
This commit is contained in:
parent
ba08c38a74
commit
38289b1a47
7 changed files with 224 additions and 2 deletions
39
config/environments/integration_1.rb
Normal file
39
config/environments/integration_1.rb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
Diaspora::Application.configure do
|
||||
# Settings specified here will take precedence over those in config/environment.rb
|
||||
|
||||
# In the development environment your application's code is reloaded on
|
||||
# every request. This slows down response time but is perfect for development
|
||||
# since you don't have to restart the webserver when you make code changes.
|
||||
config.cache_classes = true
|
||||
|
||||
# Log error messages when you accidentally call methods on nil.
|
||||
config.whiny_nils = true
|
||||
|
||||
# Show full error reports and disable caching
|
||||
config.consider_all_requests_local = true
|
||||
config.action_view.debug_rjs = true
|
||||
config.action_controller.perform_caching = false
|
||||
|
||||
# Don't care if the mailer can't send
|
||||
config.action_mailer.raise_delivery_errors = false
|
||||
config.active_support.deprecation = :log
|
||||
#config.threadsafe!
|
||||
|
||||
# Monkeypatch around the nasty "2.5MB exception page" issue, caused by very large environment vars
|
||||
# This snippet via: http://stackoverflow.com/questions/3114993/exception-pages-in-development-mode-take-upwards-of-15-30-seconds-to-render-why
|
||||
# Relevant Rails ticket: https://rails.lighthouseapp.com/projects/8994/tickets/5027-_request_and_responseerb-and-diagnosticserb-take-an-increasingly-long-time-to-render-in-development-with-multiple-show-tables-calls
|
||||
config.after_initialize do
|
||||
module SmallInspect
|
||||
def inspect
|
||||
"<#{self.class.name} - tooooo long>"
|
||||
end
|
||||
end
|
||||
[ActionController::Base, ActionDispatch::RemoteIp::RemoteIpGetter, OmniAuth::Strategy, Warden::Proxy].each do |klazz|
|
||||
klazz.send(:include, SmallInspect)
|
||||
end
|
||||
end
|
||||
end
|
||||
39
config/environments/integration_2.rb
Normal file
39
config/environments/integration_2.rb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
Diaspora::Application.configure do
|
||||
# Settings specified here will take precedence over those in config/environment.rb
|
||||
|
||||
# In the development environment your application's code is reloaded on
|
||||
# every request. This slows down response time but is perfect for development
|
||||
# since you don't have to restart the webserver when you make code changes.
|
||||
config.cache_classes = true
|
||||
|
||||
# Log error messages when you accidentally call methods on nil.
|
||||
config.whiny_nils = true
|
||||
|
||||
# Show full error reports and disable caching
|
||||
config.consider_all_requests_local = true
|
||||
config.action_view.debug_rjs = true
|
||||
config.action_controller.perform_caching = false
|
||||
|
||||
# Don't care if the mailer can't send
|
||||
config.action_mailer.raise_delivery_errors = false
|
||||
config.active_support.deprecation = :log
|
||||
#config.threadsafe!
|
||||
|
||||
# Monkeypatch around the nasty "2.5MB exception page" issue, caused by very large environment vars
|
||||
# This snippet via: http://stackoverflow.com/questions/3114993/exception-pages-in-development-mode-take-upwards-of-15-30-seconds-to-render-why
|
||||
# Relevant Rails ticket: https://rails.lighthouseapp.com/projects/8994/tickets/5027-_request_and_responseerb-and-diagnosticserb-take-an-increasingly-long-time-to-render-in-development-with-multiple-show-tables-calls
|
||||
config.after_initialize do
|
||||
module SmallInspect
|
||||
def inspect
|
||||
"<#{self.class.name} - tooooo long>"
|
||||
end
|
||||
end
|
||||
[ActionController::Base, ActionDispatch::RemoteIp::RemoteIpGetter, OmniAuth::Strategy, Warden::Proxy].each do |klazz|
|
||||
klazz.send(:include, SmallInspect)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -6,6 +6,21 @@ namespace :db do
|
|||
desc "rebuild and prepare test db"
|
||||
task :rebuild => [:drop, :create, :migrate, 'db:test:prepare']
|
||||
|
||||
namespace :integration do
|
||||
# desc 'Check for pending migrations and load the integration schema'
|
||||
task :prepare => :environment do
|
||||
abcs = ActiveRecord::Base.configurations
|
||||
envs = abcs.keys.select{ |k| k.include?("integration") }
|
||||
envs.each do |env|
|
||||
ActiveRecord::Base.establish_connection(env)
|
||||
ActiveRecord::Base.connection.drop_database(abcs[env]["database"])
|
||||
ActiveRecord::Base.connection.create_database(abcs[env]["database"])
|
||||
ActiveRecord::Base.establish_connection(env)
|
||||
ActiveRecord::Migrator.migrate("db/migrate", nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Seed the current RAILS_ENV database from db/seeds.rb'
|
||||
namespace :seed do
|
||||
task :tom do
|
||||
|
|
|
|||
48
lib/tasks/integration.rake
Normal file
48
lib/tasks/integration.rake
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
namespace :integration do
|
||||
desc "rebuild and prepare test db"
|
||||
task :gogogo => ['db:integration:prepare', :start_servers, :run_specs]
|
||||
|
||||
task :start_servers => :environment do
|
||||
abcs = ActiveRecord::Base.configurations
|
||||
envs = abcs.keys.select{ |k| k.include?("integration") }
|
||||
if servers_active?(envs.map{ |env| abcs[env]["app_server_port"] })
|
||||
puts "Servers are already running, using running integration servers."
|
||||
next
|
||||
end
|
||||
$integration_server_pids = []
|
||||
envs.each do |env|
|
||||
$integration_server_pids << fork do
|
||||
Process.exec "thin start -e #{env} -p #{abcs[env]["app_server_port"]}"
|
||||
end
|
||||
end
|
||||
while(!servers_active?(envs.map{ |env| abcs[env]["app_server_port"] })) do
|
||||
sleep(1)
|
||||
end
|
||||
end
|
||||
|
||||
task :run_servers => :start_servers do
|
||||
while(true) do
|
||||
sleep 1000
|
||||
end
|
||||
end
|
||||
|
||||
require 'rspec/core'
|
||||
require 'rspec/core/rake_task'
|
||||
RSpec::Core::RakeTask.new(:run_specs => :start_servers) do |t|
|
||||
t.pattern = "./spec/multi_server/**/*_spec.rb"
|
||||
end
|
||||
|
||||
def servers_active? ports
|
||||
begin
|
||||
ports.each { |port| RestClient.get("localhost:#{port}/users/sign_in") }
|
||||
true
|
||||
rescue
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
45
spec/multi_server/server_spec.rb
Normal file
45
spec/multi_server/server_spec.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#This is a spec for the class that runs the servers used in the other multi-server specs
|
||||
|
||||
require 'spec_helper'
|
||||
if Server.all.first.running?
|
||||
describe Server do
|
||||
before do
|
||||
Server.all.each{|s| s.truncate_database }
|
||||
end
|
||||
describe '.all' do
|
||||
it 'returns a server object for each server' do
|
||||
integration_envs = ActiveRecord::Base.configurations.keys.select{ |k| k.include?("integration") }
|
||||
integration_envs.count.should == Server.all.count
|
||||
end
|
||||
end
|
||||
describe '#initialize' do
|
||||
it 'takes an environment' do
|
||||
server = Server.new("integration_1")
|
||||
server.env.should == "integration_1"
|
||||
server.port.should == ActiveRecord::Base.configurations["integration_1"]["app_server_port"]
|
||||
end
|
||||
end
|
||||
describe "#running?" do
|
||||
it "verifies that the server is running" do
|
||||
Server.new("integration_1").running?.should be_true
|
||||
end
|
||||
end
|
||||
describe '#db' do
|
||||
it 'runs the given code in the context of that server' do
|
||||
servers = Server.all
|
||||
|
||||
test_user_count = User.count
|
||||
servers.first.db do
|
||||
User.count.should == 0
|
||||
Factory :user
|
||||
User.count.should == 1
|
||||
end
|
||||
User.count.should == test_user_count
|
||||
|
||||
servers.last.db do
|
||||
User.count.should == 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -2,8 +2,6 @@
|
|||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
|
||||
# from the project root directory.
|
||||
ENV["RAILS_ENV"] ||= 'test'
|
||||
require File.join(File.dirname(__FILE__), '..', 'config', 'environment') unless defined?(Rails)
|
||||
require 'helper_methods'
|
||||
|
|
@ -12,6 +10,7 @@ require 'webmock/rspec'
|
|||
require 'factory_girl'
|
||||
|
||||
include WebMock::API
|
||||
WebMock::Config.instance.allow_localhost = true
|
||||
include HelperMethods
|
||||
|
||||
# Force fixture rebuild
|
||||
|
|
|
|||
37
spec/support/server.rb
Normal file
37
spec/support/server.rb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#This class is for running servers in the background during integration testing. This will not run on Windows.
|
||||
class Server
|
||||
def self.all
|
||||
@servers ||= ActiveRecord::Base.configurations.keys.select{
|
||||
|k| k.include?("integration")
|
||||
}.map{ |k| self.new(k) }
|
||||
end
|
||||
|
||||
attr_reader :port, :env
|
||||
def initialize(env)
|
||||
@config = ActiveRecord::Base.configurations[env]
|
||||
@port = @config["app_server_port"]
|
||||
@env = env
|
||||
end
|
||||
|
||||
def running?
|
||||
begin
|
||||
RestClient.get("localhost:#{@port}/users/sign_in")
|
||||
true
|
||||
rescue Errno::ECONNREFUSED
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def db
|
||||
former_env = Rails.env
|
||||
ActiveRecord::Base.establish_connection(env)
|
||||
yield
|
||||
ActiveRecord::Base.establish_connection(former_env)
|
||||
end
|
||||
|
||||
def truncate_database
|
||||
db do
|
||||
DatabaseCleaner.clean_with(:truncation)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue