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 @admin.create_class(domain, "klassy", 1)
95 assert_raises(MogileFS::Backend::ClassExistsError) do
96 @admin.create_class(domain, "klassy", 1)
99 @admin.update_class(domain, "klassy",
100 :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
103 yield_for_monitor_update do
104 tmp = @admin.get_domains[domain]["klassy"]
105 break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
107 assert tmp, "domain did not show up"
108 assert_equal 1, tmp["mindevcount"]
109 assert_equal "MultipleHosts(1)", tmp["replpolicy"]
110 @admin.update_class(domain, "klassy", 2)
112 @admin.delete_class(domain, "klassy") rescue nil
115 def add_host_device_domain
116 assert_equal [], @admin.get_hosts
117 args = { :ip => @test_host, :port => @mogstored_http_port }
118 args[:status] = "alive"
119 @admin.create_host("me", args)
120 assert File.directory?("#@docroot/dev1")
121 assert File.directory?("#@docroot/dev2")
122 yield_for_monitor_update { @admin.get_hosts.empty? or break }
124 me = @admin.get_hosts.find { |x| x["hostname"] == "me" }
125 assert_instance_of Hash, me, me.inspect
126 assert_kind_of Integer, me["hostid"], me
127 assert_equal true, @admin.create_device(me["hostid"], 1)
128 yield_for_monitor_update { @admin.get_devices.empty? or break }
129 wait_for_usage_file "dev1"
130 assert_equal true, @admin.create_device("me", 2)
131 wait_for_usage_file "dev2"
133 # MogileFS::Server 2.60+ shows reject_bad_md5 monitor status
134 dev = @admin.get_devices[0]
135 if dev.include?("reject_bad_md5")
136 assert [true, false].include?(dev["reject_bad_md5"])
144 status, out, err = mogadm("check")
145 assert status.success?, status.inspect
146 if (tries += 1) > 100
149 raise "mogadm failed"
152 end until out.read =~ /write?able/
154 domain = "rbmogtest.#$$"
155 @admin.create_domain(domain)
156 yield_for_monitor_update { @admin.get_domains.include?(domain) and break }
160 def test_device_file_add
161 add_host_device_domain
162 client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
172 assert_equal 20, client.store_file("pipe", nil, r)
173 assert_equal :ok, thr.value
175 assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
178 def teardown_mogilefs
180 pid = File.read(@mogstored_pid.path).to_i
181 Process.kill(:TERM, pid) if pid > 0
184 s = TCPSocket.new(@test_host, @tracker_port)
185 s.write "!shutdown\r\n"
188 FileUtils.rmtree(@docroot)
191 def wait_for_usage_file(device)
192 uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
195 res = Net::HTTP.get_response(uri)
196 if Net::HTTPOK === res
197 puts res.body if $DEBUG
200 puts res.inspect if $DEBUG
203 raise "#{uri} failed to appear: #{res.inspect}"
207 @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
208 Dir.mkdir("#@docroot/dev1")
209 Dir.mkdir("#@docroot/dev2")
210 @mogstored_mgmt = TCPServer.new(@test_host, 0)
211 @mogstored_http = TCPServer.new(@test_host, 0)
212 @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
213 @mogstored_http_port = @mogstored_http.addr[1]
214 @mogstored_conf = Tempfile.new(["mogstored", "conf"])
215 @mogstored_pid = Tempfile.new(["mogstored", "pid"])
216 @mogstored_conf.write <<EOF
217 pidfile = #{@mogstored_pid.path}
219 httplisten = #@test_host:#{@mogstored_http_port}
220 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
223 @mogstored_conf.flush
224 @mogstored_mgmt.close
225 @mogstored_http.close
227 x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
228 wait_for_port @mogstored_mgmt_port
229 wait_for_port @mogstored_http_port