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