backend: retry all trackers on send failure
[ruby-mogilefs-client.git] / test / fresh.rb
blobe614b7dbb9379ddfb0f327763a91fa3d9f725beb
1 # -*- encoding: binary -*-
2 require "./test/exec"
3 require "tmpdir"
4 require "fileutils"
5 require "net/http"
7 module TestFreshSetup
8   include TestExec
10   def setup
11     setup_mogilefs
12   end
14   def setup_mogilefs(plugins = nil)
15     @test_host = "127.0.0.1"
16     setup_mogstored
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
25     x!(*cmd)
27     @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}"
28     @mogilefsd_conf.write <<EOF
29 conf_port #@tracker_port
30 listen #@test_host
31 pidfile #{@mogilefsd_pid.path}
32 replicate_jobs 1
33 fsck_jobs 1
34 query_jobs 1
35 mogstored_stream_port #{@mogstored_mgmt_port}
36 node_timeout 10
37 EOF
38     @mogilefsd_conf.flush
40     @trackers = @hosts = [ "#@test_host:#@tracker_port" ]
41     @tracker.close
42     start_tracker
43     @admin = MogileFS::Admin.new(:hosts => @hosts)
44     50.times do
45       break if File.size(@mogstored_pid.path) > 0
46       sleep 0.1
47     end
48   end
50   def start_tracker
51     x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
52     wait_for_port @tracker_port
53   end
55   def wait_for_port(port)
56     tries = 50
57     begin
58       TCPSocket.new(@test_host, port).close
59       return
60     rescue
61       sleep 0.1
62     end while (tries -= 1) > 0
63     raise "#@test_host:#{port} never became ready"
64   end
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
82   end
84   def test_replicate_now
85     assert_equal({"count" => 0}, @admin.replicate_now)
86   end
88   def test_clear_cache
89     assert_nil @admin.clear_cache
90   end
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)
101     end
103     @admin.update_class(domain, "klassy",
104                         :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
106     tmp = nil
107     yield_for_monitor_update do
108       tmp = @admin.get_domains[domain]["klassy"]
109       break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
110     end
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)
115     ensure
116       @admin.delete_class(domain, "klassy") rescue nil
117   end
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"])
141     end
143     out = err = nil
144     tries = 0
145     begin
146       out.close! if out
147       err.close! if err
148       status, out, err = mogadm("check")
149       assert status.success?, status.inspect
150       if (tries += 1) > 100
151         warn err.read
152         puts out.read
153         raise "mogadm failed"
154       end
155       sleep 0.1
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 }
161     @domain = domain
162   end
164   def test_device_file_add
165     add_host_device_domain
166     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
167     r, w = IO.pipe
168     thr = Thread.new do
169       (0..9).each do |i|
170         sleep 0.05
171         w.write("#{i}\n")
172       end
173       w.close
174       :ok
175     end
176     assert_equal 20, client.store_file("pipe", nil, r)
177     assert_equal :ok, thr.value
178     r.close
179     assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
180   end
182   def teardown_mogilefs
183     if @mogstored_pid
184       pid = File.read(@mogstored_pid.path).to_i
185       Process.kill(:TERM, pid) if pid > 0
186     end
187     if @mogilefsd_pid
188       s = TCPSocket.new(@test_host, @tracker_port)
189       s.write "!shutdown\r\n"
190       s.close
191     end
192     FileUtils.rmtree(@docroot)
193   end
195   def wait_for_usage_file(device)
196     uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
197     res = nil
198     100.times do
199       res = Net::HTTP.get_response(uri)
200       if Net::HTTPOK === res
201         puts res.body if $DEBUG
202         return
203       end
204       puts res.inspect if $DEBUG
205       sleep 0.1
206     end
207     raise "#{uri} failed to appear: #{res.inspect}"
208   end
210   def setup_mogstored
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}
222 maxconns = 1000
223 httplisten = #@test_host:#{@mogstored_http_port}
224 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
225 docroot = #@docroot
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
234   end