Raise MogileFS::ReadOnly error for readonly instances
[ruby-mogilefs-client.git] / test / test_mogilefs.rb
blob6ac9d55dbb50d60e0ef4a83fef3824b1ce022e4b
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_empty
207     socket = FakeSocket.new 'HTTP/1.0 200 OK'
209     TCPSocket.sockets << socket
211     @backend.create_open = {
212       'devid' => '1',
213       'path' => 'http://example.com/path',
214     }
216     @client.store_content 'new_key', 'test', ''
218     expected = <<-EOF
219 PUT /path HTTP/1.0\r
220 Content-Length: 0\r
222     EOF
224     assert_equal expected, socket.write_s.string
226     assert_equal ['example.com', 80], TCPSocket.connections.shift
227     assert_empty TCPSocket.connections
228   end
230   def test_store_content_nfs
231     @backend.create_open = {
232       'dev_count' => '1',
233       'devid_1' => '1',
234       'path_1' => '/path',
235     }
237     @client.store_content 'new_key', 'test', 'data'
239     dest_file = File.join(@root, 'path')
241     assert File.exist?(dest_file)
242     assert_equal 'data', File.read(dest_file)
243   end
245   def test_store_content_nfs_empty
246     @backend.create_open = {
247       'dev_count' => '1',
248       'devid_1' => '1',
249       'path_1' => '/path',
250     }
252     @client.store_content 'new_key', 'test', ''
254     dest_file = File.join(@root, 'path')
256     assert File.exist?(dest_file)
257     assert_equal '', File.read(dest_file)
258   end
260   def test_store_content_readonly
261     @client.readonly = true
263     assert_raises MogileFS::ReadOnlyError do
264       @client.store_content 'new_key', 'test', nil
265     end
266   end
268   def test_store_file_readonly
269     @client.readonly = true
270     assert_raises MogileFS::ReadOnlyError do
271       @client.store_file 'new_key', 'test', nil
272     end
273   end
275   def test_rename_existing
276     @backend.rename = {}
278     assert_nil @client.rename('from_key', 'to_key')
279   end
281   def test_rename_nonexisting
282     @backend.rename = 'unknown_key', ''
284     assert_raises MogileFS::Backend::UnknownKeyError do
285       @client.rename('from_key', 'to_key')
286     end
287   end
289   def test_rename_no_key
290     @backend.rename = 'no_key', 'no_key'
292     e = assert_raises MogileFS::Backend::NoKeyError do
293       @client.rename 'new_key', 'test'
294     end
296     assert_equal 'no_key', e.message
297   end
299   def test_rename_readonly
300     @client.readonly = true
302     e = assert_raises MogileFS::ReadOnlyError do
303       @client.rename 'new_key', 'test'
304     end
306     assert_equal 'readonly mogilefs', e.message
307   end
309   def test_sleep
310     @backend.sleep = {}
311     assert_nothing_raised do
312       assert_equal({}, @client.sleep(2))
313     end
314   end