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" ]
43 @admin = MogileFS::Admin.new(:hosts => @hosts)
45 break if File.size(@mogstored_pid.path) > 0
51 x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
52 wait_for_port @tracker_port
55 def wait_for_port(port)
58 TCPSocket.new(@test_host, port).close
62 end while (tries -= 1) > 0
63 raise "#@test_host:#{port} never became ready"
66 def test_admin_setup_new_host_and_devices
67 assert_equal [], @admin.get_hosts
68 args = { :ip => @test_host, :port => @mogstored_http_port }
69 @admin.create_host("me", args)
70 yield_for_monitor_update { @admin.get_hosts.empty? or break }
71 hosts = @admin.get_hosts
72 assert_equal 1, hosts.size
73 host = @admin.get_hosts[0]
74 assert_equal "me", host["hostname"]
75 assert_equal @mogstored_http_port, host["http_port"]
76 assert_nil host["http_get_port"]
77 assert_equal @test_host, host["hostip"]
78 assert_kind_of Integer, host["hostid"]
79 assert_equal hosts, @admin.get_hosts(host["hostid"])
81 assert_equal [], @admin.get_devices
84 def test_replicate_now
85 assert_equal({"count" => 0}, @admin.replicate_now)
89 assert_nil @admin.clear_cache
92 def test_create_update_delete_class
93 domain = "rbmogtest#{Time.now.strftime('%Y%m%d%H%M%S')}.#{uuid}"
94 @admin.create_domain(domain)
95 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
97 @admin.create_class(domain, "klassy", 1)
99 assert_raises(MogileFS::Backend::ClassExistsError) do
100 @admin.create_class(domain, "klassy", 1)
103 @admin.update_class(domain, "klassy",
104 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
107 yield_for_monitor_update do
108 tmp = @admin.get_domains[domain]["klassy"]
109 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
111 assert tmp, "domain did not show up"
112 assert_equal 1, tmp["mindevcount"]
113 assert_equal "MultipleHosts(1)", tmp["replpolicy"]
114 @admin.update_class(domain, "klassy", 2)
116 @admin.delete_class(domain, "klassy") rescue nil
119 def add_host_device_domain
120 assert_equal [], @admin.get_hosts
121 args = { :ip => @test_host, :port => @mogstored_http_port }
122 args[:status] = "alive"
123 @admin.create_host("me", args)
124 assert File.directory?("#@docroot/dev1")
125 assert File.directory?("#@docroot/dev2")
126 yield_for_monitor_update { @admin.get_hosts.empty? or break }
128 me = @admin.get_hosts.find { |x| x["hostname"] == "me" }
129 assert_instance_of Hash, me, me.inspect
130 assert_kind_of Integer, me["hostid"], me
131 assert_equal true, @admin.create_device(me["hostid"], 1)
132 yield_for_monitor_update { @admin.get_devices.empty? or break }
133 wait_for_usage_file "dev1"
134 assert_equal true, @admin.create_device("me", 2)
135 wait_for_usage_file "dev2"
137 # MogileFS::Server 2.60+ shows reject_bad_md5 monitor status
138 dev = @admin.get_devices[0]
139 if dev.include?("reject_bad_md5")
140 assert [true, false].include?(dev["reject_bad_md5"])
148 status, out, err = mogadm("check")
149 assert status.success?, status.inspect
150 if (tries += 1) > 100
153 raise "mogadm failed"
156 end until out.read =~ /write?able/
158 domain = "rbmogtest.#$$"
159 @admin.create_domain(domain)
160 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
164 def test_device_file_add
165 add_host_device_domain
166 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
176 assert_equal 20, client.store_file("pipe", nil, r)
177 assert_equal :ok, thr.value
179 assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
182 def teardown_mogilefs
184 pid = File.read(@mogstored_pid.path).to_i
185 Process.kill(:TERM, pid) if pid > 0
188 s = TCPSocket.new(@test_host, @tracker_port)
189 s.write "!shutdown\r\n"
192 FileUtils.rmtree(@docroot)
195 def wait_for_usage_file(device)
196 uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
199 res = Net::HTTP.get_response(uri)
200 if Net::HTTPOK === res
201 puts res.body if $DEBUG
204 puts res.inspect if $DEBUG
207 raise "#{uri} failed to appear: #{res.inspect}"
211 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
212 Dir.mkdir("#@docroot/dev1")
213 Dir.mkdir("#@docroot/dev2")
214 @mogstored_mgmt = TCPServer.new(@test_host, 0)
215 @mogstored_http = TCPServer.new(@test_host, 0)
216 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
217 @mogstored_http_port = @mogstored_http.addr[1]
218 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
219 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
220 @mogstored_conf.write <<EOF
221 pidfile = #{@mogstored_pid.path}
223 httplisten = #@test_host:#{@mogstored_http_port}
224 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
227 @mogstored_conf.flush
228 @mogstored_mgmt.close
229 @mogstored_http.close
231 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
232 wait_for_port @mogstored_mgmt_port
233 wait_for_port @mogstored_http_port