admin: add support for replicate_now command
[ruby-mogilefs-client.git] / test / test_fresh.rb
blob9b279c627095464b925e93cc2cb671f6ee62cd0f
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     setup_mogilefs
11   end
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
31     x!(*cmd)
33     @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
34     @mogilefsd_conf.write <<EOF
35 conf_port #@tracker_port
36 listen #@test_host
37 pidfile #{@mogilefsd_pid.path}
38 replicate_jobs 1
39 fsck_jobs 1
40 query_jobs 1
41 mogstored_stream_port #{@mogstored_mgmt_port}
42 node_timeout 10
43 EOF
44     @mogilefsd_conf.flush
46     @mogstored_conf.write <<EOF
47 pidfile = #{@mogstored_pid.path}
48 maxconns = 1000
49 httplisten = #@test_host:#{@mogstored_http_port}
50 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
51 docroot = #@docroot
52 EOF
53     @mogstored_conf.flush
55     @hosts = [ "#@test_host:#@tracker_port" ]
56     @mogstored_mgmt.close
57     @mogstored_http.close
58     @tracker.close
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)
65     10.times do
66       break if @mogstored_pid.size > 0
67       sleep 0.1
68     end
69   end
71   def wait_for_port(port)
72     tries = 50
73     begin
74       TCPSocket.new(@test_host, port).close
75       return
76     rescue
77       sleep 0.1
78     end while (tries -= 1) > 0
79     raise "#@test_host:#{port} never became ready"
80   end
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
96   end
98   def test_replicate_now
99     assert_equal({"count" => 0}, @admin.replicate_now)
100   end
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)
109     end
110     assert_raises(MogileFS::Backend::ClassExistsError) do
111       @admin.create_class(domain, "klassy", 1)
112     end
114     assert_nothing_raised do
115       @admin.update_class(domain, "klassy",
116                           :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
117     end
119     tmp = nil
120     yield_for_monitor_update do
121       tmp = @admin.get_domains[domain]["klassy"]
122       break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
123     end
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) }
128     ensure
129       @admin.delete_class(domain, "klassy") rescue nil
130   end
132   def teardown
133     if @mogstored_pid && @mogstored_pid.size > 0
134       Process.kill(:TERM, @mogstored_pid.read.to_i)
135     end
136     if @mogilefsd_pid
137       s = TCPSocket.new(@test_host, @tracker_port)
138       s.write "!shutdown\r\n"
139       s.close
140     end
141     FileUtils.rmtree(@docroot)
142   end