2012-03-07 16:17:58 -05:00
|
|
|
require 'sinatra/base'
|
|
|
|
require 'rbconfig'
|
|
|
|
require 'open-uri'
|
2014-02-11 19:17:06 -05:00
|
|
|
require 'sinatra/runner'
|
2012-03-07 16:17:58 -05:00
|
|
|
|
|
|
|
module IntegrationHelper
|
2014-02-11 19:17:06 -05:00
|
|
|
class BaseServer < Sinatra::Runner
|
2012-03-07 16:17:58 -05:00
|
|
|
extend Enumerable
|
2014-02-11 19:17:06 -05:00
|
|
|
attr_accessor :server, :port
|
2012-03-07 16:17:58 -05:00
|
|
|
alias name server
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
@all ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.each(&block)
|
|
|
|
all.each(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(server, port)
|
|
|
|
new(server, port).run
|
|
|
|
end
|
|
|
|
|
|
|
|
def app_file
|
|
|
|
File.expand_path('../integration/app.rb', __FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment
|
|
|
|
"development"
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(server, port)
|
|
|
|
@installed, @pipe, @server, @port = nil, nil, server, port
|
|
|
|
Server.all << self
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
return unless installed?
|
2012-03-08 02:54:34 -05:00
|
|
|
kill
|
2012-03-07 16:17:58 -05:00
|
|
|
@log = ""
|
2014-02-11 19:17:06 -05:00
|
|
|
super
|
2012-03-07 16:17:58 -05:00
|
|
|
at_exit { kill }
|
|
|
|
end
|
|
|
|
|
|
|
|
def installed?
|
|
|
|
return @installed unless @installed.nil?
|
2013-03-10 15:56:21 -04:00
|
|
|
s = server == 'HTTP' ? 'net/http/server' : server
|
|
|
|
require s
|
2012-03-07 16:17:58 -05:00
|
|
|
@installed = true
|
|
|
|
rescue LoadError
|
|
|
|
warn "#{server} is not installed, skipping integration tests"
|
|
|
|
@installed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def command
|
|
|
|
@command ||= begin
|
2016-08-01 07:14:48 -04:00
|
|
|
cmd = ["APP_ENV=#{environment}", "exec"]
|
2012-03-07 16:17:58 -05:00
|
|
|
if RbConfig.respond_to? :ruby
|
|
|
|
cmd << RbConfig.ruby.inspect
|
|
|
|
else
|
|
|
|
file, dir = RbConfig::CONFIG.values_at('ruby_install_name', 'bindir')
|
|
|
|
cmd << File.expand_path(file, dir).inspect
|
|
|
|
end
|
2020-07-26 05:10:00 -04:00
|
|
|
cmd << "-w" unless net_http_server?
|
2012-03-18 08:25:06 -04:00
|
|
|
cmd << "-I" << File.expand_path('../../lib', __FILE__).inspect
|
2012-03-07 16:17:58 -05:00
|
|
|
cmd << app_file.inspect << '-s' << server << '-o' << '127.0.0.1' << '-p' << port
|
|
|
|
cmd << "-e" << environment.to_s << '2>&1'
|
|
|
|
cmd.join " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-08 03:08:14 -05:00
|
|
|
def webrick?
|
|
|
|
name.to_s == "webrick"
|
|
|
|
end
|
2012-03-18 05:18:00 -04:00
|
|
|
|
2020-07-26 05:10:00 -04:00
|
|
|
def rainbows?
|
|
|
|
name.to_s == "rainbows"
|
2012-03-18 08:25:06 -04:00
|
|
|
end
|
|
|
|
|
2012-04-26 02:42:53 -04:00
|
|
|
def puma?
|
|
|
|
name.to_s == "puma"
|
|
|
|
end
|
|
|
|
|
2016-01-31 06:51:16 -05:00
|
|
|
def reel?
|
|
|
|
name.to_s == "reel"
|
|
|
|
end
|
|
|
|
|
2012-05-13 16:32:18 -04:00
|
|
|
def trinidad?
|
|
|
|
name.to_s == "trinidad"
|
|
|
|
end
|
|
|
|
|
2013-03-10 15:56:21 -04:00
|
|
|
def net_http_server?
|
|
|
|
name.to_s == 'HTTP'
|
|
|
|
end
|
|
|
|
|
2012-03-18 05:18:00 -04:00
|
|
|
def warnings
|
|
|
|
log.scan(%r[(?:\(eval|lib/sinatra).*warning:.*$])
|
|
|
|
end
|
2012-03-18 09:33:48 -04:00
|
|
|
|
|
|
|
def run_test(target, &block)
|
|
|
|
retries ||= 3
|
|
|
|
target.server = self
|
2012-05-13 18:22:37 -04:00
|
|
|
run unless alive?
|
|
|
|
target.instance_eval(&block)
|
2012-03-18 09:33:48 -04:00
|
|
|
rescue Exception => error
|
|
|
|
retries -= 1
|
|
|
|
kill
|
|
|
|
retries < 0 ? retry : raise(error)
|
|
|
|
end
|
2012-03-07 16:17:58 -05:00
|
|
|
end
|
|
|
|
|
2012-03-10 15:50:31 -05:00
|
|
|
if RUBY_ENGINE == "jruby"
|
|
|
|
class JRubyServer < BaseServer
|
|
|
|
def start_vm
|
|
|
|
require 'java'
|
|
|
|
# Create a new container, set load paths and env
|
|
|
|
# SINGLETHREAD means create a new runtime
|
|
|
|
vm = org.jruby.embed.ScriptingContainer.new(org.jruby.embed.LocalContextScope::SINGLETHREAD)
|
|
|
|
vm.load_paths = [File.expand_path('../../lib', __FILE__)]
|
2016-08-01 07:14:48 -04:00
|
|
|
vm.environment = ENV.merge('APP_ENV' => environment.to_s)
|
2012-03-10 15:50:31 -05:00
|
|
|
|
|
|
|
# This ensures processing of RUBYOPT which activates Bundler
|
|
|
|
vm.provider.ruby_instance_config.process_arguments []
|
|
|
|
vm.argv = ['-s', server.to_s, '-o', '127.0.0.1', '-p', port.to_s, '-e', environment.to_s]
|
|
|
|
|
|
|
|
# Set stdout/stderr so we can retrieve log
|
|
|
|
@pipe = java.io.ByteArrayOutputStream.new
|
|
|
|
vm.output = java.io.PrintStream.new(@pipe)
|
|
|
|
vm.error = java.io.PrintStream.new(@pipe)
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
# Hack to ensure that Kernel#caller has the same info as
|
2013-03-14 14:20:51 -04:00
|
|
|
# when run from command-line, for Sinatra::Application.app_file.
|
2012-03-10 15:50:31 -05:00
|
|
|
# Also, line numbers are zero-based in JRuby's parser
|
|
|
|
vm.provider.runtime.current_context.set_file_and_line(app_file, 0)
|
|
|
|
# Run the app
|
|
|
|
vm.run_scriptlet org.jruby.embed.PathType::ABSOLUTE, app_file
|
|
|
|
# terminate launches at_exit hooks which start server
|
|
|
|
vm.terminate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
return unless installed?
|
|
|
|
kill
|
|
|
|
@thread = start_vm
|
|
|
|
@started = Time.now
|
|
|
|
warn "#{server} up and running on port #{port}" if ping
|
|
|
|
at_exit { kill }
|
|
|
|
end
|
|
|
|
|
|
|
|
def log
|
|
|
|
String.from_java_bytes @pipe.to_byte_array
|
|
|
|
end
|
|
|
|
|
|
|
|
def kill
|
|
|
|
@thread.kill if @thread
|
|
|
|
@thread = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Server = JRubyServer
|
|
|
|
else
|
|
|
|
Server = BaseServer
|
|
|
|
end
|
|
|
|
|
2012-03-07 16:17:58 -05:00
|
|
|
def it(message, &block)
|
|
|
|
Server.each do |server|
|
|
|
|
next unless server.installed?
|
2012-03-18 09:33:48 -04:00
|
|
|
super("with #{server.name}: #{message}") { server.run_test(self, &block) }
|
2012-03-07 16:17:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.extend_object(obj)
|
|
|
|
super
|
|
|
|
|
2012-03-07 16:26:54 -05:00
|
|
|
base_port = 5000 + Process.pid % 100
|
2012-03-07 16:17:58 -05:00
|
|
|
Sinatra::Base.server.each_with_index do |server, index|
|
2013-07-02 01:37:39 -04:00
|
|
|
Server.run(server, base_port+index)
|
2012-03-07 16:17:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|