df01.sh 2.8 KB

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