1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2015 Fujitsu Ltd. 4# Author: Zhang Jin <jy_zhangjin@cn.fujitsu.com> 5# 6# Test df command with some basic options. 7 8TST_CNT=12 9TST_SETUP=setup 10TST_CLEANUP=tst_umount 11TST_TESTFUNC=test 12TST_OPTS="f:" 13TST_USAGE=usage 14TST_PARSE_ARGS=parse_args 15TST_NEEDS_ROOT=1 16TST_NEEDS_DEVICE=1 17. tst_test.sh 18 19usage() 20{ 21 cat << EOF 22usage: $0 [-f <ext2|ext3|ext4|vfat|...>] 23 24OPTIONS 25-f Specify the type of filesystem to be built. If not 26 specified, the default filesystem type (currently ext2) 27 is used. 28EOF 29} 30 31TST_FS_TYPE=ext2 32 33parse_args() 34{ 35 TST_FS_TYPE="$2" 36} 37 38setup() 39{ 40 tst_mkfs 41 tst_mount 42 DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk 'NR==1{print $5}') 43} 44 45df_test() 46{ 47 local cmd="$1 -P" 48 49 df_verify $cmd 50 if [ $? -ne 0 ]; then 51 return 52 fi 53 54 df_check $cmd 55 if [ $? -ne 0 ]; then 56 tst_res TFAIL "'$cmd' failed, not expected." 57 return 58 fi 59 60 ROD_SILENT dd if=/dev/zero of=mntpoint/testimg bs=1024 count=1024 61 62 df_verify $cmd 63 64 df_check $cmd 65 if [ $? -eq 0 ]; then 66 tst_res TPASS "'$cmd' passed." 67 else 68 tst_res TFAIL "'$cmd' failed." 69 fi 70 71 ROD_SILENT rm -rf mntpoint/testimg 72 73 # flush file system buffers, then we can get the actual sizes. 74 sync 75} 76 77df_verify() 78{ 79 $@ >output 2>&1 80 if [ $? -ne 0 ]; then 81 grep -q -E "unrecognized option | invalid option" output 82 if [ $? -eq 0 ]; then 83 tst_res TCONF "'$@' not supported." 84 return 32 85 else 86 tst_res TFAIL "'$@' failed." 87 cat output 88 return 1 89 fi 90 fi 91} 92 93df_check() 94{ 95 if [ "$(echo $@)" = "df -i -P" ]; then 96 local total=$(stat -f mntpoint --printf=%c) 97 local free=$(stat -f mntpoint --printf=%d) 98 local used=$((total-free)) 99 else 100 local total=$(stat -f mntpoint --printf=%b) 101 local free=$(stat -f mntpoint --printf=%f) 102 local used=$((total-free)) 103 local bsize=$(stat -f mntpoint --printf=%s) 104 total=$((($total * $bsize + 512)/ 1024)) 105 used=$((($used * $bsize + 512) / 1024)) 106 fi 107 108 grep ${TST_DEVICE} output | grep -q "${total}.*${used}" 109 if [ $? -ne 0 ]; then 110 return 1 111 fi 112} 113 114test1() 115{ 116 df_test "df" 117} 118 119test2() 120{ 121 df_test "df -a" 122} 123 124test3() 125{ 126 df_test "df -i" 127} 128 129test4() 130{ 131 df_test "df -k" 132} 133 134test5() 135{ 136 df_test "df -t ${DF_FS_TYPE}" 137} 138 139test6() 140{ 141 df_test "df -T" 142} 143 144test7() 145{ 146 df_test "df -v ${TST_DEVICE}" 147} 148 149test8() 150{ 151 df_verify "df -h" 152 if [ $? -eq 0 ]; then 153 tst_res TPASS "'df -h' passed." 154 fi 155} 156 157test9() 158{ 159 df_verify "df -H" 160 if [ $? -eq 0 ]; then 161 tst_res TPASS "'df -H' passed." 162 fi 163} 164 165test10() 166{ 167 df_verify "df -m" 168 if [ $? -eq 0 ]; then 169 tst_res TPASS "'df -m' passed." 170 fi 171} 172 173test11() 174{ 175 df_verify "df --version" 176 if [ $? -eq 0 ]; then 177 tst_res TPASS "'df --version' passed." 178 fi 179} 180 181test12() 182{ 183 local cmd="df -x ${DF_FS_TYPE} -P" 184 185 df_verify $cmd 186 if [ $? -ne 0 ]; then 187 return 188 fi 189 190 grep ${TST_DEVICE} output | grep -q mntpoint 191 if [ $? -ne 0 ]; then 192 tst_res TPASS "'$cmd' passed." 193 else 194 tst_res TFAIL "'$cmd' failed." 195 fi 196} 197 198tst_run 199