remove 'open-uri' dependency
[ruby-mogilefs-client.git] / test / test_mogilefs.rb
blobe6970f7ae7c3d8b9e196a1d71fa7f6aed33b98ab
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_equal nil, @client.get_paths('key')
87   end
89   def test_delete_existing
90     @backend.delete = { }
91     assert_nothing_raised do
92       @client.delete 'no_such_key'
93     end
94   end
96   def test_delete_nonexisting
97     @backend.delete = 'unknown_key', ''
98     assert_nothing_raised do
99       assert_equal nil, @client.delete('no_such_key')
100     end
101   end
103   def test_delete_readonly
104     @client.readonly = true
105     assert_raises RuntimeError do
106       @client.delete 'no_such_key'
107     end
108   end
110   def test_each_key
111     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
112                            'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
113     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_4',
114                            'key_1' => 'new_key_3', 'key_2' => 'new_key_4' }
115     @backend.list_keys = { 'key_count' => 0, 'next_after' => 'new_key_4' }
116     keys = []
117     @client.each_key 'new' do |key|
118       keys << key
119     end
121     assert_equal %w[new_key_1 new_key_2 new_key_3 new_key_4], keys
122   end
124   def test_list_keys
125     @backend.list_keys = { 'key_count' => 2, 'next_after' => 'new_key_2',
126                            'key_1' => 'new_key_1', 'key_2' => 'new_key_2' }
128     keys, next_after = @client.list_keys 'new'
129     assert_equal ['new_key_1', 'new_key_2'], keys.sort
130     assert_equal 'new_key_2', next_after
131   end
133   def test_new_file_http
134     @client.readonly = true
135     assert_raises RuntimeError do
136       @client.new_file 'new_key', 'test'
137     end
138   end
140   def test_new_file_readonly
141     @client.readonly = true
142     assert_raises RuntimeError do
143       @client.new_file 'new_key', 'test'
144     end
145   end
147   def test_size_http
148     socket = FakeSocket.new <<-EOF
149 HTTP/1.0 200 OK\r
150 Content-Length: 5\r
151     EOF
153     TCPSocket.sockets << socket
155     path = 'http://example.com/path'
157     @backend.get_paths = { 'paths' => 1, 'path1' => path }
159     assert_equal 5, @client.size('key')
161     socket.write_s.rewind
163     assert_equal "HEAD /path HTTP/1.0\r\n", socket.write_s.gets
165     assert_equal ['example.com', 80], TCPSocket.connections.shift
166     assert_empty TCPSocket.connections
167   end
169   def test_size_nfs
170     path = File.join @root, 'path'
172     File.open path, 'w' do |fp| fp.write 'data!' end
174     @backend.get_paths = { 'paths' => 1, 'path1' => 'path' }
176     assert_equal 5, @client.size('key')
177   end
179   def test_store_content_http
180     socket = FakeSocket.new 'HTTP/1.0 200 OK'
182     TCPSocket.sockets << socket
184     @backend.create_open = {
185       'devid' => '1',
186       'path' => 'http://example.com/path',
187     }
189     @client.store_content 'new_key', 'test', 'data'
191     expected = <<-EOF.chomp
192 PUT /path HTTP/1.0\r
193 Content-Length: 4\r
195 data
196     EOF
198     assert_equal expected, socket.write_s.string
200     assert_equal ['example.com', 80], TCPSocket.connections.shift
201     assert_empty TCPSocket.connections
202   end
204   def test_store_content_http_empty
205     socket = FakeSocket.new 'HTTP/1.0 200 OK'
207     TCPSocket.sockets << socket
209     @backend.create_open = {
210       'devid' => '1',
211       'path' => 'http://example.com/path',
212     }
214     @client.store_content 'new_key', 'test', ''
216     expected = <<-EOF
217 PUT /path HTTP/1.0\r
218 Content-Length: 0\r
220     EOF
222     assert_equal expected, socket.write_s.string
224     assert_equal ['example.com', 80], TCPSocket.connections.shift
225     assert_empty TCPSocket.connections
226   end
228   def test_store_content_nfs
229     @backend.create_open = {
230       'dev_count' => '1',
231       'devid_1' => '1',
232       'path_1' => '/path',
233     }
235     @client.store_content 'new_key', 'test', 'data'
237     dest_file = File.join(@root, 'path')
239     assert File.exist?(dest_file)
240     assert_equal 'data', File.read(dest_file)
241   end
243   def test_store_content_nfs_empty
244     @backend.create_open = {
245       'dev_count' => '1',
246       'devid_1' => '1',
247       'path_1' => '/path',
248     }
250     @client.store_content 'new_key', 'test', ''
252     dest_file = File.join(@root, 'path')
254     assert File.exist?(dest_file)
255     assert_equal '', File.read(dest_file)
256   end
258   def test_store_content_readonly
259     @client.readonly = true
261     assert_raises RuntimeError do
262       @client.store_content 'new_key', 'test', nil
263     end
264   end
266   def test_store_file_readonly
267     @client.readonly = true
268     assert_raises RuntimeError do
269       @client.store_file 'new_key', 'test', nil
270     end
271   end
273   def test_rename_existing
274     @backend.rename = {}
276     assert_nil @client.rename('from_key', 'to_key')
277   end
279   def test_rename_nonexisting
280     @backend.rename = 'unknown_key', ''
282     assert_nil @client.rename('from_key', 'to_key')
283   end
285   def test_rename_no_key
286     @backend.rename = 'no_key', ''
288     e = assert_raises RuntimeError do
289       @client.rename 'new_key', 'test'
290     end
292     assert_equal 'unable to rename new_key to test: no_key', e.message
293   end
295   def test_rename_readonly
296     @client.readonly = true
298     e = assert_raises RuntimeError do
299       @client.rename 'new_key', 'test'
300     end
302     assert_equal 'readonly mogilefs', e.message
303   end
305   def test_sleep
306     @backend.sleep = {}
307     assert_nothing_raised do
308       assert_equal({}, @client.sleep(2))
309     end
310   end