http_file: remove unused attrs
[ruby-mogilefs-client.git] / test / fresh.rb
blobfec8769f8728be9e1a478552e0a3095ce856a654
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     assert_nothing_raised do
94       @admin.create_class(domain, "klassy", 1)
95     end
96     assert_raises(MogileFS::Backend::ClassExistsError) do
97       @admin.create_class(domain, "klassy", 1)
98     end
100     assert_nothing_raised do
101       @admin.update_class(domain, "klassy",
102                           :mindevcount => 1, :replpolicy => "MultipleHosts(1)")
103     end
105     tmp = nil
106     yield_for_monitor_update do
107       tmp = @admin.get_domains[domain]["klassy"]
108       break if tmp && tmp["replpolicy"] == "MultipleHosts(1)"
109     end
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) }
114     ensure
115       @admin.delete_class(domain, "klassy") rescue nil
116   end
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"
133     out = err = nil
134     tries = 0
135     begin
136       out.close! if out
137       err.close! if err
138       status, out, err = mogadm("check")
139       if (tries += 1) > 100
140         warn err.read
141         puts out.read
142         raise "mogadm failed"
143       end
144       sleep 0.1
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 }
150     @domain = domain
151   end
153   def test_device_file_add
154     add_host_device_domain
155     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
156     r, w = IO.pipe
157     thr = Thread.new do
158       (0..9).each do |i|
159         sleep 0.05
160         w.write("#{i}\n")
161       end
162       w.close
163       :ok
164     end
165     assert_equal 20, client.store_file("pipe", nil, r)
166     assert_equal :ok, thr.value
167     r.close
168     assert_equal "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", client.get_file_data("pipe")
169   end
171   def teardown_mogilefs
172     if @mogstored_pid
173       pid = File.read(@mogstored_pid.path).to_i
174       Process.kill(:TERM, pid) if pid > 0
175     end
176     if @mogilefsd_pid
177       s = TCPSocket.new(@test_host, @tracker_port)
178       s.write "!shutdown\r\n"
179       s.close
180     end
181     FileUtils.rmtree(@docroot)
182   end
184   def wait_for_usage_file(device)
185     uri = URI("http://#@test_host:#@mogstored_http_port/#{device}/usage")
186     res = nil
187     100.times do
188       res = Net::HTTP.get_response(uri)
189       if Net::HTTPOK === res
190         puts res.body if $DEBUG
191         return
192       end
193       puts res.inspect if $DEBUG
194       sleep 0.1
195     end
196     raise "#{uri} failed to appear: #{res.inspect}"
197   end
199   def setup_mogstored
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}
209 maxconns = 1000
210 httplisten = #@test_host:#{@mogstored_http_port}
211 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
212 docroot = #@docroot
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
221   end