6 class TestMogileFS__MogileFS < TestMogileFS
9 @klass = MogileFS::MogileFS
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'
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')
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)
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|
60 fp.sysread(16384, buf)
63 assert_equal read_nr, data.syswrite(buf), "partial write"
68 assert_equal expect_size, nr, "size mismatch"
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
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')
91 def test_delete_existing
93 assert_nothing_raised do
94 @client.delete 'no_such_key'
98 def test_delete_nonexisting
99 @backend.delete = 'unknown_key', ''
100 assert_raises MogileFS::Backend::UnknownKeyError do
101 @client.delete('no_such_key')
105 def test_delete_readonly
106 @client.readonly = true
107 assert_raises MogileFS::ReadOnlyError do
108 @client.delete 'no_such_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' }
119 @client.each_key 'new' do |key|
123 assert_equal %w[new_key_1 new_key_2 new_key_3 new_key_4], 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
135 def test_new_file_http
136 @client.readonly = true
137 assert_raises MogileFS::ReadOnlyError do
138 @client.new_file 'new_key', 'test'
142 def test_new_file_readonly
143 @client.readonly = true
144 assert_raises MogileFS::ReadOnlyError do
145 @client.new_file 'new_key', 'test'
150 socket = FakeSocket.new <<-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
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')
181 def test_store_content_http
182 socket = FakeSocket.new 'HTTP/1.0 200 OK'
184 TCPSocket.sockets << socket
186 @backend.create_open = {
188 'path' => 'http://example.com/path',
191 @client.store_content 'new_key', 'test', 'data'
193 expected = <<-EOF.chomp
200 assert_equal expected, socket.write_s.string
202 assert_equal ['example.com', 80], TCPSocket.connections.shift
203 assert_empty TCPSocket.connections
206 def test_store_content_http_empty
207 socket = FakeSocket.new 'HTTP/1.0 200 OK'
209 TCPSocket.sockets << socket
211 @backend.create_open = {
213 'path' => 'http://example.com/path',
216 @client.store_content 'new_key', 'test', ''
224 assert_equal expected, socket.write_s.string
226 assert_equal ['example.com', 80], TCPSocket.connections.shift
227 assert_empty TCPSocket.connections
230 def test_store_content_nfs
231 @backend.create_open = {
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)
245 def test_store_content_nfs_empty
246 @backend.create_open = {
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)
260 def test_store_content_readonly
261 @client.readonly = true
263 assert_raises MogileFS::ReadOnlyError do
264 @client.store_content 'new_key', 'test', nil
268 def test_store_file_readonly
269 @client.readonly = true
270 assert_raises MogileFS::ReadOnlyError do
271 @client.store_file 'new_key', 'test', nil
275 def test_rename_existing
278 assert_nil @client.rename('from_key', 'to_key')
281 def test_rename_nonexisting
282 @backend.rename = 'unknown_key', ''
284 assert_raises MogileFS::Backend::UnknownKeyError do
285 @client.rename('from_key', 'to_key')
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'
296 assert_equal 'no_key', e.message
299 def test_rename_readonly
300 @client.readonly = true
302 e = assert_raises MogileFS::ReadOnlyError do
303 @client.rename 'new_key', 'test'
306 assert_equal 'readonly mogilefs', e.message
311 assert_nothing_raised do
312 assert_equal({}, @client.sleep(2))