Re: [PATCH v2] xfs: test xfs_quota's 'dump' and 'report' -L/-R range parameters

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]



On Tue, Aug 02, 2022 at 03:05:55PM +0200, Andrey Albershteyn wrote:
> These parameters define ID range of users/groups/projects to show.
> This test adds more checks for wider coverage (e.g. empty range,
> full range, open range).
> 
> Signed-off-by: Andrey Albershteyn <aalbersh@xxxxxxxxxx>
> ---
> 
> This is regression test in relation to the patch [1].
> 
> Changes from v1:
>  - Moved to separate test (initially added to 152)
> 
> [1]: https://lore.kernel.org/all/20220328222503.146496-1-aalbersh@xxxxxxxxxx/
....
> +filter_dump()
> +{
> +	_filter_scratch| _filter_trailing_spaces | \
> +		sed -E '/^[0-9]+/s/^[0-9]+/#ID/g'
> +}

Some sed hints. This looks like you are converting a line like:

"123 xyz 456 abc     "

to

"#ID xyz 456 abc"

In this situation, you don't need a line filter regex for sed to do
the right thing - it's redundant because the search regex will pull
exactly the same line into the pattern space as the line filter
would that the regex then acts upon. Hence the sed expression can be
simplified to:

sed -E 's/^[0-9]+/#ID/g'


Then because the regex is matching the start of the line, there
cannot more than one match per line. Hence the "g" suffix that tells
sed to continue searching the pattern space after the first match
has been found is not necessary. Now we have:

sed -E 's/^[0-9]+/#ID/'

And then if we look at _filter_trailing_spaces() we see it's just
another simple sed expression. But sed has this annoying CLI issue
where if you use extended regexes (-E ...) then you can only specify
one regex to apply to the pattern space. This makes matching the
start and end of the line a bit messy and error prone. e.g:

sed -E 's/^[0-9]+(\s+.*[[:alnum:]])\s*$/#ID\1/'

Will match the ID at the start, store the bit in the middle that we
want to retain in a register by assuming it ends with a number or
letter then whitespace, then matches the whitespace at the end of
the line. It then outputs #ID followed by the contents of register
1, thereby stripping the whitespace at the end of the line.

It's a complex regex, hard to read, and quite fragile.

But if we drop the extended regexes, we can now chain multiple
simple regexes together to be applied sequentially to the pattern
space line via multiple CLI "-e" expressions. So the sed command
becomes:

_filter_scratch | sed -e 's/^[0-9]\+/#ID/' -e 's/\s*$//'

Which is much easier to understand, and does all the work in a
single sed invocation.

> +
> +filter_report()
> +{
> +	_filter_quota | _filter_trailing_spaces | grep -v "^root \|^\#0 " \
> +		| sed -e '/^#[0-9]*/s/^#[0-9]*/#ID/g'
> +}

And the same here - this removes lines starting with root or #0 from
the output with grep, then replaces the initial ID with #ID via sed.
This can all be done with a single sed invocation, and this time we
use line matching regexes to delete the pattern space before
applying the substution regexes which then don't match anything
because the pattern space is empty for the lines we want to delete
from the output:

	_filter_quota | sed -e '/^root/d' \
				-e '/^#0/d' \
				-e 's/^[0-9]\+/#ID/' \
				-e 's/\s*$//'


> +set_quota_limit()
> +{
> +	local bs=$1
> +	local bh=$2
> +	local is=$3
> +	local ih=$4
> +	local user=$5
> +
> +	$XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
> +			-c "limit -$type bsoft=$bs bhard=$bh $user" \
> +			-c "limit -$type isoft=$is ihard=$ih $user" \
> +			$SCRATCH_MNT
> +}
> +
> +test_dump()
> +{
> +	local opt="$*"
> +
> +	rm -f $tmp.backup 2>>/dev/null
> +	$XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
> +			-c "dump -$type $opt -f $tmp.backup" \
> +			$SCRATCH_MNT | _filter_scratch
> +	cat $tmp.backup | filter_dump
> +}
> +
> +test_report()
> +{
> +	local opt="$*"
> +
> +	$XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
> +			-c "report -$type $opt -bi" \
> +			$SCRATCH_MNT | filter_report
> +}
> +
> +test_xfs_quota()
> +{
> +	set_quota_limit 512k 2048k 10 20 $id
> +	set_quota_limit 1024k 4096k 10 20 $id2
> +
> +	echo "dump options test (type=$type)"
> +	echo "no options (full range)"; test_dump
> +	echo "-L option"; test_dump -L $id
> +	echo "-U option"; test_dump -U $id
> +	echo "-L/-U options (one element range)"; test_dump -L $id -U $id
> +	echo "-L/-U options (multiple elements range)"; test_dump -L $id -U $id2
> +	echo "-L/-U options (empty range)"; test_dump -L $id2 -U $id
> +	echo "-L/-U options (full range)"; test_dump -L 0 -U 0

While compact, this is difficult to read. Can you separate the echo
command from the actual test that is run? i.e.

	echo "dump options test (type=$type)"
	echo "no options (full range)";
	test_dump

	echo "-L option"
	test_dump -L $id

	echo "-U option"
	test_dump -U $id

Another option, which is also compact, is to call the test function
like so:

	test_dump "no options"
	test_dump "-L option" -L $id
	test_dump "-U option" -U $id
	.....

And capture the test description like so:

test_dump()
{
	local description=$1 ; shift
	local opt="$*"

	echo "Options: $description"

	....
}

Cheers,

Dave.
-- 
Dave Chinner
david@xxxxxxxxxxxxx



[Index of Archives]     [Linux Filesystems Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux