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