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