Skip to contentSkip to conversation

bpf/selftests: Small vmtest.sh fixes

PATCH In review Daniel Xu 3 participants 6 messages Aug 9, 2022

Subsystems (3)
lore
Share & tools
lore

Daniel Xu

author
#0 lore raw

cover bpf/selftests: Small vmtest.sh fixes

Two small quality of life fixes for vmtest.sh:

* Don't require root for -h
* Fix unset variable errors for failed option parsing

Daniel Xu (2):
selftests/bpf: Fix vmtest.sh -h to not require root selftests/bpf: Fix vmtest.sh getopts optstring

 tools/testing/selftests/bpf/vmtest.sh | 34 +++++++++++++--------------
 1 file changed, 17 insertions(+), 17 deletions(-)
Signature · 1 line
2.37.1
author
#1 lore raw

PATCH 1/2 selftests/bpf: Fix vmtest.sh -h to not require root

Set the exit trap only after argument parsing is done. This way argument parse failure or -h will not require sudo.

Reasoning is that it's confusing that a help message would require root access.

---
 tools/testing/selftests/bpf/vmtest.sh | 32 +++++++++++++--------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/vmtest.sh +16 -16
index b86ae4a2e5c5..976ef7585b33 100755
@@ -307,6 +307,20 @@ update_kconfig()
 	fi
 }
 
+catch()
+{
+	local exit_code=$1
+	local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
+	# This is just a cleanup and the directory may
+	# have already been unmounted. So, don't let this
+	# clobber the error code we intend to return.
+	unmount_image || true
+	if [[ -f "${exit_status_file}" ]]; then
+		exit_code="$(cat ${exit_status_file})"
+	fi
+	exit ${exit_code}
+}
+
 main()
 {
 	local script_dir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
@@ -353,6 +367,8 @@ main()
 	done
 	shift $((OPTIND -1))
 
+	trap 'catch "$?"' EXIT
+
 	if [[ $# -eq 0  && "${debug_shell}" == "no" ]]; then
 		echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
 	else
@@ -409,20 +425,4 @@ main()
 	fi
 }
 
-catch()
-{
-	local exit_code=$1
-	local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
-	# This is just a cleanup and the directory may
-	# have already been unmounted. So, don't let this
-	# clobber the error code we intend to return.
-	unmount_image || true
-	if [[ -f "${exit_status_file}" ]]; then
-		exit_code="$(cat ${exit_status_file})"
-	fi
-	exit ${exit_code}
-}
-
-trap 'catch "$?"' EXIT
-
 main "$@"
Signature · 1 line
2.37.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] selftests/bpf: Fix vmtest.sh -h to not require root

Quoted 69 lines · Daniel Xu

Set the exit trap only after argument parsing is done. This way argument parse failure or -h will not require sudo.

Reasoning is that it's confusing that a help message would require root access.

---
 tools/testing/selftests/bpf/vmtest.sh | 32 +++++++++++++--------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/vmtest.sh +16 -16
index b86ae4a2e5c5..976ef7585b33 100755
@@ -307,6 +307,20 @@ update_kconfig()
 	fi
 }
 
