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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#!/usr/bin/env ruby
#
# Copyright 2006,2007,2008,2009,2010 Martin Pärtel <martin.partel@gmail.com>
#
# This file is part of bindfs.
#
# bindfs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# bindfs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bindfs. If not, see <http://www.gnu.org/licenses/>.
#
require 'fileutils.rb'
include FileUtils
# Set the default umask for all tests
File.umask 0022
EXECUTABLE_PATH = '../src/bindfs'
TESTDIR_NAME = 'tmp_test_bindfs'
# If set to an array of test names, only those will be run
$only_these_tests = nil
# Prepares a test environment with a mounted directory
def testenv(bindfs_args, &block)
# todo: less repetitive and more careful error handling and cleanup
testcase_title = bindfs_args
return unless $only_these_tests == nil or $only_these_tests.member? testcase_title
puts "--- #{testcase_title} ---"
puts "[ #{bindfs_args} ]"
begin
Dir.mkdir TESTDIR_NAME
rescue Exception => ex
$stderr.puts "ERROR creating testdir at #{TESTDIR_NAME}"
$stderr.puts ex
exit! 1
end
begin
Dir.chdir TESTDIR_NAME
Dir.mkdir 'src'
Dir.mkdir 'mnt'
rescue Exception => ex
$stderr.puts "ERROR preparing testdir at #{TESTDIR_NAME}"
$stderr.puts ex
exit! 1
end
bindfs_pid = nil
begin
cmd = "../#{EXECUTABLE_PATH} #{bindfs_args} src mnt"
bindfs_pid = Process.fork do
exec cmd
exit! 127
end
rescue Exception => ex
$stderr.puts "ERROR running bindfs"
$stderr.puts ex
system("rm -Rf #{TESTDIR_NAME}")
exit! 1
end
# Wait for bindfs to daemonize itself
Process.wait bindfs_pid
# Wait for the mount to appear (fuse4bsd is sometimes slow)
if `uname -a`.downcase.include? 'freebsd'
puts "Waiting for fuse4bsd"
# The mount is sometimes not ready even after it appears in `mount`.
# If there's a better solution, I'd like to hear it.
sleep 0.5
end
# TODO: check that mounting was successful
testcase_ok = true
begin
yield
rescue Exception => ex
$stderr.puts "ERROR: testcase `#{testcase_title}' failed"
$stderr.puts ex
$stderr.puts ex.backtrace
testcase_ok = false
end
begin
unless system(umount_cmd + ' mnt')
raise Exception.new(umount_cmd + " failed with status #{$?}")
end
rescue Exception => ex
$stderr.puts "ERROR: failed to umount"
$stderr.puts ex
$stderr.puts ex.backtrace
testcase_ok = false
end
begin
Dir.chdir '..'
rescue Exception => ex
$stderr.puts "ERROR: failed to exit test env"
$stderr.puts ex
$stderr.puts ex.backtrace
exit! 1
end
unless system "rm -Rf #{TESTDIR_NAME}"
$stderr.puts "ERROR: failed to clear test directory"
exit! 1
end
if testcase_ok
puts "OK"
else
exit! 1
end
end
# Like testenv but skips the test if not running as root
def root_testenv(bindfs_args, &block)
if Process.uid == 0
testenv(bindfs_args, &block)
else
puts "--- #{bindfs_args} ---"
puts "[ #{bindfs_args} ]"
puts "SKIP (requires root)"
end
end
def umount_cmd
if `which fusermount`.strip.empty?
then 'umount'
else 'fusermount -uz'
end
end
def assert
raise Exception.new('test failed') unless yield
end
def assert_exception(ex)
begin
yield
rescue ex
return
end
raise Exception.new('expected exception ' + ex.to_s)
end
|