From b16f7973174fc34466f4265c27d41e3429b78bb9 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Tue, 24 Jan 2017 15:44:48 -0800 Subject: [PATCH] `firewall-cmd --state` returns 0 on 'running' non 0 on 'not running', so check retval and not text returned. FirewallD conversion to multicall IPTables test --- automated install/basic-install.sh | 2 +- test/test_automated_install.py | 48 +++++++++++++++++------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index fe4d7bfb..18bff00d 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -879,7 +879,7 @@ create_pihole_user() { configureFirewall() { # Allow HTTP and DNS traffic - if [[ $(firewall-cmd --state) == "running" ]]; then + if firewall-cmd --state &> /dev/null; then whiptail --title "Firewall in use" --yesno "We have detected a running firewall\n\nPi-hole currently requires HTTP and DNS port access.\n\n\n\nInstall Pi-hole default firewall rules?" ${r} ${c} || \ { echo -e ":::\n::: Not installing firewall rulesets."; return 1; } echo -e ":::\n:::\n Configuring FirewallD for httpd and dnsmasq." diff --git a/test/test_automated_install.py b/test/test_automated_install.py index 58d98939..14b2add2 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -67,9 +67,9 @@ def test_setupVars_saved_to_file(Pihole): def test_configureFirewall_firewalld_running_no_errors(Pihole): ''' confirms firewalld rules are applied when firewallD is running ''' # firewallD returns 'running' as status - mock_command('firewall-cmd', 'running', '0', Pihole) + mock_command('firewall-cmd', {'*':('running', 0)}, Pihole) # Whiptail dialog returns Ok for user prompt - mock_command('whiptail', '', '0', Pihole) + mock_command('whiptail', {'*':('', 0)}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -84,7 +84,7 @@ def test_configureFirewall_firewalld_running_no_errors(Pihole): def test_configureFirewall_firewalld_disabled_no_errors(Pihole): ''' confirms firewalld rules are not applied when firewallD is not running ''' # firewallD returns non-running status - mock_command('firewall-cmd', 'stopped', '0', Pihole) + mock_command('firewall-cmd', {'*':('not running', '1')}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -95,9 +95,9 @@ def test_configureFirewall_firewalld_disabled_no_errors(Pihole): def test_configureFirewall_firewalld_enabled_declined_no_errors(Pihole): ''' confirms firewalld rules are not applied when firewallD is running, user declines ruleset ''' # firewallD returns running status - mock_command('firewall-cmd', 'running', '0', Pihole) + mock_command('firewall-cmd', {'*':('running', 0)}, Pihole) # Whiptail dialog returns Cancel for user prompt - mock_command('whiptail', '', '1', Pihole) + mock_command('whiptail', {'*':('', 1)}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -117,11 +117,11 @@ def test_configureFirewall_no_firewall(Pihole): def test_configureFirewall_IPTables_enabled_declined_no_errors(Pihole): ''' confirms IPTables rules are not applied when IPTables is running, user declines ruleset ''' # iptables command exists - mock_command('iptables', '', '0', Pihole) + mock_command('iptables', {'*':('', '0')}, Pihole) # modinfo returns always true (ip_tables module check) - mock_command('modinfo', '', '0', Pihole) + mock_command('modinfo', {'*':('', '0')}, Pihole) # Whiptail dialog returns Cancel for user prompt - mock_command('whiptail', '', '1', Pihole) + mock_command('whiptail', {'*':('', '1')}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -132,11 +132,11 @@ def test_configureFirewall_IPTables_enabled_declined_no_errors(Pihole): def test_configureFirewall_IPTables_enabled_rules_exist_no_errors(Pihole): ''' confirms IPTables rules are not applied when IPTables is running and rules exist ''' # iptables command exists and returns 0 on calls (should return 0 on iptables -C) - mock_command('iptables', '', '0', Pihole) + mock_command('iptables', {'-S':('-P INPUT DENY', '0')}, Pihole) # modinfo returns always true (ip_tables module check) - mock_command('modinfo', '', '0', Pihole) + mock_command('modinfo', {'*':('', '0')}, Pihole) # Whiptail dialog returns Cancel for user prompt - mock_command('whiptail', '', '0', Pihole) + mock_command('whiptail', {'*':('', '0')}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -150,12 +150,12 @@ def test_configureFirewall_IPTables_enabled_rules_exist_no_errors(Pihole): def test_configureFirewall_IPTables_enabled_not_exist_no_errors(Pihole): ''' confirms IPTables rules are applied when IPTables is running and rules do not exist ''' - # iptables command and returns 1 on calls (should return 1 on iptables -C) - mock_command('iptables', '', '1', Pihole) + # iptables command and returns 0 on calls (should return 1 on iptables -C) + mock_command('iptables', {'-S':('-P INPUT DENY', '0'), '-C':('', 1), '-I':('', 0)}, Pihole) # modinfo returns always true (ip_tables module check) - mock_command('modinfo', '', '0', Pihole) + mock_command('modinfo', {'*':('', '0')}, Pihole) # Whiptail dialog returns Cancel for user prompt - mock_command('whiptail', '', '0', Pihole) + mock_command('whiptail', {'*':('', '0')}, Pihole) configureFirewall = Pihole.run(''' source /opt/pihole/basic-install.sh configureFirewall @@ -167,20 +167,26 @@ def test_configureFirewall_IPTables_enabled_not_exist_no_errors(Pihole): assert 'iptables -I INPUT 1 -p tcp -m tcp --dport 80 -j ACCEPT' in firewall_calls # Helper functions -def mock_command(script, result, retVal, container): +def mock_command(script, args, 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} - echo {result} - exit {retcode} - '''.format(script=script, result=result,retcode=retVal)) + case "\$1" in'''.format(script=script)) + for k, v in args.iteritems(): + case = dedent(''' + {arg}) + echo {res} + exit {retcode} + ;;'''.format(arg=k, res=v[0], retcode=v[1])) + mock_script += case + mock_script += dedent(''' + esac''') container.run(''' cat < {script}\n{content}\nEOF chmod +x {script} - '''.format(script=full_script_path, content=mock_script)) + rm -f /var/log/{scriptlog}'''.format(script=full_script_path, content=mock_script, scriptlog=script)) def run_script(Pihole, script): result = Pihole.run(script)