2016-10-11 04:14:39 +00:00
|
|
|
import pytest
|
|
|
|
from textwrap import dedent
|
|
|
|
|
|
|
|
SETUPVARS = {
|
2016-11-02 15:22:45 +00:00
|
|
|
'PIHOLE_INTERFACE' : 'eth99',
|
|
|
|
'IPV4_ADDRESS' : '1.1.1.1',
|
|
|
|
'IPV6_ADDRESS' : 'FE80::240:D0FF:FE48:4672',
|
2016-11-02 15:52:23 +00:00
|
|
|
'PIHOLE_DNS_1' : '4.2.2.1',
|
|
|
|
'PIHOLE_DNS_2' : '4.2.2.2'
|
2016-10-11 04:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_setupVars_are_sourced_to_global_scope(Pihole):
|
2016-11-03 04:25:13 +00:00
|
|
|
''' currently update_dialogs sources setupVars with a dot,
|
|
|
|
then various other functions use the variables.
|
|
|
|
This confirms the sourced variables are in scope between functions '''
|
2016-10-11 04:14:39 +00:00
|
|
|
setup_var_file = 'cat <<EOF> /etc/pihole/setupVars.conf\n'
|
|
|
|
for k,v in SETUPVARS.iteritems():
|
|
|
|
setup_var_file += "{}={}\n".format(k, v)
|
|
|
|
setup_var_file += "EOF\n"
|
|
|
|
Pihole.run(setup_var_file)
|
|
|
|
|
|
|
|
script = dedent('''\
|
2016-11-03 05:02:28 +00:00
|
|
|
set -e
|
2016-10-11 04:14:39 +00:00
|
|
|
printSetupVars() {
|
|
|
|
# Currently debug test function only
|
|
|
|
echo "Outputting sourced variables"
|
2016-11-03 05:02:28 +00:00
|
|
|
echo "PIHOLE_INTERFACE=${PIHOLE_INTERFACE}"
|
|
|
|
echo "IPV4_ADDRESS=${IPV4_ADDRESS}"
|
|
|
|
echo "IPV6_ADDRESS=${IPV6_ADDRESS}"
|
|
|
|
echo "PIHOLE_DNS_1=${PIHOLE_DNS_1}"
|
|
|
|
echo "PIHOLE_DNS_2=${PIHOLE_DNS_2}"
|
2016-10-11 04:14:39 +00:00
|
|
|
}
|
|
|
|
update_dialogs() {
|
|
|
|
. /etc/pihole/setupVars.conf
|
|
|
|
}
|
|
|
|
update_dialogs
|
|
|
|
printSetupVars
|
|
|
|
''')
|
|
|
|
|
|
|
|
output = run_script(Pihole, script).stdout
|
|
|
|
|
|
|
|
for k,v in SETUPVARS.iteritems():
|
|
|
|
assert "{}={}".format(k, v) in output
|
|
|
|
|
|
|
|
def test_setupVars_saved_to_file(Pihole):
|
|
|
|
''' confirm saved settings are written to a file for future updates to re-use '''
|
|
|
|
set_setup_vars = '\n' # dedent works better with this and padding matching script below
|
|
|
|
for k,v in SETUPVARS.iteritems():
|
|
|
|
set_setup_vars += " {}={}\n".format(k, v)
|
|
|
|
Pihole.run(set_setup_vars).stdout
|
|
|
|
|
|
|
|
script = dedent('''\
|
2016-11-03 05:02:28 +00:00
|
|
|
set -e
|
2016-10-11 04:14:39 +00:00
|
|
|
echo start
|
|
|
|
TERM=xterm
|
|
|
|
source /opt/pihole/basic-install.sh
|
|
|
|
{}
|
|
|
|
finalExports
|
|
|
|
cat /etc/pihole/setupVars.conf
|
|
|
|
'''.format(set_setup_vars))
|
|
|
|
|
|
|
|
output = run_script(Pihole, script).stdout
|
|
|
|
|
|
|
|
for k,v in SETUPVARS.iteritems():
|
|
|
|
assert "{}={}".format(k, v) in output
|
|
|
|
|
2017-01-24 02:21:50 +00:00
|
|
|
def test_configureFirewall_firewalld_no_errors(Pihole):
|
2017-01-24 02:12:26 +00:00
|
|
|
''' confirms firewalld rules are applied when appropriate '''
|
|
|
|
mock_command('firewall-cmd', 'running', '0', Pihole)
|
2017-01-24 02:47:52 +00:00
|
|
|
mock_command('whiptail', '', '0', Pihole)
|
2016-11-03 04:25:13 +00:00
|
|
|
configureFirewall = Pihole.run('''
|
|
|
|
source /opt/pihole/basic-install.sh
|
|
|
|
configureFirewall
|
2016-11-03 04:58:54 +00:00
|
|
|
''')
|
2017-01-24 02:52:05 +00:00
|
|
|
expected_stdout = 'Configuring FirewallD for httpd and dnsmasq.'
|
2016-11-03 04:25:13 +00:00
|
|
|
assert expected_stdout in configureFirewall.stdout
|
|
|
|
firewall_calls = Pihole.run('cat /var/log/firewall-cmd').stdout
|
|
|
|
assert 'firewall-cmd --state' in firewall_calls
|
2016-12-30 00:11:13 +00:00
|
|
|
assert 'firewall-cmd --permanent --add-port=80/tcp --add-port=53/tcp --add-port=53/udp' in firewall_calls
|
2016-11-03 04:25:13 +00:00
|
|
|
assert 'firewall-cmd --reload' in firewall_calls
|
|
|
|
|
|
|
|
|
|
|
|
# Helper functions
|
2017-01-24 02:12:26 +00:00
|
|
|
def mock_command(script, result, retVal, container):
|
2016-11-03 04:25:13 +00:00
|
|
|
''' Allows for setup of commands we don't really want to have to run for real in unit tests '''
|
|
|
|
''' TODO: support array of results that enable the results to change over multiple executions of a command '''
|
|
|
|
full_script_path = '/usr/local/bin/{}'.format(script)
|
|
|
|
mock_script = dedent('''\
|
|
|
|
#!/bin/bash -e
|
|
|
|
echo "\$0 \$@" >> /var/log/{script}
|
2017-01-24 02:21:50 +00:00
|
|
|
echo {result}
|
2016-11-03 04:25:13 +00:00
|
|
|
exit {retcode}
|
2017-01-24 02:21:50 +00:00
|
|
|
'''.format(script=script, result=result,retcode=retVal))
|
|
|
|
container.run('''
|
|
|
|
cat <<EOF> {script}\n{content}\nEOF
|
|
|
|
chmod +x {script}
|
|
|
|
'''.format(script=full_script_path, content=mock_script))
|
2016-11-03 04:25:13 +00:00
|
|
|
|
2016-11-03 05:02:28 +00:00
|
|
|
def run_script(Pihole, script):
|
|
|
|
result = Pihole.run(script)
|
2016-10-11 04:14:39 +00:00
|
|
|
assert result.rc == 0
|
|
|
|
return result
|