6 class TestMogileFS__MogileFS < TestMogileFS
10 @klass = MogileFS::MogileFS
15 assert_equal 'test', @client.domain
17 assert_raises ArgumentError do
18 MogileFS::MogileFS.new :hosts => ['kaa:6001']
22 def test_get_file_data_http
23 tmp = Tempfile.new('accept')
24 accept = File.open(tmp.path, "ab")
25 svr = Proc.new do |serv, port|
26 client, client_addr = serv.accept
28 readed = client.recv(4096, 0)
30 %r{\AGET /dev[12]/0/000/000/0000000062\.fid HTTP/1.[01]\r\n\r\n\Z})
32 client.send("HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\ndata!", 0)
35 t1 = TempServer.new(svr)
36 t2 = TempServer.new(svr)
37 path1 = "http://127.0.0.1:#{t1.port}/dev1/0/000/000/0000000062.fid"
38 path2 = "http://127.0.0.1:#{t2.port}/dev2/0/000/000/0000000062.fid"
40 @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
42 assert_equal 'data!', @client.get_file_data('key')
43 assert_equal 1, accept.stat.size
45 TempServer.destroy_all!
48 def test_get_file_data_http_not_found_failover
49 tmp = Tempfile.new('accept')
50 accept = File.open(tmp.path, 'ab')
51 svr1 = Proc.new do |serv, port|
52 client, client_addr = serv.accept
54 readed = client.recv(4096, 0)
56 %r{\AGET /dev1/0/000/000/0000000062\.fid HTTP/1.[01]\r\n\r\n\Z})
57 client.send("HTTP/1.0 404 Not Found\r\n\r\ndata!", 0)
62 svr2 = Proc.new do |serv, port|
63 client, client_addr = serv.accept
65 readed = client.recv(4096, 0)
67 %r{\AGET /dev2/0/000/000/0000000062\.fid HTTP/1.[01]\r\n\r\n\Z})
68 client.send("HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\ndata!", 0)
73 t1 = TempServer.new(svr1)
74 t2 = TempServer.new(svr2)
75 path1 = "http://127.0.0.1:#{t1.port}/dev1/0/000/000/0000000062.fid"
76 path2 = "http://127.0.0.1:#{t2.port}/dev2/0/000/000/0000000062.fid"
77 @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
79 assert_equal 'data!', @client.get_file_data('key')
80 assert_equal 2, accept.stat.size
82 TempServer.destroy_all!
85 def test_get_file_data_http_block
86 tmpfp = Tempfile.new('test_mogilefs.open_data')
88 chunk_size = 1024 * 1024
89 expect_size = nr * chunk_size
90 header = "HTTP/1.0 200 OK\r\n" \
91 "Content-Length: #{expect_size}\r\n\r\n"
92 assert_equal header.size, tmpfp.syswrite(header)
93 nr.times { assert_equal chunk_size, tmpfp.syswrite(' ' * chunk_size) }
94 assert_equal expect_size + header.size, File.size(tmpfp.path)
97 accept = Tempfile.new('accept')
98 svr = Proc.new do |serv, port|
99 client, client_addr = serv.accept
102 readed = client.recv(4096, 0)
104 %r{\AGET /dev[12]/0/000/000/0000000062\.fid HTTP/1.[01]\r\n\r\n\Z})
105 sysrwloop(tmpfp, client)
109 t1 = TempServer.new(svr)
110 t2 = TempServer.new(svr)
111 path1 = "http://127.0.0.1:#{t1.port}/dev1/0/000/000/0000000062.fid"
112 path2 = "http://127.0.0.1:#{t2.port}/dev2/0/000/000/0000000062.fid"
114 @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
116 data = Tempfile.new('test_mogilefs.dest_data')
118 @client.get_file_data('key') do |fp|
122 fp.sysread(16384, buf)
125 assert_equal read_nr, data.syswrite(buf), "partial write"
133 assert_equal expect_size, nr, "size mismatch"
134 assert_equal 1, accept.stat.size
138 path1 = 'http://rur-1/dev1/0/000/000/0000000062.fid'
139 path2 = 'http://rur-2/dev2/0/000/000/0000000062.fid'
141 @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
143 expected = [ path1, path2 ]
145 assert_equal expected, @client.get_paths('key').sort
148 def test_get_paths_unknown_key
149 @backend.get_paths = ['unknown_key', '']
151 assert_raises MogileFS::Backend::UnknownKeyError do
152 assert_equal nil, @client.get_paths('key')
156 def test_delete_existing
157 @backend.delete = { }
158 assert_nothing_raised do
159 @client.delete 'no_such_key'
163 def test_delete_nonexisting
164 @backend.delete = 'unknown_key', ''
165 assert_raises MogileFS::Backend::UnknownKeyError do
166 @client.delete('no_such_key')
170 def test_delete_readonly
171 @client.readonly = true
172 assert_raises MogileFS::ReadOnlyError do
173 @client.delete 'no_such_key'
178 @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
179 'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
180 @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_4',
181 'key_1' => 'new_key_3', 'key_2' => 'new_key_4' }
182 @backend.list_keys = { 'key_count' => 0, 'next_after' => 'new_key_4' }
184 @client.each_key 'new' do |key|
188 assert_equal %w[new_key_1 new_key_2 new_key_3 new_key_4], keys
192 @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
193 'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
195 keys, next_after = @client.list_keys 'new'
196 assert_equal ['new_key_1', 'new_key_2'], keys.sort
197 assert_equal 'new_key_2', next_after
200 def test_list_keys_block
201 @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
202 'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
203 http_resp = "HTTP/1.0 200 OK\r\nContent-Length: %u\r\n"
204 srv = Proc.new do |serv, port, size|
205 client, client_addr = serv.accept
207 readed = client.readpartial(4096)
208 assert %r{\AHEAD } =~ readed
209 client.send(http_resp % size, 0)
212 t1 = TempServer.new(Proc.new { |serv, port| srv.call(serv, port, 5) })
213 t2 = TempServer.new(Proc.new { |serv, port| srv.call(serv, port, 5) })
214 t3 = TempServer.new(Proc.new { |serv, port| srv.call(serv, port, 10) })
215 @backend.get_paths = { 'paths' => 2,
216 'path1' => "http://127.0.0.1:#{t1.port}/",
217 'path2' => "http://127.0.0.1:#{t2.port}/" }
218 @backend.get_paths = { 'paths' => 1,
219 'path1' => "http://127.0.0.1:#{t3.port}/" }
222 keys, next_after = @client.list_keys('new') do |key,length,devcount|
223 res << [ key, length, devcount ]
226 expect_res = [ [ 'new_key_1', 5, 2 ], [ 'new_key_2', 10, 1 ] ]
227 assert_equal expect_res, res
228 assert_equal ['new_key_1', 'new_key_2'], keys.sort
229 assert_equal 'new_key_2', next_after
231 TempServer.destroy_all!
234 def test_new_file_http
235 @client.readonly = true
236 assert_raises MogileFS::ReadOnlyError do
237 @client.new_file 'new_key', 'test'
241 def test_new_file_readonly
242 @client.readonly = true
243 assert_raises MogileFS::ReadOnlyError do
244 @client.new_file 'new_key', 'test'
249 accept = Tempfile.new('accept')
250 t = TempServer.new(Proc.new do |serv,port|
251 client, client_addr = serv.accept
253 readed = client.recv(4096, 0) rescue nil
255 assert_equal "HEAD /path HTTP/1.0\r\n\r\n", readed
256 client.send("HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\n", 0)
260 path = "http://127.0.0.1:#{t.port}/path"
261 @backend.get_paths = { 'paths' => 1, 'path1' => path }
263 assert_equal 5, @client.size('key')
264 assert_equal 1, accept.stat.size
267 def test_bad_size_http
268 tmp = Tempfile.new('accept')
269 t = TempServer.new(Proc.new do |serv,port|
270 client, client_addr = serv.accept
272 readed = client.recv(4096, 0) rescue nil
273 assert_equal "HEAD /path HTTP/1.0\r\n\r\n", readed
274 client.send("HTTP/1.0 404 Not Found\r\nContent-Length: 5\r\n\r\n", 0)
279 path = "http://127.0.0.1:#{t.port}/path"
280 @backend.get_paths = { 'paths' => 1, 'path1' => path }
282 assert_nil @client.size('key')
283 assert_equal 1, tmp.stat.size
286 def test_store_content_http
287 received = Tempfile.new('recieved')
288 expected = "PUT /path HTTP/1.0\r\nContent-Length: 4\r\n\r\ndata"
290 t = TempServer.new(Proc.new do |serv, accept|
291 client, client_addr = serv.accept
293 received.syswrite(client.recv(4096, 0))
294 client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
298 @backend.create_open = {
300 'path' => "http://127.0.0.1:#{t.port}/path",
303 @client.store_content 'new_key', 'test', 'data'
306 assert_equal expected, received.sysread(4096)
308 TempServer.destroy_all!
312 def test_store_content_with_writer_callback
313 received = Tempfile.new('recieved')
314 expected = "PUT /path HTTP/1.0\r\nContent-Length: 40\r\n\r\n"
318 t = TempServer.new(Proc.new do |serv, accept|
319 client, client_addr = serv.accept
323 buf = client.readpartial(8192) or break
324 break if buf.length == 0
325 assert_equal buf.length, received.syswrite(buf)
327 break if nr >= expected.size
329 client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
333 @backend.create_open = {
335 'path' => "http://127.0.0.1:#{t.port}/path",
338 cbk = MogileFS::Util::StoreContent.new(40) do |write_callback|
340 write_callback.call("data")
343 @client.store_content('new_key', 'test', cbk)
346 assert_equal expected, received.sysread(4096)
348 TempServer.destroy_all!
351 def test_store_content_multi_dest_failover
352 received1 = Tempfile.new('received')
353 received2 = Tempfile.new('received')
354 expected = "PUT /path HTTP/1.0\r\nContent-Length: 4\r\n\r\ndata"
356 t1 = TempServer.new(Proc.new do |serv, accept|
357 client, client_addr = serv.accept
359 received1.syswrite(client.recv(4096, 0))
360 client.send("HTTP/1.0 500 Internal Server Error\r\n\r\n", 0)
364 t2 = TempServer.new(Proc.new do |serv, accept|
365 client, client_addr = serv.accept
367 received2.syswrite(client.recv(4096, 0))
368 client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
372 @backend.create_open = {
375 'path_1' => "http://127.0.0.1:#{t1.port}/path",
377 'path_2' => "http://127.0.0.1:#{t2.port}/path",
380 @client.store_content 'new_key', 'test', 'data'
383 assert_equal expected, received1.sysread(4096)
384 assert_equal expected, received2.sysread(4096)
386 TempServer.destroy_all!
389 def test_store_content_http_fail
390 t = TempServer.new(Proc.new do |serv, accept|
391 client, client_addr = serv.accept
394 client.send("HTTP/1.0 500 Internal Server Error\r\n\r\n", 0)
398 @backend.create_open = {
400 'path' => "http://127.0.0.1:#{t.port}/path",
403 assert_raises MogileFS::HTTPFile::BadResponseError do
404 @client.store_content 'new_key', 'test', 'data'
408 def test_store_content_http_empty
409 received = Tempfile.new('received')
410 expected = "PUT /path HTTP/1.0\r\nContent-Length: 0\r\n\r\n"
411 t = TempServer.new(Proc.new do |serv, accept|
412 client, client_addr = serv.accept
414 received.syswrite(client.recv(4096, 0))
415 client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
419 @backend.create_open = {
421 'path' => "http://127.0.0.1:#{t.port}/path",
424 @client.store_content 'new_key', 'test', ''
426 assert_equal expected, received.sysread(4096)
429 def test_store_content_nfs
430 @backend.create_open = {
435 assert_raises MogileFS::UnsupportedPathError do
436 @client.store_content 'new_key', 'test', 'data'
440 def test_new_file_http_large
441 expect = Tempfile.new('test_mogilefs.expect')
442 to_put = Tempfile.new('test_mogilefs.to_put')
443 received = Tempfile.new('test_mogilefs.received')
446 chunk_size = 1024 * 1024
447 expect_size = nr * chunk_size
449 header = "PUT /path HTTP/1.0\r\n" \
450 "Content-Length: #{expect_size}\r\n\r\n"
451 assert_equal header.size, expect.syswrite(header)
453 assert_equal chunk_size, expect.syswrite(' ' * chunk_size)
454 assert_equal chunk_size, to_put.syswrite(' ' * chunk_size)
456 assert_equal expect_size + header.size, expect.stat.size
457 assert_equal expect_size, to_put.stat.size
459 readed = Tempfile.new('readed')
460 t = TempServer.new(Proc.new do |serv, accept|
461 client, client_addr = serv.accept
465 buf = client.readpartial(8192) or break
466 break if buf.length == 0
467 assert_equal buf.length, received.syswrite(buf)
469 break if nr >= expect.stat.size
471 readed.syswrite("#{nr}")
472 client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
476 @backend.create_open = {
478 'path' => "http://127.0.0.1:#{t.port}/path",
481 @client.store_file('new_key', 'test', to_put.path)
483 assert_equal expect.stat.size, readed.sysread(4096).to_i
485 ENV['PATH'].split(/:/).each do |path|
486 cmp_bin = "#{path}/cmp"
487 File.executable?(cmp_bin) or next
488 # puts "running #{cmp_bin} #{expect.path} #{received.path}"
489 assert( system(cmp_bin, expect.path, received.path) )
494 TempServer.destroy_all!
497 def test_store_content_readonly
498 @client.readonly = true
500 assert_raises MogileFS::ReadOnlyError do
501 @client.store_content 'new_key', 'test', nil
505 def test_store_file_readonly
506 @client.readonly = true
507 assert_raises MogileFS::ReadOnlyError do
508 @client.store_file 'new_key', 'test', nil
512 def test_rename_existing
515 assert_nil @client.rename('from_key', 'to_key')
518 def test_rename_nonexisting
519 @backend.rename = 'unknown_key', ''
521 assert_raises MogileFS::Backend::UnknownKeyError do
522 @client.rename('from_key', 'to_key')
526 def test_rename_no_key
527 @backend.rename = 'no_key', 'no_key'
529 e = assert_raises MogileFS::Backend::NoKeyError do
530 @client.rename 'new_key', 'test'
533 assert_equal 'no_key', e.message
536 def test_rename_readonly
537 @client.readonly = true
539 e = assert_raises MogileFS::ReadOnlyError do
540 @client.rename 'new_key', 'test'
543 assert_equal 'readonly mogilefs', e.message
548 assert_nothing_raised do
549 assert_equal({}, @client.sleep(2))
555 # tested with 1000, though it takes a while
557 ENV['NR_CHUNKS'] ? ENV['NR_CHUNKS'].to_i : 10