cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob bdaf161e2b42a58638166731e3eca9b4a378287a 9166 bytes (raw)
$ git show v0.3.0:test/cmogstored-cfg.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
require 'test/test_helper'
require 'stringio'
require 'net/http'

class TestCmogstoredConfig < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-cfg-test')
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close
    cmd = [ "cmogstored" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    @cmd = cmd
    @pid = nil
    @to_close = []
  end

  def teardown
    @to_close.each { |io| io.close unless io.closed? }
    FileUtils.rm_rf(@tmpdir)
  end

  def pre_kill
    # need to ensure connections are accepted before we can safely kill
    c = get_client
    c.write "HI\n"
    assert_kind_of String, c.gets
  end

  def children(pid = @pid)
    pids = `ps --ppid #{pid} --no-headers -o pid 2>/dev/null`.strip
    if RUBY_PLATFORM =~ /linux/
      assert $?.success?, $?.inspect
    end
    pids.split(/\s+/).grep(/\A\d+\z/).map { |x| x.to_i }.sort
  end

  def test_worker_processes
    nproc = 2
    @cmd << "--worker-processes=#{nproc}"
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--mgmtlisten=#@host:#@port"
    tmp = Tempfile.new("err")
    @pid = fork do
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    pre_kill # ensure workers are running
    pids = children

    # kill gently
    pids.each do |pid|
      begin
        Process.kill(:QUIT, pid)
        t_yield
      rescue Errno::ESRCH
        break
      end while true
    end

    pre_kill # ensure workers are respawned

    if pids[0]
      p pids if $VERBOSE
      new_pids = children
      assert pids != new_pids, "new=#{new_pids.inspect} old=#{pids.inspect}"
      pids.each do |pid|
        assert_raises(Errno::ESRCH) { Process.kill(0, pid) }
      end
    end

    # kill brutally
    pids = children
    pids.each do |pid|
      begin
        Process.kill(:KILL, pid)
        t_yield
      rescue Errno::ESRCH
        break
      end while true
    end

    pre_kill # ensure workers are respawned

    if pids[0]
      p pids if $VERBOSE
      new_pids = children
      assert pids != new_pids, "new=#{new_pids.inspect} old=#{pids.inspect}"
      pids.each do |pid|
        assert_raises(Errno::ESRCH) { Process.kill(0, pid) }
      end
    end

    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
    if new_pids && new_pids[0]
      new_pids.each do |pid|
        assert_raises(Errno::ESRCH) { Process.kill(0, pid) }
      end
    end
  end

  def test_config_file
    tmp = Tempfile.new("cmogstored-cfg-test")
    tmp.write("mgmtlisten = #@host:#@port\n")
    tmp.write("maxconns = 50\n")
    tmp.write("docroot = #@tmpdir")
    tmp.flush
    @cmd << "--config=#{tmp.path}"
    @pid = fork { exec(*@cmd) }
    pre_kill
    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
  end

  def test_multi_config_file_fail
    Dir.mkdir("#@tmpdir/a")
    Dir.mkdir("#@tmpdir/b")
    tmp = Tempfile.new("cmogstored-cfg-test")
    tmp.write("mgmtlisten = #@host:#@port\n")
    tmp.write("maxconns = 50\n")
    tmp.write("docroot = #@tmpdir/a")
    tmp.flush
    @cmd << "--config=#{tmp.path}"

    tmp = Tempfile.new("cmogstored-cfg-test")
    tmp.write("mgmtlisten = #@host:#@port\n")
    tmp.write("maxconns = 50\n")
    tmp.write("docroot = #@tmpdir/b")
    tmp.flush
    @cmd << "--config=#{tmp.path}"
    cmd = @cmd.join(' ')
    msg = `#{cmd} 2>&1`
    assert ! $?.success?, $?.inspect
    assert_match(/--multi/, msg)
  end

  def test_stale_pidfile
    pidfile = Tempfile.new("cmogstored.test_stale_pidfile")
    tmppid = fork {}
    _, status = Process.waitpid2(tmppid)
    assert status.success?, status.inspect
    pidfile.puts tmppid
    pidfile.flush
    @cmd << "--docroot=#{Dir.pwd}"
    @cmd << "--maxconns=50"
    @cmd << "--httplisten=#@host:#@port"
    @cmd << "--pidfile=#{pidfile.path}"
    @pid = fork { exec(*@cmd) }
    pre_kill
    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
  end

  def test_active_pidfile
    pidfile = Tempfile.new("cmogstored.test_active_pidfile")
    tmperr = Tempfile.new("cmogstored.tmperr")
    tmppid = fork { sleep }
    pidfile.puts tmppid
    pidfile.flush
    @cmd << "--docroot=#{Dir.pwd}"
    @cmd << "--maxconns=50"
    @cmd << "--httplisten=#@host:#@port"
    @cmd << "--pidfile=#{pidfile.path}"
    @pid = fork {
      $stderr.reopen(tmperr)
      exec(*@cmd)
    }
    _, status = Process.waitpid2(@pid)
    assert ! status.success?, status.inspect
    Process.kill(:KILL, tmppid)
    pid, status = Process.waitpid2(tmppid)
    assert_equal tmppid, pid, status.inspect

    tmperr.rewind
    assert_equal "already running on PID: #{tmppid}\n",
                 tmperr.readlines.grep(/already running/)[0]
  end

  def rand_port
    srv = TCPServer.new(@host, 0)
    port = srv.addr[1]
    srv.close
    port
  end

  def test_multi_config
    http_a = @port
    http_b = rand_port
    mgmt_a = rand_port
    mgmt_b = rand_port
    site_a = Tempfile.new("site_a")
    site_b = Tempfile.new("site_b")
    FileUtils.mkpath(@tmpdir + "/a/dev123")
    FileUtils.mkpath(@tmpdir + "/b/dev456")
    site_a.puts "docroot = #@tmpdir/a"
    site_a.puts "httplisten = #@host:#{http_a}"
    site_a.puts "mgmtlisten = #@host:#{mgmt_a}"
    site_a.flush
    site_b.puts "docroot = #@tmpdir/b"
    site_b.puts "httplisten = #@host:#{http_b}"
    site_b.puts "mgmtlisten = #@host:#{mgmt_b}"
    site_b.flush
    @cmd << "--multi"
    @cmd << "--maxconns=100"
    @cmd << "--config=#{site_a.path}"
    @cmd << "--config=#{site_b.path}"
    @pid = fork do
      exec(*@cmd)
    end

    ma = get_client(300, mgmt_a)
    mb = get_client(300, mgmt_b)

    ma.write "MD5 /dev123/usage\r\n"
    assert_match(%r{\A/dev123/usage MD5=[a-f0-9]{32}\r\n}, ma.gets)
    mb.write "MD5 /dev123/usage\r\n"
    assert_equal "/dev123/usage MD5=-1\r\n", mb.gets

    ma.write "MD5 /dev456/usage\r\n"
    assert_equal "/dev456/usage MD5=-1\r\n", ma.gets
    mb.write "MD5 /dev456/usage\r\n"
    assert_match(%r{\A/dev456/usage MD5=[a-f0-9]{32}\r\n}, mb.gets)

    if RUBY_PLATFORM =~ /linux/
      ma.write "watch\r\n"
      mb.write "watch\r\n"
      2.times do
        assert_kind_of String, ma.gets
        assert_kind_of String, mb.gets
      end
      a_line = ma.gets
      b_line = mb.gets
      assert_match(/^123\t/, a_line)
      assert_match(/^456\t/, b_line)
    end

    Net::HTTP.start(@host, http_a) do |http|
      resp = http.request(Net::HTTP::Get.new("/dev123/usage"))
      assert_kind_of Net::HTTPOK, resp
      resp = http.request(Net::HTTP::Get.new("/dev456/usage"))
      assert_kind_of Net::HTTPNotFound, resp
    end
    Net::HTTP.start(@host, http_b) do |http|
      resp = http.request(Net::HTTP::Get.new("/dev123/usage"))
      assert_kind_of Net::HTTPNotFound, resp
      resp = http.request(Net::HTTP::Get.new("/dev456/usage"))
      assert_kind_of Net::HTTPOK, resp
    end

    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
  end

  def test_server_none
    http = rand_port
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--server=none"
    @cmd << "--mgmtlisten=#@host:#@port"
    @cmd << "--httplisten=#@host:#{http}"
    tmp = Tempfile.new('err')
    @pid = fork do
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    pre_kill
    assert_raises(Errno::ECONNREFUSED) { TCPSocket.new(@host, http) }
    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
  end

  def test_server_perlbal
    http = rand_port
    FileUtils.mkpath("#@tmpdir/dev666")
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--server=perlbal"
    @cmd << "--mgmtlisten=#@host:#@port"
    @cmd << "--httplisten=#@host:#{http}"
    tmp = Tempfile.new('err')
    @pid = fork do
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    pre_kill
    Net::HTTP.start(@host, http) do |c|
      put = Net::HTTP::Put.new("/dev666/foo")
      body = StringIO.new("BODY!")
      put.content_type = "application/octet-stream"
      put.body_stream = body
      put.content_length = body.size
      resp = c.request(put)
      assert_equal 201, resp.code.to_i
      assert_equal "BODY!", IO.read("#@tmpdir/dev666/foo")
    end
    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
    tmp.rewind
    assert_match(/W: using internal HTTP for 'server = perlbal'/, tmp.read)
  end

  def test_unsupported_servers
    FileUtils.mkpath("#@tmpdir/dev666")
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--httplisten=#@host:#@port"
    tmp = Tempfile.new("err")

    %w(apache lighttpd nginx).each do |srv|
      cmd = @cmd.dup
      cmd << "--server=#{srv}"
      tmp.rewind
      tmp.truncate(0)
      pid = fork do
        $stderr.reopen(tmp)
        exec(*cmd)
      end
      _, status = Process.waitpid2(pid)
      assert ! status.success?, status.inspect
      tmp.rewind
      assert_match(%r{E: 'server = #{srv}' not understood by cmogstored},
                   tmp.read)
    end
  end
end

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