common/rc: Add _require_{chown,chmod}()
[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
53 _require_quota
54 _require_xfs_quota_foreign
55 _require_scratch
56
57 # Make sure the hard limits reported are what was set.
58 # It is entirely too clever...
59 # It exploits the fact that we've set the soft and hard limits to
60 # the same value, and as a result the value in the second field in
61 # both the "df" and the "report" output.  For "report", the line we're
62 # interested in contains our project name in the first field.  For "df"
63 # it contains our project directory in the last field.
64 # But if the device name is too long, the "df" output is broke into two
65 # lines, the second field is not correct, so take $(NF-4) of "df"
66 _filter_quota_rpt() {
67         awk '
68         BEGIN {
69                 proj_name = "'${proj_name}'";
70                 proj_dir = "'${proj_dir}'";
71                 qlimit_meg = '${qlimit_meg}';
72                 qlimit = qlimit_meg * 1024 * 1024;
73         }
74         # This function parses the human-readable values produced
75         # by xfs_quota output
76         function byte_size(value,  result) {
77                 result = strtonum(value);
78                 unit = value;
79                 gsub("[0-9][0-9]*", "", unit);
80                 shift = index("KMGTPE", unit);
81                 while (shift--)
82                         result *= 1024;
83                 return result;
84         }
85         {
86                 if ($1 ~ proj_name) {
87                         # this is the "report" output
88                         bsize = byte_size($4);
89                 } else if ($NF ~ proj_dir) {
90                         # this is the "df" output
91                         bsize = byte_size($(NF-4));
92                 } else {
93                         next;
94                 }
95                 if (bsize != qlimit)
96                         printf("hard limit %d bytes, expected %d\n",
97                                 bsize, qlimit);
98         }
99         '
100 }
101
102 _quota_cmd() {
103         $XFS_QUOTA_PROG -P "$my_projid" -D "$my_projects" -x \
104                 -c "$@" "$SCRATCH_MNT"
105 }
106
107 # Set up--mount scratch and create the project directory
108
109 echo $proj_name:$proj_num > "$my_projid"
110 echo $proj_num:$proj_dir > "$my_projects"
111
112 _scratch_mkfs                                   >> "$seqres.full" 2>&1
113 _scratch_enable_pquota
114
115 _qmount_option "prjquota"
116 _qmount
117 _require_prjquota $SCRATCH_DEV
118
119 mkdir -p "${proj_dir}"
120
121 # Setup the project quota directory
122 _quota_cmd "project -s ${proj_name}"                    >> "$seqres.full" 2>&1
123
124 # Assign block quota limits
125 _quota_cmd "limit -p bhard=${qlimit_meg}m bsoft=${qlimit_meg}m ${proj_name}" \
126                                                         2>> "$seqres.full" 1>&2
127
128 # See what gets reported
129 _quota_cmd "report"             | _filter_quota_rpt     2>> "$seqres.full"
130 _quota_cmd "df"                 | _filter_quota_rpt     2>> "$seqres.full"
131
132 # This time using "human readable" output
133 _quota_cmd "report -h"          | _filter_quota_rpt     2>> "$seqres.full"
134 _quota_cmd "df -h"              | _filter_quota_rpt     2>> "$seqres.full"
135
136 # Clean up
137 rm -rf "$proj_dir"
138 _scratch_unmount
139
140 status=0        # success, all done