From 632aead691f747036c8d78a1096d705cf26820e5 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Sat, 14 Jan 2023 23:11:42 +0000 Subject: [PATCH] add wrappers to utils.sh for setting FTL config & add tests Signed-off-by: Adam Warner --- advanced/Scripts/utils.sh | 23 ++++++++++++++++ test/test_any_utils.py | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh index f655e56c..c97ad0cf 100755 --- a/advanced/Scripts/utils.sh +++ b/advanced/Scripts/utils.sh @@ -145,3 +145,26 @@ getFTLPID() { FTL_PID=${FTL_PID:=-1} echo "${FTL_PID}" } + +####################### +# returns value from FTLs config file using pihole-FTL --config +# +# Takes one argument: key +# Example getFTLConfigValue dns.piholePTR +####################### +getFTLConfigValue(){ + pihole-FTL --config "${1}" +} + +####################### +# sets value in FTLs config file using pihole-FTL --config +# +# Takes two arguments: key and value +# Example setFTLConfigValue dns.piholePTR PI.HOLE +# +# Note, for complex values such as dnsmasq.upstreams, you should wrap the value in single quotes: +# setFTLConfigValue dnsmasq.upstreams '[ "8.8.8.8" , "8.8.4.4" ]' +####################### +setFTLConfigValue(){ + pihole-FTL --config "${1}" "${2}" +} diff --git a/test/test_any_utils.py b/test/test_any_utils.py index b3fabe6c..741b1127 100644 --- a/test/test_any_utils.py +++ b/test/test_any_utils.py @@ -170,3 +170,58 @@ def test_getFTLPIDFile_and_getFTLPID_custom(host): ) expected_stdout = "1234\n" assert expected_stdout == output.stdout + + +def test_setFTLConfigValue_getFTLConfigValue(host): + """ + Confirms setFTLConfigValue works + Requires FTL to be installed, so we do that first (taken from test_FTL_binary_installed_and_responsive_no_errors) + """ + host.run( + """ + source /opt/pihole/basic-install.sh + create_pihole_user + funcOutput=$(get_binary_name) + echo "new/http" > /etc/pihole/ftlbranch + binary="pihole-FTL${funcOutput##*pihole-FTL}" + theRest="${funcOutput%pihole-FTL*}" + FTLdetect "${binary}" "${theRest}" + """ + ) + + output = host.run( + """ + source /opt/pihole/utils.sh + setFTLConfigValue "dnsmasq.upstreams" '["1.1.1.1"]' + """ + ) + + assert '[ "1.1.1.1" ]' in output.stdout + + +def test_getFTLConfigValue_getFTLConfigValue(host): + """ + Confirms getFTLConfigValue works (also assumes setFTLConfigValue works) + Requires FTL to be installed, so we do that first (taken from test_FTL_binary_installed_and_responsive_no_errors) + """ + host.run( + """ + source /opt/pihole/basic-install.sh + create_pihole_user + funcOutput=$(get_binary_name) + echo "new/http" > /etc/pihole/ftlbranch + binary="pihole-FTL${funcOutput##*pihole-FTL}" + theRest="${funcOutput%pihole-FTL*}" + FTLdetect "${binary}" "${theRest}" + """ + ) + + output = host.run( + """ + source /opt/pihole/utils.sh + setFTLConfigValue "dnsmasq.upstreams" '["9.9.9.9"]' > /dev/null + getFTLConfigValue "dnsmasq.upstreams" + """ + ) + + assert '[ "9.9.9.9" ]' in output.stdout