Create mock_command_passthrough that allows intercepting of specific arguments - everything else is passed through to the proper command. Use this new command instead of making changes in basic-install.sh to make the tests pass.

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
Adam Warner 2021-11-11 16:10:53 +00:00
parent ad7d02a7d3
commit fdc2c624bd
No known key found for this signature in database
GPG key ID: 872950F3ECF2B173
3 changed files with 41 additions and 4 deletions

View file

@ -514,10 +514,7 @@ update_repo() {
git stash --all --quiet &> /dev/null || true # Okay for stash failure git stash --all --quiet &> /dev/null || true # Okay for stash failure
git clean --quiet --force -d || true # Okay for already clean directory git clean --quiet --force -d || true # Okay for already clean directory
# Pull the latest commits # Pull the latest commits
if ! git branch | grep --quiet -E '^[*] [(](HEAD detached at|detached from) pull/[[:digit:]]+/merge[)]$'; then git pull --no-rebase --quiet &> /dev/null || return $?
# if tests are run with Github Actions, pulling changes will not be possible because of 'detached HEAD' state
git pull --no-rebase --quiet &> /dev/null || return $?
fi
# Check current branch. If it is master, then reset to the latest available tag. # Check current branch. If it is master, then reset to the latest available tag.
# In case extra commits have been added after tagging/release (i.e in case of metadata updates/README.MD tweaks) # In case extra commits have been added after tagging/release (i.e in case of metadata updates/README.MD tweaks)
curBranch=$(git rev-parse --abbrev-ref HEAD) curBranch=$(git rev-parse --abbrev-ref HEAD)

View file

@ -121,6 +121,41 @@ def mock_command(script, args, container):
scriptlog=script)) scriptlog=script))
def mock_command_passthrough(script, args, container):
'''
Per other mock_command* functions, allows intercepting of commands we don't want to run for real
in unit tests, however also allows only specific arguments to be mocked. Anything not defined will
be passed through to the actual command.
Example use-case: mocking `git pull` but still allowing `git clone` to work as intended
'''
orig_script_path = check_output('which {}'.format(script))
full_script_path = '/usr/local/bin/{}'.format(script)
mock_script = dedent(r'''\
#!/bin/bash -e
echo "\$0 \$@" >> /var/log/{script}
case "\$1" in'''.format(script=script))
for k, v in args.items():
case = dedent('''
{arg})
echo {res}
exit {retcode}
;;'''.format(arg=k, res=v[0], retcode=v[1]))
mock_script += case
mock_script += dedent(r'''
*)
{orig_script_path} "\$@"
;;'''.format(orig_script_path=orig_script_path))
mock_script += dedent('''
esac''')
container.run('''
cat <<EOF> {script}\n{content}\nEOF
chmod +x {script}
rm -f /var/log/{scriptlog}'''.format(script=full_script_path,
content=mock_script,
scriptlog=script))
def mock_command_run(script, args, container): def mock_command_run(script, args, container):
''' '''
Allows for setup of commands we don't really want to have to run for real Allows for setup of commands we don't really want to have to run for real

View file

@ -9,6 +9,7 @@ from .conftest import (
mock_command, mock_command,
mock_command_run, mock_command_run,
mock_command_2, mock_command_2,
mock_command_passthrough,
run_script run_script
) )
@ -153,6 +154,8 @@ def test_installPihole_fresh_install_readableFiles(Pihole):
''' '''
# Whiptail dialog returns Cancel for user prompt # Whiptail dialog returns Cancel for user prompt
mock_command('whiptail', {'*': ('', '0')}, Pihole) mock_command('whiptail', {'*': ('', '0')}, Pihole)
# mock git pull
mock_command_passthrough('git', {'pull': ('', '0')}, Pihole)
# mock systemctl to not start lighttpd and FTL # mock systemctl to not start lighttpd and FTL
mock_command_2( mock_command_2(
'systemctl', 'systemctl',
@ -396,6 +399,8 @@ def test_installPihole_fresh_install_readableBlockpage(Pihole, test_webpage):
] ]
# Whiptail dialog returns Cancel for user prompt # Whiptail dialog returns Cancel for user prompt
mock_command('whiptail', {'*': ('', '0')}, Pihole) mock_command('whiptail', {'*': ('', '0')}, Pihole)
# mock git pull
mock_command_passthrough('git', {'pull': ('', '0')}, Pihole)
# mock systemctl to start lighttpd and FTL # mock systemctl to start lighttpd and FTL
ligthttpdcommand = dedent(r'''\"\" ligthttpdcommand = dedent(r'''\"\"
echo 'starting lighttpd with {}' echo 'starting lighttpd with {}'