test: fix some warnings found with "ruby -w -c"
[ruby-mogilefs-client.git] / test / fresh.rb
blob4db1e2caa0d2990872bb977c32e9c3c57d6f0e8f
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     x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}")
43     wait_for_port @tracker_port
44     @admin = MogileFS::Admin.new(:hosts => @hosts)
45     50.times do
46       break if File.size(@mogstored_pid.path) > 0
47       sleep 0.1
48     end
49   end
51   def wait_for_port(port)
52     tries = 50
53     begin
54       TCPSocket.new(@test_host, port).close
55       return
56     rescue
57       sleep 0.1
58     end while (tries -= 1) > 0
59     raise "#@test_host:#{port} never became ready"
60   end
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
78   end
80   def test_replicate_now
81     assert_equal({"count" => 0}, @admin.replicate_now)
82   end
84   def test_clear_cache
85     assert_nil @admin.clear_cache
86   end
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)
97     end
99     @admin.update_class(domain, "klassy",
100                         :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
102     tmp = nil
103     yield_for_monitor_update do
104       tmp = @admin.get_domains[domain]["klassy"]
105       break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
106     end
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)
111     ensure
112       @admin.delete_class(domain, "klassy") rescue nil
113   end
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"])
137     end
139     out = err = nil
140     tries = 0
141     begin
142       out.close! if out
143       err.close! if err
144       status, out, err = mogadm("check")
145       assert status.success?, status.inspect
146       if (tries += 1) > 100
147         warn err.read
148         puts out.read
149         raise "mogadm failed"
150       end
151       sleep 0.1
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 }
157     @domain = domain
158   end
160   def test_device_file_add
161     add_host_device_domain
162     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
163     r, w = IO.pipe
164     thr = Thread.new do
165       (0..9).each do |i|
166         sleep 0.05
167         w.write("#{i}\n")
168       end
169       w.close
170       :ok
171     end
172     assert_equal 20, client.store_file("pipe", nil, r)
173     assert_equal :ok, thr.value
174     r.close
175     assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
176   end
178   def teardown_mogilefs
179     if @mogstored_pid
180       pid = File.read(@mogstored_pid.path).to_i
181       Process.kill(:TERM, pid) if pid > 0
182     end
183     if @mogilefsd_pid
184       s = TCPSocket.new(@test_host, @tracker_port)
185       s.write "!shutdown\r\n"
186       s.close
187     end
188     FileUtils.rmtree(@docroot)
189   end
191   def wait_for_usage_file(device)
192     uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
193     res = nil
194     100.times do
195       res = Net::HTTP.get_response(uri)
196       if Net::HTTPOK === res
197         puts res.body if $DEBUG
198         return
199       end
200       puts res.inspect if $DEBUG
201       sleep 0.1
202     end
203     raise "#{uri} failed to appear: #{res.inspect}"
204   end
206   def setup_mogstored
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}
218 maxconns = 1000
219 httplisten = #@test_host:#{@mogstored_http_port}
220 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
221 docroot = #@docroot
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
230   end