fix(tests): reject matcher failures in negative file assertions

Signed-off-by: Ahmed Kamal <email.ahmedkamal@googlemail.com>
Assisted-by: LLM
(cherry picked from commit f12887636c)
Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ahmed Kamal authored and Myasnikov Daniil committed 2026-11-25 09:58:10 +00:00
1 parent 50bc327df8
commit 84c5adef48
14 files changed
+114 -340

No files matched your search

-30
View File
@@ -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
@@ -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
-30
View File
@@ -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
@@ -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
-30
View File
@@ -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
@@ -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
-31
View File
@@ -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,
@@ -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
@@ -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
@@ -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)
-31
View File
@@ -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
-31
View File
@@ -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.
+85
View File
@@ -0,0 +1,85 @@
#!/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 '
. hack/test_helper.bash
assert_file_lacks_pattern "$1" "$2"
printf "checked\n"
' _ "$@"
}
@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:'* ]]
}
+29
View File
@@ -9,3 +9,32 @@ strict_setup() {
setup() {
strict_setup
}
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
# Validate the regex even for an empty file. Only status 1 means no match;
# keeping awk in an OR-list lets that normal result survive errexit.
awk -v pattern="${pattern}" '
BEGIN { match("", 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
}