"new_file" gets many options for Content-MD5 handling
[ruby-mogilefs-client.git] / test / test_mogstored_rack.rb
blobe3fb889daebe08802db07db8d66295188b4880a5
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_md5_check
18     add_host_device_domain
19     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
20     node = "#@test_host:#@mogstored_http_port"
21     pid = fork do
22       # not modifying this hash in the same process
23       MogileFS::HTTPFile::MD5_TRAILER_NODES[node] = true
24       client.store_content("md5_me", nil, "HELLO WORLD")
25     end
26     _, status = Process.waitpid2(pid)
27     assert status.success?, status.inspect
28     assert_equal "HELLO WORLD", client.get_file_data("md5_me")
29   end
31   def setup_mogstored
32     @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
33     @mogstored_mgmt = TCPServer.new(@test_host, 0)
34     @mogstored_http = TCPServer.new(@test_host, 0)
35     @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
36     @mogstored_http_port = @mogstored_http.addr[1]
37     @mogstored_conf = Tempfile.new(["mogstored", "conf"])
38     @mogstored_pid = Tempfile.new(["mogstored", "pid"])
39     @mogstored_conf.write <<EOF
40 pidfile = #{@mogstored_pid.path}
41 maxconns = 1000
42 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
43 server = none
44 docroot = #@docroot
45 EOF
46     @mogstored_conf.flush
47     @mogstored_mgmt.close
49     unicorn_setup
51     x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
52     wait_for_port @mogstored_mgmt_port
53   end
55   # I would use Rainbows! + *Threads + Ruby 1.9.3 in production
56   def unicorn_setup
57     @ru = Tempfile.new(%w(mogstored_rack .ru))
58     @ru.write <<EOF
59 run MogstoredRack.new("#@docroot")
60 EOF
61     @ru.flush
63     @unicorn_pid = Tempfile.new(%w(unicorn .pid))
64     @unicorn_conf = Tempfile.new(%w(unicorn.conf .rb))
65     @unicorn_stderr = Tempfile.new(%w(unicorn .stderr))
66     @unicorn_stdout = Tempfile.new(%w(unicorn .stdout))
67     @unicorn_conf.write <<EOF
68 require "mogstored_rack"
69 listen "#@test_host:#{@mogstored_http_port}"
70 pid "#{@unicorn_pid.path}"
71 stderr_path "#{@unicorn_stderr.path}"
72 stdout_path "#{@unicorn_stdout.path}"
73 rewindable_input false
74 EOF
75     @unicorn_conf.flush
77     @mogstored_http.close
78     x!("unicorn", "-E", "deployment",
79        "--daemon", "--config", @unicorn_conf.path, @ru.path)
80     wait_for_port @mogstored_http_port
81     40.times do
82       break if File.size(@unicorn_pid.path) > 0
83       sleep 0.1
84     end
85   end
87   def teardown
88     pid = File.read(@unicorn_pid.path).to_i
89     Process.kill(:QUIT, pid) if pid > 0
90     teardown_mogilefs
91     puts(@unicorn_stderr.read) if $DEBUG
92   end
93 end if ok && `which unicorn`.chomp.size > 0