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_equal nil, @client.get_paths('key')
89 def test_delete_existing
91 assert_nothing_raised do
92 @client.delete 'no_such_key'
96 def test_delete_nonexisting
97 @backend.delete = 'unknown_key', ''
98 assert_nothing_raised do
99 assert_equal nil, @client.delete('no_such_key')
103 def test_delete_readonly
104 @client.readonly = true
105 assert_raises RuntimeError do
106 @client.delete 'no_such_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' }
117 @client.each_key 'new' do |key|
121 assert_equal %w[new_key_1 new_key_2 new_key_3 new_key_4], 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
133 def test_new_file_http
134 @client.readonly = true
135 assert_raises RuntimeError do
136 @client.new_file 'new_key', 'test'
140 def test_new_file_readonly
141 @client.readonly = true
142 assert_raises RuntimeError do
143 @client.new_file 'new_key', 'test'
148 socket = FakeSocket.new <<-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
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')
179 def test_store_content_http
180 socket = FakeSocket.new 'HTTP/1.0 200 OK'
182 TCPSocket.sockets << socket
184 @backend.create_open = {
186 'path' => 'http://example.com/path',
189 @client.store_content 'new_key', 'test', 'data'
191 expected = <<-EOF.chomp
198 assert_equal expected, socket.write_s.string
200 assert_equal ['example.com', 80], TCPSocket.connections.shift
201 assert_empty TCPSocket.connections
204 def test_store_content_http_empty
205 socket = FakeSocket.new 'HTTP/1.0 200 OK'
207 TCPSocket.sockets << socket
209 @backend.create_open = {
211 'path' => 'http://example.com/path',
214 @client.store_content 'new_key', 'test', ''
222 assert_equal expected, socket.write_s.string
224 assert_equal ['example.com', 80], TCPSocket.connections.shift
225 assert_empty TCPSocket.connections
228 def test_store_content_nfs
229 @backend.create_open = {
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)
243 def test_store_content_nfs_empty
244 @backend.create_open = {
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)
258 def test_store_content_readonly
259 @client.readonly = true
261 assert_raises RuntimeError do
262 @client.store_content 'new_key', 'test', nil
266 def test_store_file_readonly
267 @client.readonly = true
268 assert_raises RuntimeError do
269 @client.store_file 'new_key', 'test', nil
273 def test_rename_existing
276 assert_nil @client.rename('from_key', 'to_key')
279 def test_rename_nonexisting
280 @backend.rename = 'unknown_key', ''
282 assert_nil @client.rename('from_key', 'to_key')
285 def test_rename_no_key
286 @backend.rename = 'no_key', ''
288 e = assert_raises RuntimeError do
289 @client.rename 'new_key', 'test'
292 assert_equal 'unable to rename new_key to test: no_key', e.message
295 def test_rename_readonly
296 @client.readonly = true
298 e = assert_raises RuntimeError do
299 @client.rename 'new_key', 'test'
302 assert_equal 'readonly mogilefs', e.message
307 assert_nothing_raised do
308 assert_equal({}, @client.sleep(2))