1 # -*- encoding: binary -*-
14 def setup_mogilefs(plugins = nil)
15 @test_host = "127.0.0.1"
17 @tracker = TCPServer.new(@test_host, 0)
18 @tracker_port = @tracker.addr[1]
20 @dbname = Tempfile.new(["mogfresh", ".sqlite3"])
21 @mogilefsd_conf = Tempfile.new(["mogilefsd", "conf"])
22 @mogilefsd_pid = Tempfile.new(["mogilefsd", "pid"])
24 cmd = %w(mogdbsetup --yes --type=SQLite --dbname) << @dbname.path
27 @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
28 @mogilefsd_conf.write <<EOF
29 conf_port #@tracker_port
31 pidfile #{@mogilefsd_pid.path}
35 mogstored_stream_port #{@mogstored_mgmt_port}
40 @trackers = @hosts = [ "#@test_host:#@tracker_port" ]
42 x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
43 wait_for_port @tracker_port
44 @admin = MogileFS::Admin.new(:hosts => @hosts)
46 break if File.size(@mogstored_pid.path) > 0
51 def wait_for_port(port)
54 TCPSocket.new(@test_host, port).close
58 end while (tries -= 1) > 0
59 raise "#@test_host:#{port} never became ready"
62 def test_admin_setup_new_host_and_devices
63 assert_equal [], @admin.get_hosts
64 args = { :ip => @test_host, :port => @mogstored_http_port }
65 @admin.create_host("me", args)
66 yield_for_monitor_update { @admin.get_hosts.empty? or break }
67 hosts = @admin.get_hosts
68 assert_equal 1, hosts.size
69 host = @admin.get_hosts[0]
70 assert_equal "me", host["hostname"]
71 assert_equal @mogstored_http_port, host["http_port"]
72 assert_nil host["http_get_port"]
73 assert_equal @test_host, host["hostip"]
74 assert_kind_of Integer, host["hostid"]
75 assert_equal hosts, @admin.get_hosts(host["hostid"])
77 assert_equal [], @admin.get_devices
80 def test_replicate_now
81 assert_equal({"count" => 0}, @admin.replicate_now)
85 assert_nil @admin.clear_cache
88 def test_create_update_delete_class
89 domain = "rbmogtest#{Time.now.strftime('%Y%m%d%H%M%S')}.#{uuid}"
90 @admin.create_domain(domain)
91 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
93 assert_nothing_raised do
94 @admin.create_class(domain, "klassy", 1)
96 assert_raises(MogileFS::Backend::ClassExistsError) do
97 @admin.create_class(domain, "klassy", 1)
100 assert_nothing_raised do
101 @admin.update_class(domain, "klassy",
102 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
106 yield_for_monitor_update do
107 tmp = @admin.get_domains[domain]["klassy"]
108 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
110 assert tmp, "domain did not show up"
111 assert_equal 1, tmp["mindevcount"]
112 assert_equal "MultipleHosts(1)", tmp["replpolicy"]
113 assert_nothing_raised { @admin.update_class(domain, "klassy", 2) }
115 @admin.delete_class(domain, "klassy") rescue nil
118 def add_host_device_domain
119 assert_equal [], @admin.get_hosts
120 args = { :ip => @test_host, :port => @mogstored_http_port }
121 args[:status] = "alive"
122 @admin.create_host("me", args)
123 Dir.mkdir("#@docroot/dev1")
124 Dir.mkdir("#@docroot/dev2")
125 yield_for_monitor_update { @admin.get_hosts.empty? or break }
127 # TODO: allow adding devices via our MogileFS::Admin class
128 mogadm!("device", "add", "me", "dev1")
129 yield_for_monitor_update { @admin.get_devices.empty? or break }
130 wait_for_usage_file "dev1"
131 mogadm!("device", "add", "me", "dev2")
132 wait_for_usage_file "dev2"
138 status, out, err = mogadm("check")
139 if (tries += 1) > 100
142 raise "mogadm failed"
145 end until out.read =~ /write?able/
147 domain = "rbmogtest.#$$"
148 @admin.create_domain(domain)
149 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
153 def test_device_file_add
154 add_host_device_domain
155 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
165 assert_equal 20, client.store_file("pipe", nil, r)
166 assert_equal :ok, thr.value
168 assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
171 def teardown_mogilefs
173 pid = File.read(@mogstored_pid.path).to_i
174 Process.kill(:TERM, pid) if pid > 0
177 s = TCPSocket.new(@test_host, @tracker_port)
178 s.write "!shutdown\r\n"
181 FileUtils.rmtree(@docroot)
184 def wait_for_usage_file(device)
185 uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
188 res = Net::HTTP.get_response(uri)
189 if Net::HTTPOK === res
190 puts res.body if $DEBUG
193 puts res.inspect if $DEBUG
196 raise "#{uri} failed to appear: #{res.inspect}"
200 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
201 @mogstored_mgmt = TCPServer.new(@test_host, 0)
202 @mogstored_http = TCPServer.new(@test_host, 0)
203 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
204 @mogstored_http_port = @mogstored_http.addr[1]
205 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
206 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
207 @mogstored_conf.write <<EOF
208 pidfile = #{@mogstored_pid.path}
210 httplisten = #@test_host:#{@mogstored_http_port}
211 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
214 @mogstored_conf.flush
215 @mogstored_mgmt.close
216 @mogstored_http.close
218 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
219 wait_for_port @mogstored_mgmt_port
220 wait_for_port @mogstored_http_port