diff mbox series

[v4,3/4] tst_test.sh: Fix _tst_cleanup_timer() on set -e

Message ID 20220808113756.11582-4-pvorel@suse.cz
State Accepted
Headers show
Series set -e and bashism fixes | expand

Commit Message

Petr Vorel Aug. 8, 2022, 11:37 a.m. UTC
If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
have nothing to kill therefore following wait exits 143.

set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit code,
fix hardens _tst_cleanup_timer() to be able to be used on scripts with it.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
I can use 'if ...; then ; fi' if you prefer:

	if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]; then
		return 0
	fi

	if [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ]; then
		return 1
	fi

...
	if [ "$color" = 1 ]; then
		tst_flag2color "$1"
	fi
	printf "$2"
	if [ "$color" = 1 ]; then
		printf '\033[0m'
	fi


Kind regards,
Petr

 testcases/lib/tst_test.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Li Wang Aug. 9, 2022, 9:35 a.m. UTC | #1
On Mon, Aug 8, 2022 at 7:39 PM Petr Vorel <pvorel@suse.cz> wrote:

> If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
> have nothing to kill therefore following wait exits 143.
>
> set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit code,
> fix hardens _tst_cleanup_timer() to be able to be used on scripts with it.
>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
> I can use 'if ...; then ; fi' if you prefer:
>
>         if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0"
> ]; then
>                 return 0
>         fi
>
>         if [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" =
> "1" ]; then
>                 return 1
>         fi
>
> ...
>         if [ "$color" = 1 ]; then
>                 tst_flag2color "$1"
>         fi
>         printf "$2"
>         if [ "$color" = 1 ]; then
>                 printf '\033[0m'
>         fi
>

These ^ clarifications should be added in patch 2/4, right?

Anyway, this one looks good.
Reviewed-by: Li Wang <liwang@redhat.com>
Petr Vorel Aug. 9, 2022, 12:13 p.m. UTC | #2
Hi Li,

> These ^ clarifications should be added in patch 2/4, right?
Yes, put it there.

> Anyway, this one looks good.
> Reviewed-by: Li Wang <liwang@redhat.com>
Thanks for your time!

Kind regards,
Petr
Martin Doucha Aug. 10, 2022, 3:08 p.m. UTC | #3
Hi,
a comment explaining why this is needed would be nice, otherwise looks
good. For patches 2 and 3:

Reviewed-by: Martin Doucha <mdoucha@suse.cz>

On 08. 08. 22 13:37, Petr Vorel wrote:
> If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
> have nothing to kill therefore following wait exits 143.
> 
> set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit code,
> fix hardens _tst_cleanup_timer() to be able to be used on scripts with it.
> 
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
> I can use 'if ...; then ; fi' if you prefer:
> 
> 	if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]; then
> 		return 0
> 	fi
> 
> 	if [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ]; then
> 		return 1
> 	fi
> 
> ...
> 	if [ "$color" = 1 ]; then
> 		tst_flag2color "$1"
> 	fi
> 	printf "$2"
> 	if [ "$color" = 1 ]; then
> 		printf '\033[0m'
> 	fi
> 
> 
> Kind regards,
> Petr
> 
>  testcases/lib/tst_test.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
> index 356af0106..1d2bf06cc 100644
> --- a/testcases/lib/tst_test.sh
> +++ b/testcases/lib/tst_test.sh
> @@ -518,7 +518,7 @@ _tst_cleanup_timer()
>  {
>  	if [ -n "$_tst_setup_timer_pid" ]; then
>  		kill -TERM $_tst_setup_timer_pid 2>/dev/null
> -		wait $_tst_setup_timer_pid 2>/dev/null
> +		wait $_tst_setup_timer_pid 2>/dev/null || true
>  	fi
>  }
>
Petr Vorel Aug. 10, 2022, 4:29 p.m. UTC | #4
Hi Martin,

> Hi,
> a comment explaining why this is needed would be nice, otherwise looks
> good. For patches 2 and 3:

