Skip to content

Commit

Permalink
E2E tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
miry committed Sep 7, 2021
1 parent cdc5682 commit 459924e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
with:
go-version: 1.17

-
name: E2E tests
run: make e2e

-
name: Build
uses: goreleaser/goreleaser-action@5a54d7e660bda43b405e8463261b3d25631ffe86
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
82 changes: 82 additions & 0 deletions bin/e2e
Original file line number Diff line number Diff line change
@@ -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}
4 changes: 4 additions & 0 deletions testing/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func BenchmarkDirect(b *testing.B) {
}
resp.Body.Close()
}
client.CloseIdleConnections()
}

// Test the backend through toxiproxy, use 64k random endpoint
Expand All @@ -60,6 +61,7 @@ func BenchmarkProxy(b *testing.B) {
}
resp.Body.Close()
}
client.CloseIdleConnections()
}

// Test the backend server directly, use "hello world" endpoint
Expand All @@ -76,6 +78,7 @@ func BenchmarkDirectSmall(b *testing.B) {
}
resp.Body.Close()
}
client.CloseIdleConnections()
}

// Test the backend through toxiproxy, use "hello world" endpoint
Expand All @@ -92,4 +95,5 @@ func BenchmarkProxySmall(b *testing.B) {
}
resp.Body.Close()
}
client.CloseIdleConnections()
}
5 changes: 4 additions & 1 deletion testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/hex"
"fmt"
"log"
"net/http"
)

Expand Down Expand Up @@ -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))
}

0 comments on commit 459924e

Please sign in to comment.