2 # -*- encoding: binary -*-
3 # Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
4 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
5 require 'test/test_helper'
8 class TestMgmtIostat < Test::Unit::TestCase
9 TEST_PATH = File.dirname(__FILE__) + ":#{ENV['PATH']}"
10 RUBY = ENV["RUBY"] || "ruby"
13 "#{RUBY} #{File.dirname(__FILE__)}/iostat-mock.rb"
17 @iostat_pid = Tempfile.new('testt-iostat-pid')
18 @tmpdir = Dir.mktmpdir('cmogstored-mgmt-iostat-test')
20 srv = TCPServer.new(@host, 0)
23 cmd = [ "cmogstored", "--docroot=#@tmpdir", "--mgmtlisten=#@host:#@port",
25 vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
32 @to_close.each { |io| io.close unless io.closed? }
34 Process.kill(:QUIT, @pid) rescue nil
35 _, status = Process.waitpid2(@pid)
36 assert status.success?, status.inspect
38 FileUtils.rm_rf(@tmpdir)
41 def __test_iostat_dies(workers = false)
42 Dir.mkdir "#@tmpdir/dev666"
43 err = Tempfile.new('err')
45 ENV["PATH"] = TEST_PATH
46 ENV["MOG_IOSTAT_CMD"] = "#{iostat_mock} #{@iostat_pid.path} slow"
47 $stderr.reopen(err.path, 'a')
48 @cmd << workers if workers
52 Timeout.timeout(30) do
54 iostat_pid = @iostat_pid.read.to_i
55 end while iostat_pid == 0 && sleep(0.05)
56 Process.kill(:TERM, iostat_pid)
59 expect = %r{iostat done \(pid=#{iostat_pid}, status=\d}
60 Timeout.timeout(30) do
62 stderr = File.read(err.path)
63 break if stderr =~ expect
75 def test_iostat_dies_with_workers
76 __test_iostat_dies("--worker-processes=1")
80 Dir.mkdir "#@tmpdir/dev666"
82 ENV["PATH"] = TEST_PATH
83 ENV["MOG_IOSTAT_CMD"] = "#{iostat_mock} #{@iostat_pid.path} fast"
90 nr = RUBY_PLATFORM =~ /linux/ ? 400 : 10
92 threads << Thread.new do
94 assert_equal 6, c.write("watch\n")
95 100.times { assert_kind_of(String, c.gets) }
101 threads.each { |th| th.value.close }
102 assert og.readpartial(16384)
104 assert og.read_nonblock(512)
106 assert og.read_nonblock(512)
107 iostat_pid = @iostat_pid.read.to_i
109 Process.kill(:TERM, iostat_pid)
113 def test_iostat_bursty1
114 iostat_edge_case("bursty1")
117 def test_iostat_bursty2
118 iostat_edge_case("bursty2")
122 iostat_edge_case("slow")
125 def iostat_edge_case(type)
126 Dir.mkdir "#@tmpdir/dev666"
128 ENV["PATH"] = TEST_PATH
129 ENV["MOG_IOSTAT_CMD"] = "#{iostat_mock} #{@iostat_pid.path} #{type}"
136 assert_match(/\n$/, x = og.gets)