cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 694547c6e4f1e642cb2073400d785408d9beeb56 7421 bytes (raw)
$ git show v0.5.0:test/mgmt.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
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2012, Eric Wong <normalperson@yhbt.net>
# License: GPLv3 or later (see COPYING for details)
require 'test/test_helper'
require 'digest/md5'

class TestMgmt < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-mgmt-test')
    @to_close = []
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close
    @err = Tempfile.new("stderr")
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--mgmtlisten=#@host:#@port",
            "--maxconns=500" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    @pid = fork {
      $stderr.reopen(@err)
      @err.close
      exec(*cmd)
    }
    @client = get_client
  end

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

  def t(expect_out, input)
    expect_out += "\r\n"
    input += "\r\n"

    @client.write(input)
    output = @client.gets
    assert_equal expect_out, output
  end

  def test_size
    File.open("#@tmpdir/foo", "wb") { |fp| fp.write(' ' * 3) }
    t("/foo 3", "size /foo")
    t("/missing -1", "size /missing")

    Dir.mkdir("#@tmpdir/dev666")
    File.open("#@tmpdir/dev666/sausage", "wb").close
    t("/dev666/sausage 0", "size /dev666/sausage")
  end

  def test_unknown_command
    t("ERROR: unknown command: hello", "  hello  ")
    t("ERROR: unknown command: hello", "hello")
    t("ERROR: unknown command: size", "size bad")
  end

  def test_invalid_uri
    t("ERROR: uri invalid (contains ..)", "size /..")
    t("ERROR: uri invalid (contains ..)", "size /..")
    t("ERROR: uri invalid (contains ..)", "size /a/..")
    t("ERROR: uri invalid (contains ..)", "size /a/../b")
    longest = "/dev16777215/0/000/000/0123456789.fid"
    t("#{longest} -1", "size #{longest}")

    # non-sensical error, but whatever...
    too_long = longest + "-"
    t("ERROR: uri invalid (contains ..)", "size #{too_long}")
  end

  def test_no_command
    t("", "");
    t("", " ");
  end

  def test_md5
    buf = ' ' * 3
    expect = "/foo MD5=#{Digest::MD5.hexdigest(buf)}"
    File.open("#@tmpdir/foo", "wb") { |fp| fp.write(buf) }
    t(expect, "MD5 /foo")
    t("/missing MD5=-1", "MD5 /missing")
    %w(fsck create_close replicate).each do |reason|
      t(expect, "MD5 /foo #{reason}")
      t("/missing MD5=-1", "MD5 /missing #{reason}")
    end
  end

  def test_sha1
    buf = ' ' * 3
    expect = "/foo SHA-1=#{Digest::SHA1.hexdigest(buf)}"
    File.open("#@tmpdir/foo", "wb") { |fp| fp.write(buf) }
    t(expect, "SHA-1 /foo")
    t("/missing SHA-1=-1", "SHA-1 /missing")
    %w(fsck create_close replicate).each do |reason|
      t(expect, "SHA-1 /foo #{reason}")
      t("/missing SHA-1=-1", "SHA-1 /missing #{reason}")
    end
  end

  def test_continuous_feed
    n = 0
    File.open("#@tmpdir/-1.fid", "wb").close

    200.times do |i|
      File.rename("#@tmpdir/#{i-1}.fid", "#@tmpdir/#{i}.fid")
      n += @client.write("size /#{i}.fid\r\n")
      x = @client.gets
      assert_equal "/#{i}.fid 0\r\n", x
    end

    @client.write "bad command\r\n"
    assert_equal "ERROR: unknown command: bad\r\n", @client.gets
    @client.write "  bad command\r\n"
    assert_equal "ERROR: unknown command: bad\r\n", @client.gets

    @client.write "size /foo bar\r\n"
    assert_equal "ERROR: unknown command: size\r\n", @client.gets

    @client.write "size /foo\r\n"
    assert_equal "/foo -1\r\n", @client.gets
  end

  def test_continuous_feed_mt
    n = 0
    nr = 200
    pfx = rand.to_s
    delay = 0.01
    thr = Thread.new do
      nr.times do |i|
        sleep delay
        x = @client.gets
        assert_equal "/#{pfx}-#{i} -1\r\n", x
      end
    end

    nr.times do |i|
      n += @client.write("size /#{pfx}-#{i}\r\n")
    end
    delay = 0
    thr.join
  end

  def test_monster_line
    assert_raises(Errno::ECONNRESET, Errno::EPIPE) {
      @client.write("size /#{'0' * (400 * 1024)}\r\n")
      @client.gets
    }

    @client = TCPSocket.new(@host, @port)
    @to_close << @client
    t("/missing -1", "size /missing")
  end

  def test_trickle
    "size /missing\r\n".split(//).each do |c|
      @client.write c
      sleep 0.01
    end
    assert_equal "/missing -1\r\n", @client.gets
  end

  def test_client_eof
    t("/missing -1", "size /missing")
    @client.close
    @client = TCPSocket.new(@host, @port)
    @to_close << @client
    t("/missing -1", "size /missing")
  end

  def test_md5_feed
    buf = ' ' * 3
    50.times do |i|
      pfx = (rand * i).to_s[0..rand(30)] + "a"
      expect = "/#{pfx} MD5=#{Digest::MD5.hexdigest(buf)}\r\n"
      File.open("#@tmpdir/#{pfx}", "wb") { |fp| fp.write(buf) }
      nr = 100
      th = Thread.new { @client.write("MD5 /#{pfx}\r\n" * nr) }
      nr.times do |j|
        assert_equal expect, @client.gets, "j=#{j}" # pfx=#{pfx} i=#{i}"
      end
      th.join
    end
  end

  def test_size_huge
    Dir.mkdir("#@tmpdir/dev666")
    big = 2 * 1024 * 1024 * 1024 * 1020 # 2TB
    File.open("#@tmpdir/dev666/sparse-file.fid", "w") do |fp|
      begin
        fp.seek(big - 1)
      rescue Errno::EINVAL, Errno::ENOSPC
        big /= 2
        warn "trying large file size: #{big}"
        retry
      end
      fp.write('.')
    end
    t("/dev666/sparse-file.fid #{big}", "size /dev666/sparse-file.fid")
  rescue Errno::ENOSPC
  end

  def test_concurrent_md5_fsck
    Dir.mkdir("#@tmpdir/dev666")
    big = 1024 * 1024 * 500 # only 500M
    big /= 10 if ENV["VALGRIND"] # valgrind slows us down enough :P
    File.open("#@tmpdir/dev666/sparse-file.fid", "w") do |fp|
      begin
        fp.seek(big - 1)
      rescue Errno::EINVAL, Errno::ENOSPC
        big /= 2
        warn "trying large file size: #{big}"
        retry
      end
      fp.write('.')
    end

    threads = (0..5).map do
      Thread.new do
        c = get_client
        c.write("MD5 /dev666/sparse-file.fid fsck\r\n")
        c.gets
      end
    end
    answers = {}
    threads.each do |thr|
      val = thr.value
      answers[val] = true
      assert_match(%r{\A/dev666/sparse-file\.fid MD5=[a-f0-9]{32}\r\n}, val)
    end
    assert_equal 1, answers.size
  rescue Errno::ENOSPC
  end

  def test_iostat_watch
    Dir.mkdir("#@tmpdir/dev666")
    @client.write "watch\n"

    # wait for iostat to catch up
    2.times { assert_kind_of String, @client.gets }
    util = RUBY_PLATFORM =~ /linux/ ? %r{\d+\.\d\d} : %r{\d+(?:\.\d+)?}
    assert_match(/^666\t#{util}\n/, @client.gets)
    assert_equal ".\n", @client.gets
  end if `which iostat 2>/dev/null`.chomp.size != 0 &&
         RUBY_PLATFORM !~ /kfreebsd-gnu/

  def test_iostat_watch_multidir
    Dir.mkdir("#@tmpdir/dev666")
    Dir.mkdir("#@tmpdir/dev999")
    @client.write "watch\n"

    # wait for iostat to catch up
    3.times { assert_kind_of String, @client.gets }
    util = RUBY_PLATFORM =~ /linux/ ? %r{\d+\.\d\d} : %r{\d+(?:\.\d+)?}
    lines = []
    lines << @client.gets
    lines << @client.gets
    assert_match(/^(666|999)\t#{util}\n/, lines[0])
    assert_match(/^(666|999)\t#{util}\n/, lines[1])
    assert_not_equal(lines[0], lines[1])

    assert_equal ".\n", @client.gets
  end if `which iostat 2>/dev/null`.chomp.size != 0 &&
         RUBY_PLATFORM !~ /kfreebsd-gnu/
end

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