File: application_spawner_spec.rb

package info (click to toggle)
passenger 2.2.11debian-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,576 kB
  • ctags: 28,138
  • sloc: cpp: 66,323; ruby: 9,646; ansic: 2,425; python: 141; sh: 56; makefile: 29
file content (99 lines) | stat: -rw-r--r-- 3,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'support/config'
require 'support/test_helper'
require 'phusion_passenger/rack/application_spawner'

describe PhusionPassenger::Rack::ApplicationSpawner do
	include TestHelper
	
	before :each do
		@stub = setup_stub('rack')
	end
	
	after :each do
		@stub.destroy
	end
	
	it "can spawn our stub application" do
		spawn(@stub.app_root).close
	end
	
	it "propagates exceptions in application startup" do
		File.prepend("#{@stub.app_root}/config.ru", "raise StandardError, 'foo'\n")
		spawn = lambda { spawn(@stub.app_root, "print_exceptions" => false) }
		spawn.should raise_error(StandardError)
	end
	
	it "lowers privilege to the owner of config.ru" do
		system("chown", "-R", CONFIG['normal_user_1'], @stub.app_root)
		File.prepend("#{@stub.app_root}/config.ru", %q{
			File.new('touch.txt', 'w').close
		})
		spawn(@stub.app_root).close
		config_ru_owner = File.stat("#{@stub.app_root}/config.ru").uid
		touch_txt_owner = File.stat("#{@stub.app_root}/touch.txt").uid
		config_ru_owner.should == touch_txt_owner
	end if Process.euid == 0
	
	it "sets the environment variables passed in the environment_variables option" do
		File.append("#{@stub.app_root}/config.ru", %q{
			File.open("env.txt", "w") do |f|
				ENV.each_pair do |key, value|
					f.puts("#{key} = #{value}")
				end
			end
		})
		env_vars_string = "PATH\0/usr/bin:/opt/sw/bin\0FOO\0foo bar!\0"
		options = { "environment_variables" => [env_vars_string].pack("m") }
		spawn(@stub.app_root, options).close
		
		contents = File.read("#{@stub.app_root}/env.txt")
		contents.should =~ %r(PATH = /usr/bin:/opt/sw/bin\n)
		contents.should =~ %r(FOO = foo bar\!\n)
	end
	
	it "calls the starting_worker_process event after config.ru has been loaded" do
		File.append("#{@stub.app_root}/config.ru", %q{
			PhusionPassenger.on_event(:starting_worker_process) do
				File.append("rackresult.txt", "worker_process_started\n")
			end
			File.append("rackresult.txt", "end of config.ru\n");
		})
		spawn(@stub.app_root).close
		spawn(@stub.app_root).close
		
		# Give some time for the starting_worker_process hook to be executed.
		sleep 0.2
		
		contents = File.read("#{@stub.app_root}/rackresult.txt")
		contents.should == "end of config.ru\n" +
			"worker_process_started\n" +
			"end of config.ru\n" +
			"worker_process_started\n"
	end
	
	it "calls the stopping_worker_process event" do
		File.append("#{@stub.app_root}/config.ru", %q{
			PhusionPassenger.on_event(:stopping_worker_process) do
				File.append("rackresult.txt", "worker_process_stopped\n")
			end
			File.append("rackresult.txt", "end of config.ru\n");
		})
		spawn(@stub.app_root).close
		spawn(@stub.app_root).close
		
		# Give some time for the starting_worker_process hook to be executed.
		sleep 0.2
		
		contents = File.read("#{@stub.app_root}/rackresult.txt")
		contents.should == "end of config.ru\n" +
			"worker_process_stopped\n" +
			"end of config.ru\n" +
			"worker_process_stopped\n"
	end	
	
	def spawn(app_root, extra_options = {})
		options = { "lowest_user" => CONFIG['lowest_user'] }.merge(extra_options)
		PhusionPassenger::Rack::ApplicationSpawner.spawn_application(app_root, options)
	end
end