+catch()
+{
+	local exit_code=$1
+	local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
+	# This is just a cleanup and the directory may
+	# have already been unmounted. So, don't let this
+	# clobber the error code we intend to return.
+	unmount_image || true
+	if [[ -f "${exit_status_file}" ]]; then
+		exit_code="$(cat ${exit_status_file})"
+	fi
+	exit ${exit_code}
+}
+
 main()
 {
 	local script_dir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
@@ -353,6 +367,8 @@ main()
 	done
 	shift $((OPTIND -1))
 
+	trap 'catch "$?"' EXIT
+
 	if [[ $# -eq 0  && "${debug_shell}" == "no" ]]; then
 		echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
 	else
@@ -409,20 +425,4 @@ main()
 	fi
 }
 
-catch()
-{
-	local exit_code=$1
-	local exit_status_file="${OUTPUT_DIR}/${EXIT_STATUS_FILE}"
-	# This is just a cleanup and the directory may
-	# have already been unmounted. So, don't let this
-	# clobber the error code we intend to return.
-	unmount_image || true
-	if [[ -f "${exit_status_file}" ]]; then
-		exit_code="$(cat ${exit_status_file})"
-	fi
-	exit ${exit_code}
-}
-
-trap 'catch "$?"' EXIT
-
 main "$@"
Signature · 1 line
2.37.1

Makes sense and looks good to me.

author
#3 lore raw

PATCH 2/2 selftests/bpf: Fix vmtest.sh getopts optstring

In reply to Daniel Xu

Before, you could see the following errors:

```
$ ./vmtest.sh -j
./vmtest.sh: option requires an argument -- j
./vmtest.sh: line 357: OPTARG: unbound variable

$ ./vmtest.sh -z
./vmtest.sh: illegal option -- z
./vmtest.sh: line 357: OPTARG: unbound variable
```

Fix by adding ':' as first character of optstring. Reason is that getopts requires ':' as the first character for OPTARG to be set in the ? and : error cases.

---
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/vmtest.sh +1 -1
index 976ef7585b33..a29aa05ebb3e 100755
@@ -333,7 +333,7 @@ main()
 	local exit_command="poweroff -f"
 	local debug_shell="no"
 
-	while getopts 'hskid:j:' opt; do
+	while getopts ':hskid:j:' opt; do
 		case ${opt} in
 		i)
 			update_image="yes"
Signature · 1 line
2.37.1
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Fix vmtest.sh getopts optstring

Quoted 37 lines · Daniel Xu

Before, you could see the following errors:

```
$ ./vmtest.sh -j
./vmtest.sh: option requires an argument -- j
./vmtest.sh: line 357: OPTARG: unbound variable

$ ./vmtest.sh -z
./vmtest.sh: illegal option -- z
./vmtest.sh: line 357: OPTARG: unbound variable
```

Fix by adding ':' as first character of optstring. Reason is that getopts requires ':' as the first character for OPTARG to be set in the ? and : error cases.

---
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/vmtest.sh +1 -1
index 976ef7585b33..a29aa05ebb3e 100755
@@ -333,7 +333,7 @@ main()
 	local exit_command="poweroff -f"
 	local debug_shell="no"
 
-	while getopts 'hskid:j:' opt; do
+	while getopts ':hskid:j:' opt; do
 		case ${opt} in
 		i)
 			update_image="yes"
Signature · 1 line
2.37.1

I tested with this change and it worked fine for me. One thing to consider pointing out more clearly in the description is that ':' as the first character of the optstring switches getopts to silent mode. The desire to run in this mode seems to have been there all along, as the script takes care of reporting errors.

maintainer
#5 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Fix vmtest.sh getopts optstring

Quoted 30 lines · Daniel Müller
Quoted 29 lines · Daniel Xu

Before, you could see the following errors:

```
$ ./vmtest.sh -j
./vmtest.sh: option requires an argument -- j
./vmtest.sh: line 357: OPTARG: unbound variable

$ ./vmtest.sh -z
./vmtest.sh: illegal option -- z
./vmtest.sh: line 357: OPTARG: unbound variable
```

Fix by adding ':' as first character of optstring. Reason is that getopts requires ':' as the first character for OPTARG to be set in the ? and : error cases.

---
  tools/testing/selftests/bpf/vmtest.sh | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/vmtest.sh +0 -0
index 976ef7585b33..a29aa05ebb3e 100755
@@ -333,7 +333,7 @@ main()
  	local exit_command="poweroff -f"
  	local debug_shell="no"
 
Quoted 11 lines

tools/testing/selftests/bpf/vmtest.sh main() · line 339

Quoted 5 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
-	while getopts 'hskid:j:' opt; do
+	while getopts ':hskid:j:' opt; do
  		case ${opt} in
  		i)
  			update_image="yes"

I tested with this change and it worked fine for me. One thing to consider pointing out more clearly in the description is that ':' as the first character of the optstring switches getopts to silent mode. The desire to run in this mode seems to have been there all along, as the script takes care of reporting errors.

I've added this description to the commit message as well for future reference while applying. Thanks everyone!

Quoted 2 lines