generic/386: check the correct field from df output
[xfstests-dev.git] / tests / generic / 386
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2011 SGI.  All Rights Reserved.
4 #
5 # FS QA Test No. 386
6 #
7 # This test checks the project quota values reported by the quota
8 # "df" and "report" subcommands to ensure they match what they
9 # should be.  There was a bug (fixed by xfsprogs commit 7cb2d41b)
10 # where the values reported were double what they should have been.
11 #
12 # SGI PV 1015651
13 #
14 seq=$(basename $0)
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17
18 here=$(pwd)
19
20 rm -f $seqres.full
21
22 tmp=/tmp/$$
23 my_projects=$tmp.projects
24 my_projid=$tmp.projid
25 proj_name=test_project
26 proj_num=1
27
28 qlimit_meg=500  # 500M limit imposed = 500 * 1024 * 1024 bytes
29
30 status=1        # failure is the default!
31 trap "_cleanup; exit \$status" 0 1 2 3 15
32
33 _cleanup()
34 {
35     cd /
36     rm -f $tmp.*
37 }
38
39 # get standard environment, filters and checks
40 . ./common/rc
41 . ./common/filter
42 . ./common/quota
43
44 echo "Silence is golden."
45
46 # real QA test starts here
47
48 proj_dir="$SCRATCH_MNT/test"
49
50 # Modify as appropriate.
51 _supported_fs generic
52 _supported_os Linux
53
54 _require_quota
55 _require_xfs_quota_foreign
56 _require_scratch
57
58 # Make sure the hard limits reported are what was set.
59 # It is entirely too clever...
60 # It exploits the fact that we've set the soft and hard limits to
61 # the same value, and as a result the value in the second field in
62 # both the "df" and the "report" output.  For "report", the line we're
63 # interested in contains our project name in the first field.  For "df"
64 # it contains our project directory in the last field.
65 # But if the device name is too long, the "df" output is broke into two
66 # lines, the second field is not correct, so take $(NF-4) of "df"
67 _filter_quota_rpt() {
68         awk '
69         BEGIN {
70                 proj_name = "'${proj_name}'";
71                 proj_dir = "'${proj_dir}'";
72                 qlimit_meg = '${qlimit_meg}';
73                 qlimit = qlimit_meg * 1024 * 1024;
74         }
75         # This function parses the human-readable values produced
76         # by xfs_quota output
77         function byte_size(value,  result) {
78                 result = strtonum(value);
79                 unit = value;
80                 gsub("[0-9][0-9]*", "", unit);
81                 shift = index("KMGTPE", unit);
82                 while (shift--)
83                         result *= 1024;
84                 return result;
85         }
86         {
87                 if ($1 ~ proj_name) {
88                         # this is the "report" output
89                         bsize = byte_size($4);
90                 } else if ($NF ~ proj_dir) {
91                         # this is the "df" output
92                         bsize = byte_size($NF-4));
93                 } else {
94                         next;
95                 }
96                 if (bsize != qlimit)
97                         printf("hard limit %d bytes, expected %d\n",
98                                 bsize, qlimit);
99         }
100         '
101 }
102
103 _quota_cmd() {
104         $XFS_QUOTA_PROG -P "$my_projid" -D "$my_projects" -x \
105                 -c "$@" "$SCRATCH_MNT"
106 }
107
108 # Set up--mount scratch and create the project directory
109
110 echo $proj_name:$proj_num > "$my_projid"
111 echo $proj_num:$proj_dir > "$my_projects"
112
113 _scratch_mkfs                                   >> "$seqres.full" 2>&1
114 _scratch_enable_pquota
115
116 _qmount_option "prjquota"
117 _qmount
118 _require_prjquota $SCRATCH_DEV
119
120 mkdir -p "${proj_dir}"
121
122 # Setup the project quota directory
123 _quota_cmd "project -s ${proj_name}"                    >> "$seqres.full" 2>&1
124
125 # Assign block quota limits
126 _quota_cmd "limit -p bhard=${qlimit_meg}m bsoft=${qlimit_meg}m ${proj_name}" \
127                                                         2>> "$seqres.full" 1>&2
128
129 # See what gets reported
130 _quota_cmd "report"             | _filter_quota_rpt     2>> "$seqres.full"
131 _quota_cmd "df"                 | _filter_quota_rpt     2>> "$seqres.full"
132
133 # This time using "human readable" output
134 _quota_cmd "report -h"          | _filter_quota_rpt     2>> "$seqres.full"
135 _quota_cmd "df -h"              | _filter_quota_rpt     2>> "$seqres.full"
136
137 # Clean up
138 rm -rf "$proj_dir"
139 _scratch_unmount
140
141 status=0        # success, all done