|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Test for /etc/s6/postfix/run |
| 4 | + |
| 5 | +setup() { |
| 6 | + BATS_TMPDIR=$(mktemp -d -t bats-XXXXXXXX) |
| 7 | + export BATS_TMPDIR |
| 8 | + |
| 9 | + # Backup original files that the script might modify |
| 10 | + [ -f /etc/postfix/main.cf ] && cp /etc/postfix/main.cf "${BATS_TMPDIR}/main.cf.bak" |
| 11 | + [ -f /etc/sasldb2 ] && cp /etc/sasldb2 "${BATS_TMPDIR}/sasldb2.bak" |
| 12 | + cp /usr/lib/postfix/configure-instance.sh "${BATS_TMPDIR}/configure-instance.sh.bak" |
| 13 | + |
| 14 | + # Create a version of the run script without the final 'exec' to prevent blocking |
| 15 | + sed 's/exec \/usr\/sbin\/postfix start-fg//' /etc/s6/postfix/run > "${BATS_TMPDIR}/run" |
| 16 | + chmod +x "${BATS_TMPDIR}/run" |
| 17 | + |
| 18 | + # The script might create these files, so ensure the directory exists |
| 19 | + mkdir -p /etc/postfix/sasl |
| 20 | +} |
| 21 | + |
| 22 | +teardown() { |
| 23 | + # Restore original files |
| 24 | + [ -f "${BATS_TMPDIR}/main.cf.bak" ] && mv "${BATS_TMPDIR}/main.cf.bak" /etc/postfix/main.cf |
| 25 | + [ -f "${BATS_TMPDIR}/sasldb2.bak" ] && mv "${BATS_TMPDIR}/sasldb2.bak" /etc/sasldb2 |
| 26 | + mv "${BATS_TMPDIR}/configure-instance.sh.bak" /usr/lib/postfix/configure-instance.sh |
| 27 | + rm -rf "${BATS_TMPDIR}" |
| 28 | +} |
| 29 | + |
| 30 | +@test "should configure main.cf with custom values from environment variables" { |
| 31 | + # Run the modified script in a subshell with custom environment variables |
| 32 | + ( |
| 33 | + # Set env vars to test against |
| 34 | + export MAILNAME="test.example.com" |
| 35 | + export MYNETWORKS="10.0.0.0/8 127.0.0.0/8" |
| 36 | + export SIZELIMIT="20480000" |
| 37 | + export RELAYHOST="[mail.example.com]:587" |
| 38 | + export POSTFIX_ADD_MISSING_HEADERS="yes" |
| 39 | + export INET_PROTOCOLS="ipv4" |
| 40 | + export DISABLE_VRFY_COMMAND="no" |
| 41 | + export USE_TLS="no" # Disable TLS to avoid slow cert generation |
| 42 | + |
| 43 | + # Execute the setup script |
| 44 | + bash -x "${BATS_TMPDIR}/run" |
| 45 | + ) |
| 46 | + |
| 47 | + # Use postconf to verify the settings in /etc/postfix/main.cf |
| 48 | + run postconf -h myhostname |
| 49 | + [ "$status" -eq 0 ] |
| 50 | + [ "$output" = "test.example.com" ] |
| 51 | + |
| 52 | + run postconf -h mynetworks |
| 53 | + [ "$status" -eq 0 ] |
| 54 | + [ "$output" = "10.0.0.0/8 127.0.0.0/8" ] |
| 55 | + |
| 56 | + run postconf -h message_size_limit |
| 57 | + [ "$status" -eq 0 ] |
| 58 | + [ "$output" = "20480000" ] |
| 59 | + |
| 60 | + run postconf -h relayhost |
| 61 | + [ "$status" -eq 0 ] |
| 62 | + [ "$output" = "[mail.example.com]:587" ] |
| 63 | + |
| 64 | + run postconf -h always_add_missing_headers |
| 65 | + [ "$status" -eq 0 ] |
| 66 | + [ "$output" = "yes" ] |
| 67 | + |
| 68 | + run postconf -h inet_protocols |
| 69 | + [ "$status" -eq 0 ] |
| 70 | + [ "$output" = "ipv4" ] |
| 71 | + |
| 72 | + run postconf -h disable_vrfy_command |
| 73 | + [ "$status" -eq 0 ] |
| 74 | + [ "$output" = "no" ] |
| 75 | +} |
| 76 | + |
| 77 | +@test "should enable DKIM milter when USE_DKIM is 'yes'" { |
| 78 | + ( |
| 79 | + export MAILNAME="test.example.com" |
| 80 | + export USE_DKIM="yes" |
| 81 | + bash -x "${BATS_TMPDIR}/run" |
| 82 | + ) |
| 83 | + |
| 84 | + run postconf -h smtpd_milters |
| 85 | + [ "$status" -eq 0 ] |
| 86 | + [ "$output" = "inet:localhost:8891" ] |
| 87 | + |
| 88 | + run postconf -h non_smtpd_milters |
| 89 | + [ "$status" -eq 0 ] |
| 90 | + [ "$output" = "inet:localhost:8891" ] |
| 91 | +} |
0 commit comments