From 459924e992fb074dbc86ad5fe61d5a2f6298b50f Mon Sep 17 00:00:00 2001 From: Michael Nikitochkin Date: Tue, 7 Sep 2021 13:29:29 +0200 Subject: [PATCH] E2E tests Add script to run e2e tests to test client and server. Use benchmarking script as sample client application. Automate the process with Github Actions in test workflow. --- .github/workflows/test.yml | 4 ++ CHANGELOG.md | 3 +- Makefile | 20 +++++++--- bin/e2e | 82 ++++++++++++++++++++++++++++++++++++++ testing/benchmark_test.go | 4 ++ testing/endpoint.go | 5 ++- 6 files changed, 110 insertions(+), 8 deletions(-) create mode 100755 bin/e2e diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 46a7e4352..478be65ac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,6 +44,10 @@ jobs: with: go-version: 1.17 + - + name: E2E tests + run: make e2e + - name: Build uses: goreleaser/goreleaser-action@5a54d7e660bda43b405e8463261b3d25631ffe86 diff --git a/CHANGELOG.md b/CHANGELOG.md index e086ee8f0..0c7c0cded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ * Use CHANGELOG.md for release description (#306, @miry) * Dependency updates in #294 introduced a breaking change in CLI argument parsing. Now [flags must be specified before arguments](https://github.com/urfave/cli/blob/master/docs/migrate-v1-to-v2.md#flags-before-args). Previously, arguments could be specified prior to flags. - Update usage help text and documentation. (@miry) + Update usage help text and documentation. (#308, @miry) +* Run e2e tests to validate the command line and basic features of server, client and application (#309, @miry). # [2.1.5] diff --git a/Makefile b/Makefile index 1fb550f53..4d867b876 100644 --- a/Makefile +++ b/Makefile @@ -5,19 +5,27 @@ all: setup build test: go test -v -race ./... +.PHONY: e2e +e2e: build + bin/e2e + .PHONY: build -build: clean - goreleaser build --snapshot --rm-dist --skip-post-hooks --skip-validate +build: dist clean + go build -ldflags="-s -w" -o ./dist/toxiproxy-server ./cmd + go build -ldflags="-s -w" -o ./dist/toxiproxy-cli ./cli .PHONY: release release: goreleaser release --rm-dist -.PHONY: clean -clean: - rm -fr dist/* - .PHONY: setup setup: go mod download go mod tidy + +dist: + mkdir -p dist + +.PHONY: clean +clean: + rm -fr dist/* diff --git a/bin/e2e b/bin/e2e new file mode 100755 index 000000000..4f019b71f --- /dev/null +++ b/bin/e2e @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +set -e + +function wait_for_url() { + echo "--- Waiting for HTTP connection available" + timeout -s TERM 30s bash -c \ + 'while [[ "$(curl -s -o /dev/null -L -w ''%{http_code}'' ${0})" != "200" ]];\ + do echo "Waiting for ${0}" && sleep 2;\ + done' ${1} + curl -s -I -X GET $1 +} + +echo "== Setup" + +# Stop all background jobs on exit +trap 'pkill -15 -f "dist/toxiproxy-server$"; pkill -15 -f "exe/endpoint$"' EXIT SIGINT SIGTERM + +echo "=== Starting Web service" +go run testing/endpoint.go 2>&1 | sed -e 's/^/[web] /' & + +echo "=== Starting Toxiproxy" +./dist/toxiproxy-server 2>&1 | sed -e 's/^/[toxiproxy] /' & + +echo "=== Wait when service are available" +wait_for_url http://localhost:20002/test2 +wait_for_url http://localhost:8474/version + +echo "=== Test client to manipulate proxy" +./dist/toxiproxy-cli create -l localhost:20000 -u localhost:20002 shopify_http +./dist/toxiproxy-cli list +./dist/toxiproxy-cli toggle shopify_http +./dist/toxiproxy-cli inspect shopify_http +./dist/toxiproxy-cli toggle shopify_http +echo -e "-----------------\n" + +echo "== Benchmarking" + +echo +echo "=== Without toxics" +go test -bench=. ./testing -v +echo -e "-----------------\n" + +echo "=== Latency toxic" +./dist/toxiproxy-cli toxic add --type latency --toxicName "latency_downstream" --attribute "latency=1000" --attribute="jitter=50" shopify_http +go test -bench=. ./testing -v + +./dist/toxiproxy-cli inspect shopify_http +./dist/toxiproxy-cli toxic update --toxicName "latency_downstream" --attribute="jitter=20" shopify_http +./dist/toxiproxy-cli inspect shopify_http + +./dist/toxiproxy-cli toxic delete --toxicName "latency_downstream" shopify_http +echo -e "-----------------\n" + +echo "=== Bandwidth toxic" + +./dist/toxiproxy-cli toxic add --type bandwidth --toxicName "bandwidth_kb_per_second" --attribute "rate=1" shopify_http +./dist/toxiproxy-cli toxic update --toxicName "bandwidth_kb_per_second" --attribute="rate=10" shopify_http + +go test -bench=. ./testing -v + +./dist/toxiproxy-cli toxic delete --toxicName "bandwidth_kb_per_second" shopify_http +echo -e "-----------------\n" + +echo "=== Timeout toxic" + +./dist/toxiproxy-cli toxic add --type timeout --toxicName "timeout_ms" --attribute "timeout=10" shopify_http +./dist/toxiproxy-cli toxic delete --toxicName "timeout_ms" shopify_http +echo -e "-----------------\n" + +echo "=== Slicer toxic" + +./dist/toxiproxy-cli toxic add --type slicer --toxicName "slicer_us" --attribute "average_size=64" --attribute "size_variation=32" --attribute="delay=10" shopify_http +go test -bench=. ./testing -v +./dist/toxiproxy-cli toxic delete --toxicName "slicer_us" shopify_http +echo -e "-----------------\n" + +echo "== Teardown" + +./dist/toxiproxy-cli delete shopify_http + +exit ${test_exit} diff --git a/testing/benchmark_test.go b/testing/benchmark_test.go index 0f7bd9ce8..366142b4f 100644 --- a/testing/benchmark_test.go +++ b/testing/benchmark_test.go @@ -44,6 +44,7 @@ func BenchmarkDirect(b *testing.B) { } resp.Body.Close() } + client.CloseIdleConnections() } // Test the backend through toxiproxy, use 64k random endpoint @@ -60,6 +61,7 @@ func BenchmarkProxy(b *testing.B) { } resp.Body.Close() } + client.CloseIdleConnections() } // Test the backend server directly, use "hello world" endpoint @@ -76,6 +78,7 @@ func BenchmarkDirectSmall(b *testing.B) { } resp.Body.Close() } + client.CloseIdleConnections() } // Test the backend through toxiproxy, use "hello world" endpoint @@ -92,4 +95,5 @@ func BenchmarkProxySmall(b *testing.B) { } resp.Body.Close() } + client.CloseIdleConnections() } diff --git a/testing/endpoint.go b/testing/endpoint.go index 8696bef13..9bc0ab567 100644 --- a/testing/endpoint.go +++ b/testing/endpoint.go @@ -3,6 +3,7 @@ package main import ( "encoding/hex" "fmt" + "log" "net/http" ) @@ -42,5 +43,7 @@ func main() { hex.Encode(out, stuff) http.HandleFunc("/test1", handler1) http.HandleFunc("/test2", handler2) - http.ListenAndServe(":20002", nil) + + log.Println("Listening :20002") + log.Fatal(http.ListenAndServe(":20002", nil)) }