http_file: remove unnecessary check
[ruby-mogilefs-client.git] / test / test_fresh.rb
blob48b33958331381d5de58f8a49c1c74822a057464
1 # -*- encoding: binary -*-
2 require "./test/exec"
3 require "tmpdir"
4 require "fileutils"
6 class TestMogFresh < Test::Unit::TestCase
7   include TestExec
9   def setup
10     @test_host = "127.0.0.1"
11     @tracker = TCPServer.new(@test_host, 0)
12     @tracker_port = @tracker.addr[1]
14     @mogstored_mgmt = TCPServer.new(@test_host, 0)
15     @mogstored_http = TCPServer.new(@test_host, 0)
16     @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
17     @mogstored_http_port = @mogstored_http.addr[1]
19     @dbname = Tempfile.new(["mogfresh", "sqlite3"])
20     @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
21     @mogstored_conf = Tempfile.new(["mogstored", "conf"])
22     @mogilefsd_conf = Tempfile.new(["mogilefsd", "conf"])
23     @mogstored_pid = Tempfile.new(["mogstored", "pid"])
24     @mogilefsd_pid = Tempfile.new(["mogilefsd", "pid"])
26     cmd = %w(mogdbsetup --yes --type=SQLite --dbname) << @dbname.path
27     x!(*cmd)
29     @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
30     @mogilefsd_conf.write <<EOF
31 conf_port #@tracker_port
32 listen #@test_host
33 pidfile #{@mogilefsd_pid.path}
34 replicate_jobs 1
35 fsck_jobs 1
36 query_jobs 1
37 mogstored_stream_port #{@mogstored_mgmt_port}
38 node_timeout 10
39 EOF
40     @mogilefsd_conf.flush
42     @mogstored_conf.write <<EOF
43 pidfile = #{@mogstored_pid.path}
44 maxconns = 1000
45 httplisten = #@test_host:#{@mogstored_http_port}
46 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
47 docroot = #@docroot
48 EOF
49     @mogstored_conf.flush
51     @hosts = [ "#@test_host:#@tracker_port" ]
52     @mogstored_mgmt.close
53     @mogstored_http.close
54     @tracker.close
55     x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
56     wait_for_port @mogstored_http_port
57     wait_for_port @mogstored_mgmt_port
58     x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
59     wait_for_port @tracker_port
60     @admin = MogileFS::Admin.new(:hosts => @hosts)
61     10.times do
62       break if @mogstored_pid.size > 0
63       sleep 0.1
64     end
65   end
67   def wait_for_port(port)
68     tries = 50
69     begin
70       TCPSocket.new(@test_host, port).close
71       return
72     rescue
73       sleep 0.1
74     end while (tries -= 1) > 0
75     raise "#@test_host:#{port} never became ready"
76   end
78   def test_admin_setup_new_host_and_devices
79     assert_equal [], @admin.get_hosts
80     args = { :ip => @test_host, :port => @mogstored_http_port }
81     x = @admin.create_host("me", args)
82     yield_for_monitor_update { @admin.get_hosts.empty? or break }
83     hosts = @admin.get_hosts
84     assert_equal 1, hosts.size
85     host = @admin.get_hosts[0]
86     assert_equal "me", host["hostname"]
87     assert_equal @mogstored_http_port, host["http_port"].to_i
88     assert_equal @test_host, host["hostip"]
89     assert_equal hosts, @admin.get_hosts(host["hostid"])
91     assert_equal [], @admin.get_devices
92   end
94   def teardown
95     if @mogstored_pid && @mogstored_pid.size > 0
96       Process.kill(:TERM, @mogstored_pid.read.to_i)
97     end
98     if @mogilefsd_pid
99       s = TCPSocket.new(@test_host, @tracker_port)
100       s.write "!shutdown\r\n"
101       s.close
102     end
103     FileUtils.rmtree(@docroot)
104   end