1 # -*- encoding: binary -*-
7 class TestMogFresh < Test::Unit::TestCase
15 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
16 @mogstored_mgmt = TCPServer.new(@test_host, 0)
17 @mogstored_http = TCPServer.new(@test_host, 0)
18 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
19 @mogstored_http_port = @mogstored_http.addr[1]
20 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
21 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
22 @mogstored_conf.write <<EOF
23 pidfile = #{@mogstored_pid.path}
25 httplisten = #@test_host:#{@mogstored_http_port}
26 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
33 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
34 wait_for_port @mogstored_mgmt_port
35 wait_for_port @mogstored_http_port
38 def setup_mogilefs(plugins = nil)
39 @test_host = "127.0.0.1"
41 @tracker = TCPServer.new(@test_host, 0)
42 @tracker_port = @tracker.addr[1]
44 @dbname = Tempfile.new(["mogfresh", ".sqlite3"])
45 @mogilefsd_conf = Tempfile.new(["mogilefsd", "conf"])
46 @mogilefsd_pid = Tempfile.new(["mogilefsd", "pid"])
48 cmd = %w(mogdbsetup --yes --type=SQLite --dbname) << @dbname.path
51 @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
52 @mogilefsd_conf.write <<EOF
53 conf_port #@tracker_port
55 pidfile #{@mogilefsd_pid.path}
59 mogstored_stream_port #{@mogstored_mgmt_port}
64 @trackers = @hosts = [ "#@test_host:#@tracker_port" ]
66 x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
67 wait_for_port @tracker_port
68 @admin = MogileFS::Admin.new(:hosts => @hosts)
70 break if @mogstored_pid.size > 0
75 def wait_for_port(port)
78 TCPSocket.new(@test_host, port).close
82 end while (tries -= 1) > 0
83 raise "#@test_host:#{port} never became ready"
86 def test_admin_setup_new_host_and_devices
87 assert_equal [], @admin.get_hosts
88 args = { :ip => @test_host, :port => @mogstored_http_port }
89 @admin.create_host("me", args)
90 yield_for_monitor_update { @admin.get_hosts.empty? or break }
91 hosts = @admin.get_hosts
92 assert_equal 1, hosts.size
93 host = @admin.get_hosts[0]
94 assert_equal "me", host["hostname"]
95 assert_equal @mogstored_http_port, host["http_port"]
96 assert_nil host["http_get_port"]
97 assert_equal @test_host, host["hostip"]
98 assert_kind_of Integer, host["hostid"]
99 assert_equal hosts, @admin.get_hosts(host["hostid"])
101 assert_equal [], @admin.get_devices
104 def test_replicate_now
105 assert_equal({"count" => 0}, @admin.replicate_now)
109 assert_nil @admin.clear_cache
112 def test_create_update_delete_class
113 domain = "rbmogtest#{Time.now.strftime('%Y%m%d%H%M%S')}.#{uuid}"
114 @admin.create_domain(domain)
115 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
117 assert_nothing_raised do
118 @admin.create_class(domain, "klassy", 1)
120 assert_raises(MogileFS::Backend::ClassExistsError) do
121 @admin.create_class(domain, "klassy", 1)
124 assert_nothing_raised do
125 @admin.update_class(domain, "klassy",
126 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
130 yield_for_monitor_update do
131 tmp = @admin.get_domains[domain]["klassy"]
132 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
134 assert tmp, "domain did not show up"
135 assert_equal 1, tmp["mindevcount"]
136 assert_equal "MultipleHosts(1)", tmp["replpolicy"]
137 assert_nothing_raised { @admin.update_class(domain, "klassy", 2) }
139 @admin.delete_class(domain, "klassy") rescue nil
142 def test_device_file_add
143 assert_equal [], @admin.get_hosts
144 args = { :ip => @test_host, :port => @mogstored_http_port }
145 args[:status] = "alive"
146 @admin.create_host("me", args)
147 Dir.mkdir("#@docroot/dev1")
148 Dir.mkdir("#@docroot/dev2")
150 yield_for_monitor_update { @admin.get_hosts.empty? or break }
152 # TODO: allow adding devices via our MogileFS::Admin class
153 mogadm!("device", "add", "me", "dev1")
154 yield_for_monitor_update { @admin.get_devices.empty? or break }
155 wait_for_usage_file "dev1"
156 mogadm!("device", "add", "me", "dev2")
157 wait_for_usage_file "dev2"
163 status, out, err = mogadm("check")
164 if (tries += 1) > 100
170 end until out.read =~ /write?able/
172 domain = "rbmogtest.#$$"
173 @admin.create_domain(domain)
174 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
175 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => domain
185 assert_equal 20, client.store_file("pipe", nil, r)
186 assert_equal :ok, thr.value
188 assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
192 if @mogstored_pid && @mogstored_pid.size > 0
193 Process.kill(:TERM, @mogstored_pid.read.to_i)
196 s = TCPSocket.new(@test_host, @tracker_port)
197 s.write "!shutdown\r\n"
200 FileUtils.rmtree(@docroot)
203 def wait_for_usage_file(device)
204 uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
207 res = Net::HTTP.get_response(uri)
208 if Net::HTTPOK === res
209 puts res.body if $DEBUG
212 puts res.inspect if $DEBUG
215 raise "#{uri} failed to appear: #{res.inspect}"