From 0782d31819660ed88badadf379e5dd4f34ea69cd Mon Sep 17 00:00:00 2001
From: Anders Roxell <anders.roxell@linaro.org>
Date: Mon, 8 Oct 2018 11:21:42 +0200
Subject: [PATCH] selftests: sysctl: fix always skipped test

The test will always be skipped if generating the kernel .config file
with kselftest-merge. The sysctl.sh always test test_modprobe and that
function looks for the directory /sys/module/test_sysctl/ and that isn't
created since the config fragment in
tools/testing/selftests/sysctl/config buildin the CONFIG_TEST_SYSCTL=y and
not as a module.
This will result in that the test will always be skipped.

selftests: sysctl: sysctl.sh
./sysctl.sh: /sys/module/test_sysctl/ not present
You must have the following enabled in your kernel:
cat: ./config: No such file or directory
not ok 1..1 selftests: sysctl: sysctl.sh [SKIP]

Reworked so that the script:
1. check to load module.
2. check that the files exists in /sys/module/test_sysctl/ that you want
   to test.
3. run the test.

Fixes: 64b671204afd ("test_sysctl: add generic script to expand on tests")
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 tools/testing/selftests/sysctl/sysctl.sh | 41 +++++++++++---------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
index 584eb8ea780a..a925d040dfe0 100755
--- a/tools/testing/selftests/sysctl/sysctl.sh
+++ b/tools/testing/selftests/sysctl/sysctl.sh
@@ -19,7 +19,6 @@ ksft_skip=4
 
 TEST_NAME="sysctl"
 TEST_DRIVER="test_${TEST_NAME}"
-TEST_DIR=$(dirname $0)
 TEST_FILE=$(mktemp)
 
 # This represents
@@ -38,21 +37,8 @@ ALL_TESTS="$ALL_TESTS 0003:1:1"
 ALL_TESTS="$ALL_TESTS 0004:1:1"
 ALL_TESTS="$ALL_TESTS 0005:3:1"
 
-test_modprobe()
-{
-       if [ ! -d $DIR ]; then
-               echo "$0: $DIR not present" >&2
-               echo "You must have the following enabled in your kernel:" >&2
-               cat $TEST_DIR/config >&2
-               exit $ksft_skip
-       fi
-}
-
 function allow_user_defaults()
 {
-	if [ -z $DIR ]; then
-		DIR="/sys/module/test_sysctl/"
-	fi
 	if [ -z $DEFAULT_NUM_TESTS ]; then
 		DEFAULT_NUM_TESTS=50
 	fi
@@ -120,16 +106,7 @@ test_reqs()
 
 function load_req_mod()
 {
-	if [ ! -d $DIR ]; then
-		if ! modprobe -q -n $TEST_DRIVER; then
-			echo "$0: module $TEST_DRIVER not found [SKIP]"
-			exit $ksft_skip
-		fi
-		modprobe $TEST_DRIVER
-		if [ $? -ne 0 ]; then
-			exit
-		fi
-	fi
+	modprobe $TEST_DRIVER
 }
 
 reset_vals()
@@ -548,9 +525,18 @@ run_stringtests()
 	test_rc
 }
 
+check_sysctl_file()
+{
+	if [ ! -f ${1} ]; then
+		echo "$0: ${1} not present" >&2
+		exit $ksft_skip
+	fi
+}
+
 sysctl_test_0001()
 {
 	TARGET="${SYSCTL}/int_0001"
+	check_sysctl_file ${TARGET}
 	reset_vals
 	ORIG=$(cat "${TARGET}")
 	TEST_STR=$(( $ORIG + 1 ))
@@ -562,6 +548,7 @@ sysctl_test_0001()
 sysctl_test_0002()
 {
 	TARGET="${SYSCTL}/string_0001"
+	check_sysctl_file ${TARGET}
 	reset_vals
 	ORIG=$(cat "${TARGET}")
 	TEST_STR="Testing sysctl"
@@ -575,6 +562,7 @@ sysctl_test_0002()
 sysctl_test_0003()
 {
 	TARGET="${SYSCTL}/int_0002"
+	check_sysctl_file ${TARGET}
 	reset_vals
 	ORIG=$(cat "${TARGET}")
 	TEST_STR=$(( $ORIG + 1 ))
@@ -587,6 +575,7 @@ sysctl_test_0003()
 sysctl_test_0004()
 {
 	TARGET="${SYSCTL}/uint_0001"
+	check_sysctl_file ${TARGET}
 	reset_vals
 	ORIG=$(cat "${TARGET}")
 	TEST_STR=$(( $ORIG + 1 ))
@@ -599,6 +588,7 @@ sysctl_test_0004()
 sysctl_test_0005()
 {
 	TARGET="${SYSCTL}/int_0003"
+	check_sysctl_file ${TARGET}
 	reset_vals
 	ORIG=$(cat "${TARGET}")
 
@@ -620,8 +610,6 @@ list_tests()
 	echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
 }
 
-test_reqs
-
 usage()
 {
 	NUM_TESTS=$(grep -o ' ' <<<"$ALL_TESTS" | grep -c .)
@@ -770,7 +758,6 @@ function parse_args()
 test_reqs
 allow_user_defaults
 check_production_sysctl_writes_strict
-test_modprobe
 load_req_mod
 
 trap "test_finish" EXIT
-- 
2.11.0

