[英]How can I make sure a filesystem is ready for unmounting in a test script?
I'm using bats to test some bash scripts.我正在使用蝙蝠来测试一些 bash 脚本。 In one of the test, I need to mount a generated iso disk image and make assertions on its content.
在其中一项测试中,我需要挂载生成的 iso 磁盘映像并对其内容进行断言。
When I try to unmount the disk image right after the test, I get a Device or resource busy
error unless I insert a sleep-time before the unmount operation.当我尝试在测试后立即卸载磁盘映像时,我会收到
Device or resource busy
错误,除非我在卸载操作之前插入睡眠时间。
The script looks like this:脚本如下所示:
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
}
teardown() {
if [ -f "$BATS_TEST_TMPDIR"/mnt/my_file.txt ]; then
sleep 1
fusermount -u "$BATS_TEST_TMPDIR"/mnt
fi
}
@test 'check iso content' {
generate_iso "$BATS_TEST_TMPDIR"/my_iso.iso
mkdir "$BATS_TEST_TMPDIR"/mnt
fuseiso "$BATS_TEST_TMPDIR"/my_iso.iso "$BATS_TEST_TMPDIR"/mnt
assert grep 'A required string' < "$BATS_TEST_TMPDIR"/mnt/my_file.txt
}
This somehow works but I'm not 100% happy with the arbitrary sleep-time that may or may not be sufficient for the unmount to be successful depending on how soon the file is closed.这在某种程度上可行,但我对任意睡眠时间不是 100% 满意,这可能足以或可能不足以使卸载成功,具体取决于文件关闭的时间。
I tried to kill the process accessing the mounted filesystem by using fuser -mk "$BATS_TEST_TMPDIR"/mnt
instead of sleeping but this would eventually kill the process running the tests.我试图通过使用
fuser -mk "$BATS_TEST_TMPDIR"/mnt
而不是休眠来终止访问已安装文件系统的进程,但这最终会终止运行测试的进程。
Is there any way I can avoid the arbitrary sleep-time?有什么办法可以避免任意睡眠时间? Can I ask the OS to wait for the file to be closed before proceeding?
我可以要求操作系统在继续之前等待文件关闭吗? Any help would be greatly appreciated.
任何帮助将不胜感激。
That was actually really easy, I just needed to close the file in the last grep
(note the <&-
):这实际上非常简单,我只需要关闭最后一个
grep
中的文件(注意<&-
):
assert grep 'A required string' <&- "$BATS_TEST_TMPDIR"/mnt/my_file.txt
teardown() {
if [ -f "$BATS_TEST_TMPDIR"/mnt/my_file.txt ]; then
fusermount -u "$BATS_TEST_TMPDIR"/mnt
fi
}
@test 'check iso content' {
generate_iso "$BATS_TEST_TMPDIR"/my_iso.iso
mkdir "$BATS_TEST_TMPDIR"/mnt
fuseiso "$BATS_TEST_TMPDIR"/my_iso.iso "$BATS_TEST_TMPDIR"/mnt
assert grep 'A required string' <&- "$BATS_TEST_TMPDIR"/mnt/my_file.txt
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.