From 9b39d2ca14733fe082fcd6e05d6c928219726d07 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Thu, 15 Nov 2012 16:16:36 -0800 Subject: replace thin w/ webrick for func. test server --- Gemfile | 6 ----- spec/functional/knife/cookbook_delete_spec.rb | 2 -- spec/functional/knife/exec_spec.rb | 2 -- spec/functional/knife/ssh_spec.rb | 1 - spec/functional/resource/remote_file_spec.rb | 1 - spec/functional/tiny_server_spec.rb | 9 ++++--- spec/tiny_server.rb | 39 ++++++++++++++++++++------- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Gemfile b/Gemfile index a1d35569e1..4a84958e39 100644 --- a/Gemfile +++ b/Gemfile @@ -7,12 +7,6 @@ gem "ronn" group(:development, :test) do gem 'rack' - gem 'thin' - - # Eventmachine 1.0.0 is causing functional test failures on Solaris - # 9 SPARC. Pinning em to 0.12.10 solves this issue until we can - # replace thin with webrat or some other alternative. - gem 'eventmachine', '0.12.10', :platforms => :ruby gem 'ruby-shadow', :platforms => :ruby unless RUBY_PLATFORM.downcase.match(/(darwin|freebsd)/) # gem 'awesome_print' diff --git a/spec/functional/knife/cookbook_delete_spec.rb b/spec/functional/knife/cookbook_delete_spec.rb index 54081263f0..ef38cb2e1f 100644 --- a/spec/functional/knife/cookbook_delete_spec.rb +++ b/spec/functional/knife/cookbook_delete_spec.rb @@ -23,8 +23,6 @@ describe Chef::Knife::CookbookDelete do before(:all) do @original_config = Chef::Config.hash_dup - Thin::Logging.silent = true - @server = TinyServer::Manager.new @server.start end diff --git a/spec/functional/knife/exec_spec.rb b/spec/functional/knife/exec_spec.rb index fa4a448fbb..114a30d791 100644 --- a/spec/functional/knife/exec_spec.rb +++ b/spec/functional/knife/exec_spec.rb @@ -23,8 +23,6 @@ describe Chef::Knife::Exec do before(:all) do @original_config = Chef::Config.hash_dup - Thin::Logging.silent = false - @server = TinyServer::Manager.new#(:debug => true) @server.start end diff --git a/spec/functional/knife/ssh_spec.rb b/spec/functional/knife/ssh_spec.rb index 8f87e53bf7..696fd58c4d 100644 --- a/spec/functional/knife/ssh_spec.rb +++ b/spec/functional/knife/ssh_spec.rb @@ -24,7 +24,6 @@ describe Chef::Knife::Ssh do before(:all) do @original_config = Chef::Config.hash_dup Chef::Knife::Ssh.load_deps - Thin::Logging.silent = true @server = TinyServer::Manager.new @server.start end diff --git a/spec/functional/resource/remote_file_spec.rb b/spec/functional/resource/remote_file_spec.rb index e695e8feae..2ebeecd3d1 100644 --- a/spec/functional/resource/remote_file_spec.rb +++ b/spec/functional/resource/remote_file_spec.rb @@ -40,7 +40,6 @@ describe Chef::Resource::RemoteFile do end before(:all) do - Thin::Logging.silent = false @server = TinyServer::Manager.new @server.start @api = TinyServer::API.instance diff --git a/spec/functional/tiny_server_spec.rb b/spec/functional/tiny_server_spec.rb index 0cfef4305f..68ab9e7294 100644 --- a/spec/functional/tiny_server_spec.rb +++ b/spec/functional/tiny_server_spec.rb @@ -37,15 +37,16 @@ describe TinyServer::API do it "creates a route for a GET request" do @api.get('/foo/bar', 200, 'hello foobar') - response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/foo/bar') - response.should == [200, {'Content-Type' => 'application/json'}, 'hello foobar'] + # WEBrick gives you the full URI with host, Thin only gave the part after scheme+host+port + response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/foo/bar') + response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello foobar' ]] end it "creates a route for a request with a block" do block_called = false @api.get('/bar/baz', 200) { block_called = true; 'hello barbaz' } - response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => '/bar/baz') - response.should == [200, {'Content-Type' => 'application/json'}, 'hello barbaz'] + response = @api.call("REQUEST_METHOD" => "GET", "REQUEST_URI" => 'http://localhost:1974/bar/baz') + response.should == [200, {'Content-Type' => 'application/json'}, [ 'hello barbaz' ]] block_called.should be_true end diff --git a/spec/tiny_server.rb b/spec/tiny_server.rb index ae8518097b..ba75cd57c1 100644 --- a/spec/tiny_server.rb +++ b/spec/tiny_server.rb @@ -17,11 +17,13 @@ # require 'rubygems' +require 'webrick' require 'rack' -require 'thin' +#require 'thin' require 'singleton' require 'chef/json_compat' require 'open-uri' +require 'chef/config' module TinyServer @@ -29,30 +31,42 @@ module TinyServer attr_writer :app - def self.run(options=nil, &block) + def self.setup(options=nil, &block) tiny_app = new(options) app_code = Rack::Builder.new(&block).to_app tiny_app.app = app_code - tiny_app.start + tiny_app + end + + def shutdown + server.shutdown end end class Manager - DEFAULT_OPTIONS = {:server => 'thin', :Port => 9000, :Host => 'localhost', :environment => :none} + # 5 == debug, 3 == warning + LOGGER = WEBrick::Log.new(STDOUT, 3) + DEFAULT_OPTIONS = { + :server => 'webrick', + :Port => 9000, + :Host => 'localhost', + :environment => :none, + :Logger => LOGGER, + :AccessLog => [] # Remove this option to enable the access log when debugging. + } def initialize(options=nil) @options = options ? DEFAULT_OPTIONS.merge(options) : DEFAULT_OPTIONS @creator = caller.first - - Thin::Logging.silent = !@options[:debug] end def start @server_thread = Thread.new do - @server = Server.run(@options) do + @server = Server.setup(@options) do run API.instance end + @server.start end block_until_started end @@ -63,7 +77,10 @@ module TinyServer def block_until_started 200.times do - return true if started? + if started? + raise "ivar weirdness" if @server.nil? + return true + end end raise "TinyServer failed to boot :/" end @@ -84,6 +101,7 @@ module TinyServer def stop # yes, this is terrible. + @server.shutdown @server_thread.kill @server_thread.join @server_thread = nil @@ -132,7 +150,7 @@ module TinyServer debug_info = {:message => "no data matches the request for #{env['REQUEST_URI']}", :available_routes => @routes, :request => env} # Uncomment me for glorious debugging - #pp :not_found => debug_info + # pp :not_found => debug_info [404, {'Content-Type' => 'application/json'}, debug_info.to_json] end end @@ -152,6 +170,7 @@ module TinyServer end def matches_request?(uri) + uri = URI.parse(uri).request_uri @path_spec === uri end @@ -171,7 +190,7 @@ module TinyServer def call data = @data || @block.call - [@response_code, HEADERS, data] + [@response_code, HEADERS, Array(data)] end def to_s -- cgit v1.2.1