Skip to content

Commit 69a09f4

Browse files
authored
Add CI/testing to examples (envoyproxy#12491)
This PR incorporates the tests that i created for the envoy examples (originally here: https://github.com/phlax/envoy-examples) It also adds a test to ensure that example configs have been added to the examples `BUILD` config (ref: envoyproxy#12256 (comment)) Signed-off-by: Ryan Northey <[email protected]>
1 parent 82e611e commit 69a09f4

File tree

21 files changed

+681
-47
lines changed

21 files changed

+681
-47
lines changed

.azure-pipelines/pipelines.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ jobs:
160160
artifactName: docker
161161
condition: always()
162162

163+
- job: examples
164+
dependsOn: ["docker"]
165+
displayName: "Verify examples run as documented"
166+
pool:
167+
vmImage: "ubuntu-18.04"
168+
steps:
169+
- task: DownloadBuildArtifacts@0
170+
inputs:
171+
buildType: current
172+
artifactName: "docker"
173+
itemPattern: "docker/envoy-docker-images.tar.xz"
174+
downloadType: single
175+
targetPath: $(Build.StagingDirectory)
176+
- bash: ./ci/do_ci.sh verify_examples
177+
env:
178+
ENVOY_DOCKER_BUILD_DIR: $(Build.StagingDirectory)
179+
NO_BUILD_SETUP: 1
180+
163181
- job: macOS
164182
dependsOn: ["format"]
165183
timeoutInMinutes: 360

ci/do_ci.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ if [[ "$1" == "fix_format" || "$1" == "check_format" || "$1" == "check_repositor
1212
fi
1313

1414
SRCDIR="${PWD}"
15-
. "$(dirname "$0")"/setup_cache.sh
16-
. "$(dirname "$0")"/build_setup.sh $build_setup_args
15+
NO_BUILD_SETUP="${NO_BUILD_SETUP:-}"
16+
if [[ -z "$NO_BUILD_SETUP" ]]; then
17+
. "$(dirname "$0")"/setup_cache.sh
18+
. "$(dirname "$0")"/build_setup.sh $build_setup_args
19+
fi
1720
cd "${SRCDIR}"
1821

1922
if [[ "${ENVOY_BUILD_ARCH}" == "x86_64" ]]; then
@@ -399,6 +402,23 @@ elif [[ "$CI_TARGET" == "docs" ]]; then
399402
echo "generating docs..."
400403
docs/build.sh
401404
exit 0
405+
elif [[ "$CI_TARGET" == "verify_examples" ]]; then
406+
echo "verify examples..."
407+
docker load < "$ENVOY_DOCKER_BUILD_DIR/docker/envoy-docker-images.tar.xz"
408+
images=($(docker image list --format "{{.Repository}}"))
409+
tags=($(docker image list --format "{{.Tag}}"))
410+
for i in "${!images[@]}"; do
411+
if [[ "${images[i]}" =~ "envoy" ]]; then
412+
docker tag "${images[$i]}:${tags[$i]}" "${images[$i]}:latest"
413+
fi
414+
done
415+
docker images
416+
sudo apt-get update -y
417+
sudo apt-get install -y -qq --no-install-recommends redis-tools
418+
export DOCKER_NO_PULL=1
419+
umask 027
420+
ci/verify_examples.sh
421+
exit 0
402422
else
403423
echo "Invalid do_ci.sh target, see ci/README.md for valid targets."
404424
exit 1

ci/verify_examples.sh

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,67 @@
1-
#!/bin/bash
1+
#!/bin/bash -E
22

3-
set -e
3+
TESTFILTER="${1:-*}"
4+
FAILED=()
5+
SRCDIR="${SRCDIR:-$(pwd)}"
6+
EXCLUDED_BUILD_CONFIGS=${EXCLUDED_BUILD_CONFIGS:-"^./jaeger-native-tracing|docker-compose"}
47

5-
verify() {
6-
echo $1
7-
CONTAINER_ID="$(docker ps -aqf name=$1)"
8-
if [ "false" == "$(docker inspect -f {{.State.Running}} ${CONTAINER_ID})" ]
9-
then
10-
echo "error: $1 not running"
11-
exit 1
12-
fi
8+
9+
trap_errors () {
10+
local frame=0 command line sub file
11+
if [[ -n "$example" ]]; then
12+
command=" (${example})"
13+
fi
14+
set +v
15+
while read -r line sub file < <(caller "$frame"); do
16+
if [[ "$frame" -ne "0" ]]; then
17+
FAILED+=(" > ${sub}@ ${file} :${line}")
18+
else
19+
FAILED+=("${sub}@ ${file} :${line}${command}")
20+
fi
21+
((frame++))
22+
done
23+
set -v
24+
}
25+
26+
trap trap_errors ERR
27+
trap exit 1 INT
28+
29+
30+
run_examples () {
31+
local examples example
32+
cd "${SRCDIR}/examples" || exit 1
33+
examples=$(find . -mindepth 1 -maxdepth 1 -type d -name "$TESTFILTER" | sort)
34+
for example in $examples; do
35+
pushd "$example" > /dev/null || return 1
36+
./verify.sh
37+
popd > /dev/null || return 1
38+
done
1339
}
1440

15-
# Test front proxy example
16-
cd examples/front-proxy
17-
docker-compose up --build -d
18-
for CONTAINER_NAME in "frontproxy_front-envoy" "frontproxy_service1" "frontproxy_service2"
19-
do
20-
verify $CONTAINER_NAME
21-
done
22-
cd ../
23-
24-
# Test grpc bridge example
25-
# install go
26-
GO_VERSION="1.14.7"
27-
curl -O https://storage.googleapis.com/golang/go$GO_VERSION.linux-amd64.tar.gz
28-
tar -xf go$GO_VERSION.linux-amd64.tar.gz
29-
sudo mv go /usr/local
30-
export PATH=$PATH:/usr/local/go/bin
31-
export GOPATH=$HOME/go
32-
mkdir -p $GOPATH/src/github.com/envoyproxy/envoy/examples/
33-
cp -r grpc-bridge $GOPATH/src/github.com/envoyproxy/envoy/examples/
34-
# build example
35-
cd $GOPATH/src/github.com/envoyproxy/envoy/examples/grpc-bridge
36-
./script/bootstrap
37-
./script/build
38-
# verify example works
39-
docker-compose up --build -d
40-
for CONTAINER_NAME in "grpcbridge_python" "grpcbridge_grpc"
41-
do
42-
verify $CONTAINER_NAME
43-
done
41+
verify_build_configs () {
42+
local config configs missing
43+
missing=()
44+
cd "${SRCDIR}/examples" || return 1
45+
configs="$(find . -name "*.yaml" -o -name "*.lua" | grep -vE "${EXCLUDED_BUILD_CONFIGS}" | cut -d/ -f2-)"
46+
for config in $configs; do
47+
grep "\"$config\"" BUILD || missing+=("$config")
48+
done
49+
if [[ -n "${missing[*]}" ]]; then
50+
for config in "${missing[@]}"; do
51+
echo "Missing config: $config" >&2
52+
done
53+
return 1
54+
fi
55+
}
56+
57+
verify_build_configs
58+
run_examples
59+
60+
61+
if [[ "${#FAILED[@]}" -ne "0" ]]; then
62+
echo "TESTS FAILED:"
63+
for failed in "${FAILED[@]}"; do
64+
echo "$failed" >&2
65+
done
66+
exit 1
67+
fi

examples/cors/verify.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash -e
2+
3+
export NAME=cors
4+
export PATHS=frontend,backend
5+
6+
# shellcheck source=examples/verify-common.sh
7+
. "$(dirname "${BASH_SOURCE[0]}")/../verify-common.sh"
8+
9+
10+
run_log "Test service"
11+
responds_with \
12+
"Envoy CORS Webpage" \
13+
http://localhost:8000
14+
15+
run_log "Test cors server: disabled"
16+
responds_with \
17+
Success \
18+
-H "Origin: http://example.com" \
19+
http://localhost:8002/cors/disabled
20+
responds_without_header \
21+
access-control-allow-origin \
22+
-H "Origin: http://example.com" \
23+
http://localhost:8002/cors/disabled
24+
25+
run_log "Test cors server: open"
26+
responds_with \
27+
Success \
28+
-H 'Origin: http://example.com' \
29+
http://localhost:8002/cors/open
30+
responds_with_header \
31+
"access-control-allow-origin: http://example.com" \
32+
-H "Origin: http://example.com" \
33+
http://localhost:8002/cors/open
34+
35+
run_log "Test cors server: restricted"
36+
responds_with \
37+
Success \
38+
-H "Origin: http://example.com" \
39+
http://localhost:8002/cors/restricted
40+
responds_without_header \
41+
access-control-allow-origin \
42+
-H "Origin: http://example.com" \
43+
http://localhost:8002/cors/restricted
44+
responds_with_header \
45+
"access-control-allow-origin: http://foo.envoyproxy.io" \
46+
-H "Origin: http://foo.envoyproxy.io" \
47+
http://localhost:8002/cors/restricted

examples/csrf/verify.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash -e
2+
3+
export NAME=csrf
4+
export PATHS=samesite,crosssite
5+
6+
# shellcheck source=examples/verify-common.sh
7+
. "$(dirname "${BASH_SOURCE[0]}")/../verify-common.sh"
8+
9+
10+
run_log "Test services"
11+
responds_with \
12+
"Envoy CSRF Demo" \
13+
http://localhost:8002
14+
responds_with \
15+
"Envoy CSRF Demo" \
16+
http://localhost:8000
17+
18+
run_log "Test stats server"
19+
responds_with \
20+
":" \
21+
http://localhost:8001/stats
22+
23+
run_log "Test csrf server: disabled"
24+
responds_with \
25+
Success \
26+
-X POST \
27+
-H "Origin: http://example.com" \
28+
http://localhost:8000/csrf/disabled
29+
responds_with_header \
30+
"access-control-allow-origin: http://example.com" \
31+
-X POST \
32+
-H "Origin: http://example.com" \
33+
http://localhost:8000/csrf/disabled
34+
35+
run_log "Test csrf server: shadow"
36+
responds_with \
37+
Success \
38+
-X POST \
39+
-H "Origin: http://example.com" \
40+
http://localhost:8000/csrf/shadow
41+
responds_with_header \
42+
"access-control-allow-origin: http://example.com" \
43+
-X POST \
44+
-H "Origin: http://example.com" \
45+
http://localhost:8000/csrf/shadow
46+
47+
run_log "Test csrf server: enabled"
48+
responds_with \
49+
"Invalid origin" \
50+
-X POST \
51+
-H "Origin: http://example.com" \
52+
http://localhost:8000/csrf/enabled
53+
responds_with_header \
54+
"HTTP/1.1 403 Forbidden" \
55+
-X POST \
56+
-H "Origin: http://example.com" \
57+
http://localhost:8000/csrf/enabled
58+
59+
run_log "Test csrf server: additional_origin"
60+
responds_with \
61+
Success \
62+
-X POST \
63+
-H "Origin: http://example.com" \
64+
http://localhost:8000/csrf/additional_origin
65+
responds_with_header \
66+
"access-control-allow-origin: http://example.com" \
67+
-X POST \
68+
-H "Origin: http://example.com" \
69+
http://localhost:8000/csrf/additional_origin

examples/ext_authz/verify.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash -e
2+
3+
export NAME=ext_authz
4+
5+
# shellcheck source=examples/verify-common.sh
6+
. "$(dirname "${BASH_SOURCE[0]}")/../verify-common.sh"
7+
8+
9+
run_log "Test services responds with 403"
10+
responds_with_header \
11+
"HTTP/1.1 403 Forbidden"\
12+
http://localhost:8000/service
13+
14+
run_log "Restart front-envoy with FRONT_ENVOY_YAML=config/http-service.yaml"
15+
docker-compose down
16+
FRONT_ENVOY_YAML=config/http-service.yaml docker-compose up -d
17+
sleep 10
18+
19+
run_log "Test service responds with 403"
20+
responds_with_header \
21+
"HTTP/1.1 403 Forbidden"\
22+
http://localhost:8000/service
23+
24+
run_log "Test authenticated service responds with 200"
25+
responds_with_header \
26+
"HTTP/1.1 200 OK" \
27+
-H "Authorization: Bearer token1" \
28+
http://localhost:8000/service
29+
30+
run_log "Restart front-envoy with FRONT_ENVOY_YAML=config/opa-service/v2.yaml"
31+
docker-compose down
32+
FRONT_ENVOY_YAML=config/opa-service/v2.yaml docker-compose up -d
33+
sleep 10
34+
35+
run_log "Test OPA service responds with 200"
36+
responds_with_header \
37+
"HTTP/1.1 200 OK" \
38+
http://localhost:8000/service
39+
40+
run_log "Check OPA logs"
41+
docker-compose logs ext_authz-opa-service | grep decision_id -A 30
42+
43+
run_log "Check OPA service rejects POST"
44+
responds_with_header \
45+
"HTTP/1.1 403 Forbidden" \
46+
-X POST \
47+
http://localhost:8000/service

examples/fault-injection/verify.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash -e
2+
3+
export NAME=fault-injection
4+
5+
# shellcheck source=examples/verify-common.sh
6+
. "$(dirname "${BASH_SOURCE[0]}")/../verify-common.sh"
7+
8+
9+
run_log "Send requests for 20 seconds"
10+
docker-compose exec -T envoy bash -c \
11+
"bash send_request.sh & export pid=\$! && sleep 20 && kill \$pid" \
12+
&> /dev/null
13+
14+
run_log "Check logs"
15+
docker-compose logs | grep "HTTP/1.1\" 200"
16+
17+
18+
_fault_injection_test () {
19+
local action code existing_200s existing_codes
20+
action="$1"
21+
code="$2"
22+
existing_codes=0
23+
24+
# enable fault injection and check for http hits of type $code
25+
existing_codes=$(docker-compose logs | grep -c "HTTP/1.1\" ${code}" || :)
26+
run_log "Enable ${action} fault injection"
27+
docker-compose exec -T envoy bash "enable_${action}_fault_injection.sh"
28+
run_log "Send requests for 20 seconds"
29+
docker-compose exec -T envoy bash -c \
30+
"bash send_request.sh & export pid=\$! && sleep 20 && kill \$pid" \
31+
&> /dev/null
32+
run_log "Check logs again"
33+
new_codes=$(docker-compose logs | grep -c "HTTP/1.1\" ${code}")
34+
if [[ "$new_codes" -le "$existing_codes" ]]; then
35+
echo "ERROR: expected to find new logs with response code $code" >&2
36+
return 1
37+
fi
38+
39+
# disable fault injection and check for http hits of type 200
40+
existing_200s=$(docker-compose logs | grep -c "HTTP/1.1\" 200")
41+
run_log "Disable ${action} fault injection"
42+
docker-compose exec -T envoy bash "disable_${action}_fault_injection.sh"
43+
run_log "Send requests for 20 seconds"
44+
docker-compose exec -T envoy bash -c \
45+
"bash send_request.sh & export pid=\$! && sleep 20 && kill \$pid" \
46+
&> /dev/null
47+
run_log "Check logs again"
48+
new_200s=$(docker-compose logs | grep -c "HTTP/1.1\" 200")
49+
if [[ "$new_200s" -le "$existing_200s" ]]; then
50+
echo "ERROR: expected to find new logs with response code 200" >&2
51+
return 1
52+
fi
53+
}
54+
55+
_fault_injection_test abort 503
56+
_fault_injection_test delay 200
57+
58+
run_log "Check tree"
59+
docker-compose exec -T envoy tree /srv/runtime

0 commit comments

Comments
 (0)