MogileFS::Mysql: list_keys returns nil if nothing was found
[ruby-mogilefs-client.git] / test / test_mogilefs.rb
blobfafb9cf05a04b63617ff3a6510e2178e2047656a
1 require 'test/setup'
2 require 'stringio'
3 require 'tempfile'
4 require 'fileutils'
6 class TestMogileFS__MogileFS < TestMogileFS
8   def setup
9     @klass = MogileFS::MogileFS
10     super
11   end
13   def test_initialize
14     assert_equal 'test', @client.domain
15     assert_equal @root, @client.root
17     assert_raises ArgumentError do
18       MogileFS::MogileFS.new :hosts => ['kaa:6001'], :root => '/mogilefs/test'
19     end
20   end
22   def test_get_file_data_http
23     socket = FakeSocket.new("HTTP/1.0 200 OK\r\n" \
24                             "Content-Length: 5\r\n\r\ndata!")
25     TCPSocket.sockets << socket
27     path1 = 'http://rur-1/dev1/0/000/000/0000000062.fid'
28     path2 = 'http://rur-2/dev2/0/000/000/0000000062.fid'
30     @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
32     assert_equal 'data!', @client.get_file_data('key')
33   end
35   def test_get_file_data_http_block
36     tmpfp = Tempfile.new('test_mogilefs.open_data')
37     nr = 100 # tested with 1000
38     chunk_size = 1024 * 1024
39     expect_size = nr * chunk_size
40     header = "HTTP/1.0 200 OK\r\n" \
41              "Content-Length: #{expect_size}\r\n\r\n"
42     assert_equal header.size, tmpfp.syswrite(header)
43     nr.times { assert_equal chunk_size, tmpfp.syswrite(' ' * chunk_size) }
44     assert_equal expect_size + header.size, File.size(tmpfp.path)
45     tmpfp.sysseek(0)
46     socket = FakeSocket.new(tmpfp)
47     TCPSocket.sockets << socket
49     path1 = 'http://rur-1/dev1/0/000/000/0000000062.fid'
50     path2 = 'http://rur-2/dev2/0/000/000/0000000062.fid'
52     @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
54     data = Tempfile.new('test_mogilefs.dest_data')
55     @client.get_file_data('key') do |fp|
56       buf = ''
57       read_nr = nr = 0
58       loop do
59         begin
60           fp.sysread(16384, buf)
61           read_nr = buf.size
62           nr += read_nr
63           assert_equal read_nr, data.syswrite(buf), "partial write"
64         rescue EOFError
65           break
66         end
67       end
68       assert_equal expect_size, nr, "size mismatch"
69     end
70   end
72   def test_get_paths
73     path1 = 'rur-1/dev1/0/000/000/0000000062.fid'
74     path2 = 'rur-2/dev2/0/000/000/0000000062.fid'
76     @backend.get_paths = { 'paths' => 2, 'path1' => path1, 'path2' => path2 }
78     expected = ["#{@root}/#{path1}", "#{@root}/#{path2}"]
80     assert_equal expected, @client.get_paths('key').sort
81   end
83   def test_get_paths_unknown_key
84     @backend.get_paths = ['unknown_key', '']
86     assert_raises MogileFS::Backend::UnknownKeyError do
87       assert_equal nil, @client.get_paths('key')
88     end
89   end
91   def test_delete_existing
92     @backend.delete = { }
93     assert_nothing_raised do
94       @client.delete 'no_such_key'
95     end
96   end
98   def test_delete_nonexisting
99     @backend.delete = 'unknown_key', ''
100     assert_raises MogileFS::Backend::UnknownKeyError do
101       @client.delete('no_such_key')
102     end
103   end
105   def test_delete_readonly
106     @client.readonly = true
107     assert_raises MogileFS::ReadOnlyError do
108       @client.delete 'no_such_key'
109     end
110   end
112   def test_each_key
113     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
114                            'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
115     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_4',
116                            'key_1' => 'new_key_3', 'key_2' => 'new_key_4' }
117     @backend.list_keys = { 'key_count' => 0, 'next_after' => 'new_key_4' }
118     keys = []
119     @client.each_key 'new' do |key|
120       keys << key
121     end
123     assert_equal %w[new_key_1 new_key_2 new_key_3 new_key_4], keys
124   end
126   def test_list_keys
127     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
128                            'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
130     keys, next_after = @client.list_keys 'new'
131     assert_equal ['new_key_1', 'new_key_2'], keys.sort
132     assert_equal 'new_key_2', next_after
133   end
135   def test_new_file_http
136     @client.readonly = true
137     assert_raises MogileFS::ReadOnlyError do
138       @client.new_file 'new_key', 'test'
139     end
140   end
142   def test_new_file_readonly
143     @client.readonly = true
144     assert_raises MogileFS::ReadOnlyError do
145       @client.new_file 'new_key', 'test'
146     end
147   end
149   def test_size_http
150     socket = FakeSocket.new <<-EOF
151 HTTP/1.0 200 OK\r
152 Content-Length: 5\r
153     EOF
155     TCPSocket.sockets << socket
157     path = 'http://example.com/path'
159     @backend.get_paths = { 'paths' => 1, 'path1' => path }
161     assert_equal 5, @client.size('key')
163     socket.write_s.rewind
165     assert_equal "HEAD /path HTTP/1.0\r\n", socket.write_s.gets
167     assert_equal ['example.com', 80], TCPSocket.connections.shift
168     assert_empty TCPSocket.connections
169   end
171   def test_size_nfs
172     path = File.join @root, 'path'
174     File.open path, 'w' do |fp| fp.write 'data!' end
176     @backend.get_paths = { 'paths' => 1, 'path1' => 'path' }
178     assert_equal 5, @client.size('key')
179   end
181   def test_store_content_http
182     socket = FakeSocket.new 'HTTP/1.0 200 OK'
184     TCPSocket.sockets << socket
186     @backend.create_open = {
187       'devid' => '1',
188       'path' => 'http://example.com/path',
189     }
191     @client.store_content 'new_key', 'test', 'data'
193     expected = <<-EOF.chomp
194 PUT /path HTTP/1.0\r
195 Content-Length: 4\r
197 data
198     EOF
200     assert_equal expected, socket.write_s.string
202     assert_equal ['example.com', 80], TCPSocket.connections.shift
203     assert_empty TCPSocket.connections
204   end
206   def test_store_content_http_fail
207     TCPSocket.sockets << FakeSocket.new('HTTP/1.0 500 Internal Server Error')
209     @backend.create_open = {
210       'devid' => '1',
211       'path' => 'http://example.com/path',
212     }
214     assert_raises MogileFS::HTTPFile::BadResponseError do
215       @client.store_content 'new_key', 'test', 'data'
216     end
217   end
219   def test_store_content_http_empty
220     socket = FakeSocket.new 'HTTP/1.0 200 OK'
222     TCPSocket.sockets << socket
224     @backend.create_open = {
225       'devid' => '1',
226       'path' => 'http://example.com/path',
227     }
229     @client.store_content 'new_key', 'test', ''
231     expected = <<-EOF
232 PUT /path HTTP/1.0\r
233 Content-Length: 0\r
235     EOF
237     assert_equal expected, socket.write_s.string
239     assert_equal ['example.com', 80], TCPSocket.connections.shift
240     assert_empty TCPSocket.connections
241   end
243   def test_store_content_nfs
244     @backend.create_open = {
245       'dev_count' => '1',
246       'devid_1' => '1',
247       'path_1' => '/path',
248     }
250     @client.store_content 'new_key', 'test', 'data'
252     dest_file = File.join(@root, 'path')
254     assert File.exist?(dest_file)
255     assert_equal 'data', File.read(dest_file)
256   end
258   def test_store_content_nfs_empty
259     @backend.create_open = {
260       'dev_count' => '1',
261       'devid_1' => '1',
262       'path_1' => '/path',
263     }
265     @client.store_content 'new_key', 'test', ''
267     dest_file = File.join(@root, 'path')
269     assert File.exist?(dest_file)
270     assert_equal '', File.read(dest_file)
271   end
273   def test_store_content_readonly
274     @client.readonly = true
276     assert_raises MogileFS::ReadOnlyError do
277       @client.store_content 'new_key', 'test', nil
278     end
279   end
281   def test_store_file_readonly
282     @client.readonly = true
283     assert_raises MogileFS::ReadOnlyError do
284       @client.store_file 'new_key', 'test', nil
285     end
286   end
288   def test_rename_existing
289     @backend.rename = {}
291     assert_nil @client.rename('from_key', 'to_key')
292   end
294   def test_rename_nonexisting
295     @backend.rename = 'unknown_key', ''
297     assert_raises MogileFS::Backend::UnknownKeyError do
298       @client.rename('from_key', 'to_key')
299     end
300   end
302   def test_rename_no_key
303     @backend.rename = 'no_key', 'no_key'
305     e = assert_raises MogileFS::Backend::NoKeyError do
306       @client.rename 'new_key', 'test'
307     end
309     assert_equal 'no_key', e.message
310   end
312   def test_rename_readonly
313     @client.readonly = true
315     e = assert_raises MogileFS::ReadOnlyError do
316       @client.rename 'new_key', 'test'
317     end
319     assert_equal 'readonly mogilefs', e.message
320   end
322   def test_sleep
323     @backend.sleep = {}
324     assert_nothing_raised do
325       assert_equal({}, @client.sleep(2))
326     end
327   end