Path: blob/main/tests/sys/fs/unionfs/unionfs_test.sh
96309 views
#!/bin/sh1#-2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2025 Klara, Inc.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# Create and mount a filesystem for use in our tests29unionfs_mkfs() {30local name=$131local size=${2:-1}32# Create mountpoint33atf_check mkdir ${name}34# Create filesystem image35atf_check -e ignore dd if=/dev/zero of=${name}.img bs=1m count=${size}36echo ${name} >>imgs37# Create memory disk38atf_check -o save:${name}.md mdconfig ${name}.img39md=$(cat ${name}.md)40echo ${md} >>mds41# Format and mount filesystem42atf_check -o ignore newfs /dev/${md}43atf_check mount /dev/${md} ${name}44echo ${name} >>mounts45}4647# Mount a unionfs48unionfs_mount() {49local upper=$150local lower=$251# Mount upper over lower52atf_check mount -t unionfs ${upper} ${lower}53echo ${lower} >>mounts54}5556# Clean up after a test57unionfs_cleanup() {58# Unmount filesystems59if [ -f mounts ]; then60tail -r mounts | while read mount; do61umount ${mount} || true62done63fi64# Destroy memory disks65if [ -f mds ]; then66tail -r mds | while read md; do67mdconfig -d -u ${md} || true68done69fi70# Delete filesystem images and mountpoints71if [ -f imgs ]; then72tail -r imgs | while read name; do73rm -f ${name}.img || true74rmdir ${name} || true75done76fi77}7879atf_test_case unionfs_basic cleanup80unionfs_basic_head() {81atf_set "descr" "Basic function test"82atf_set "require.user" "root"83atf_set "require.kmods" "unionfs"84}85unionfs_basic_body() {86# Create upper and lower87unionfs_mkfs upper88unionfs_mkfs lower89# Mount upper over lower90unionfs_mount upper lower91# Create object on unionfs92atf_check touch upper/file93atf_check mkdir upper/dir94atf_check touch lower/dir/file95# Verify that objects were created on upper96atf_check test -f lower/file97atf_check test -d lower/dir98atf_check test -f upper/dir/file99}100unionfs_basic_cleanup() {101unionfs_cleanup102}103104atf_test_case unionfs_exec cleanup105unionfs_exec_head() {106atf_set "descr" "Test executing programs"107atf_set "require.user" "root"108atf_set "require.kmods" "unionfs"109}110unionfs_exec_body() {111# Create upper and copy a binary to it112unionfs_mkfs upper113atf_check cp -p /usr/bin/true upper/upper114# Create lower and copy a binary to it115unionfs_mkfs lower116atf_check cp -p /usr/bin/true lower/lower117# Mount upper over lower118unionfs_mount upper lower119# Execute both binaries120atf_check lower/lower121atf_check lower/upper122}123unionfs_exec_cleanup() {124unionfs_cleanup125}126127atf_test_case unionfs_rename cleanup128unionfs_rename_head() {129atf_set "descr" "Test renaming objects on lower"130atf_set "require.user" "root"131atf_set "require.kmods" "unionfs"132}133unionfs_rename_body() {134# Create upper and lower135unionfs_mkfs upper136unionfs_mkfs lower137# Create objects on lower138atf_check touch lower/file139atf_check mkdir lower/dir140atf_check ln -s dead lower/link141# Mount upper over lower142unionfs_mount upper lower143# Rename objects144atf_check mv lower/file lower/newfile145atf_check mv lower/dir lower/newdir146atf_check mv lower/link lower/newlink147# Verify that old names no longer exist148atf_check test ! -f lower/file149atf_check test ! -d lower/dir150atf_check test ! -L lower/link151# Verify that new names exist on upper152atf_check test -f upper/newfile153atf_check test -d upper/newdir154atf_check test -L upper/newlink155}156unionfs_rename_cleanup() {157unionfs_cleanup158}159160atf_init_test_cases() {161atf_add_test_case unionfs_basic162atf_add_test_case unionfs_exec163atf_add_test_case unionfs_rename164}165166167