cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob f9733034a0710e68560cb4352db59109f8a46085 2300 bytes (raw)
$ git show HEAD:test/mgmt-usage.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
require 'test/test_helper'

class TestCmogstoredUsage < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-usage-test')
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close
    @cmd = [ 'cmogstored', '--maxconns=50', '--daemonize',
             "--mgmtlisten=#@host:#@port", "--docroot=#@tmpdir" ]
  end

  def teardown
    FileUtils.rm_rf(@tmpdir)
  end

  def test_usage_daemon
    devdir = "#@tmpdir/dev666"
    pidf = "#@tmpdir/pid"
    Dir.mkdir(devdir)
    usage = "#{devdir}/usage"
    assert ! File.exist?(usage)
    @cmd << "--pidfile=#{pidf}"
    assert(system(*@cmd), proc { $?.to_s })
    sleep(0.05) until File.exist?(usage)
    before = check_usage_file(usage, devdir)
    sleep 1.2
    after = check_usage_file(usage, devdir)
    assert(before[3].split(/ /)[1].to_i <= after[3].split(/ /)[1].to_i)
    pid = File.read(pidf).to_i
    assert(pid > 0 && pid != $$, pid.inspect)
    Process.kill(:TERM, pid)
    begin
      Process.kill(0, pid)
      sleep 0.01
    rescue Errno::ESRCH
      break
    end while true
    assert ! File.exist?(pidf), "pidfile=#{pidf} exists"
  end

  def check_usage_file(usage, devdir)
    lines = File.readlines(usage)
    assert_match(/^available: \d+$/, lines[0])
    assert_match(%r{^device: /?\S+$}, lines[1])
    assert_match(/^disk: #{Regexp.escape(devdir)}$/, lines[2])
    assert_match(/^time: \d+\n$/, lines[3])
    assert_match(/^total: \d+\n$/, lines[4])
    assert_match(/^use: \d+%$/, lines[5])
    assert_match(/^used: \d+$/, lines[6])
    lines
  end

  def test_usage_daemon_already_running
    old_err = $stderr.dup
    devdir = "#@tmpdir/dev666"
    pidf = "#@tmpdir/pid"
    Dir.mkdir(devdir)
    usage = "#{devdir}/usage"
    File.open(pidf, "w") { |fp| fp.write("#$$\n") }
    @cmd << "--pidfile=#{pidf}"
    $stderr.reopen("#@tmpdir/err", "ab")
    assert(! system(*@cmd))
    assert_equal "#$$\n", File.read(pidf)
    assert_match(/already running on PID: #$$\n/, File.read("#@tmpdir/err"))
    assert ! File.exist?(usage)
  ensure
    $stderr.reopen(old_err)
    old_err.close
  end
end

git clone https://yhbt.net/cmogstored.git