From 05e114173db93f9395866995c004138d1a9e56db Mon Sep 17 00:00:00 2001 From: diginc Date: Wed, 2 Nov 2016 23:25:13 -0500 Subject: [PATCH] update comments, add configureFirewall test * Comments to clarify some of the existing tests * mock_command to allow recording of calls and mocking return calls in bash * new configureFirewall test (only the first one of it's many paths) --- test/test_automated_install.py | 41 ++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/test/test_automated_install.py b/test/test_automated_install.py index 458536eb..b720b94d 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -10,8 +10,9 @@ SETUPVARS = { } def test_setupVars_are_sourced_to_global_scope(Pihole): - ''' currently update_dialogs sources setupVars with a dot, - then various other functions use the variables ''' + ''' 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 ''' setup_var_file = 'cat < /etc/pihole/setupVars.conf\n' for k,v in SETUPVARS.iteritems(): setup_var_file += "{}={}\n".format(k, v) @@ -64,6 +65,42 @@ def test_setupVars_saved_to_file(Pihole): for k,v in SETUPVARS.iteritems(): assert "{}={}".format(k, v) in output +def test_configureFirewall_firewalld_no_errors(Pihole): + ''' confirms firewalld rules are applied when appopriate ''' + mock_command('firewall-cmd', '0', Pihole) + configureFirewall = Pihole.run(''' + bash -c " + PHTEST=TRUE + source /opt/pihole/basic-install.sh + configureFirewall + " ''') + expected_stdout = '::: Configuring firewalld for httpd and dnsmasq.' + assert expected_stdout in configureFirewall.stdout + firewall_calls = Pihole.run('cat /var/log/firewall-cmd').stdout + assert 'firewall-cmd --state' in firewall_calls + assert 'firewall-cmd --permanent --add-port=80/tcp' in firewall_calls + assert 'firewall-cmd --permanent --add-port=53/tcp' in firewall_calls + assert 'firewall-cmd --permanent --add-port=53/udp' in firewall_calls + assert 'firewall-cmd --reload' in firewall_calls + + +# Helper functions +def mock_command(script, result, container): + ''' 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} + exit {retcode} + '''.format(script=script, retcode=result)) + container.run(''' + cat < {script}\n{content}\nEOF + chmod +x {script} + '''.format(script=full_script_path, content=mock_script)) + print container.run('cat {}'.format(full_script_path)).stdout + + def run_script(Pihole, script, file="/test.sh"): _write_test_script(Pihole, script, file=file) result = Pihole.run(file)