|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2020 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Adapted from: https://github.com/kubernetes-sigs/iptables-wrappers/blob/master/test/run-test.sh |
| 18 | + |
| 19 | +set -o errexit |
| 20 | +set -o nounset |
| 21 | +set -o pipefail |
| 22 | + |
| 23 | +if [[ -n "${DEBUG:-}" ]]; then |
| 24 | + set -x |
| 25 | + dash_x="-x" |
| 26 | +fi |
| 27 | + |
| 28 | +build_arg="" |
| 29 | +build_fail=0 |
| 30 | +nft_fail=0 |
| 31 | + |
| 32 | +while [[ $# -gt 1 ]]; do |
| 33 | + case "$1" in |
| 34 | + --build-arg=*) |
| 35 | + build_arg="${1#--build_arg=}" |
| 36 | + ;; |
| 37 | + --build-arg) |
| 38 | + shift |
| 39 | + build_arg="$1" |
| 40 | + ;; |
| 41 | + --build-fail) |
| 42 | + build_fail=1 |
| 43 | + ;; |
| 44 | + --nft-fail) |
| 45 | + nft_fail=1 |
| 46 | + ;; |
| 47 | + *) |
| 48 | + echo "Unrecognized flag '$1'" 1>&2 |
| 49 | + exit 1 |
| 50 | + ;; |
| 51 | + esac |
| 52 | + shift |
| 53 | +done |
| 54 | + |
| 55 | +if podman -h &> /dev/null; then |
| 56 | + docker_binary=podman |
| 57 | +elif docker -h &> /dev/null; then |
| 58 | + if docker version &> /dev/null; then |
| 59 | + docker_binary=docker |
| 60 | + else |
| 61 | + docker_binary="sudo docker" |
| 62 | + # Get the password prompting out of the way now |
| 63 | + sudo docker version > /dev/null |
| 64 | + fi |
| 65 | +else |
| 66 | + echo "Could not find podman or docker" 1>&2 |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +function docker() { |
| 71 | + if [[ -n "${DEBUG:-}" ]]; then |
| 72 | + command ${docker_binary} "$@" |
| 73 | + else |
| 74 | + if [[ "$1" == "build" ]]; then |
| 75 | + echo " docker $*" |
| 76 | + fi |
| 77 | + # Redirect stdout to /dev/null and indent stderr |
| 78 | + command ${docker_binary} "$@" 2>&1 > /dev/null | \ |
| 79 | + sed -e '/debconf: delaying package configuration/ d' \ |
| 80 | + -e 's/^/ /' |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +function build() { |
| 85 | + shift |
| 86 | + |
| 87 | + if ! docker buildx build -t test-hyperkube-base . --load; then |
| 88 | + FAIL "building base image failed" |
| 89 | + fi |
| 90 | + if ! docker buildx build --build-arg IMAGE=test-hyperkube-base -q -t iptables-wrapper-test -f tests/Dockerfile "$@" . --load; then |
| 91 | + FAIL "building test image failed" |
| 92 | + fi |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +function PASS() { |
| 97 | + printf "\033[1;92mPASS: $@\033[0m\n\n" |
| 98 | + exit 0 |
| 99 | +} |
| 100 | + |
| 101 | +function FAIL() { |
| 102 | + echo "update-alternatives configuration:" |
| 103 | + docker run iptables-wrapper-test update-alternatives --query iptables |
| 104 | + |
| 105 | + printf "\033[1;31mFAIL: $@\033[0m\n" 1>&2 |
| 106 | + |
| 107 | + exit 1 |
| 108 | +} |
| 109 | + |
| 110 | +if ! build iptables-wrapper-test ${build_arg}; then |
| 111 | + if [[ "${build_fail}" = 1 ]]; then |
| 112 | + PASS "build failed as expected" |
| 113 | + fi |
| 114 | + FAIL "build failed unexpectedly" |
| 115 | +fi |
| 116 | + |
| 117 | +if ! docker run --privileged -e iptables_binary=/usr/sbin/iptables iptables-wrapper-test /bin/sh ${dash_x:-} /test.sh legacy; then |
| 118 | + FAIL "failed legacy iptables / new rules test" |
| 119 | +fi |
| 120 | +if ! docker run --privileged -e iptables_binary=/usr/sbin/iptables iptables-wrapper-test /bin/sh ${dash_x:-} /test.sh nft; then |
| 121 | + FAIL "failed nft iptables / new rules test" |
| 122 | +fi |
| 123 | +if ! docker run --privileged -e iptables_binary=/usr/sbin/ip6tables iptables-wrapper-test /bin/sh ${dash_x:-} /test.sh legacy; then |
| 124 | + FAIL "failed legacy ip6tables / new rules test" |
| 125 | +fi |
| 126 | +if ! docker run --privileged -e iptables_binary=/usr/sbin/ip6tables iptables-wrapper-test /bin/sh ${dash_x:-} /test.sh nft; then |
| 127 | + FAIL "failed nft ip6tables / new rules test" |
| 128 | +fi |
| 129 | + |
| 130 | +PASS "success" |
0 commit comments