cmogstored 1.8.1 - use default system stack size
[cmogstored.git] / test / inherit.rb
blob240c48549ec3d96f3c7e85ac997eb7c5e4472b13
1 #!/usr/bin/env ruby
2 # -*- encoding: binary -*-
3 # Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
4 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
5 require 'test/test_helper'
6 require 'net/http'
8 class TestInherit < Test::Unit::TestCase
9   def setup
10     @tmpdir = Dir.mktmpdir('cmogstored-inherit-test')
11     @to_close = []
12     @host = TEST_HOST
13     @srv = TCPServer.new(@host, 0)
14     @to_close << @srv
15     @port = @srv.addr[1]
16     @err = Tempfile.new("stderr")
17     @pid = nil
18     @exec_fds = {}
19   end
21   # Ruby 1.8 did not have the close_on_exec= method, but it
22   # also defaulted to close_on_exec=false (same as POSIX)
23   # 2.0.0 will be the first release with close_on_exec=true
24   # by default, but 1.9 already added the close_on_exec= method
25   def maybe_cloexec(io, val)
26     if io.respond_to?(:close_on_exec=)
27       io.close_on_exec = val
28       @exec_fds[io.fileno] = io
29     end
30   end
32   # FD inheritance is explicit in Ruby 2.0.0
33   def exec(*cmd)
34     cmd << @exec_fds if @exec_fds.size > 0
35     Process.exec(*cmd)
36   end
38   def teardown
39     if @pid
40       Process.kill(:QUIT, @pid) rescue nil
41       _, status = Process.waitpid2(@pid)
42       @err.rewind
43       $stderr.write(@err.read)
44       assert status.success?, status.inspect
45     end
46     @to_close.each { |io| io.close unless io.closed? }
47     FileUtils.rm_rf(@tmpdir)
48   end
50   def test_inherit_bad
51     cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
52             "--maxconns=100" ]
53     pid = fork do
54       r, w = IO.pipe
55       maybe_cloexec(r, false)
56       maybe_cloexec(w, false)
57       $stderr.reopen(@err)
58       ENV["CMOGSTORED_FD"] = "#{r.fileno}"
59       exec(*cmd)
60     end
61     _, status = Process.waitpid2(pid)
62     assert ! status.success?, status.inspect
63     @err.rewind
64     assert_match(/getsockname.*failed/, @err.read)
65   end
67   def test_inherit
68     cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
69             "--maxconns=100" ]
70     vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
71     drop = TCPServer.new(@host, 0)
72     @to_close << drop
74     @pid = fork do
75       maybe_cloexec(drop, false)
76       maybe_cloexec(@srv, false)
77       ENV["CMOGSTORED_FD"] = "#{@srv.fileno},#{drop.fileno}"
78       exec(*cmd)
79     end
81     # just ensure HTTP works after being inherited
82     Net::HTTP.start(@host, @port) do |http|
83       [ Net::HTTP::Get, Net::HTTP::Head ].each do |meth|
84         resp = http.request(meth.new("/"))
85         assert_kind_of Net::HTTPOK, resp
86       end
87     end
89     # ensure inherited FDs get closed if they're unused
90     drop_port = drop.addr[1]
92     # still works since drop is open in _this_ process
93     c = TCPSocket.new(@host, drop_port)
94     @to_close << c
96     # drop is no longer valid after this:
97     drop.close
98     assert_raises(Errno::ECONNREFUSED) { TCPSocket.new(@host, drop_port) }
99   end
101   def test_inherit_fake
102     cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
103             "--maxconns=100" ]
104     @srv.close
105     pid = fork do
106       ENV["CMOGSTORED_FD"] = "666"
107       $stderr.reopen(@err)
108       exec(*cmd)
109     end
110     _, status = Process.waitpid2(pid)
111     assert ! status.success?, status.inspect
112   end
114   def test_inherit_bogus
115     cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
116             "--maxconns=100" ]
117     pid = fork do
118       maybe_cloexec(@srv, false)
119       ENV["CMOGSTORED_FD"] = "#{@srv.fileno};"
120       $stderr.reopen(@err)
121       exec(*cmd)
122     end
123     _, status = Process.waitpid2(pid)
124     @err.rewind
125     assert_match(/invalid byte: 59$/, @err.read)
126     assert ! status.success?, status.inspect
127   end
129   def test_inherit_high
130     cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
131             "--maxconns=100" ]
132     pid = fork do
133       maybe_cloexec(@srv, false)
134       fd = 0xffffffffffffffffffffffffff.to_s
135       ENV["CMOGSTORED_FD"] = "#{@srv.fileno},#{fd}"
136       $stderr.reopen(@err)
137       exec(*cmd)
138     end
139     _, status = Process.waitpid2(pid)
140     assert ! status.success?, status.inspect
141     @err.rewind
142     assert_match(/failed to parse/, @err.read)
143   end
145   def test_inherit_systemd
146     # disabled test on old Rubies: https://bugs.ruby-lang.org/issues/11336
147     # [ruby-core:69895] [Bug #11336] fixed by r51576
148     return unless RUBY_VERSION.to_f >= 2.3
150     mgmt = TCPServer.new(@host, 0)
151     @to_close << mgmt
152     mport = mgmt.addr[1]
153     cmd = %W(cmogstored --docroot=#@tmpdir --httplisten=#@host:#@port
154              --mgmtlisten=#@host:#{mport} --maxconns=100)
155     cmd << { 3 => mgmt.fileno, 4 => @srv.fileno }
156     @pid = fork do
157       ENV['LISTEN_PID'] = "#$$"
158       ENV['LISTEN_FDS'] = '2'
159       exec(*cmd)
160     end
162     # just ensure HTTP works after being inherited
163     Net::HTTP.start(@host, @port) do |http|
164       [ Net::HTTP::Get, Net::HTTP::Head ].each do |meth|
165         resp = http.request(meth.new("/"))
166         assert_kind_of Net::HTTPOK, resp
167       end
168     end
170     # still works since drop is open in _this_ process
171     c = TCPSocket.new(@host, mport)
172     @to_close << c
173     c.write "hello\n"
174     assert_match(/ERROR: unknown command/, c.gets)
175   end