cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 07dfc6ab064d63695b2668b659aff8720d8132cd 15342 bytes (raw)
$ git show empty-header-values: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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
require 'test/test_helper'
require 'stringio'
require 'net/http'
require 'timeout'

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
    # just because a client gets a response from one thread does not
    # mean they all started.
    t_yield
  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 expand_suppressions!(cmd)
    cmd.map! do |x|
      case x
      when /\A--suppressions=(\S+)\z/
        "--suppressions=#{File.expand_path($1)}"
      else
        x
      end
    end
  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

    # ensure USR2 (upgrade for master) is no-op for children
    running = children
    10.times do
      running.each do |pid|
        Process.kill(:USR2, pid)
      end
      pre_kill # ensure workers are still running
    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_match(/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

  def test_http_only_no_usage
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--httplisten=#@host:#@port"
    tmp = Tempfile.new('err')
    Dir.mkdir("#@tmpdir/dev666")
    File.open("#@tmpdir/dev666/cmogstored.test", "a") { |fp| fp.write "HI" }
    @pid = fork do
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    get_client
    Net::HTTP.start(@host, @port) do |http|
      resp = http.request(Net::HTTP::Get.new("/dev666/usage"))
      assert_kind_of Net::HTTPNotFound, resp
      resp = http.request(Net::HTTP::Get.new("/dev666/cmogstored.test"))
      assert_kind_of Net::HTTPOK, resp
      assert_equal "HI", resp.body
    end
    Process.kill(:QUIT, @pid)
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
  end

  def test_worker_processes_mgmt_shutdown
    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
    mgmt = get_client
    pre_kill
    pids = children
    mgmt.write "shutdown\n"
    assert_nil mgmt.gets
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
    pids.each do |pid|
      100.times do
        begin
          Process.kill(0, pid)
          sleep 0.1
        rescue Errno::ESRCH
          break
        end
      end
      assert_raises(Errno::ESRCH) { Process.kill(0, pid) }
    end
  end

  def PATH_env_has_relpath(badpath)
    @cmd << "--docroot=#@tmpdir"
    @cmd << "--daemonize"
    @cmd << "--mgmtlisten=#@host:#@port"
    tmp = Tempfile.new("err")
    @pid = fork do
      ENV["PATH"] = badpath
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    _, status = Process.waitpid2(@pid)
    assert ! status.success?, "#{status.inspect} badpath=#{badpath.inspect}"
    tmp.rewind
    lines = tmp.read
    assert_match(/PATH environment contains relative path/, lines)
    assert_match(/relative paths are incompatible with --daemonize/, lines)
  end

  def test_PATH_env_has_relpath
    [
      "#{ENV["PATH"]}::#{ENV["PATH"]}",
      "#{ENV["PATH"]}:",
      "#{ENV["PATH"]}:.",
      ".:#{ENV["PATH"]}",
      ":#{ENV["PATH"]}"
    ].each do |badpath|
      PATH_env_has_relpath(badpath)
    end
  end

  def test_docroot_has_relpath
    @cmd << "--daemonize"
    @cmd << "--mgmtlisten=#@host:#@port"
    tmp = Tempfile.new("err")
    dirname, basename = File.split(@tmpdir)
    @cmd << "--docroot=#{basename}"
    @pid = fork do
      expand_suppressions!(@cmd)
      Dir.chdir(dirname)
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    _, status = Process.waitpid2(@pid)
    assert ! status.success?, status.inspect
    tmp.rewind
    lines = tmp.read
    assert_match(/docroot=#{basename} must use an absolute/, lines)
    assert_match(/relative paths are incompatible with --daemonize/, lines)
  end

  def test_config_is_relpath
    cfg = Tempfile.new("cfg")
    cfg.puts "mgmtlisten = #@host:#@port"
    cfg.puts "docroot = #@tmpdir"
    cfg.puts "daemonize"
    cfg.flush
    dirname, basename = File.split(cfg.path)
    @cmd << "--config=#{basename}"
    tmp = Tempfile.new("err")
    @pid = fork do
      expand_suppressions!(@cmd)
      Dir.chdir(dirname)
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    _, status = Process.waitpid2(@pid)
    assert ! status.success?, status.inspect
    tmp.rewind
    lines = tmp.read
    assert_match(/config=#{basename} must use an absolute/, lines)
    assert_match(/relative paths are incompatible with --daemonize/, lines)
  end

  def test_config_has_pidfile_relpath
    pid = Tempfile.new("pid")
    dirname, basename = File.split(pid.path)
    cfg = Tempfile.new("cfg")
    cfg.puts "mgmtlisten = #@host:#@port"
    cfg.puts "docroot = #@tmpdir"
    cfg.puts "pidfile = #{basename}"
    cfg.puts "daemonize = 1"
    cfg.flush
    @cmd << "--config=#{cfg.path}"
    tmp = Tempfile.new("err")
    @pid = fork do
      expand_suppressions!(@cmd)
      Dir.chdir(dirname)
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    _, status = Process.waitpid2(@pid)
    assert ! status.success?, status.inspect
    tmp.rewind
    lines = tmp.read
    assert_match(/pidfile=#{basename} must use an absolute/, lines)
    assert_match(/relative paths are incompatible with --daemonize/, lines)
  end

  def test_daemonized_config_has_pidfile
    pid = Tempfile.new("pid")
    cfg = Tempfile.new("cfg")
    cfg.puts "mgmtlisten = #@host:#@port"
    cfg.puts "docroot = #@tmpdir"
    cfg.puts "pidfile = #{pid.path}"
    cfg.puts "daemonize = 1"
    cfg.flush
    @cmd << "--config=#{cfg.path}"
    tmp = Tempfile.new("err")
    @pid = fork do
      expand_suppressions!(@cmd)
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    _, status = Process.waitpid2(@pid)
    assert status.success?, status.inspect
    pre_kill
    read_pid = File.read(pid.path)
    assert_match(%r{\A\d+\n\z}, read_pid)
    read_pid = read_pid.to_i
    assert_equal 1, Process.kill(0, read_pid)
    assert_equal 1, Process.kill(:QUIT, read_pid)
  end

  def test_daemonized_0
    pid = Tempfile.new("pid")
    cfg = Tempfile.new("cfg")
    cfg.puts "mgmtlisten = #@host:#@port"
    cfg.puts "docroot = #@tmpdir"
    cfg.puts "pidfile = #{pid.path}"
    cfg.puts "daemonize = 0"
    cfg.flush
    @cmd << "--config=#{cfg.path}"
    tmp = Tempfile.new("err")
    @pid = fork do
      expand_suppressions!(@cmd)
      $stderr.reopen(tmp)
      exec(*@cmd)
    end
    pre_kill
    read_pid = File.read(pid.path)
    assert_equal @pid, read_pid.to_i
    assert_equal 1, Process.kill(:QUIT, @pid)
  end
end

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