> Reviewed-by: Martin Doucha <mdoucha@suse.cz>

> On 08. 08. 22 13:37, Petr Vorel wrote:
> > If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
> > have nothing to kill therefore following wait exits 143.
I thought this is the explanation. Or would you prefer anything else to add?

Kind regards,
Petr

> > set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit code,
> > fix hardens _tst_cleanup_timer() to be able to be used on scripts with it.

> > Signed-off-by: Petr Vorel <pvorel@suse.cz>
> > ---
> > I can use 'if ...; then ; fi' if you prefer:

> > 	if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]; then
> > 		return 0
> > 	fi

> > 	if [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ]; then
> > 		return 1
> > 	fi

> > ...
> > 	if [ "$color" = 1 ]; then
> > 		tst_flag2color "$1"
> > 	fi
> > 	printf "$2"
> > 	if [ "$color" = 1 ]; then
> > 		printf '\033[0m'
> > 	fi


> > Kind regards,
> > Petr

> >  testcases/lib/tst_test.sh | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)

> > diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
> > index 356af0106..1d2bf06cc 100644
> > --- a/testcases/lib/tst_test.sh
> > +++ b/testcases/lib/tst_test.sh
> > @@ -518,7 +518,7 @@ _tst_cleanup_timer()
> >  {
> >  	if [ -n "$_tst_setup_timer_pid" ]; then
> >  		kill -TERM $_tst_setup_timer_pid 2>/dev/null
> > -		wait $_tst_setup_timer_pid 2>/dev/null
> > +		wait $_tst_setup_timer_pid 2>/dev/null || true
> >  	fi
> >  }
Martin Doucha Aug. 11, 2022, 8 a.m. UTC | #5
On 10. 08. 22 18:29, Petr Vorel wrote:
> Hi Martin,
> 
>> Hi,
>> a comment explaining why this is needed would be nice, otherwise looks
>> good. For patches 2 and 3:
> 
>> Reviewed-by: Martin Doucha <mdoucha@suse.cz>
> 
>> On 08. 08. 22 13:37, Petr Vorel wrote:
>>> If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
>>> have nothing to kill therefore following wait exits 143.
> I thought this is the explanation. Or would you prefer anything else to add?

The commit message will get buried deep in Git history. It's better to
comment this in the code because it's not obvious why the "|| true" is
needed after "wait ..."
Petr Vorel Aug. 11, 2022, 8:08 a.m. UTC | #6
> On 10. 08. 22 18:29, Petr Vorel wrote:
> > Hi Martin,

> >> Hi,
> >> a comment explaining why this is needed would be nice, otherwise looks
> >> good. For patches 2 and 3:

> >> Reviewed-by: Martin Doucha <mdoucha@suse.cz>

> >> On 08. 08. 22 13:37, Petr Vorel wrote:
> >>> If test exits on time (i.e. no timeout) kill in _tst_cleanup_timer()
> >>> have nothing to kill therefore following wait exits 143.
> > I thought this is the explanation. Or would you prefer anything else to add?

> The commit message will get buried deep in Git history. It's better to
> comment this in the code because it's not obvious why the "|| true" is
> needed after "wait ..."
Ah, didn't get "comment" means comment in the code. Makes sense, thx!

Kind regards,
Petr
Petr Vorel Aug. 11, 2022, 8:35 a.m. UTC | #7
Hi all,

FYI merged.

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
index 356af0106..1d2bf06cc 100644
--- a/testcases/lib/tst_test.sh
+++ b/testcases/lib/tst_test.sh
@@ -518,7 +518,7 @@  _tst_cleanup_timer()
 {
 	if [ -n "$_tst_setup_timer_pid" ]; then
 		kill -TERM $_tst_setup_timer_pid 2>/dev/null
-		wait $_tst_setup_timer_pid 2>/dev/null
+		wait $_tst_setup_timer_pid 2>/dev/null || true
 	fi
 }