Message ID | 20240520090611.10406-13-jonatan.schlag@ipfire.org |
---|---|
State | New |
Headers |
Return-Path: <development-bounces@lists.ipfire.org> 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 4VjWsm1V5Mz3x0S for <patchwork@web04.haj.ipfire.org>; Mon, 20 May 2024 09:07:08 +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 4VjWsQ6RSbz5nl; Mon, 20 May 2024 09:06:50 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4VjWsD4rplz337d; Mon, 20 May 2024 09:06:40 +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 4VjWs75H4gz32w8 for <development@lists.ipfire.org>; Mon, 20 May 2024 09:06:35 +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 4VjWs72gRYz5mn; Mon, 20 May 2024 09:06:35 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1716195995; 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=vmXyHDMJx/m5wyBkFJgPJsJ5w5WbrErOioTztTxF8AM=; b=YKsO21FHFM6pauhFvFgVIDfiaDQTL+iqDOa+BboC9zXdqyyv28w4pkH4JZxoL4+m5IOktM rb4Busvug+mXxODg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1716195995; 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=vmXyHDMJx/m5wyBkFJgPJsJ5w5WbrErOioTztTxF8AM=; b=sZ09kme8lIwFYb7n4czodnscAA+i+vY01YjQU4YVX4EO3Pray6hEFgGxDyjUXItmAnHWZ/ 5wtW6s4hpW+mHNp4biv5tXbqvtwGAepSk5k5jL+PSZml8HFrgZOekDE6t0tkSS/vXsfTBw ds7Sg5cyOWjKfe447N0Ez7CZx8d2ZNPHQkG+EWVP+VgrejycN3goHAPvMkyrC+TExEHZvD fjQrMpfP0UEiy7X4SKHa758kxtl4TqoQu74Z2bebpEtUBO9LZ2vYPW4J/NtLbY4y+2VeGJ o9/SxYlVnk6WC5k5qgq/59/4a+PrCWUC+flzrNQ8mkcHeTK99g7279RQMdwmyg== From: Jonatan Schlag <jonatan.schlag@ipfire.org> To: development@lists.ipfire.org Subject: [PATCH 12/21] initscript functions: add readhash Date: Mon, 20 May 2024 11:06:02 +0200 Message-Id: <20240520090611.10406-13-jonatan.schlag@ipfire.org> In-Reply-To: <20240520090611.10406-1-jonatan.schlag@ipfire.org> References: <20240520090611.10406-1-jonatan.schlag@ipfire.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: YZH2BSTE7MXTGNPEZLVRM2XHHKRGSIY2 X-Message-ID-Hash: YZH2BSTE7MXTGNPEZLVRM2XHHKRGSIY2 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 <development.lists.ipfire.org> Archived-At: <https://lists.ipfire.org/hyperkitty/list/development@lists.ipfire.org/message/YZH2BSTE7MXTGNPEZLVRM2XHHKRGSIY2/> List-Archive: <https://lists.ipfire.org/hyperkitty/list/development@lists.ipfire.org/> List-Help: <mailto:development-request@lists.ipfire.org?subject=help> List-Owner: <mailto:development-owner@lists.ipfire.org> List-Post: <mailto:development@lists.ipfire.org> List-Subscribe: <mailto:development-join@lists.ipfire.org> List-Unsubscribe: <mailto:development-leave@lists.ipfire.org> |
Series |
[01/21] test: Add bash lib for colors
|
|
Commit Message
Jonatan Schlag
May 20, 2024, 9:06 a.m. UTC
To avoid the usage of eval and to store the config in an key value array, we introduce an new function. The tests only check if we read the correct value to the correct variable. One comment on the implementation as this has created some headache: >From https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins "When used in a function, declare makes each name local, as with the local command, unless the -g option is used." So we need to use -g here Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> --- src/initscripts/system/functions | 15 +++++++++++++++ tests/src/initscripts/system/functions/data/1 | 17 +++++++++++++++++ tests/src/initscripts/system/functions/test.sh | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/src/initscripts/system/functions/data/1 create mode 100755 tests/src/initscripts/system/functions/test.sh
Comments
Hello, > On 20 May 2024, at 10:06, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote: > > To avoid the usage of eval and to store the config in an key value > array, we introduce an new function. The tests only check if we > read the correct value to the correct variable. > > One comment on the implementation as this has created some headache: > >> From https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins > > "When used in a function, declare makes each name local, as with the local command, unless the -g option is used." > > So we need to use -g here > > Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> > --- > src/initscripts/system/functions | 15 +++++++++++++++ > tests/src/initscripts/system/functions/data/1 | 17 +++++++++++++++++ > tests/src/initscripts/system/functions/test.sh | 16 ++++++++++++++++ > 3 files changed, 48 insertions(+) > create mode 100644 tests/src/initscripts/system/functions/data/1 > create mode 100755 tests/src/initscripts/system/functions/test.sh > > diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions > index b610143ab..44ce999d3 100644 > --- a/src/initscripts/system/functions > +++ b/src/initscripts/system/functions > @@ -891,3 +891,18 @@ volume_fs_type() { > > stat -f --format="%T" ${1} > } > + > +readhash() { > + local array="${1}" > + local file="${2}" > + > + declare -A -g "${array}" > + > + local line > + while read -r line; do > + local key="${line%=*}" > + local val="${line#*=}" > + > + printf -v "${array}[${key}]" "%s" "${val}" > + done < "${file}" > +} Okay, so here we are getting to the main bit. I think this is nice and simply. The function creates the array regardless and would empty any content which is what we want. You should however check if the file exists and can be read as the function would now not return any error. It would write some error message, but you cannot programmatically check that. Should we have a matching writehash function? -Michael > diff --git a/tests/src/initscripts/system/functions/data/1 b/tests/src/initscripts/system/functions/data/1 > new file mode 100644 > index 000000000..8aca9422b > --- /dev/null > +++ b/tests/src/initscripts/system/functions/data/1 > @@ -0,0 +1,17 @@ > +CONFIG_TYPE=3 > +GREEN_DEV=green0 > +GREEN_MACADDR=00:c0:08:8a:a0:47 > +GREEN_DRIVER=r8175 > +RED_DEV=red0 > +RED_MACADDR=00:c0:08:8a:a0:56 > +RED_DRIVER=r8283 > +BLUE_DEV='blue0 net0' > +BLUE_MACADDR=bc:30:7d:58:6b:e3 > +BLUE_DRIVER=rt2800 > +RED_DHCP_HOSTNAME=ipfire > +RED_DHCP_FORCE_MTU= > +RED_ADDRESS=0.0.0.0 > +RED_NETMASK=0.0.0.0 > +RED_TYPE=PPPOE > +RED_NETADDRESS=0.0.0.0 > + > diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh > new file mode 100755 > index 000000000..ec502e199 > --- /dev/null > +++ b/tests/src/initscripts/system/functions/test.sh > @@ -0,0 +1,16 @@ > +#!/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" > -- > 2.39.2 >
Hi, Am Freitag, dem 31.05.2024 um 10:53 +0100 schrieb Michael Tremer: > Hello, > > > On 20 May 2024, at 10:06, Jonatan Schlag > > <jonatan.schlag@ipfire.org> wrote: > > > > To avoid the usage of eval and to store the config in an key value > > array, we introduce an new function. The tests only check if we > > read the correct value to the correct variable. > > > > One comment on the implementation as this has created some > > headache: > > > > > From > > > https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins > > > > "When used in a function, declare makes each name local, as with > > the local command, unless the -g option is used." > > > > So we need to use -g here > > > > Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> > > --- > > src/initscripts/system/functions | 15 +++++++++++++++ > > tests/src/initscripts/system/functions/data/1 | 17 > > +++++++++++++++++ > > tests/src/initscripts/system/functions/test.sh | 16 > > ++++++++++++++++ > > 3 files changed, 48 insertions(+) > > create mode 100644 tests/src/initscripts/system/functions/data/1 > > create mode 100755 tests/src/initscripts/system/functions/test.sh > > > > diff --git a/src/initscripts/system/functions > > b/src/initscripts/system/functions > > index b610143ab..44ce999d3 100644 > > --- a/src/initscripts/system/functions > > +++ b/src/initscripts/system/functions > > @@ -891,3 +891,18 @@ volume_fs_type() { > > > > stat -f --format="%T" ${1} > > } > > + > > +readhash() { > > + local array="${1}" > > + local file="${2}" > > + > > + declare -A -g "${array}" > > + > > + local line > > + while read -r line; do > > + local key="${line%=*}" > > + local val="${line#*=}" > > + > > + printf -v "${array}[${key}]" "%s" "${val}" > > + done < "${file}" > > +} > > Okay, so here we are getting to the main bit. > > I think this is nice and simply. The function creates the array > regardless and would empty any content which is what we want. Thank you. > > You should however check if the file exists and can be read as the > function would now not return any error. It would write some error > message, but you cannot programmatically check that. I will add code for that. > > Should we have a matching writehash function? I would add this function when we need it. Less dead code :-). Jonatan > > -Michael > > > diff --git a/tests/src/initscripts/system/functions/data/1 > > b/tests/src/initscripts/system/functions/data/1 > > new file mode 100644 > > index 000000000..8aca9422b > > --- /dev/null > > +++ b/tests/src/initscripts/system/functions/data/1 > > @@ -0,0 +1,17 @@ > > +CONFIG_TYPE=3 > > +GREEN_DEV=green0 > > +GREEN_MACADDR=00:c0:08:8a:a0:47 > > +GREEN_DRIVER=r8175 > > +RED_DEV=red0 > > +RED_MACADDR=00:c0:08:8a:a0:56 > > +RED_DRIVER=r8283 > > +BLUE_DEV='blue0 net0' > > +BLUE_MACADDR=bc:30:7d:58:6b:e3 > > +BLUE_DRIVER=rt2800 > > +RED_DHCP_HOSTNAME=ipfire > > +RED_DHCP_FORCE_MTU= > > +RED_ADDRESS=0.0.0.0 > > +RED_NETMASK=0.0.0.0 > > +RED_TYPE=PPPOE > > +RED_NETADDRESS=0.0.0.0 > > + > > diff --git a/tests/src/initscripts/system/functions/test.sh > > b/tests/src/initscripts/system/functions/test.sh > > new file mode 100755 > > index 000000000..ec502e199 > > --- /dev/null > > +++ b/tests/src/initscripts/system/functions/test.sh > > @@ -0,0 +1,16 @@ > > +#!/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" > > -- > > 2.39.2 > > >
diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions index b610143ab..44ce999d3 100644 --- a/src/initscripts/system/functions +++ b/src/initscripts/system/functions @@ -891,3 +891,18 @@ volume_fs_type() { stat -f --format="%T" ${1} } + +readhash() { + local array="${1}" + local file="${2}" + + declare -A -g "${array}" + + local line + while read -r line; do + local key="${line%=*}" + local val="${line#*=}" + + printf -v "${array}[${key}]" "%s" "${val}" + done < "${file}" +} diff --git a/tests/src/initscripts/system/functions/data/1 b/tests/src/initscripts/system/functions/data/1 new file mode 100644 index 000000000..8aca9422b --- /dev/null +++ b/tests/src/initscripts/system/functions/data/1 @@ -0,0 +1,17 @@ +CONFIG_TYPE=3 +GREEN_DEV=green0 +GREEN_MACADDR=00:c0:08:8a:a0:47 +GREEN_DRIVER=r8175 +RED_DEV=red0 +RED_MACADDR=00:c0:08:8a:a0:56 +RED_DRIVER=r8283 +BLUE_DEV='blue0 net0' +BLUE_MACADDR=bc:30:7d:58:6b:e3 +BLUE_DRIVER=rt2800 +RED_DHCP_HOSTNAME=ipfire +RED_DHCP_FORCE_MTU= +RED_ADDRESS=0.0.0.0 +RED_NETMASK=0.0.0.0 +RED_TYPE=PPPOE +RED_NETADDRESS=0.0.0.0 + diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh new file mode 100755 index 000000000..ec502e199 --- /dev/null +++ b/tests/src/initscripts/system/functions/test.sh @@ -0,0 +1,16 @@ +#!/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"