tests: create fresh intances for all integration tests
[ruby-mogilefs-client.git] / test / test_mogtool_bigfile.rb
blob01de78ea4cda4a6de3b0f1a917227a01556e5793
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   def mogtool!(*args)
30     x!("mogtool", "--trackers=#{@trackers.join(',')}",
31        "--domain=#@domain", *args)
32   end
34   # the mogtool definition of gzip is wrong and just raw zlib deflate
35   def test_bigfile_gzip_mogtool
36     mogtool!("inject", "--gzip", "--bigfile", RAND.path, @big_uuid)
37     sha1_check
38   end
40   def test_bigfile_mogtool
41     mogtool!("inject", "--bigfile", RAND.path, @big_uuid)
42     sha1_check
44     # ensure fallback works for rebalanced/replaced files
45     part1 = "#@big_uuid,1"
46     tmp = tmpfile("part1")
47     before_uris = @client.get_uris(part1)
48     @client.get_file_data(part1, tmp)
49     @client.delete(part1)
50     @client.store_file(part1, nil, tmp.path)
51     wait_for_DELETE(before_uris)
52     sha1_check
54     # corrupt the existing data in part1
55     @client.store_content(part1, nil, "HELLO")
56     @client.get_uris(part1)
58     # corruption is detected on verify
59     junk = tmpfile("junk")
60     assert_raises(MogileFS::ChecksumMismatchError) do
61       @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => true)
62     end
64     # corruption is NOT detected on verify
65     junk = tmpfile("junk")
66     @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => false)
68     # restoring no-corrupted data succeeds!
69     @client.store_file(part1, nil, tmp.path)
70     sha1_check
72     # missing parts fail
73     before_uris = @client.get_uris(part1)
74     @client.delete(part1)
75     junk = tmpfile("junk")
76     assert_raises(MogileFS::Backend::UnknownKeyError) do
77       @client.bigfile_write("_big_info:#@big_uuid", junk, :verify => true)
78     end
79   end
81   def wait_for_DELETE(uris)
82     uris.each do |uri|
83       tries = 0
84       begin
85         Net::HTTP.start(uri.host, uri.port) do |http|
86           sleep(0.1) while Net::HTTPOK === http.head(uri.path)
87         end
88       rescue
89         if (tries += 1) < 666
90           sleep(0.1)
91           retry
92         end
93         raise
94       end
95     end
96   end
98   def sha1_check
99     r, w = IO.pipe
100     @to_close << r
101     @to_close << w
102     th = Thread.new do
103       sha1 = Digest::SHA1.new
104       buf = ""
105       while r.read(16384, buf)
106         sha1 << buf
107       end
108       sha1.hexdigest
109     end
110     res = @client.bigfile_write("_big_info:#@big_uuid", w, :verify => true)
111     w.close
112     read_sha1 = th.value
113     assert_equal RAND_SHA1, read_sha1
114     assert_equal RAND.size, res[0]
115   end
116 end if ok