split deprecated paths_size to its own file
[ruby-mogilefs-client.git] / test / test_fresh.rb
blob1b0fa15e71dad2fdad5893f765c1ec713788cdf8
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     @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 test_create_update_delete_class
95     domain = "rbmogtest#{Time.now.strftime('%Y%m%d%H%M%S')}.#{uuid}"
96     @admin.create_domain(domain)
97     yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
99     assert_nothing_raised do
100       @admin.create_class(domain, "klassy", 1)
101     end
102     assert_raises(MogileFS::Backend::ClassExistsError) do
103       @admin.create_class(domain, "klassy", 1)
104     end
106     assert_nothing_raised do
107       @admin.update_class(domain, "klassy",
108                           :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
109     end
111     tmp = nil
112     yield_for_monitor_update do
113       tmp = @admin.get_domains[domain]["klassy"]
114       break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
115     end
116     assert tmp, "domain did not show up"
117     assert_equal 1, tmp["mindevcount"]
118     assert_equal "MultipleHosts(1)", tmp["replpolicy"]
119     assert_nothing_raised { @admin.update_class(domain, "klassy", 2) }
120     ensure
121       @admin.delete_class(domain, "klassy") rescue nil
122   end
124   def teardown
125     if @mogstored_pid && @mogstored_pid.size > 0
126       Process.kill(:TERM, @mogstored_pid.read.to_i)
127     end
128     if @mogilefsd_pid
129       s = TCPSocket.new(@test_host, @tracker_port)
130       s.write "!shutdown\r\n"
131       s.close
132     end
133     FileUtils.rmtree(@docroot)
134   end