cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob b342ab058b0adab6f82623b62507486bfc0b7c93 4159 bytes (raw)
$ git show v1.4.2:test/inherit.rb	# shows this blob on the CLI

  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
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2012-2015 all contributors <cmogstored-public@bogomips.org>
# License: GPLv3 or later (see COPYING for details)
require 'test/test_helper'
require 'net/http'

class TestInherit < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-inherit-test')
    @to_close = []
    @host = TEST_HOST
    @srv = TCPServer.new(@host, 0)
    @to_close << @srv
    @port = @srv.addr[1]
    @err = Tempfile.new("stderr")
    @pid = nil
    @exec_fds = {}
  end

  # Ruby 1.8 did not have the close_on_exec= method, but it
  # also defaulted to close_on_exec=false (same as POSIX)
  # 2.0.0 will be the first release with close_on_exec=true
  # by default, but 1.9 already added the close_on_exec= method
  def maybe_cloexec(io, val)
    if io.respond_to?(:close_on_exec=)
      io.close_on_exec = val
      @exec_fds[io.fileno] = io
    end
  end

  # FD inheritance is explicit in Ruby 2.0.0
  def exec(*cmd)
    cmd << @exec_fds if @exec_fds.size > 0
    Process.exec(*cmd)
  end

  def teardown
    if @pid
      Process.kill(:QUIT, @pid) rescue nil
      _, status = Process.waitpid2(@pid)
      @err.rewind
      $stderr.write(@err.read)
      assert status.success?, status.inspect
    end
    @to_close.each { |io| io.close unless io.closed? }
    FileUtils.rm_rf(@tmpdir)
  end

  def test_inherit_bad
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=100" ]
    pid = fork do
      r, w = IO.pipe
      maybe_cloexec(r, false)
      maybe_cloexec(w, false)
      $stderr.reopen(@err)
      ENV["CMOGSTORED_FD"] = "#{r.fileno}"
      exec(*cmd)
    end
    _, status = Process.waitpid2(pid)
    assert ! status.success?, status.inspect
    @err.rewind
    assert_match(/getsockname.*failed/, @err.read)
  end

  def test_inherit
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=100" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    drop = TCPServer.new(@host, 0)
    @to_close << drop

    @pid = fork do
      maybe_cloexec(drop, false)
      maybe_cloexec(@srv, false)
      ENV["CMOGSTORED_FD"] = "#{@srv.fileno},#{drop.fileno}"
      exec(*cmd)
    end

    # just ensure HTTP works after being inherited
    Net::HTTP.start(@host, @port) do |http|
      [ Net::HTTP::Get, Net::HTTP::Head ].each do |meth|
        resp = http.request(meth.new("/"))
        assert_kind_of Net::HTTPOK, resp
      end
    end

    # ensure inherited FDs get closed if they're unused
    drop_port = drop.addr[1]

    # still works since drop is open in _this_ process
    c = TCPSocket.new(@host, drop_port)
    assert_instance_of(TCPSocket, c)
    @to_close << c

    # drop is no longer valid after this:
    drop.close
    assert_raises(Errno::ECONNREFUSED) { TCPSocket.new(@host, drop_port) }
  end

  def test_inherit_fake
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=100" ]
    @srv.close
    pid = fork do
      ENV["CMOGSTORED_FD"] = "666"
      $stderr.reopen(@err)
      exec(*cmd)
    end
    _, status = Process.waitpid2(pid)
    assert ! status.success?, status.inspect
  end

  def test_inherit_bogus
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=100" ]
    pid = fork do
      maybe_cloexec(@srv, false)
      ENV["CMOGSTORED_FD"] = "#{@srv.fileno};"
      $stderr.reopen(@err)
      exec(*cmd)
    end
    _, status = Process.waitpid2(pid)
    @err.rewind
    assert_match(/invalid byte: 59$/, @err.read)
    assert ! status.success?, status.inspect
  end

  def test_inherit_high
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=100" ]
    pid = fork do
      maybe_cloexec(@srv, false)
      fd = 0xffffffffffffffffffffffffff.to_s
      ENV["CMOGSTORED_FD"] = "#{@srv.fileno},#{fd}"
      $stderr.reopen(@err)
      exec(*cmd)
    end
    _, status = Process.waitpid2(pid)
    assert ! status.success?, status.inspect
    @err.rewind
    assert_match(/failed to parse/, @err.read)
  end
end

git clone https://yhbt.net/cmogstored.git