From 047c6b3057ad2a35073ea4ec14e3e529d87cdeda Mon Sep 17 00:00:00 2001 From: Palasanu George Date: Mon, 4 Apr 2022 16:03:05 +0300 Subject: [PATCH] Add the option to read the token from disk to the performance tests (#24) * Performance tests, add the option to read the token from file * Add so you can use the local env to pass parameters to the performance tests Co-authored-by: George Palasanu --- Makefile | 14 +++++-- docs/developer-guides/testing.md | 16 ++++++-- test/performance/scripts/benchmark.sh | 10 +++-- .../performance/scripts/run_bech_container.sh | 40 +++++++++++++++---- 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 413ada69..9cd60a89 100644 --- a/Makefile +++ b/Makefile @@ -152,13 +152,19 @@ test-e2e: @. local/.env.local && go test -race github.com/adobe/cluster-registry/test/e2e -count=1 -v $(shell pwd)/local/cleanup.sh -## Make sure you have set the APISERVER_AUTH_TOKEN env variable -## Use PERFORMANCE_TEST_TIME env var to set the benchmark time per endpoint -## Use APISERVER_ENDPOINT env var to set the endpoint for the benchmark +## Make sure you have set the APISERVER_AUTH_TOKEN env variable. +## Use PERFORMANCE_TEST_TIME env var to set the benchmark time per endpoint. +## Use APISERVER_ENDPOINT env var to set the endpoint for the benchmark. +## The env variables from the local env will overwrite th local/.env.local ones. .PHONY: test-performance test-performance: ## Outputs requests/s, average req time, 99.9th percentile req time @. local/.env.local \ - && export APISERVER_ENDPOINT=${APISERVER_ENDPOINT} \ + && export LOCAL_ENV_APISERVER_ENDPOINT=${APISERVER_ENDPOINT} \ + && export LOCAL_ENV_APISERVER_AUTH_TOKEN=${APISERVER_AUTH_TOKEN} \ + && export LOCAL_ENV_PERFORMANCE_TEST_TIME=${PERFORMANCE_TEST_TIME} \ + && export LOCAL_ENV_IMAGE_PERFORMANCE_TESTS=${IMAGE_PERFORMANCE_TESTS} \ + && export LOCAL_ENV_TAG=${TAG} \ + && export LOCAL_ENV_NETWORK=${NETWORK} \ && test/performance/scripts/run_bech_container.sh diff --git a/docs/developer-guides/testing.md b/docs/developer-guides/testing.md index c7734b26..6e036978 100644 --- a/docs/developer-guides/testing.md +++ b/docs/developer-guides/testing.md @@ -10,15 +10,25 @@ To run integration tests execute `make test-e2e`. To run performance test locally you should run: -`. local/.env.local` To set the IMAGE_PERFORMANCE_TESTS its TAG and the container NETWORK. -`make test-performance $APISERVER_ENDPOINT=http://api-url/ PERFORMANCE_TEST_TIME=10 APISERVER_AUTH_TOKEN=$token`. +`make test-performance APISERVER_AUTH_TOKEN=$token APISERVER_ENDPOINT=http://api-url/ PERFORMANCE_TEST_TIME=10`. -The `APISERVER_ENDPOINT` and `PERFORMANCE_TEST_TIME` have the defaults http://localhost:8080 and 10s, but the `APISERVER_AUTH_TOKEN` is mandatory. +It will take the defaults from the `local/.env.local` file but it will overwrite them form the local env if the environment variables exists. +If the file and the env vars are missing the `APISERVER_ENDPOINT` and `PERFORMANCE_TEST_TIME` have the defaults `http://localhost:8080` and `10` seconds, but the `APISERVER_AUTH_TOKEN` is mandatory. To generate a local token you can use the following code: ``` + import ( + "github.com/adobe/cluster-registry/pkg/config" + "github.com/adobe/cluster-registry/test/jwt" + + "fmt" +) + +func main() { + appConfig, _ := config.LoadApiConfig() jwtToken := jwt.GenerateSignedToken(appConfig, false, "test/testdata/dummyRsaPrivateKey.pem", "", jwt.Claim{}) fmt.Println(jwtToken) +} ``` \ No newline at end of file diff --git a/test/performance/scripts/benchmark.sh b/test/performance/scripts/benchmark.sh index 34bd111a..f6256b42 100755 --- a/test/performance/scripts/benchmark.sh +++ b/test/performance/scripts/benchmark.sh @@ -3,6 +3,7 @@ # Environment variables as input: # APISERVER_ENDPOINT (required), the url to the api service # APISERVER_AUTH_TOKEN (required), the token used to authenticate to the api +# TOKEN_PATH, use this env variable to set the APISERVER_AUTH_TOKEN from a file # PERFORMANCE_TEST_TIME, the time it will take for a benchmark to run on one endpoint # the default is 10 # LIMIT, the number of clusters on the '/clusters' endpoint. We need a consistent nr @@ -66,11 +67,14 @@ run_benckmarks "${ENDPOINT}/readyz" "${NR_OF_SECS}" run_benckmarks "${ENDPOINT}/livez" "${NR_OF_SECS}" if [ -z "${APISERVER_AUTH_TOKEN}" ]; then - printf "ERROR: Missing env variable API_AUTH_TOKEN for the rest of the endpoints.\n" 1>&2 - exit 1 + if [ -z "${TOKEN_PATH}" ]; then + printf "ERROR: Missing env variable APISERVER_AUTH_TOKEN for the rest of the endpoints.\n" 1>&2 + exit 1 + fi + APISERVER_AUTH_TOKEN=$(cat "${TOKEN_PATH}") fi -TOKEN="${APISERVER_AUTH_TOKEN}" +TOKEN="${APISERVER_AUTH_TOKEN}" LIMIT="${LIMIT:-200}" OFFSET="${OFFSET:-0}" diff --git a/test/performance/scripts/run_bech_container.sh b/test/performance/scripts/run_bech_container.sh index 2379e86f..ad343a88 100755 --- a/test/performance/scripts/run_bech_container.sh +++ b/test/performance/scripts/run_bech_container.sh @@ -1,22 +1,48 @@ #!/usr/bin/env bash # This script is so we can pass the enviorment variables from the /local/.env.local -# file to the container that runs the benchmark.sh script when ran from the Makefile +# file to the container that runs the benchmark.sh script when ran from the Makefile. +# +# The env variables with the prefix LOCAL_ENV are from the env of the user that ran +# the make target, and the ones without the prefix are from the /local/.env.local file. +# +# Only the container run env variables have defaults in this script the APISERVER_ENDPOINT +# and PERFORMANCE_TEST_TIME have defaults inside the container. The APISERVER_AUTH_TOKEN +# is required. -if [ -z "${TAG}" ]; then - TAG="v$(cat "$(git rev-parse --show-toplevel)/VERSION")-$(git rev-parse --short HEAD)" +# If LOCAL_ENV_ exists and is not empty +if [[ -n "${LOCAL_ENV_APISERVER_ENDPOINT}" ]]; then + APISERVER_ENDPOINT="${LOCAL_ENV_APISERVER_ENDPOINT}" fi -if [ -z "${IMAGE_PERFORMANCE_TESTS}" ]; then - IMAGE_PERFORMANCE_TESTS="ghcr.io/adobe/performance-tests" +if [[ -n "${LOCAL_ENV_APISERVER_AUTH_TOKEN}" ]]; then + APISERVER_AUTH_TOKEN="${LOCAL_ENV_APISERVER_AUTH_TOKEN}" fi -if [ -z "${NETWORK}" ]; then +if [[ -n "${LOCAL_ENV_PERFORMANCE_TEST_TIME}" ]]; then + PERFORMANCE_TEST_TIME="${LOCAL_ENV_PERFORMANCE_TEST_TIME}" +fi + +if [[ -n "${LOCAL_ENV_NETWORK}" ]]; then + NETWORK="${LOCAL_ENV_NETWORK}" +elif [ -z "${NETWORK}" ]; then NETWORK="cluster-registry-net" fi +if [[ -n "${LOCAL_ENV_IMAGE_PERFORMANCE_TESTS}" ]]; then + IMAGE_PERFORMANCE_TESTS="${LOCAL_ENV_IMAGE_PERFORMANCE_TESTS}" +elif [ -z "${IMAGE_PERFORMANCE_TESTS}" ]; then + IMAGE_PERFORMANCE_TESTS="ghcr.io/adobe/performance-tests" +fi + +if [[ -n "${LOCAL_ENV_TAG}" ]]; then + TAG="${LOCAL_ENV_TAG}" +elif [ -z "${TAG}" ]; then + TAG="v$(cat "$(git rev-parse --show-toplevel)/VERSION")-$(git rev-parse --short HEAD)" +fi + docker run --rm --network="${NETWORK}" \ -e APISERVER_ENDPOINT \ -e PERFORMANCE_TEST_TIME \ -e APISERVER_AUTH_TOKEN \ - "${IMAGE_PERFORMANCE_TESTS}:${TAG}" \ No newline at end of file + "${IMAGE_PERFORMANCE_TESTS}:${TAG}"