diff --git a/hack/run-kubernetes-block-io_test.bats b/hack/run-kubernetes-block-io_test.bats index b083f9e4f..18b12d253 100644 --- a/hack/run-kubernetes-block-io_test.bats +++ b/hack/run-kubernetes-block-io_test.bats @@ -151,36 +151,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded into an `if`, both land in the passing - # branch and the assertion "this is absent" is satisfied by the matcher giving - # up. Every negative claim in this file rests on the three-way split. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command lands on the very stderr being diff --git a/hack/run-kubernetes-cpu-throttle_test.bats b/hack/run-kubernetes-cpu-throttle_test.bats index d254c7d49..c4ed0dfa4 100644 --- a/hack/run-kubernetes-cpu-throttle_test.bats +++ b/hack/run-kubernetes-cpu-throttle_test.bats @@ -94,45 +94,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". awk exits 2 on an unreadable path, - # which folded into an if reads the same way, so the file is checked first. - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status, not on truthiness. awk exits 1 for "no line - # matched" and 2 for "I could not evaluate this" -- an unparseable regex, most - # of all. Folded into `if ... then fail; fi`, both land in the passing branch, - # so the assertion "this pattern is absent" is satisfied by the matcher giving - # up. That is the wrong direction for a negative assertion: a positive one - # fails loudly when its matcher breaks and gets fixed, this one goes green and - # stays green. Every claim in this file about something NOT appearing rests on - # the three-way split below. - # `|| _rc=$?` rather than a bare call: both runners set -e, and awk exiting 1 - # for "no match" -- the ordinary success case here -- would otherwise abort the - # test before the branch below ever runs. The `if` form this replaces was - # exempt only because conditions are exempt. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # COZY_REPORT_DIR is set inline in each test below rather than through a helper, # which is the opposite of what the node-join suite does. The difference is # deliberate and shrinks to one line: there the assignment carries a ten-line diff --git a/hack/run-kubernetes-kvm-exits_test.bats b/hack/run-kubernetes-kvm-exits_test.bats index 0473e642f..47ff07d9e 100644 --- a/hack/run-kubernetes-kvm-exits_test.bats +++ b/hack/run-kubernetes-kvm-exits_test.bats @@ -81,36 +81,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded together, a negative assertion is - # satisfied by its own matcher giving up, which is the direction that goes - # green and stays green. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command lands on the very stderr being diff --git a/hack/run-kubernetes-network-counters_test.bats b/hack/run-kubernetes-network-counters_test.bats index 73da4c2b2..42f986821 100644 --- a/hack/run-kubernetes-network-counters_test.bats +++ b/hack/run-kubernetes-network-counters_test.bats @@ -148,36 +148,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded into an `if`, both land in the passing - # branch and the assertion "this is absent" is satisfied by the matcher giving - # up. Every negative claim in this file rests on the three-way split. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command lands on the very stderr being diff --git a/hack/run-kubernetes-node-join_test.bats b/hack/run-kubernetes-node-join_test.bats index 42dbe2f11..27bf9bdb9 100644 --- a/hack/run-kubernetes-node-join_test.bats +++ b/hack/run-kubernetes-node-join_test.bats @@ -300,36 +300,6 @@ assert_file_contains() { return 1 } -# The sibling suites' three-way split, and here for the same reason. A bare -# `! grep -q` succeeds on a file that does not exist, which is -# indistinguishable from the file existing without the pattern; and awk exits 1 -# for "no line matched" and 2 for "I could not evaluate this", so folding those -# together lets a broken matcher satisfy a negative assertion. A positive -# assertion fails loudly when its matcher breaks and gets fixed; this one would -# go green and stay green. -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - local _rc=0 - - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The ceilings both budget guards below price their sums from, read out of the # helper rather than restated in each. They are the same numbers held against # two different bounds -- the phase budget from below, the operation window from diff --git a/hack/run-kubernetes-runner-canary_test.bats b/hack/run-kubernetes-runner-canary_test.bats index 7e2f85f90..65c0626f8 100644 --- a/hack/run-kubernetes-runner-canary_test.bats +++ b/hack/run-kubernetes-runner-canary_test.bats @@ -67,37 +67,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded together, a negative assertion is - # satisfied by its own matcher giving up, which is the direction that goes - # green and stays green. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - cat "${file}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The clock the arms are timed by, replaced by a scripted sequence. # # Real durations are the one thing this suite must not depend on: they are set diff --git a/hack/run-kubernetes-runner-cpu_test.bats b/hack/run-kubernetes-runner-cpu_test.bats index 4eca0a3fc..eee4da69a 100644 --- a/hack/run-kubernetes-runner-cpu_test.bats +++ b/hack/run-kubernetes-runner-cpu_test.bats @@ -58,37 +58,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded together, a negative assertion is - # satisfied by its own matcher giving up, which is the direction that goes - # green and stays green. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - cat "${file}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The functions under test redirect a command's stderr into a report file and # decide which artifact to write from it. cozytest.sh runs under `set -x`, so the # tracer's own line for that command lands on the very stderr being redirected, diff --git a/hack/run-kubernetes-sandbox-cpu-time_test.bats b/hack/run-kubernetes-sandbox-cpu-time_test.bats index 0cd1bace4..c6f3028ca 100644 --- a/hack/run-kubernetes-sandbox-cpu-time_test.bats +++ b/hack/run-kubernetes-sandbox-cpu-time_test.bats @@ -83,36 +83,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded together, a negative assertion is - # satisfied by its own matcher giving up, which is the direction that goes - # green and stays green. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command lands on the very stderr being diff --git a/hack/run-kubernetes-serial-console_test.bats b/hack/run-kubernetes-serial-console_test.bats index 3dd91773d..4c77bd269 100644 --- a/hack/run-kubernetes-serial-console_test.bats +++ b/hack/run-kubernetes-serial-console_test.bats @@ -129,23 +129,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: awk exits 2 on an - # unreadable path, which is indistinguishable from "no line matched" once the - # status is folded into an if. - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - if awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}"; then - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command is emitted on the very stderr being diff --git a/hack/run-kubernetes-talos-diagnostics_test.bats b/hack/run-kubernetes-talos-diagnostics_test.bats index c5411837f..5ce8eef66 100644 --- a/hack/run-kubernetes-talos-diagnostics_test.bats +++ b/hack/run-kubernetes-talos-diagnostics_test.bats @@ -114,16 +114,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - if awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}"; then - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi -} - @test "worker VMI rows retain names whose default interface has no IP" { . hack/e2e-chainsaw/_lib/run-kubernetes.sh rows=$(printf '%s\n' '{"items":[{"metadata":{"name":"worker-a"},"status":{"interfaces":[{"name":"default","ipAddress":"10.244.1.92"}]}},{"metadata":{"name":"worker-b"},"status":{"interfaces":[{"name":"other","ipAddress":"192.0.2.1"}]}}]}' | cozy_tenant_worker_vmi_rows) diff --git a/hack/run-kubernetes-thread-cpu_test.bats b/hack/run-kubernetes-thread-cpu_test.bats index 4d9f7c90b..b37c749e5 100644 --- a/hack/run-kubernetes-thread-cpu_test.bats +++ b/hack/run-kubernetes-thread-cpu_test.bats @@ -85,37 +85,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the label". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded into an `if`, both land in the passing - # branch and the assertion "this pattern is absent" is satisfied by the - # matcher giving up. Every claim in this file about something NOT appearing - # rests on the three-way split below. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The function under test redirects a command's stderr into a report file and # decides which artifact to write from it. cozytest.sh runs under `set -x`, so # the tracer's own line for that command lands on the very stderr being diff --git a/hack/runner-identity.bats b/hack/runner-identity.bats index acfa36e32..ea42444f5 100644 --- a/hack/runner-identity.bats +++ b/hack/runner-identity.bats @@ -83,37 +83,6 @@ assert_file_contains() { return 1 } -assert_file_lacks_pattern() { - local pattern="$1" - local file="$2" - - # A missing file must fail rather than vacuously pass: a bare `! grep -q` - # succeeds on an unreadable path, which is indistinguishable from "the file - # exists and does not carry the pattern". - if [ ! -f "${file}" ]; then - printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 - return 1 - fi - # Branched on awk's exact status. awk exits 1 for "no line matched" and 2 for - # "I could not evaluate this"; folded together, a negative assertion is - # satisfied by its own matcher giving up, which is the direction that goes - # green and stays green. - local _rc=0 - awk -v pattern="${pattern}" '$0 ~ pattern { found = 1 } END { exit found ? 0 : 1 }' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - cat "${file}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac -} - # The shape sizing object, as the `shapeConfig` subpath serves it: a flat object of # sizing numbers with one string in it, pretty-printed because the service is free # to answer either way and the reader buffers its whole input for that reason. diff --git a/hack/test-helper_test.bats b/hack/test-helper_test.bats new file mode 100644 index 000000000..902ed8af9 --- /dev/null +++ b/hack/test-helper_test.bats @@ -0,0 +1,102 @@ +#!/usr/bin/env bats + +load test_helper + +# A separate shell keeps errexit active inside the assertion, unlike calling +# the function directly through Bats' run helper. +run_lacks_pattern() { + run bash -eu -c ' + . "$3" + assert_file_lacks_pattern "$1" "$2" + printf "checked\n" + ' _ "$@" "$BATS_TEST_DIRNAME/test_helper.bash" +} + +@test "a valid non-matching pattern passes under errexit" { + printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact" + + run_lacks_pattern 'secret:|secretName:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 0 ] + [ "$output" = checked ] +} + +@test "an empty readable file passes for a valid pattern" { + : >"$BATS_TEST_TMPDIR/artifact" + + run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 0 ] + [ "$output" = checked ] +} + +@test "a matching pattern fails the assertion" { + printf 'safe content\nsecretName: credential\n' >"$BATS_TEST_TMPDIR/artifact" + + run_lacks_pattern 'secret:|secretName:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'not to match: secret:|secretName:'* ]] +} + +@test "an invalid pattern fails on a populated file" { + printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact" + + run_lacks_pattern '[' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'could not evaluate pattern ['* ]] +} + +@test "an invalid pattern fails even on an empty file" { + : >"$BATS_TEST_TMPDIR/artifact" + + run_lacks_pattern '[' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'could not evaluate pattern ['* ]] +} + +@test "a missing artifact fails rather than passing as no match" { + run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/missing" + + [ "$status" -eq 1 ] + [[ "$output" == *'to exist so it could be checked'* ]] +} + +@test "a directory is not an artifact file" { + run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR" + + [ "$status" -eq 1 ] + [[ "$output" == *'to exist so it could be checked'* ]] +} + +@test "an unreadable artifact fails rather than passing as no match" { + printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact" + chmod 000 "$BATS_TEST_TMPDIR/artifact" + if [ -r "$BATS_TEST_TMPDIR/artifact" ]; then + skip 'this user can read files without read permission' + fi + + run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'could not evaluate pattern secret:'* ]] +} + +@test "a matcher error with status one cannot pass as no match" { + printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact" + mkdir "$BATS_TEST_TMPDIR/bin" + cat >"$BATS_TEST_TMPDIR/bin/awk" <<'SH' +#!/bin/sh +printf '0\n' +printf 'awk: evaluation failed\n' >&2 +exit 1 +SH + chmod +x "$BATS_TEST_TMPDIR/bin/awk" + + PATH="$BATS_TEST_TMPDIR/bin:$PATH" run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'could not evaluate pattern secret:'* ]] +} diff --git a/hack/test_helper.bash b/hack/test_helper.bash index f3944c9c0..bbbef8de8 100644 --- a/hack/test_helper.bash +++ b/hack/test_helper.bash @@ -9,3 +9,31 @@ strict_setup() { setup() { strict_setup } + +# Only a successful matcher explicitly reporting no match passes; missing, +# non-regular or unreadable files and matcher errors fail closed. +assert_file_lacks_pattern() { + local pattern="$1" + local file="$2" + local found + + if [ ! -f "${file}" ]; then + printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 + return 1 + fi + # Validate even an empty file, and keep the match result separate from awk's + # status: some implementations also use status 1 for evaluation errors. + if ! found=$(awk -v pattern="${pattern}" ' + BEGIN { match("", pattern) } + $0 ~ pattern { found = 1 } + END { print found ? 1 : 0 } + ' "${file}"); then + printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 + return 1 + fi + if [ "${found}" != 0 ]; then + printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 + return 1 + fi + return 0 +}