1 # -*- encoding: binary -*-
6 class TestMogFresh < Test::Unit::TestCase
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
29 @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
30 @mogilefsd_conf.write <<EOF
31 conf_port #@tracker_port
33 pidfile #{@mogilefsd_pid.path}
37 mogstored_stream_port #{@mogstored_mgmt_port}
42 @mogstored_conf.write <<EOF
43 pidfile = #{@mogstored_pid.path}
45 httplisten = #@test_host:#{@mogstored_http_port}
46 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
51 @hosts = [ "#@test_host:#@tracker_port" ]
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)
62 break if @mogstored_pid.size > 0
67 def wait_for_port(port)
70 TCPSocket.new(@test_host, port).close
74 end while (tries -= 1) > 0
75 raise "#@test_host:#{port} never became ready"
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
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)
102 assert_raises(MogileFS::Backend::ClassExistsError) do
103 @admin.create_class(domain, "klassy", 1)
106 assert_nothing_raised do
107 @admin.update_class(domain, "klassy",
108 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
112 yield_for_monitor_update do
113 tmp = @admin.get_domains[domain]["klassy"]
114 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
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) }
121 @admin.delete_class(domain, "klassy") rescue nil
125 if @mogstored_pid && @mogstored_pid.size > 0
126 Process.kill(:TERM, @mogstored_pid.read.to_i)
129 s = TCPSocket.new(@test_host, @tracker_port)
130 s.write "!shutdown\r\n"
133 FileUtils.rmtree(@docroot)