Fix addKey to handle substrings of existing keys

Fix addKey to handle the case where a key is being added, and that key
is the leading substring of an already existing key within that file.

For example: add "server=192.168.1.1", when "server=192.168.1.178"
already exists within the /etc/dnsmasq.d/01-pihole.conf file.

Check pihole docker with PIHOLE_DNS="192.168.1.178;192.168.1.1". Its
/etc/dnsmasq/01-pihole.conf will be missing its second server= entry.

Add the test_key_addition_substr, to test addKey when
its adding a substring key of an existing key in the file.

Signed-off-by: William Blew <william@kulian.org>
This commit is contained in:
William Blew 2023-03-16 19:36:22 -07:00
parent 75a32d22a3
commit b9a6970bfd
No known key found for this signature in database
GPG Key ID: CCCB3B7C59CC092C
2 changed files with 21 additions and 1 deletions

View File

@ -57,7 +57,7 @@ addKey(){
# touch file to prevent grep error if file does not exist yet
touch "${file}"
if ! grep -q "^${key}" "${file}"; then
if ! grep -q "^${key}$" "${file}"; then
# Key does not exist, add it.
echo "${key}" >> "${file}"
fi

View File

@ -40,6 +40,26 @@ def test_key_addition_works(host):
assert expected_stdout == output.stdout
def test_key_addition_substr(host):
"""Confirms addKey adds substring keys (no value) to a file"""
host.run(
"""
source /opt/pihole/utils.sh
addKey "./testoutput" "KEY_ONE"
addKey "./testoutput" "KEY_O"
addKey "./testoutput" "KEY_TWO"
addKey "./testoutput" "Y_TWO"
"""
)
output = host.run(
"""
cat ./testoutput
"""
)
expected_stdout = "KEY_ONE\nKEY_O\nKEY_TWO\nY_TWO\n"
assert expected_stdout == output.stdout
def test_key_removal_works(host):
"""Confirms removeKey removes a key or key/value pair"""
host.run(