|
| 1 | +import pytest |
| 2 | +from textwrap import dedent |
| 3 | + |
| 4 | +SETUPVARS = { |
| 5 | + 'piholeInterface' : 'eth99', |
| 6 | + 'IPv4_address' : '1.1.1.1', |
| 7 | + 'IPv6_address' : '2:2:2:2:2:2', |
| 8 | + 'piholeDNS1' : '4.2.2.1', |
| 9 | + 'piholeDNS2' : '4.2.2.2' |
| 10 | +} |
| 11 | + |
| 12 | +def test_setupVars_are_sourced_to_global_scope(Pihole): |
| 13 | + ''' currently update_dialogs sources setupVars with a dot, |
| 14 | + then various other functions use the variables ''' |
| 15 | + setup_var_file = 'cat <<EOF> /etc/pihole/setupVars.conf\n' |
| 16 | + for k,v in SETUPVARS.iteritems(): |
| 17 | + setup_var_file += "{}={}\n".format(k, v) |
| 18 | + setup_var_file += "EOF\n" |
| 19 | + Pihole.run(setup_var_file) |
| 20 | + |
| 21 | + script = dedent('''\ |
| 22 | + #!/bin/bash -e |
| 23 | + printSetupVars() { |
| 24 | + # Currently debug test function only |
| 25 | + echo "Outputting sourced variables" |
| 26 | + echo "piholeInterface=\${piholeInterface}" |
| 27 | + echo "IPv4_address=\${IPv4_address}" |
| 28 | + echo "IPv6_address=\${IPv6_address}" |
| 29 | + echo "piholeDNS1=\${piholeDNS1}" |
| 30 | + echo "piholeDNS2=\${piholeDNS2}" |
| 31 | + } |
| 32 | + update_dialogs() { |
| 33 | + . /etc/pihole/setupVars.conf |
| 34 | + } |
| 35 | + update_dialogs |
| 36 | + printSetupVars |
| 37 | + ''') |
| 38 | + |
| 39 | + output = run_script(Pihole, script).stdout |
| 40 | + |
| 41 | + for k,v in SETUPVARS.iteritems(): |
| 42 | + assert "{}={}".format(k, v) in output |
| 43 | + |
| 44 | +def test_setupVars_saved_to_file(Pihole): |
| 45 | + ''' confirm saved settings are written to a file for future updates to re-use ''' |
| 46 | + set_setup_vars = '\n' # dedent works better with this and padding matching script below |
| 47 | + for k,v in SETUPVARS.iteritems(): |
| 48 | + set_setup_vars += " {}={}\n".format(k, v) |
| 49 | + Pihole.run(set_setup_vars).stdout |
| 50 | + |
| 51 | + script = dedent('''\ |
| 52 | + #!/bin/bash -e |
| 53 | + echo start |
| 54 | + TERM=xterm |
| 55 | + PHTEST=TRUE |
| 56 | + source /opt/pihole/basic-install.sh |
| 57 | + {} |
| 58 | + finalExports |
| 59 | + cat /etc/pihole/setupVars.conf |
| 60 | + '''.format(set_setup_vars)) |
| 61 | + |
| 62 | + output = run_script(Pihole, script).stdout |
| 63 | + |
| 64 | + for k,v in SETUPVARS.iteritems(): |
| 65 | + assert "{}={}".format(k, v) in output |
| 66 | + |
| 67 | +def run_script(Pihole, script, file="/test.sh"): |
| 68 | + _write_test_script(Pihole, script, file=file) |
| 69 | + result = Pihole.run(file) |
| 70 | + assert result.rc == 0 |
| 71 | + return result |
| 72 | + |
| 73 | +def _write_test_script(Pihole, script, file): |
| 74 | + ''' Running the test script blocks directly can behave differently with regard to global vars ''' |
| 75 | + ''' this is a cheap work around to that until all functions no longer rely on global variables ''' |
| 76 | + Pihole.run('cat <<EOF> {file}\n{script}\nEOF'.format(file=file, script=script)) |
| 77 | + Pihole.run('chmod +x {}'.format(file)) |
0 commit comments