mog: config parser cleanup
[ruby-mogilefs-client.git] / test / test_mogstored_rack.rb
blob766192f061a36126003297c4fbe544d0382c285b
1 # -*- encoding: binary -*-
2 require "./test/fresh"
3 begin
4   require 'mogstored_rack'
5   require 'unicorn'
6   ok = true
7 rescue LoadError
8   ok = false
9 end
11 class TestMogstoredRack < Test::Unit::TestCase
12   include TestFreshSetup
13   def setup
14     setup_mogilefs
15   end
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
22     assert_nil io.close
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) }
27     assert_nil io.close
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")
33     assert_nil io.close
34     assert_equal "a\nb\nc\nd\ne\n", client.get_file_data("puts")
35   end
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"))
43       assert_nil io.md5
44     end
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"))
53     assert_nil io.close
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))
58     assert_nil io.close
59     assert_equal " ", client.get_file_data("putc")
61     io = client.new_file("print splat", :largefile => :chunked)
62     io.print(1, 2, 3)
63     assert_nil io.close
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)
68     assert_nil io.close
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")
74     assert_nil io.close
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")
80     assert_nil io.close
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 << ">>")
86     assert_nil io.close
87     assert_equal ">>", client.get_file_data("<<")
88   end
90   def test_md5_check
91     add_host_device_domain
92     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
93     node = "#@test_host:#@mogstored_http_port"
94     pid = fork do
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")
98     end
99     _, status = Process.waitpid2(pid)
100     assert status.success?, status.inspect
101     assert_equal "HELLO WORLD", client.get_file_data("md5_me")
102   end
104   def setup_mogstored
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}
114 maxconns = 1000
115 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
116 server = none
117 docroot = #@docroot
119     @mogstored_conf.flush
120     @mogstored_mgmt.close
122     unicorn_setup
124     x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
125     wait_for_port @mogstored_mgmt_port
126   end
128   # I would use Rainbows! + *Threads + Ruby 1.9.3 in production
129   def unicorn_setup
130     @ru = Tempfile.new(%w(mogstored_rack .ru))
131     @ru.write <<EOF
132 run MogstoredRack.new("#@docroot")
134     @ru.flush
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
148     @unicorn_conf.flush
150     @mogstored_http.close
151     x!("unicorn", "-E", "deployment",
152        "--daemon", "--config", @unicorn_conf.path, @ru.path)
153     wait_for_port @mogstored_http_port
154     40.times do
155       break if File.size(@unicorn_pid.path) > 0
156       sleep 0.1
157     end
158   end
160   def teardown
161     pid = File.read(@unicorn_pid.path).to_i
162     Process.kill(:QUIT, pid) if pid > 0
163     teardown_mogilefs
164     puts(@unicorn_stderr.read) if $DEBUG
165   end
166 end if ok && `which unicorn`.chomp.size > 0