1#!/bin/sh 2# 3# Copyright (c) 2015 Fujitsu Ltd. 4# Author: Zhang Jin <jy_zhangjin@cn.fujitsu.com> 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14# the GNU General Public License for more details. 15# 16# Test df command with some basic options. 17# 18 19TST_CNT=12 20TST_SETUP=setup 21TST_CLEANUP=cleanup 22TST_TESTFUNC=test 23TST_OPTS="f:" 24TST_USAGE=usage 25TST_PARSE_ARGS=parse_args 26TST_NEEDS_ROOT=1 27TST_NEEDS_TMPDIR=1 28TST_NEEDS_DEVICE=1 29. tst_test.sh 30 31usage() 32{ 33 cat << EOF 34usage: $0 [-f <ext2|ext3|ext4|vfat|...>] 35 36OPTIONS 37-f Specify the type of filesystem to be built. If not 38 specified, the default filesystem type (currently ext2) 39 is used. 40EOF 41} 42 43FS_TYPE=ext2 44 45parse_args() 46{ 47 FS_TYPE="$2" 48} 49 50setup() 51{ 52 tst_mkfs ${FS_TYPE} ${TST_DEVICE} 53 54 ROD_SILENT mkdir -p mntpoint 55 56 mount ${TST_DEVICE} mntpoint 57 ret=$? 58 if [ $ret -eq 32 ]; then 59 tst_brk TCONF "Cannot mount ${FS_TYPE}, missing driver?" 60 fi 61 62 if [ $ret -ne 0 ]; then 63 tst_brk TBROK "Failed to mount device: mount exit = $ret" 64 fi 65 66 DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}') 67} 68 69cleanup() 70{ 71 tst_umount ${TST_DEVICE} 72} 73 74df_test() 75{ 76 cmd="$1 -P" 77 78 df_verify $cmd 79 if [ $? -ne 0 ]; then 80 return 81 fi 82 83 df_check $cmd 84 if [ $? -ne 0 ]; then 85 tst_res TFAIL "'$cmd' failed, not expected." 86 return 87 fi 88 89 ROD_SILENT dd if=/dev/zero of=mntpoint/testimg bs=1024 count=1024 90 91 df_verify $cmd 92 93 df_check $cmd 94 if [ $? -eq 0 ]; then 95 tst_res TPASS "'$cmd' passed." 96 else 97 tst_res TFAIL "'$cmd' failed." 98 fi 99 100 ROD_SILENT rm -rf mntpoint/testimg 101 102 # flush file system buffers, then we can get the actual sizes. 103 sync 104} 105 106df_verify() 107{ 108 $@ >output 2>&1 109 if [ $? -ne 0 ]; then 110 grep -q -E "unrecognized option | invalid option" output 111 if [ $? -eq 0 ]; then 112 tst_res TCONF "'$1' not supported." 113 return 32 114 else 115 tst_res TFAIL "'$1' failed." 116 cat output 117 return 1 118 fi 119 fi 120} 121 122df_check() 123{ 124 if [ "$(echo $@)" = "df -i -P" ]; then 125 local total=$(stat -f mntpoint --printf=%c) 126 local free=$(stat -f mntpoint --printf=%d) 127 local used=$((total-free)) 128 else 129 local total=$(stat -f mntpoint --printf=%b) 130 local free=$(stat -f mntpoint --printf=%f) 131 local used=$((total-free)) 132 local bsize=$(stat -f mntpoint --printf=%s) 133 total=$(($total * $bsize / 1024)) 134 used=$(($used * $bsize / 1024)) 135 fi 136 137 grep ${TST_DEVICE} output | grep -q "${total}.*${used}" 138 if [ $? -ne 0 ]; then 139 return 1 140 fi 141} 142 143test1() 144{ 145 df_test "df" 146} 147 148test2() 149{ 150 df_test "df -a" 151} 152 153test3() 154{ 155 df_test "df -i" 156} 157 158test4() 159{ 160 df_test "df -k" 161} 162 163test5() 164{ 165 df_test "df -t ${DF_FS_TYPE}" 166} 167 168test6() 169{ 170 df_test "df -T" 171} 172 173test7() 174{ 175 df_test "df -v ${TST_DEVICE}" 176} 177 178test8() 179{ 180 df_verify "df -h" 181 if [ $? -eq 0 ]; then 182 tst_res TPASS "'df -h' passed." 183 fi 184} 185 186test9() 187{ 188 df_verify "df -H" 189 if [ $? -eq 0 ]; then 190 tst_res TPASS "'df -H' passed." 191 fi 192} 193 194test10() 195{ 196 df_verify "df -m" 197 if [ $? -eq 0 ]; then 198 tst_res TPASS "'df -m' passed." 199 fi 200} 201 202test11() 203{ 204 df_verify "df --version" 205 if [ $? -eq 0 ]; then 206 tst_res TPASS "'df --version' passed." 207 fi 208} 209 210test12() 211{ 212 cmd="df -x ${DF_FS_TYPE} -P" 213 214 df_verify $cmd 215 if [ $? -ne 0 ]; then 216 return 217 fi 218 219 grep ${TST_DEVICE} output | grep -q mntpoint 220 if [ $? -ne 0 ]; then 221 tst_res TPASS "'$cmd' passed." 222 else 223 tst_res TFAIL "'$cmd' failed." 224 fi 225} 226 227tst_run 228