mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob de27c535bc1edf246b65b9513170780bce71a7c2 6479 bytes (raw)
$ git show pu:test/test_mogstored_rack.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
 
# -*- encoding: binary -*-
require "./test/fresh"
begin
  require 'mogstored_rack'
  require 'unicorn'
  ok = true
rescue LoadError
  ok = false
end

class TestMogstoredRack < Test::Unit::TestCase
  include TestFreshSetup
  def setup
    setup_mogilefs
  end

  def test_range_put_new_file
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain

    io = client.new_file "range0", :largefile => :content_range
    assert_nil io.close
    assert_equal "", client.get_file_data("range0")

    io = client.new_file "writes", :largefile => :content_range
    %w(a b c d e).each { |x| io.write(x) }
    assert_nil io.close
    assert_equal "abcde", client.get_file_data("writes")

    io = client.new_file "puts", :largefile => :content_range
    %w(a b c d e).each { |x| io.puts(x) }
    assert ! client.exist?("puts")
    assert_nil io.close
    assert_equal "a\nb\nc\nd\ne\n", client.get_file_data("puts")
  end

  def test_stream_new_file
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    client.new_file("chunky", :largefile => :stream) do |io|
      assert_instance_of MogileFS::NewFile::Stream, io
      assert_equal(5, io.write("HELLO"))
      assert_nil io.md5
    end
    assert_equal "HELLO", client.get_file_data("chunky")

    io = client.new_file("puts", :largefile => :stream)
    assert_instance_of MogileFS::NewFile::Stream, io
    assert_equal io, IO.select(nil, [io])[1][0], "IO.select-able"

    assert_nil(io.puts("PUTS!"))
    assert_nil(io.puts("PUTZ"))
    assert_nil io.close
    assert_equal "PUTS!\nPUTZ\n", client.get_file_data("puts")

    io = client.new_file("putc", :largefile => :stream)
    assert_equal(0x20, io.putc(0x20))
    assert_nil io.close
    assert_equal " ", client.get_file_data("putc")

    io = client.new_file("print splat", :largefile => :stream)
    io.print(1, 2, 3)
    assert_nil io.close
    assert_equal "123", client.get_file_data("print splat")

    io = client.new_file("printf", :largefile => :stream)
    assert_nil io.printf("%x", 1638)
    assert_nil io.close
    assert_equal "666", client.get_file_data("printf")

    io = client.new_file("syswrite", :largefile => :stream)
    assert_equal 4, io.syswrite("good")
    assert_equal 7, io.syswrite("morning")
    assert_nil io.close
    assert_equal "goodmorning", client.get_file_data("syswrite")

    io = client.new_file("md5", :largefile=>:stream, :content_md5=>:trailer)
    assert_instance_of Digest::MD5, io.md5
    assert_nil io.puts("HIHI")
    assert_nil io.close
    assert_equal "HIHI\n", client.get_file_data("md5")
    assert_equal Digest::MD5.hexdigest("HIHI\n"), io.md5.hexdigest

    io = client.new_file("<<", :largefile=>:stream)
    assert_equal(io, io << ">>")
    assert_nil io.close
    assert_equal ">>", client.get_file_data("<<")
  end

  def test_stream_new_file_with_content_length
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    io = client.new_file("clen", :largefile=>:stream,:content_length=>6)
    io << "HIHIHI"
    assert_nil io.close
    assert_equal "HIHIHI", client.get_file_data("clen")

    io = client.new_file("clen", :largefile=>:stream,:content_length=>1)
    io << "FAIL"
    assert_raises(MogileFS::SizeMismatchError) { io.close }
    assert_equal "HIHIHI", client.get_file_data("clen")

    io = client.new_file("md5", :largefile=>:stream,
                                :content_length=>6, :content_md5=>:trailer)
    assert_equal(io, io << "MD5MD5")
    assert_nil io.close
    assert_equal "MD5MD5", client.get_file_data("md5")
    assert_equal Digest::MD5.hexdigest("MD5MD5"), io.md5.hexdigest

    io = client.new_file("md5", :largefile=>:stream,
                                :content_length=>6, :content_md5=>:trailer)
    assert_equal(io, io << "MD5MD")
    assert_raises(MogileFS::SizeMismatchError) { io.close }
    assert_equal Digest::MD5.hexdigest("MD5MD"), io.md5.hexdigest
  end

  def test_md5_check
    add_host_device_domain
    client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
    node = "#@test_host:#@mogstored_http_port"
    pid = fork do
      # not modifying this hash in the same process
      MogileFS::HTTPFile::MD5_TRAILER_NODES[node] = true
      client.store_content("md5_me", nil, "HELLO WORLD")
    end
    _, status = Process.waitpid2(pid)
    assert status.success?, status.inspect
    assert_equal "HELLO WORLD", client.get_file_data("md5_me")
  end

  def setup_mogstored
    @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
    Dir.mkdir("#@docroot/dev1")
    Dir.mkdir("#@docroot/dev2")
    @mogstored_mgmt = TCPServer.new(@test_host, 0)
    @mogstored_http = TCPServer.new(@test_host, 0)
    @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
    @mogstored_http_port = @mogstored_http.addr[1]
    @mogstored_conf = Tempfile.new(["mogstored", "conf"])
    @mogstored_pid = Tempfile.new(["mogstored", "pid"])
    @mogstored_conf.write <<EOF
pidfile = #{@mogstored_pid.path}
maxconns = 1000
mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
server = none
docroot = #@docroot
EOF
    @mogstored_conf.flush
    @mogstored_mgmt.close

    unicorn_setup

    x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
    wait_for_port @mogstored_mgmt_port
  end

  # I would use Rainbows! + *Threads + Ruby 1.9.3 in production
  def unicorn_setup
    @ru = Tempfile.new(%w(mogstored_rack .ru))
    @ru.write <<EOF
run MogstoredRack.new("#@docroot")
EOF
    @ru.flush

    @unicorn_pid = Tempfile.new(%w(unicorn .pid))
    @unicorn_conf = Tempfile.new(%w(unicorn.conf .rb))
    @unicorn_stderr = Tempfile.new(%w(unicorn .stderr))
    @unicorn_stdout = Tempfile.new(%w(unicorn .stdout))
    @unicorn_conf.write <<EOF
require "mogstored_rack"
listen "#@test_host:#{@mogstored_http_port}"
pid "#{@unicorn_pid.path}"
stderr_path "#{@unicorn_stderr.path}"
stdout_path "#{@unicorn_stdout.path}"
rewindable_input false
EOF
    @unicorn_conf.flush

    @mogstored_http.close
    x!("unicorn", "-E", "deployment",
       "--daemon", "--config", @unicorn_conf.path, @ru.path)
    wait_for_port @mogstored_http_port
    40.times do
      break if File.size(@unicorn_pid.path) > 0
      sleep 0.1
    end
  end

  def teardown
    pid = File.read(@unicorn_pid.path).to_i
    Process.kill(:QUIT, pid) if pid > 0
    teardown_mogilefs
    puts(@unicorn_stderr.read) if $DEBUG
  end
end if ok && `which unicorn`.chomp.size > 0

git clone https://yhbt.net/mogilefs-client.git