1 # -*- encoding: binary -*-
4 require 'mogstored_rack'
11 class TestMogstoredRack < Test::Unit::TestCase
12 include TestFreshSetup
17 def test_range_put_new_file
18 add_host_device_domain
19 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
21 io = client.new_file "range0", :largefile => :content_range
23 assert_equal "", client.get_file_data("range0")
25 io = client.new_file "writes", :largefile => :content_range
26 %w(a b c d e).each { |x| io.write(x) }
28 assert_equal "abcde", client.get_file_data("writes")
30 io = client.new_file "puts", :largefile => :content_range
31 %w(a b c d e).each { |x| io.puts(x) }
32 assert ! client.exist?("puts")
34 assert_equal "a\nb\nc\nd\ne\n", client.get_file_data("puts")
37 def test_stream_new_file
38 add_host_device_domain
39 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
40 client.new_file("chunky", :largefile => :chunked) do |io|
41 assert_instance_of MogileFS::HTTPStream, io
42 assert_equal(5, io.write("HELLO"))
45 assert_equal "HELLO", client.get_file_data("chunky")
47 io = client.new_file("puts", :largefile => :chunked)
48 assert_instance_of MogileFS::HTTPStream, io
49 assert_equal io, IO.select(nil, [io])[1][0], "IO.select-able"
51 assert_nil(io.puts("PUTS!"))
52 assert_nil(io.puts("PUTZ"))
54 assert_equal "PUTS!\nPUTZ\n", client.get_file_data("puts")
56 io = client.new_file("putc", :largefile => :chunked)
57 assert_equal(0x20, io.putc(0x20))
59 assert_equal " ", client.get_file_data("putc")
61 io = client.new_file("print splat", :largefile => :chunked)
64 assert_equal "123", client.get_file_data("print splat")
66 io = client.new_file("printf", :largefile => :chunked)
67 assert_nil io.printf("%x", 1638)
69 assert_equal "666", client.get_file_data("printf")
71 io = client.new_file("syswrite", :largefile => :chunked)
72 assert_equal 4, io.syswrite("good")
73 assert_equal 7, io.syswrite("morning")
75 assert_equal "goodmorning", client.get_file_data("syswrite")
77 io = client.new_file("md5", :largefile=>:chunked, :content_md5=>:trailer)
78 assert_instance_of Digest::MD5, io.md5
79 assert_nil io.puts("HIHI")
81 assert_equal "HIHI\n", client.get_file_data("md5")
82 assert_equal Digest::MD5.hexdigest("HIHI\n"), io.md5.hexdigest
84 io = client.new_file("<<", :largefile=>:chunked)
85 assert_equal(io, io << ">>")
87 assert_equal ">>", client.get_file_data("<<")
91 add_host_device_domain
92 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
93 node = "#@test_host:#@mogstored_http_port"
95 # not modifying this hash in the same process
96 MogileFS::HTTPFile::MD5_TRAILER_NODES[node] = true
97 client.store_content("md5_me", nil, "HELLO WORLD")
99 _, status = Process.waitpid2(pid)
100 assert status.success?, status.inspect
101 assert_equal "HELLO WORLD", client.get_file_data("md5_me")
105 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
106 @mogstored_mgmt = TCPServer.new(@test_host, 0)
107 @mogstored_http = TCPServer.new(@test_host, 0)
108 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
109 @mogstored_http_port = @mogstored_http.addr[1]
110 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
111 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
112 @mogstored_conf.write <<EOF
113 pidfile = #{@mogstored_pid.path}
115 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
119 @mogstored_conf.flush
120 @mogstored_mgmt.close
124 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
125 wait_for_port @mogstored_mgmt_port
128 # I would use Rainbows! + *Threads + Ruby 1.9.3 in production
130 @ru = Tempfile.new(%w(mogstored_rack .ru))
132 run MogstoredRack.new("#@docroot")
136 @unicorn_pid = Tempfile.new(%w(unicorn .pid))
137 @unicorn_conf = Tempfile.new(%w(unicorn.conf .rb))
138 @unicorn_stderr = Tempfile.new(%w(unicorn .stderr))
139 @unicorn_stdout = Tempfile.new(%w(unicorn .stdout))
140 @unicorn_conf.write <<EOF
141 require "mogstored_rack"
142 listen "#@test_host:#{@mogstored_http_port}"
143 pid "#{@unicorn_pid.path}"
144 stderr_path "#{@unicorn_stderr.path}"
145 stdout_path "#{@unicorn_stdout.path}"
146 rewindable_input false
150 @mogstored_http.close
151 x!("unicorn", "-E", "deployment",
152 "--daemon", "--config", @unicorn_conf.path, @ru.path)
153 wait_for_port @mogstored_http_port
155 break if File.size(@unicorn_pid.path) > 0
161 pid = File.read(@unicorn_pid.path).to_i
162 Process.kill(:QUIT, pid) if pid > 0
164 puts(@unicorn_stderr.read) if $DEBUG
166 end if ok && `which unicorn`.chomp.size > 0