diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-11-07 08:44:55 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-11-07 08:44:55 +0000 |
commit | 4f927415dc2a8e8dcaeda510d351192e3cc88747 (patch) | |
tree | edaed73c7025f26956454aad368b9075ec4d37dc | |
parent | 5a4650a07f394268884a8dad58fac42f9a9288c5 (diff) | |
download | mogilefs-client-4f927415dc2a8e8dcaeda510d351192e3cc88747.tar.gz |
We'll need to setup a fresh MogileFS instance for some tests.
-rw-r--r-- | test/exec.rb | 64 | ||||
-rw-r--r-- | test/integration.rb | 61 | ||||
-rw-r--r-- | test/test_fresh.rb | 105 |
3 files changed, 171 insertions, 59 deletions
diff --git a/test/exec.rb b/test/exec.rb new file mode 100644 index 0000000..3c98369 --- /dev/null +++ b/test/exec.rb @@ -0,0 +1,64 @@ +# -*- encoding: binary -*- +$stdout.sync = $stderr.sync = true +require 'test/unit' +require 'securerandom' +require 'tempfile' +require 'digest' +require 'stringio' +require 'pp' +require 'mogilefs' + +module TestExec + def uuid + SecureRandom.uuid + rescue NoMethodError + require "uuid" + UUID.generate + end + + def yield_for_monitor_update # mogilefsd should update every 4 seconds + 50.times do + yield + sleep 0.1 + end + end + + def mogadm(*args) + x("mogadm", "--trackers=#{@trackers.join(',')}", *args) + end + + def x(*cmd) + out, err = tmpfile("out"), tmpfile("err") + puts cmd.join(' ') if $VERBOSE + pid = fork do + $stderr.reopen(err.path, "a") + $stdout.reopen(out.path, "a") + out.close + err.close + exec(*cmd) + end + _, status = Process.waitpid2(pid) + out.rewind + err.rewind + [ status, out, err ] + end + + def mogadm!(*args) + status, out, err = mogadm(*args) + assert status.success?, "#{status.inspect} / #{out.read} / #{err.read}" + [ status, out, err ] + end + + def x!(*cmd) + status, out, err = x(*cmd) + assert status.success?, "#{status.inspect} / #{out.read} / #{err.read}" + [ status, out, err ] + end + + def tmpfile(name) + tmp = Tempfile.new(name) + defined?(@to_close) or @to_close = [] + @to_close << tmp + tmp + end +end diff --git a/test/integration.rb b/test/integration.rb index cd63c3c..f3fd618 100644 --- a/test/integration.rb +++ b/test/integration.rb @@ -1,20 +1,8 @@ # -*- encoding: binary -*- -$stdout.sync = $stderr.sync = true -require 'test/unit' -require 'securerandom' -require 'tempfile' -require 'digest' -require 'stringio' -require 'pp' -require 'mogilefs' +require './test/exec' class TestMogIntegration < Test::Unit::TestCase - def uuid - SecureRandom.uuid - rescue NoMethodError - require "uuid" - UUID.generate - end + include TestExec def setup @to_close = [] @@ -29,51 +17,6 @@ class TestMogIntegration < Test::Unit::TestCase @domain = domain end - def yield_for_monitor_update # mogilefsd should update every 4 seconds - 50.times do - yield - sleep 0.1 - end - end - - def mogadm(*args) - x("mogadm", "--trackers=#{@trackers.join(',')}", *args) - end - - def x(*cmd) - out, err = tmpfile("out"), tmpfile("err") - puts cmd.join(' ') if $VERBOSE - pid = fork do - $stderr.reopen(err.path, "a") - $stdout.reopen(out.path, "a") - out.close - err.close - exec(*cmd) - end - _, status = Process.waitpid2(pid) - out.rewind - err.rewind - [ status, out, err ] - end - - def mogadm!(*args) - status, out, err = mogadm(*args) - assert status.success?, "#{status.inspect} / #{out.read} / #{err.read}" - [ status, out, err ] - end - - def x!(*cmd) - status, out, err = x(*cmd) - assert status.success?, "#{status.inspect} / #{out.read} / #{err.read}" - [ status, out, err ] - end - - def tmpfile(name) - tmp = Tempfile.new(name) - @to_close << tmp - tmp - end - def teardown if defined?(@domain) client = MogileFS::MogileFS.new :hosts => @trackers, :domain => @domain diff --git a/test/test_fresh.rb b/test/test_fresh.rb new file mode 100644 index 0000000..48b3395 --- /dev/null +++ b/test/test_fresh.rb @@ -0,0 +1,105 @@ +# -*- encoding: binary -*- +require "./test/exec" +require "tmpdir" +require "fileutils" + +class TestMogFresh < Test::Unit::TestCase + include TestExec + + def setup + @test_host = "127.0.0.1" + @tracker = TCPServer.new(@test_host, 0) + @tracker_port = @tracker.addr[1] + + @mogstored_mgmt = TCPServer.new(@test_host, 0) + @mogstored_http = TCPServer.new(@test_host, 0) + @mogstored_mgmt_port = @mogstored_mgmt.addr[1] + @mogstored_http_port = @mogstored_http.addr[1] + + @dbname = Tempfile.new(["mogfresh", "sqlite3"]) + @docroot = Dir.mktmpdir(["mogfresh", "docroot"]) + @mogstored_conf = Tempfile.new(["mogstored", "conf"]) + @mogilefsd_conf = Tempfile.new(["mogilefsd", "conf"]) + @mogstored_pid = Tempfile.new(["mogstored", "pid"]) + @mogilefsd_pid = Tempfile.new(["mogilefsd", "pid"]) + + cmd = %w(mogdbsetup --yes --type=SQLite --dbname) << @dbname.path + x!(*cmd) + + @mogilefsd_conf.puts "db_dsn DBI:SQLite:#{@dbname.path}" + @mogilefsd_conf.write <<EOF +conf_port #@tracker_port +listen #@test_host +pidfile #{@mogilefsd_pid.path} +replicate_jobs 1 +fsck_jobs 1 +query_jobs 1 +mogstored_stream_port #{@mogstored_mgmt_port} +node_timeout 10 +EOF + @mogilefsd_conf.flush + + @mogstored_conf.write <<EOF +pidfile = #{@mogstored_pid.path} +maxconns = 1000 +httplisten = #@test_host:#{@mogstored_http_port} +mgmtlisten = #@test_host:#{@mogstored_mgmt_port} +docroot = #@docroot +EOF + @mogstored_conf.flush + + @hosts = [ "#@test_host:#@tracker_port" ] + @mogstored_mgmt.close + @mogstored_http.close + @tracker.close + x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}") + wait_for_port @mogstored_http_port + wait_for_port @mogstored_mgmt_port + x!("mogilefsd", "--daemon", "--config=#{@mogilefsd_conf.path}") + wait_for_port @tracker_port + @admin = MogileFS::Admin.new(:hosts => @hosts) + 10.times do + break if @mogstored_pid.size > 0 + sleep 0.1 + end + end + + def wait_for_port(port) + tries = 50 + begin + TCPSocket.new(@test_host, port).close + return + rescue + sleep 0.1 + end while (tries -= 1) > 0 + raise "#@test_host:#{port} never became ready" + end + + def test_admin_setup_new_host_and_devices + assert_equal [], @admin.get_hosts + args = { :ip => @test_host, :port => @mogstored_http_port } + x = @admin.create_host("me", args) + yield_for_monitor_update { @admin.get_hosts.empty? or break } + hosts = @admin.get_hosts + assert_equal 1, hosts.size + host = @admin.get_hosts[0] + assert_equal "me", host["hostname"] + assert_equal @mogstored_http_port, host["http_port"].to_i + assert_equal @test_host, host["hostip"] + assert_equal hosts, @admin.get_hosts(host["hostid"]) + + assert_equal [], @admin.get_devices + end + + def teardown + if @mogstored_pid && @mogstored_pid.size > 0 + Process.kill(:TERM, @mogstored_pid.read.to_i) + end + if @mogilefsd_pid + s = TCPSocket.new(@test_host, @tracker_port) + s.write "!shutdown\r\n" + s.close + end + FileUtils.rmtree(@docroot) + end +end |