Skip to content

Commit

Permalink
Fix a few bugs in the unit tests and the Rack handling code.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Mar 28, 2009
1 parent 33714a2 commit 625621a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/phusion_passenger/rack/application_spawner.rb
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions test/config.yml.example
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions test/integration_tests.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/ruby/rack/application_spawner_spec.rb
Expand Up @@ -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

12 changes: 8 additions & 4 deletions test/ruby/rails/application_spawner_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/ruby/rails/framework_spawner_spec.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/ruby/rails/spawner_error_handling_spec.rb
Expand Up @@ -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."
Expand All @@ -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)"
Expand All @@ -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
Expand All @@ -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
31 changes: 22 additions & 9 deletions test/ruby/spawn_manager_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/ruby/wsgi/application_spawner_spec.rb
Expand Up @@ -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

1 change: 1 addition & 0 deletions test/stub/apache2/httpd.conf.erb
Expand Up @@ -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 %>
Expand Down

0 comments on commit 625621a

Please sign in to comment.