|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2019 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd)" |
| 19 | +DIST=${REPO}/ts/demo-functions/dist |
| 20 | +TAG=${TAG:-dev} |
| 21 | +NODOCKER=${NODOCKER:-} |
| 22 | +EMPTY_OUTPUT=$( |
| 23 | + cat <<-EOF |
| 24 | +apiVersion: v1 |
| 25 | +kind: List |
| 26 | +items: [] |
| 27 | +EOF |
| 28 | +) |
| 29 | + |
| 30 | +############################ |
| 31 | +# Test framework |
| 32 | +############################ |
| 33 | + |
| 34 | +function testcase() { |
| 35 | + echo "testcase: ${1}" |
| 36 | + tmp=$(mktemp -d "/tmp/e2e.${1}.XXXXXXXX") |
| 37 | + cp -r "${REPO}"/example-configs/* "${tmp}" |
| 38 | + cd "${tmp}" |
| 39 | +} |
| 40 | + |
| 41 | +function fail() { |
| 42 | + echo "FAIL: " "$@" |
| 43 | + exit 1 |
| 44 | +} |
| 45 | + |
| 46 | +function assert_empty_list() { |
| 47 | + content="$(<$1)" |
| 48 | + [[ ${content} = "${EMPTY_OUTPUT}" ]] || fail "Not empty list: ${content}" |
| 49 | +} |
| 50 | + |
| 51 | +function assert_empty_string() { |
| 52 | + content="$(<$1)" |
| 53 | + [[ -z ${content} ]] || fail "Not empty list: ${content}" |
| 54 | +} |
| 55 | + |
| 56 | +function assert_dir_exists() { |
| 57 | + [[ -d $1 ]] || fail "Dir not exist: $1" |
| 58 | +} |
| 59 | + |
| 60 | +############################ |
| 61 | +# Node Tests |
| 62 | +############################ |
| 63 | + |
| 64 | +testcase "node_no_op_stdout" |
| 65 | +node ${DIST}/no_op_run.js -i /dev/null >out.yaml |
| 66 | +assert_empty_list out.yaml |
| 67 | + |
| 68 | +testcase "node_no_op_regular_files" |
| 69 | +echo "$EMPTY_OUTPUT" >in.yaml |
| 70 | +node ${DIST}/no_op_run.js -i in.yaml -o out.yaml |
| 71 | +assert_empty_list out.yaml |
| 72 | + |
| 73 | +testcase "node_no_op_pipe" |
| 74 | +node ${DIST}/no_op_run.js -i /dev/null | |
| 75 | + node ${DIST}/no_op_run.js >out.yaml |
| 76 | +assert_empty_list out.yaml |
| 77 | + |
| 78 | +testcase "node_no_op_devnull" |
| 79 | +node ${DIST}/no_op_run.js -i /dev/null | |
| 80 | + node ${DIST}/no_op_run.js | |
| 81 | + node ${DIST}/no_op_run.js -o /dev/null >out.yaml |
| 82 | +assert_empty_string out.yaml |
| 83 | + |
| 84 | +testcase "node_label_namespace" |
| 85 | +node ${DIST}/read_yaml_run.js -i /dev/null -d source_dir=$(pwd) | |
| 86 | + node ${DIST}/label_namespace_run.js -d label_name=color -d label_value=orange | |
| 87 | + grep -q 'color: orange' |
| 88 | + |
| 89 | +testcase "node_demo_func" |
| 90 | +node ${DIST}/read_yaml_run.js -i /dev/null -d source_dir=$(pwd) | |
| 91 | + node ${DIST}/mutate_psp_run.js | |
| 92 | + node ${DIST}/expand_team_cr_run.js | |
| 93 | + node ${DIST}/validate_rolebinding_run.js -d [email protected] | |
| 94 | + node ${DIST}/write_yaml_run.js -o /dev/null -d sink_dir=$(pwd) -d overwrite=true |
| 95 | +assert_dir_exists payments-dev |
| 96 | +assert_dir_exists payments-prod |
| 97 | +grep -q allowPrivilegeEscalation podsecuritypolicy_psp.yaml |
| 98 | + |
| 99 | +############################ |
| 100 | +# Docker Tests |
| 101 | +############################ |
| 102 | + |
| 103 | +[[ -z ${NODOCKER} ]] || { echo "Skipping docker tests"; exit 0; } |
| 104 | + |
| 105 | +testcase "docker_no_op_stdout" |
| 106 | +docker run -i gcr.io/kpt-functions/no-op:${TAG} -i /dev/null >out.yaml |
| 107 | +assert_empty_list out.yaml |
| 108 | + |
| 109 | +testcase "docker_no_op_regular_files" |
| 110 | +echo "$EMPTY_OUTPUT" >in.yaml |
| 111 | +docker run -i -u $(id -u) -v $(pwd):/source gcr.io/kpt-functions/no-op:${TAG} -i /source/in.yaml -o /source/out.yaml |
| 112 | +assert_empty_list out.yaml |
| 113 | + |
| 114 | +testcase "docker_no_op_pipe" |
| 115 | +docker run -i gcr.io/kpt-functions/no-op:${TAG} -i /dev/null | |
| 116 | + docker run -i gcr.io/kpt-functions/no-op:${TAG} >out.yaml |
| 117 | +assert_empty_list out.yaml |
| 118 | + |
| 119 | +testcase "docker_no_op_devnull" |
| 120 | +docker run -i gcr.io/kpt-functions/no-op:${TAG} -i /dev/null | |
| 121 | + docker run -i gcr.io/kpt-functions/no-op:${TAG} | |
| 122 | + docker run -i gcr.io/kpt-functions/no-op:${TAG} -o /dev/null >out.yaml |
| 123 | +assert_empty_string out.yaml |
| 124 | + |
| 125 | +testcase "docker_label_namespace" |
| 126 | +docker run -i -u $(id -u) -v $(pwd):/source gcr.io/kpt-functions/read-yaml:${TAG} -i /dev/null -d source_dir=/source | |
| 127 | + docker run -i gcr.io/kpt-functions/label-namespace:${TAG} -d label_name=color -d label_value=orange | |
| 128 | + grep -q 'color: orange' |
| 129 | + |
| 130 | +testcase "docker_demo_func" |
| 131 | +docker run -i -u $(id -u) -v $(pwd):/source gcr.io/kpt-functions/read-yaml:${TAG} -i /dev/null -d source_dir=/source | |
| 132 | + docker run -i gcr.io/kpt-functions/mutate-psp:${TAG} | |
| 133 | + docker run -i gcr.io/kpt-functions/expand-team-cr:${TAG} | |
| 134 | + docker run -i gcr.io/kpt-functions/validate-rolebinding: ${TAG} -d [email protected] | |
| 135 | + docker run -i -u $(id -u) -v $(pwd):/sink gcr.io/kpt-functions/write-yaml:${TAG} -o /dev/null -d sink_dir=/sink -d overwrite=true |
| 136 | +assert_dir_exists payments-dev |
| 137 | +assert_dir_exists payments-prod |
| 138 | +grep -q allowPrivilegeEscalation podsecuritypolicy_psp.yaml |
0 commit comments