From fd5844b2baef6c1acaca9ca1eca0509e3a4f840d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 29 Oct 2013 00:02:56 +0000 Subject: implement user switching This was documented (incorrectly) and not implemented for either the master/worker or single process cases. Implement and test all (with mocks, so not fully-tested). --- test/test_server.rb | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'test/test_server.rb') diff --git a/test/test_server.rb b/test/test_server.rb index e49c8ea..96dc546 100644 --- a/test/test_server.rb +++ b/test/test_server.rb @@ -460,4 +460,66 @@ class TestServer < Testcase quit_wait(pid) FileUtils.rm_rf(tmpdir) end + + module MockSwitchUser + def self.included(cls) + cls.__send__(:remove_method, :switch_user) + cls.__send__(:alias_method, :switch_user, :mock_switch_user) + end + + def mock_switch_user(user, group = nil) + $yahns_user = [ $$, user, group ] + end + end + + def test_user_no_workers + refute defined?($yahns_user), "$yahns_user global should be undefined" + err = @err + cfg = Yahns::Config.new + host, port = @srv.addr[3], @srv.addr[1] + cfg.instance_eval do + ru = lambda do |_| + b = $yahns_user.inspect + [ 200, {'Content-Length'=>b.size.to_s }, [b] ] + end + GTL.synchronize { app(:rack, ru) { listen "#{host}:#{port}" } } + user "nobody" + stderr_path err.path + end + pid = mkserver(cfg) { Yahns::Server.__send__(:include, MockSwitchUser) } + expect = [ pid, "nobody", nil ].inspect + run_client(host, port) { |res| assert_equal expect, res.body } + refute defined?($yahns_user), "$yahns_user global should be undefined" + ensure + quit_wait(pid) + end + + def test_user_workers + refute defined?($yahns_user), "$yahns_user global should be undefined" + err = @err + cfg = Yahns::Config.new + host, port = @srv.addr[3], @srv.addr[1] + cfg.instance_eval do + ru = lambda do |_| + b = $yahns_user.inspect + [ 200, {'Content-Length'=>b.size.to_s, 'X-Pid' => "#$$" }, [b] ] + end + GTL.synchronize { app(:rack, ru) { listen "#{host}:#{port}" } } + user "nobody" + stderr_path err.path + worker_processes 1 + end + pid = mkserver(cfg) { Yahns::Server.__send__(:include, MockSwitchUser) } + run_client(host, port) do |res| + worker_pid = res["X-Pid"].to_i + assert_operator worker_pid, :>, 0 + refute_equal pid, worker_pid + refute_equal $$, worker_pid + expect = [ worker_pid, "nobody", nil ].inspect + assert_equal expect, res.body + end + refute defined?($yahns_user), "$yahns_user global should be undefined" + ensure + quit_wait(pid) + end end -- cgit v1.2.3-24-ge0c7