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 [thread overview]
Message-ID: <20240520090611.10406-13-jonatan.schlag@ipfire.org> (raw)
In-Reply-To: <20240520090611.10406-1-jonatan.schlag@ipfire.org>
[-- Attachment #1: Type: text/plain, Size: 2817 bytes --]
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(a)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}"
+}
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
next prev parent reply other threads:[~2024-05-20 9:06 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-20 9:05 First patch series for the roadmap item "Get rid of CONFIG_TYPE in the network config" Jonatan Schlag
2024-05-20 9:05 ` [PATCH 01/21] test: Add bash lib for colors Jonatan Schlag
2024-05-20 9:05 ` [PATCH 02/21] tests: Add bash lib Jonatan Schlag
2024-05-20 9:05 ` [PATCH 03/21] tests/lib.sh: Add function test_that_key_in_arry_has_value Jonatan Schlag
2024-05-20 9:05 ` [PATCH 04/21] tests/lib.sh: Add check if variable exists to test_that_key_in_arry_has_value Jonatan Schlag
2024-05-20 9:05 ` [PATCH 05/21] tests/lib.sh: Add logging functions Jonatan Schlag
2024-05-20 9:05 ` [PATCH 06/21] tests/lib.sh: adjust to pytest logging style Jonatan Schlag
2024-05-20 9:05 ` [PATCH 07/21] tests/lib.sh: Fix check if array is defined Jonatan Schlag
2024-05-20 9:05 ` [PATCH 08/21] test_that_key_in_arry_has_value: Check if the key " Jonatan Schlag
2024-05-20 9:05 ` [PATCH 09/21] tests/lib.sh: Use namref to access an array Jonatan Schlag
2024-05-20 9:06 ` [PATCH 10/21] test_that_key_in_arry_has_value: Check if a key in an array exists Jonatan Schlag
2024-05-20 9:06 ` [PATCH 11/21] tests: Add function to test the ouput of a bash function Jonatan Schlag
2024-05-20 9:06 ` Jonatan Schlag [this message]
2024-05-20 9:06 ` [PATCH 13/21] initscript fkt: ignore blank lines in readhash Jonatan Schlag
2024-05-20 9:06 ` [PATCH 14/21] test: Add functions test_that_array_is_defined Jonatan Schlag
2024-05-20 9:06 ` [PATCH 15/21] tests: Add functions test_that_array_doesnt_have_key Jonatan Schlag
2024-05-20 9:06 ` [PATCH 16/21] initscripts fkt: Ignore comments in readhash Jonatan Schlag
2024-05-20 9:06 ` [PATCH 17/21] initscripts fkt: ignore invalid keys " Jonatan Schlag
2024-05-20 9:06 ` [PATCH 18/21] initscripts fkt: readhash should only parse lines with a = Jonatan Schlag
2024-05-20 9:06 ` [PATCH 19/21] initscripts fkt: Check for invalid values in readhash Jonatan Schlag
2024-05-20 9:06 ` [PATCH 20/21] initscripts fkt: keep readhash compatible with older implementation Jonatan Schlag
2024-05-20 9:06 ` [PATCH 21/21] initscripts fkt: Fix shebang Jonatan Schlag
[not found] <FF86902E-6928-4E2F-B05C-0A94188FDF06@ipfire.org>
2024-06-02 18:05 ` [PATCH 12/21] initscript functions: add readhash Jonatan Schlag
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240520090611.10406-13-jonatan.schlag@ipfire.org \
--to=jonatan.schlag@ipfire.org \
--cc=development@lists.ipfire.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox