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
| | # -*- encoding: binary -*-
$stdout.sync = $stderr.sync = true
Thread.abort_on_exception = true
require 'test/unit'
require 'securerandom'
require 'tempfile'
require 'digest'
require 'stringio'
require 'pp'
require 'mogilefs'
module TestExec
def uuid
SecureRandom.uuid
rescue NoMethodError
ary = SecureRandom.random_bytes(16).unpack("NnnnnN")
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
"%08x-%04x-%04x-%04x-%04x%08x" % ary
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
|