From 625621a0803923b5b990c404f5fd6c32f7fefb08 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Sat, 28 Mar 2009 12:25:34 +0100 Subject: [PATCH] Fix a few bugs in the unit tests and the Rack handling code. --- .../rack/application_spawner.rb | 2 +- test/config.yml.example | 8 +++-- test/integration_tests.rb | 4 +++ test/ruby/rack/application_spawner_spec.rb | 5 +-- test/ruby/rails/application_spawner_spec.rb | 12 ++++--- test/ruby/rails/framework_spawner_spec.rb | 6 ++-- .../ruby/rails/spawner_error_handling_spec.rb | 8 ++--- test/ruby/spawn_manager_spec.rb | 31 +++++++++++++------ test/ruby/wsgi/application_spawner_spec.rb | 5 +-- test/stub/apache2/httpd.conf.erb | 1 + 10 files changed, 56 insertions(+), 26 deletions(-) diff --git a/lib/phusion_passenger/rack/application_spawner.rb b/lib/phusion_passenger/rack/application_spawner.rb index 4b04722b58..a3631770ef 100644 --- a/lib/phusion_passenger/rack/application_spawner.rb +++ b/lib/phusion_passenger/rack/application_spawner.rb @@ -106,7 +106,7 @@ def run(channel, app_root, options) end def load_rack_app - rackup_code = File.read("config.ru") + rackup_code = ::File.read("config.ru") eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app", TOPLEVEL_BINDING, "config.ru") end end diff --git a/test/config.yml.example b/test/config.yml.example index 152352fa8b..76d82c6e72 100644 --- a/test/config.yml.example +++ b/test/config.yml.example @@ -10,8 +10,12 @@ normal_user_1: games normal_user_2: daemon # The username of a user that has less privileges than a normal user. -# NOTE: this user MUST be able to access this 'test' directory, otherwise -# the tests will fail. +# +# NOTES: +# - this user MUST be able to access this 'test' directory, otherwise +# the tests will fail. +# - this user MUST NOT have a negative UID value. So on OS X, this may +# not be the 'nobody' user. lowest_user: nobody # A nonexistant username. diff --git a/test/integration_tests.rb b/test/integration_tests.rb index 0843b5dc29..80ac560719 100644 --- a/test/integration_tests.rb +++ b/test/integration_tests.rb @@ -188,6 +188,7 @@ def name run app }) File.new("#{@stub.app_root}/tmp/restart.txt", "w").close + File.utime(2, 2, "#{@stub.app_root}/tmp/restart.txt") get('/').should == "changed" end @@ -197,6 +198,7 @@ def name File.new('foo.txt', 'w').close }) File.new("#{@stub.app_root}/tmp/restart.txt", "w").close + File.utime(1, 1, "#{@stub.app_root}/tmp/restart.txt") get('/') stat = File.stat("#{@stub.app_root}/foo.txt") stat.uid.should_not == 0 @@ -229,6 +231,7 @@ def application(env, start_response): File.write("#{@stub.app_root}/passenger_wsgi.py", code) File.new("#{@stub.app_root}/tmp/restart.txt", "w").close + File.utime(2, 2, "#{@stub.app_root}/tmp/restart.txt") get('/').should == "changed" end @@ -237,6 +240,7 @@ def application(env, start_response): File.prepend("#{@stub.app_root}/passenger_wsgi.py", "file('foo.txt', 'w').close()\n") File.new("#{@stub.app_root}/tmp/restart.txt", "w").close + File.utime(1, 1, "#{@stub.app_root}/tmp/restart.txt") get('/') stat = File.stat("#{@stub.app_root}/foo.txt") stat.uid.should_not == 0 diff --git a/test/ruby/rack/application_spawner_spec.rb b/test/ruby/rack/application_spawner_spec.rb index ead51e651e..5918344255 100644 --- a/test/ruby/rack/application_spawner_spec.rb +++ b/test/ruby/rack/application_spawner_spec.rb @@ -34,8 +34,9 @@ config_ru_owner.should == touch_txt_owner end if Process.euid == 0 - def spawn(*args) - PhusionPassenger::Rack::ApplicationSpawner.spawn_application(*args) + def spawn(app_root) + PhusionPassenger::Rack::ApplicationSpawner.spawn_application(app_root, + "lowest_user" => CONFIG['lowest_user']) end end diff --git a/test/ruby/rails/application_spawner_spec.rb b/test/ruby/rails/application_spawner_spec.rb index d82045b409..5a2a5c7211 100644 --- a/test/ruby/rails/application_spawner_spec.rb +++ b/test/ruby/rails/application_spawner_spec.rb @@ -15,7 +15,8 @@ before :each do @stub = setup_rails_stub('foobar') - @spawner = ApplicationSpawner.new(@stub.app_root) + @spawner = ApplicationSpawner.new(@stub.app_root, + "lowest_user" => CONFIG['lowest_user']) @spawner.start @server = @spawner end @@ -48,7 +49,8 @@ def spawn_arbitrary_application File.append("result.txt", "end of environment.rb\n"); }) - spawner = ApplicationSpawner.new(stub.app_root) + spawner = ApplicationSpawner.new(stub.app_root, + "lowest_user" => CONFIG['lowest_user']) spawner.start begin spawner.spawn_application.close @@ -65,7 +67,8 @@ def spawn_arbitrary_application end def spawn_stub_application(stub) - @spawner = ApplicationSpawner.new(stub.app_root) + @spawner = ApplicationSpawner.new(stub.app_root, + "lowest_user" => CONFIG['lowest_user']) begin @spawner.start return @spawner.spawn_application @@ -98,7 +101,8 @@ def spawn_stub_application(stub) end def spawn_stub_application(stub) - @spawner = ApplicationSpawner.new(stub.app_root) + @spawner = ApplicationSpawner.new(stub.app_root, + "lowest_user" => CONFIG['lowest_user']) return @spawner.spawn_application! end end diff --git a/test/ruby/rails/framework_spawner_spec.rb b/test/ruby/rails/framework_spawner_spec.rb index add4176270..8e79192ee6 100644 --- a/test/ruby/rails/framework_spawner_spec.rb +++ b/test/ruby/rails/framework_spawner_spec.rb @@ -49,7 +49,8 @@ def use_vendor_rails? end def spawn_arbitrary_application - @spawner.spawn_application(@stub.app_root) + @spawner.spawn_application(@stub.app_root, + "lowest_user" => CONFIG['lowest_user']) end end @@ -88,7 +89,8 @@ def spawn_stub_application(stub) spawner = FrameworkSpawner.new(options) spawner.start begin - return spawner.spawn_application(stub.app_root) + return spawner.spawn_application(stub.app_root, + "lowest_user" => CONFIG['lowest_user']) ensure spawner.stop end diff --git a/test/ruby/rails/spawner_error_handling_spec.rb b/test/ruby/rails/spawner_error_handling_spec.rb index 746e1c0bb3..4aefcfc37b 100644 --- a/test/ruby/rails/spawner_error_handling_spec.rb +++ b/test/ruby/rails/spawner_error_handling_spec.rb @@ -11,7 +11,7 @@ it "raises an AppInitError if the spawned app raises a standard exception during startup" do File.prepend(@stub.environment_rb, "raise 'This is a dummy exception.'\n") begin - spawn_stub_application(@stub) + spawn_stub_application(@stub).close violated "Spawning the application should have raised an InitializationError." rescue AppInitError => e e.child_exception.message.should == "This is a dummy exception." @@ -26,7 +26,7 @@ class MyError < StandardError raise MyError, "This is a custom exception." }) begin - spawn_stub_application(@stub) + spawn_stub_application(@stub).close violated "Spawning the application should have raised an InitializationError." rescue AppInitError => e e.child_exception.message.should == "This is a custom exception. (MyError)" @@ -36,7 +36,7 @@ class MyError < StandardError it "raises an AppInitError if the spawned app calls exit() during startup" do File.prepend(@stub.environment_rb, "exit\n") begin - spawn_stub_application(@stub) + spawn_stub_application(@stub).close violated "Spawning the application should have raised an InitializationError." rescue AppInitError => e e.child_exception.class.should == SystemExit @@ -47,6 +47,6 @@ class MyError < StandardError shared_examples_for "handling errors in framework initialization" do include Utils it "raises FrameworkInitError if the framework could not be loaded" do - lambda { load_nonexistant_framework }.should raise_error(FrameworkInitError) + lambda { load_nonexistant_framework.close }.should raise_error(FrameworkInitError) end end diff --git a/test/ruby/spawn_manager_spec.rb b/test/ruby/spawn_manager_spec.rb index 34e69ccaef..7800ebbb96 100644 --- a/test/ruby/spawn_manager_spec.rb +++ b/test/ruby/spawn_manager_spec.rb @@ -44,7 +44,7 @@ end def spawn_arbitrary_application - @manager.spawn_application(@stub.app_root, true, "nobody", + @manager.spawn_application(@stub.app_root, true, CONFIG['lowest_user'], "production", @spawn_method) end end @@ -64,7 +64,8 @@ def spawn_arbitrary_application end it "can spawn when the server's not running" do - app = @manager.spawn_application("app_root" => @stub.app_root) + app = @manager.spawn_application("app_root" => @stub.app_root, + "lowest_user" => CONFIG['lowest_user']) app.close end @@ -75,7 +76,9 @@ def spawn_arbitrary_application a.close sleep(1) # Give @manager the chance to start. channel = MessageChannel.new(b) - channel.write("spawn_application", "app_root", @stub.app_root) + channel.write("spawn_application", + "app_root", @stub.app_root, + "lowest_user", CONFIG['lowest_user']) channel.read pid, listen_socket = channel.read channel.recv_io.close @@ -97,7 +100,8 @@ def spawn_arbitrary_application content.sub(/^RAILS_GEM_VERSION = .*$/, '') end @stub.dont_use_vendor_rails - @manager.spawn_application("app_root" => @stub.app_root).close + @manager.spawn_application("app_root" => @stub.app_root, + "lowest_user" => CONFIG['lowest_user']).close end it "properly reloads applications that do not specify a Rails version" do @@ -119,10 +123,18 @@ def spawn_arbitrary_application it "can spawn a Rack application" do use_stub('rack') do |stub| @manager = SpawnManager.new - app = @manager.spawn_application( - "app_root" => stub.app_root, - "app_type" => "rack") - app.close + begin + app = @manager.spawn_application( + "app_root" => stub.app_root, + "app_type" => "rack", + "lowest_user" => CONFIG['lowest_user']) + app.close + rescue => e + puts e + puts e.child_exception.backtrace + ensure + @manager.cleanup + end end end end @@ -162,7 +174,8 @@ def spawn_stub_application(stub) begin return spawner.spawn_application( "app_root" => stub.app_root, - "spawn_method" => @spawn_method) + "spawn_method" => @spawn_method, + "lowest_user" => CONFIG['lowest_user']) ensure spawner.cleanup end diff --git a/test/ruby/wsgi/application_spawner_spec.rb b/test/ruby/wsgi/application_spawner_spec.rb index ba62075df9..91f73f5f0a 100644 --- a/test/ruby/wsgi/application_spawner_spec.rb +++ b/test/ruby/wsgi/application_spawner_spec.rb @@ -40,8 +40,9 @@ Dir["#{passenger_tmpdir}/passenger_wsgi.*"].should be_empty end - def spawn(*args) - PhusionPassenger::WSGI::ApplicationSpawner.spawn_application(*args) + def spawn(app_root) + PhusionPassenger::WSGI::ApplicationSpawner.spawn_application(app_root, + true, CONFIG['lowest_user']) end end diff --git a/test/stub/apache2/httpd.conf.erb b/test/stub/apache2/httpd.conf.erb index deda04c200..81ca4f559b 100644 --- a/test/stub/apache2/httpd.conf.erb +++ b/test/stub/apache2/httpd.conf.erb @@ -27,6 +27,7 @@ LoadModule passenger_module "<%= @mod_passenger %>" PassengerRoot "<%= @passenger_root %>" PassengerRuby "<%= PlatformInfo::RUBY %>" +PassengerDefaultUser <%= CONFIG['lowest_user'] %> RailsEnv production RackEnv production <% for line in @extra %>