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 (159 lines) | stat: -rw-r--r-- 4,396 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'support/config'
require 'support/test_helper'
require 'phusion_passenger/railz/application_spawner'

require 'ruby/rails/minimal_spawner_spec'
require 'ruby/spawn_server_spec'
require 'ruby/rails/spawner_privilege_lowering_spec'
require 'ruby/rails/spawner_error_handling_spec'

include PhusionPassenger
include PhusionPassenger::Railz

describe ApplicationSpawner do
	include TestHelper
	
	before :each do
		@stub = setup_rails_stub('foobar')
		@spawner = ApplicationSpawner.new(@stub.app_root,
			"lowest_user" => CONFIG['lowest_user'])
		@spawner.start
		@server = @spawner
	end
	
	after :each do
		@spawner.stop
		@stub.destroy
	end
	
	it_should_behave_like "a spawn server"
	
	def spawn_arbitrary_application
		@spawner.spawn_application
	end
end

describe ApplicationSpawner do
	include TestHelper
	
	describe "smart spawning" do
		it_should_behave_like "a minimal spawner"
		it_should_behave_like "handling errors in application initialization"
		
		it "calls the starting_worker_process event, with forked=true, after a new worker process has been forked off" do
			use_rails_stub('foobar') do |stub|
				File.append(stub.environment_rb, %q{
					PhusionPassenger.on_event(:starting_worker_process) do |forked|
						File.append("result.txt", "forked = #{forked}\n")
					end
					File.append("result.txt", "end of environment.rb\n");
				})
				
				spawner = ApplicationSpawner.new(stub.app_root,
					"lowest_user" => CONFIG['lowest_user'])
				spawner.start
				begin
					spawner.spawn_application.close
					spawner.spawn_application.close
				ensure
					spawner.stop
				end
				
				# Give some time for the starting_worker_process hook to be executed.
				sleep 0.2
				
				contents = File.read("#{stub.app_root}/result.txt")
				contents.should == "end of environment.rb\n" +
					"forked = true\n" +
					"forked = true\n"
			end
		end
		
		def spawn_stub_application(stub, extra_options = {})
			options = { "lowest_user" => CONFIG['lowest_user'] }.merge(extra_options)
			@spawner = ApplicationSpawner.new(stub.app_root, options)
			begin
				@spawner.start
				return @spawner.spawn_application
			ensure
				@spawner.stop rescue nil
			end
		end
	end
	
	describe "conservative spawning" do
		it_should_behave_like "a minimal spawner"
		it_should_behave_like "handling errors in application initialization"
		
		it "calls the starting_worker_process event, with forked=true, after environment.rb has been loaded" do
			use_rails_stub('foobar') do |stub|
				File.append(stub.environment_rb, %q{
					PhusionPassenger.on_event(:starting_worker_process) do |forked|
						File.append("result.txt", "forked = #{forked}\n")
					end
					File.append("result.txt", "end of environment.rb\n");
				})
				spawn_stub_application(stub).close
				spawn_stub_application(stub).close
				
				# Give some time for the starting_worker_process hook to be executed.
				sleep 0.2
				
				contents = File.read("#{stub.app_root}/result.txt")
				contents.should == "end of environment.rb\n" +
					"forked = false\n" +
					"end of environment.rb\n" +
					"forked = false\n"
			end
		end
		
		def spawn_stub_application(stub, extra_options = {})
			options = { "lowest_user" => CONFIG['lowest_user'] }.merge(extra_options)
			@spawner = ApplicationSpawner.new(stub.app_root, options)
			return @spawner.spawn_application!
		end
	end
end

Process.euid == ApplicationSpawner::ROOT_UID &&
describe("ApplicationSpawner privilege lowering support") do
	include TestHelper
	
	describe "regular spawning" do
		it_should_behave_like "a spawner that supports lowering of privileges"
	
		def spawn_stub_application(options = {})
			options = {
				"lower_privilege" => true,
				"lowest_user" => CONFIG['lowest_user']
			}.merge(options)
			@spawner = ApplicationSpawner.new(@stub.app_root, options)
			@spawner.start
			begin
				app = @spawner.spawn_application
				yield app
			ensure
				app.close if app
				@spawner.stop
			end
		end
	end
	
	describe "conservative spawning" do
		it_should_behave_like "a spawner that supports lowering of privileges"
	
		def spawn_stub_application(options = {})
			options = {
				"lower_privilege" => true,
				"lowest_user" => CONFIG['lowest_user']
			}.merge(options)
			@spawner = ApplicationSpawner.new(@stub.app_root, options)
			begin
				app = @spawner.spawn_application!
				yield app
			ensure
				app.close if app
			end
		end
	end
end