]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
tests/lib.sh: Add function test_value_in_array
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Jun 2024 16:02:29 +0000 (18:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 24 Aug 2024 12:19:56 +0000 (12:19 +0000)
Test if a given array has the specified value stored under key.

! does not work here to access the array by variable name. So the solution here is:
https://unix.stackexchange.com/questions/60584/how-to-use-a-variable-as-part-of-an-array-name/60585#60585

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/lib.sh
tests/src/initscripts/system/functions/test2.sh [new file with mode: 0755]

index dd5e3f535bf1cd3963aa79cd44e66fab6b14dd65..716922024219ecaa29be49888eafc57a34a8d14d 100644 (file)
@@ -25,3 +25,17 @@ test_command() {
 var_has_value() {
        [[ "${!1}" == "${2}" ]]
 }
+
+test_value_in_array() {
+       local -n array="${1}"
+       local key="${2}"
+       local value="${3}"
+
+       if [[ "${array[${key}]}" == "${value}" ]] ; then
+               echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
+               return 0
+       else
+               echo -e "${CLR_RED_BG}Test failed: The array '${1}' contains the value '${array[${key}]}' under the key '${key} and not '${value}' ${CLR_RESET}"
+               return 1
+       fi
+}
diff --git a/tests/src/initscripts/system/functions/test2.sh b/tests/src/initscripts/system/functions/test2.sh
new file mode 100755 (executable)
index 0000000..a568ed2
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/bash
+
+SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
+
+ROOT="$(readlink -f "${SCRIPT_PATH}/../../../../..")"
+
+. ${ROOT}/tests/lib.sh
+
+. ${ROOT}/src/initscripts/system/functions
+
+# read the date in
+readhash "CONFIG" "${SCRIPT_PATH}/data/1"
+
+# test if we read the correct data
+test_that_key_in_arry_has_value "CONFIG" "RED_DHCP_HOSTNAME" "ipfire"
+test_that_key_in_arry_has_value "CONFIG" "BLUE_MACADDR" "bc:30:7d:58:6b:e3"
+
+test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
+test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
+