Ruby mogilefs-client 3.12.2
[ruby-mogilefs-client.git] / test / test_mogtool_bigfile.rb
blobf5dc47336af5a1e1937c88a5e9449183a0a66afb
1 # -*- encoding: binary -*-
2 require "./test/fresh"
3 require "net/http"
4 ok = true
5 unless File.executable?(`which mogtool 2>/dev/null`.strip)
6   warn "mogtool not found, skipping #{__FILE__}"
7   ok = false
8 end
10 class TestMogtoolBigfile < Test::Unit::TestCase
11   include TestFreshSetup
12   buf = File.open("/dev/urandom") { |fp| fp.read(1024) }
13   buf *= 1024
14   RAND = Tempfile.new("rand")
15   RAND.sync = true
16   sha1 = Digest::SHA1.new
17   100.times { sha1 << buf; RAND.write(buf) }
18   buf = nil
19   RAND_SHA1 = sha1.hexdigest
21   def setup
22     setup_mogilefs
23     add_host_device_domain
24     RAND.rewind
25     @big_uuid = "big-#{uuid}"
26     @client = MogileFS::MogileFS.new(:hosts => @trackers, :domain => @domain)
27   end
29   alias teardown teardown_mogilefs
31   def mogtool!(*args)
32     x!("mogtool", "--trackers=#{@trackers.join(',')}",
33        "--domain=#@domain", *args)
34   end
36   # the mogtool definition of gzip is wrong and just raw zlib deflate
37   def test_bigfile_gzip_mogtool
38     mogtool!("inject", "--gzip", "--bigfile", RAND.path, @big_uuid)
39     sha1_check
40   end
42   def test_bigfile_mogtool
43     mogtool!("inject", "--bigfile", RAND.path, @big_uuid)
44     sha1_check
46     # ensure fallback works for rebalanced/replaced files
47     part1 = "#@big_uuid,1"
48     tmp = tmpfile("part1")
49     before_uris = @client.get_uris(part1)
50     @client.get_file_data(part1, tmp)
51     @client.delete(part1)
52     @client.store_file(part1, nil, tmp.path)
53     wait_for_DELETE(before_uris)
54     sha1_check
56     # corrupt the existing data in part1
57     @client.store_content(part1, nil, "HELLO")
58     @client.get_uris(part1)
60     # corruption is detected on verify
61     junk = tmpfile("junk")
62     assert_raises(MogileFS::ChecksumMismatchError) do
63       @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => true)
64     end
66     # corruption is NOT detected on verify
67     junk = tmpfile("junk")
68     @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => false)
70     # restoring no-corrupted data succeeds!
71     @client.store_file(part1, nil, tmp.path)
72     sha1_check
74     # missing parts fail
75     before_uris = @client.get_uris(part1)
76     @client.delete(part1)
77     junk = tmpfile("junk")
78     assert_raises(MogileFS::Backend::UnknownKeyError) do
79       @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => true)
80     end
81   end
83   def wait_for_DELETE(uris)
84     uris.each do |uri|
85       tries = 0
86       begin
87         Net::HTTP.start(uri.host, uri.port) do |http|
88           sleep(0.1) while Net::HTTPOK === http.head(uri.path)
89         end
90       rescue
91         if (tries += 1) < 666
92           sleep(0.1)
93           retry
94         end
95         raise
96       end
97     end
98   end
100   def sha1_check
101     r, w = IO.pipe
102     @to_close << r
103     @to_close << w
104     th = Thread.new do
105       sha1 = Digest::SHA1.new
106       buf = ""
107       while r.read(16384, buf)
108         sha1 << buf
109       end
110       sha1.hexdigest
111     end
112     res = @client.bigfile_write("_big_info:#@big_uuid", w, :verify => true)
113     w.close
114     read_sha1 = th.value
115     assert_equal RAND_SHA1, read_sha1
116     assert_equal RAND.size, res[0]
117   end
118 end if ok