test_mogilefs: fix bad test due to partial read
[ruby-mogilefs-client.git] / test / test_mogilefs_integration.rb
blob18ecd21de6f1b616e135cb4f15fbaf190daf7022
1 # -*- encoding: binary -*-
2 require './test/integration'
4 class TestMogileFSIntegration < TestMogIntegration
5   def setup
6     super
7     @client = MogileFS::MogileFS.new(:hosts => @trackers, :domain => @domain)
8   end
10   def test_CRUD
11     assert_equal 4, @client.store_content("CRUD", "default", "DATA")
12     assert_equal 4, @client.size("CRUD")
13     assert_equal "DATA", @client.get_file_data("CRUD")
14     assert_equal "DAT", @client.get_file_data("CRUD", nil, 3)
15     assert_equal "AT", @client.get_file_data("CRUD", nil, 2, 1)
17     tmp = Tempfile.new("z")
18     tmp_path = tmp.path
19     tmp.close!
20     assert_equal 4, @client.get_file_data("CRUD", tmp_path)
21     assert_equal "DATA", File.read(tmp_path)
22     st = File.stat(tmp_path)
23     assert_equal 0100600, st.mode
24     File.unlink(tmp_path)
26     sio = StringIO.new("")
27     rv = @client.get_file_data("CRUD", sio)
28     assert_equal 4, rv
29     assert_equal "DATA", sio.string
30     assert_equal 8, @client.store_content("CRUD", "default", "MOARDATA")
31     assert_equal "MOARDATA", @client.get_file_data("CRUD")
32     assert_equal true, @client.delete("CRUD")
33     assert_raises(MogileFS::Backend::UnknownKeyError) { @client.delete("CRUD") }
35     data = "hello world\n".freeze
36     tmp = tmpfile("blob")
37     tmp.sync = true
38     tmp.write(data)
39     tmp.rewind
40     assert_equal tmp.size, @client.store_file("blob", nil, tmp)
41     assert_equal(data, @client.get_file_data("blob"))
43     data = "pipe!\n".freeze
44     r, w = IO.pipe
45     th = Thread.new do
46       w.write(data)
47       w.close
48     end
49     assert_equal data.size, @client.store_file("pipe", nil, r)
50     assert_nothing_raised do
51       r.close
52       th.join
53     end
54     assert_equal(data, @client.get_file_data("pipe"))
56     cbk = MogileFS::Util::StoreContent.new(nil) do |write_callback|
57       10.times { write_callback.call("data") }
58     end
59     assert_nil cbk.length
60     nr = @client.store_content('store_content', nil, cbk)
61     assert_equal 40, nr
62     assert_equal("data" * 10, @client.get_file_data('store_content'))
63   end
65   def test_store_non_rewindable
66     tmp = Object.new
67     def tmp.size
68       666
69     end
71     assert_raises(MogileFS::HTTPFile::NonRetryableError) do
72       @client.store_file("non_rewindable", nil, tmp)
73     end
74   end
76   def test_file_info
77     assert_equal 3, @client.store_content("file_info", "default", "FOO")
78     res = @client.file_info("file_info")
79     assert_kind_of Integer, res["fid"]
80     assert_equal 3, res["length"]
81     assert ! res.include?("devids")
82     assert_kind_of Integer, res["devcount"]
84     res = @client.file_info("file_info", :devices => true)
85     assert_kind_of Integer, res["fid"]
86     assert_equal 3, res["length"]
87     assert_kind_of Integer, res["devcount"]
88     devids = res.delete("devids")
89     assert_instance_of Array, devids
90     devids.each { |devid| assert_kind_of Integer, devid }
91     assert_equal res["devcount"], devids.size
92   end
94   def test_file_debug
95     assert_equal 3, @client.store_content("file_debug", "default", "BUG")
96     a = @client.file_debug("file_debug")
97     b = @client.file_debug(:key => "file_debug")
98     fid = @client.file_info("file_debug")["fid"]
99     c = @client.file_debug(fid)
100     d = @client.file_debug(:fid => fid)
102     [ a, b, c, d ].each do |res|
103       assert_equal fid, res["fid_fid"]
104       assert_equal 0, res["fid_classid"]
105       assert_equal "file_debug", res["fid_dkey"]
106       assert_equal 3, res["fid_length"]
107       assert_kind_of Array, res["devids"]
108       assert_kind_of Integer, res["devids"][0]
109       res["devids"].each do |devid|
110         uri = URI.parse(res["devpath_#{devid}"])
111         assert_equal "http", uri.scheme
112       end
113       assert_equal "default", res["fid_class"]
114     end
115     @client.delete("file_debug")
116     rv = @client.file_debug(fid)
117     assert rv.keys.grep(/\Afid_/).empty?, rv.inspect
118   end
120   def test_file_debug_in_progress
121     rv = @client.new_file("file_debug_in_progress") do |http_file|
122       http_file << "ZZZZ"
123       dests = http_file.instance_variable_get(:@dests)
124       dests[0][1] =~ %r{/(\d+)\.fid\z}
125       fid = $1.to_i
126       rv = @client.file_debug(fid)
127       devids = dests.map { |x| x[0].to_i }.sort
128       assert_equal devids, rv["tempfile_devids"].sort
129       assert_equal "file_debug_in_progress", rv["tempfile_dkey"]
130     end
131     assert_equal 4, rv
132   end
134   def test_admin_get_devices
135     admin = MogileFS::Admin.new(:hosts => @trackers)
136     devices = admin.get_devices
137     if any_device = devices[0]
138       %w(mb_asof mb_free mb_used mb_total devid weight hostid).each do |field|
139         case value = any_device[field]
140         when nil
141         when Integer
142           assert value >= 0, "#{field}=#{value.inspect} is negative"
143         else
144           assert false, "#{field}=#{value.inspect} is #{value.class}"
145         end
146       end
148       field = "utilization"
149       case value = any_device[field]
150       when nil
151       when Float
152         assert value >= 0.0, "#{field}=#{value.inspect} is negative"
153       else
154         assert false, "#{field}=#{value.inspect} is #{value.class}"
155       end
156     end
157   end
159   # TODO: move this to a fresh instance
160   def test_admin_each_fid
161     admin = MogileFS::Admin.new(:hosts => @trackers)
162     seen = {}
163     count = admin.each_fid do |info|
164       seen[info["fid"]] = true
165       assert_kind_of Integer, info["fid"]
166       assert_kind_of Integer, info["length"]
167       assert_kind_of Integer, info["devcount"]
168       assert_kind_of String, info["key"]
169       assert_kind_of String, info["class"]
170       assert_kind_of String, info["domain"]
171     end
172     assert_equal count, seen.size
173   end if ENV["TEST_EXPENSIVE"]