1 # -*- encoding: binary -*-
6 class TestMogFresh < Test::Unit::TestCase
13 def setup_mogilefs(plugins = nil)
14 @test_host = "127.0.0.1"
15 @tracker = TCPServer.new(@test_host, 0)
16 @tracker_port = @tracker.addr[1]
18 @mogstored_mgmt = TCPServer.new(@test_host, 0)
19 @mogstored_http = TCPServer.new(@test_host, 0)
20 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
21 @mogstored_http_port = @mogstored_http.addr[1]
23 @dbname = Tempfile.new(["mogfresh", "sqlite3"])
24 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
25 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
26 @mogilefsd_conf = Tempfile.new(["mogilefsd", "conf"])
27 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
28 @mogilefsd_pid = Tempfile.new(["mogilefsd", "pid"])
30 cmd = %w(mogdbsetup --yes --type=SQLite --dbname) << @dbname.path
33 @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
34 @mogilefsd_conf.write <<EOF
35 conf_port #@tracker_port
37 pidfile #{@mogilefsd_pid.path}
41 mogstored_stream_port #{@mogstored_mgmt_port}
46 @mogstored_conf.write <<EOF
47 pidfile = #{@mogstored_pid.path}
49 httplisten = #@test_host:#{@mogstored_http_port}
50 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
55 @hosts = [ "#@test_host:#@tracker_port" ]
59 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
60 wait_for_port @mogstored_http_port
61 wait_for_port @mogstored_mgmt_port
62 x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
63 wait_for_port @tracker_port
64 @admin = MogileFS::Admin.new(:hosts => @hosts)
66 break if @mogstored_pid.size > 0
71 def wait_for_port(port)
74 TCPSocket.new(@test_host, port).close
78 end while (tries -= 1) > 0
79 raise "#@test_host:#{port} never became ready"
82 def test_admin_setup_new_host_and_devices
83 assert_equal [], @admin.get_hosts
84 args = { :ip => @test_host, :port => @mogstored_http_port }
85 @admin.create_host("me", args)
86 yield_for_monitor_update { @admin.get_hosts.empty? or break }
87 hosts = @admin.get_hosts
88 assert_equal 1, hosts.size
89 host = @admin.get_hosts[0]
90 assert_equal "me", host["hostname"]
91 assert_equal @mogstored_http_port, host["http_port"].to_i
92 assert_equal @test_host, host["hostip"]
93 assert_equal hosts, @admin.get_hosts(host["hostid"])
95 assert_equal [], @admin.get_devices
98 def test_replicate_now
99 assert_equal({"count" => 0}, @admin.replicate_now)
102 def test_create_update_delete_class
103 domain = "rbmogtest#{Time.now.strftime('%Y%m%d%H%M%S')}.#{uuid}"
104 @admin.create_domain(domain)
105 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
107 assert_nothing_raised do
108 @admin.create_class(domain, "klassy", 1)
110 assert_raises(MogileFS::Backend::ClassExistsError) do
111 @admin.create_class(domain, "klassy", 1)
114 assert_nothing_raised do
115 @admin.update_class(domain, "klassy",
116 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
120 yield_for_monitor_update do
121 tmp = @admin.get_domains[domain]["klassy"]
122 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
124 assert tmp, "domain did not show up"
125 assert_equal 1, tmp["mindevcount"]
126 assert_equal "MultipleHosts(1)", tmp["replpolicy"]
127 assert_nothing_raised { @admin.update_class(domain, "klassy", 2) }
129 @admin.delete_class(domain, "klassy") rescue nil
133 if @mogstored_pid && @mogstored_pid.size > 0
134 Process.kill(:TERM, @mogstored_pid.read.to_i)
137 s = TCPSocket.new(@test_host, @tracker_port)
138 s.write "!shutdown\r\n"
141 FileUtils.rmtree(@docroot)