From patchwork Sun Jun 16 16:02:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonatan Schlag X-Patchwork-Id: 7864 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4W2HqW0Qn4z3ws3 for ; Sun, 16 Jun 2024 16:03:19 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4W2HqL2WcZz5Zn; Sun, 16 Jun 2024 16:03:10 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4W2HqL208bz33Fr; Sun, 16 Jun 2024 16:03:10 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4W2HqH6dfXz2xtd for ; Sun, 16 Jun 2024 16:03:07 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4W2HqH3fCcz1X1; Sun, 16 Jun 2024 16:03:07 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1718553787; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4URYk5JjRqZEpqXWoJiqlJ2ndlrwZAmekR/9FP+oBGo=; b=HD0UxSHhvdwzAYkkGrpVFVlwdKYqiCPoPHiiXTqfs5Y+6wrNDL/RfCskjZUHWR1p8cb3Vv 1EEKxenJ/aKsWjBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1718553787; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4URYk5JjRqZEpqXWoJiqlJ2ndlrwZAmekR/9FP+oBGo=; b=eI3v90UdMYG2U2JuTluGlNMxCXzjifePZ4IY5TC61saBun84YXycHgk8WcPWDBzRG1BbFA 0zK6fvOpMCbzyyBUAVpO702oVPXos5bqrZMVtLNSOiz+omNjwM1W+mPOXVHny7cBU2W04J kHTodDi+SdEFx+H5mBfTlPN122VFVnd644sQC3wVYopef5rpWWY4fxNn/dD7+vT2hcv95s BKa/fy9JUsEwYbHkAiIUsaH3AXrhWcfWp3IsgrJZ//qHUt7191ov0cxsuzzce+QHyF+TUN eyyUVEv5WTEiJmOcfyu7vUhNBosNua34vQlqhsS4FJSFPbMXaJHecPSfBEhRxg== From: Jonatan Schlag To: development@lists.ipfire.org Subject: [PATCH v2 02/18] tests/lib.sh: Add function test_value_in_array Date: Sun, 16 Jun 2024 18:02:29 +0200 Message-Id: <20240616160245.18865-3-jonatan.schlag@ipfire.org> In-Reply-To: <20240616160245.18865-1-jonatan.schlag@ipfire.org> References: <20240616160245.18865-1-jonatan.schlag@ipfire.org> MIME-Version: 1.0 Message-ID-Hash: KFTFGH5HCCKLK7FC5UDFW2CN6O46S4PC X-Message-ID-Hash: KFTFGH5HCCKLK7FC5UDFW2CN6O46S4PC X-MailFrom: jonatan.schlag@ipfire.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: IPFire development talk Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- tests/lib.sh | 14 +++++++++++++ .../src/initscripts/system/functions/test2.sh | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 tests/src/initscripts/system/functions/test2.sh diff --git a/tests/lib.sh b/tests/lib.sh index dd5e3f535..716922024 100644 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -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 index 000000000..a568ed2a4 --- /dev/null +++ b/tests/src/initscripts/system/functions/test2.sh @@ -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" + +