|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for hack/check-kubeovn-image-copies.sh. |
| 4 | +# |
| 5 | +# packages/system/kubeovn/images/kubeovn/Dockerfile is a hand-kept copy of |
| 6 | +# upstream's dist/images/Dockerfile, and `make update` rewrites only ARG VERSION |
| 7 | +# and ARG TAG in it. When v1.15.26 added kube-ovn-bfdd-supervisor to the image, |
| 8 | +# our copy kept the v1.15.10 set and the binary went missing, which nothing in |
| 9 | +# the tree noticed. The checker compares what each file copies, and this suite |
| 10 | +# runs it over the real Dockerfile against a verbatim copy of upstream's for the |
| 11 | +# vendored appVersion -- a synthetic pair alone would only assert the shape we |
| 12 | +# already imagined. |
| 13 | +# |
| 14 | +# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its |
| 15 | +# own line; there is no bats `run` or `$status`. Assertions are direct shell |
| 16 | +# tests, and an expected failure is written as `if CHECK ...; then ... exit 1`. |
| 17 | +# ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 20 | +CHECK="$REPO_ROOT/hack/check-kubeovn-image-copies.sh" |
| 21 | +OURS="$REPO_ROOT/packages/system/kubeovn/images/kubeovn/Dockerfile" |
| 22 | +CHART="$REPO_ROOT/packages/system/kubeovn/charts/kube-ovn/Chart.yaml" |
| 23 | + |
| 24 | +# Upstream dist/images/Dockerfile at v1.15.26, copied verbatim. Bump it in the |
| 25 | +# same change that bumps the chart: `make update` re-runs the same comparison |
| 26 | +# against the tag it pulls, so the two cannot disagree for long. |
| 27 | +UPSTREAM_VERSION=1.15.26 |
| 28 | +upstream_dockerfile() { |
| 29 | + cat <<'EOF' |
| 30 | +# syntax=docker/dockerfile:1 |
| 31 | +ARG VERSION |
| 32 | +ARG BASE_TAG=$VERSION |
| 33 | +FROM kubeovn/kube-ovn-base:$BASE_TAG AS setcap |
| 34 | +
|
| 35 | +COPY *.sh /kube-ovn/ |
| 36 | +COPY kubectl-ko /kube-ovn/kubectl-ko |
| 37 | +COPY 01-kube-ovn.conflist /kube-ovn/01-kube-ovn.conflist |
| 38 | +
|
| 39 | +COPY kube-ovn /kube-ovn/kube-ovn |
| 40 | +COPY kube-ovn-cmd /kube-ovn/kube-ovn-cmd |
| 41 | +COPY kube-ovn-bfdd-supervisor /kube-ovn/kube-ovn-bfdd-supervisor |
| 42 | +COPY kube-ovn-daemon /kube-ovn/kube-ovn-daemon |
| 43 | +COPY kube-ovn-controller /kube-ovn/kube-ovn-controller |
| 44 | +RUN ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-monitor && \ |
| 45 | + ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-speaker && \ |
| 46 | + ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-webhook && \ |
| 47 | + ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-leader-checker && \ |
| 48 | + ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-ic-controller && \ |
| 49 | + ln -s /kube-ovn/kube-ovn-controller /kube-ovn/kube-ovn-pinger && \ |
| 50 | + setcap CAP_NET_BIND_SERVICE+eip /kube-ovn/kube-ovn-cmd && \ |
| 51 | + setcap CAP_NET_RAW,CAP_NET_BIND_SERVICE+eip /kube-ovn/kube-ovn-controller && \ |
| 52 | + setcap CAP_NET_ADMIN,CAP_NET_RAW,CAP_NET_BIND_SERVICE,CAP_SYS_ADMIN+eip /kube-ovn/kube-ovn-daemon |
| 53 | +
|
| 54 | +FROM kubeovn/kube-ovn-base:$BASE_TAG |
| 55 | +
|
| 56 | +COPY --chmod=0644 logrotate/* /etc/logrotate.d/ |
| 57 | +COPY grace_stop_ovn_controller /usr/share/ovn/scripts/grace_stop_ovn_controller |
| 58 | +
|
| 59 | +COPY --from=setcap /kube-ovn /kube-ovn |
| 60 | +RUN /kube-ovn/iptables-wrapper-installer.sh --no-sanity-check |
| 61 | +
|
| 62 | +WORKDIR /kube-ovn |
| 63 | +EOF |
| 64 | +} |
| 65 | + |
| 66 | +@test "the fixture tracks the vendored chart version" { |
| 67 | + # Chart.yaml quotes the value; the package Makefile leans on the shell to |
| 68 | + # strip those, this runs the awk output through a comparison instead. |
| 69 | + appversion=$(awk '$1 == "appVersion:" {gsub(/"/, "", $2); print $2}' "$CHART") |
| 70 | + if [ "$appversion" != "$UPSTREAM_VERSION" ]; then |
| 71 | + echo "FAIL: chart appVersion is $appversion, fixture is v$UPSTREAM_VERSION" >&2 |
| 72 | + echo "Refresh upstream_dockerfile() from dist/images/Dockerfile at v$appversion." >&2 |
| 73 | + return 1 |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +@test "our Dockerfile copies everything upstream's does" { |
| 78 | + upstream_dockerfile | "$CHECK" "$OURS" - |
| 79 | +} |
| 80 | + |
| 81 | +@test "a binary upstream added and we did not is reported" { |
| 82 | + ours=$(mktemp) |
| 83 | + upstream=$(mktemp) |
| 84 | + upstream_dockerfile > "$upstream" |
| 85 | + grep -v 'kube-ovn-bfdd-supervisor' "$OURS" > "$ours" |
| 86 | + |
| 87 | + if "$CHECK" "$ours" "$upstream" 2>/dev/null; then |
| 88 | + echo "FAIL: a missing binary passed the check" >&2 |
| 89 | + return 1 |
| 90 | + fi |
| 91 | + if ! "$CHECK" "$ours" "$upstream" 2>&1 | grep -q 'kube-ovn-bfdd-supervisor'; then |
| 92 | + echo "FAIL: the report does not name the missing binary" >&2 |
| 93 | + return 1 |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +@test "a copy we make and upstream does not is reported" { |
| 98 | + ours=$(mktemp) |
| 99 | + upstream=$(mktemp) |
| 100 | + upstream_dockerfile > "$upstream" |
| 101 | + sed -e 's|^COPY --from=builder /source/dist/images/kube-ovn-cmd .*|&\nCOPY --from=builder /source/dist/images/kube-ovn-invented /kube-ovn/kube-ovn-invented|' "$OURS" > "$ours" |
| 102 | + |
| 103 | + if "$CHECK" "$ours" "$upstream" 2>/dev/null; then |
| 104 | + echo "FAIL: an extra copy passed the check" >&2 |
| 105 | + return 1 |
| 106 | + fi |
| 107 | + if ! "$CHECK" "$ours" "$upstream" 2>&1 | grep -q 'kube-ovn-invented'; then |
| 108 | + echo "FAIL: the report does not name the extra copy" >&2 |
| 109 | + return 1 |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +@test "COPY lines in the builder stage are ours alone and not compared" { |
| 114 | + # `COPY patches /patches` and the regression test have no upstream |
| 115 | + # counterpart: they feed our clone-and-patch build, which upstream does not |
| 116 | + # have. Reading them would make the check fail on every run. |
| 117 | + ours=$(mktemp) |
| 118 | + upstream=$(mktemp) |
| 119 | + upstream_dockerfile > "$upstream" |
| 120 | + sed -e 's|^COPY patches /patches$|COPY patches /patches\nCOPY extra-build-input /some/where|' "$OURS" > "$ours" |
| 121 | + |
| 122 | + "$CHECK" "$ours" "$upstream" |
| 123 | +} |
| 124 | + |
| 125 | +@test "a destination that differs is reported even when the source matches" { |
| 126 | + ours=$(mktemp) |
| 127 | + upstream=$(mktemp) |
| 128 | + upstream_dockerfile > "$upstream" |
| 129 | + sed -e 's|/kube-ovn/kube-ovn-daemon$|/kube-ovn/kube-ovn-daemon-typo|' "$OURS" > "$ours" |
| 130 | + |
| 131 | + if "$CHECK" "$ours" "$upstream" 2>/dev/null; then |
| 132 | + echo "FAIL: a retargeted copy passed the check" >&2 |
| 133 | + return 1 |
| 134 | + fi |
| 135 | +} |
| 136 | + |
| 137 | +@test "the checker reads the file it is given and not the tree" { |
| 138 | + empty=$(mktemp) |
| 139 | + upstream=$(mktemp) |
| 140 | + upstream_dockerfile > "$upstream" |
| 141 | + printf 'FROM scratch\n' > "$empty" |
| 142 | + |
| 143 | + if "$CHECK" "$empty" "$upstream" 2>/dev/null; then |
| 144 | + echo "FAIL: a Dockerfile with no copies at all passed the check" >&2 |
| 145 | + return 1 |
| 146 | + fi |
| 147 | +} |
0 commit comments