From 77d3dc6bf14aea007960d4f3cdcf27d86d644050 Mon Sep 17 00:00:00 2001 From: Frank Budinsky Date: Tue, 26 May 2020 15:57:19 -0400 Subject: [PATCH] Fix mirroring test (#7399) * Fix mirroring test * fix typo * fixes * export vars * lint --- .../traffic-management/mirroring/index.md | 8 ++--- .../traffic-management/mirroring/snips.sh | 8 ++--- tests/README.md | 12 +++---- tests/tests.mk | 1 + tests/trafficmanagement/scripts/mirroring.sh | 35 ++++++++++++------- tests/util/verify.sh | 6 ++-- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/content/en/docs/tasks/traffic-management/mirroring/index.md b/content/en/docs/tasks/traffic-management/mirroring/index.md index 961cf6df11..6634c1d15e 100644 --- a/content/en/docs/tasks/traffic-management/mirroring/index.md +++ b/content/en/docs/tasks/traffic-management/mirroring/index.md @@ -198,13 +198,13 @@ log entries for `v1` and none for `v2`: {{< text bash >}} $ export V1_POD=$(kubectl get pod -l app=httpbin,version=v1 -o jsonpath={.items..metadata.name}) - $ kubectl logs -f "$V1_POD" -c httpbin + $ kubectl logs "$V1_POD" -c httpbin 127.0.0.1 - - [07/Mar/2018:19:02:43 +0000] "GET /headers HTTP/1.1" 200 321 "-" "curl/7.35.0" {{< /text >}} {{< text bash >}} $ export V2_POD=$(kubectl get pod -l app=httpbin,version=v2 -o jsonpath={.items..metadata.name}) - $ kubectl logs -f "$V2_POD" -c httpbin + $ kubectl logs "$V2_POD" -c httpbin {{< /text >}} @@ -255,13 +255,13 @@ log entries for `v1` and none for `v2`: created in `v2` are the mirrored requests that are actually going to `v1`. {{< text bash >}} - $ kubectl logs -f "$V1_POD" -c httpbin + $ kubectl logs "$V1_POD" -c httpbin 127.0.0.1 - - [07/Mar/2018:19:02:43 +0000] "GET /headers HTTP/1.1" 200 321 "-" "curl/7.35.0" 127.0.0.1 - - [07/Mar/2018:19:26:44 +0000] "GET /headers HTTP/1.1" 200 321 "-" "curl/7.35.0" {{< /text >}} {{< text bash >}} - $ kubectl logs -f "$V2_POD" -c httpbin + $ kubectl logs "$V2_POD" -c httpbin 127.0.0.1 - - [07/Mar/2018:19:26:44 +0000] "GET /headers HTTP/1.1" 200 361 "-" "curl/7.35.0" {{< /text >}} diff --git a/content/en/docs/tasks/traffic-management/mirroring/snips.sh b/content/en/docs/tasks/traffic-management/mirroring/snips.sh index b75874c878..e5ee2b2430 100644 --- a/content/en/docs/tasks/traffic-management/mirroring/snips.sh +++ b/content/en/docs/tasks/traffic-management/mirroring/snips.sh @@ -174,7 +174,7 @@ ENDSNIP snip_creating_a_default_routing_policy_3() { export V1_POD=$(kubectl get pod -l app=httpbin,version=v1 -o jsonpath={.items..metadata.name}) -kubectl logs -f "$V1_POD" -c httpbin +kubectl logs "$V1_POD" -c httpbin } ! read -r -d '' snip_creating_a_default_routing_policy_3_out <<\ENDSNIP @@ -183,7 +183,7 @@ ENDSNIP snip_creating_a_default_routing_policy_4() { export V2_POD=$(kubectl get pod -l app=httpbin,version=v2 -o jsonpath={.items..metadata.name}) -kubectl logs -f "$V2_POD" -c httpbin +kubectl logs "$V2_POD" -c httpbin } ! read -r -d '' snip_creating_a_default_routing_policy_4_out <<\ENDSNIP @@ -217,7 +217,7 @@ kubectl exec "${SLEEP_POD}" -c sleep -- curl -s http://httpbin:8000/headers } snip_mirroring_traffic_to_v2_3() { -kubectl logs -f "$V1_POD" -c httpbin +kubectl logs "$V1_POD" -c httpbin } ! read -r -d '' snip_mirroring_traffic_to_v2_3_out <<\ENDSNIP @@ -226,7 +226,7 @@ kubectl logs -f "$V1_POD" -c httpbin ENDSNIP snip_mirroring_traffic_to_v2_4() { -kubectl logs -f "$V2_POD" -c httpbin +kubectl logs "$V2_POD" -c httpbin } ! read -r -d '' snip_mirroring_traffic_to_v2_4_out <<\ENDSNIP diff --git a/tests/README.md b/tests/README.md index b376941f52..61b5c92b44 100644 --- a/tests/README.md +++ b/tests/README.md @@ -52,7 +52,7 @@ To write an `istio.io` test, follow these steps: 1. Create Go boilderplate that will invoke your test bash script using the following pattern: ```golang - package + package import ( "testing" @@ -62,19 +62,19 @@ To write an `istio.io` test, follow these steps: "istio.io/istio.io/pkg/test/istioio" ) - func Test(t *testing.T) { + func Test(t *testing.T) { framework. NewTest(t). - Run(istioio.NewBuilder(""). Add(istioio.Script{ - Input: istioio.Path("scripts/.sh"), + Input: istioio.Path("scripts/.sh"), }). Defer(istioio.Script{ Input: istioio.Inline{ FileName: "cleanup.sh", Value: ` set +e # ignore cleanup errors - source ${REPO_ROOT}/content/en/docs//snips.sh + source ${REPO_ROOT}/content/en/docs//snips.sh `, }, }). @@ -164,7 +164,7 @@ The framework includes the following built-in verify functions: This function is useful for comparing the output of commands that include some run-specific values in the output (e.g., `kubectl get pods`), or when whitespace in the output may be different. -Everery `verify_` function has a corresponding `_run_and_verify_` function that +Every `verify_` function has a corresponding `_run_and_verify_` function that first runs a function and then compares the result to the expected output. The specified function will be retried 5 times, with exponential backoff, before failing: diff --git a/tests/tests.mk b/tests/tests.mk index 6ba81e8e57..b0c86f5ddb 100644 --- a/tests/tests.mk +++ b/tests/tests.mk @@ -45,6 +45,7 @@ test.kube.%: init | $(JUNIT_REPORT) PATH=${PATH}:${ISTIO_OUT} $(GO) test -p 1 ${T} ./tests/$(subst .,/,$*)/... -timeout 30m \ --istio.test.select -postsubmit,-flaky \ --istio.test.env kube \ + --log_output_level=script:debug \ ${_INTEGRATION_TEST_FLAGS} \ 2>&1 | tee >($(JUNIT_REPORT) > $(JUNIT_OUT)) diff --git a/tests/trafficmanagement/scripts/mirroring.sh b/tests/trafficmanagement/scripts/mirroring.sh index f067e4c655..fa5a587008 100644 --- a/tests/trafficmanagement/scripts/mirroring.sh +++ b/tests/trafficmanagement/scripts/mirroring.sh @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +# shellcheck disable=SC2155 + set -e set -u set -o pipefail @@ -33,32 +35,39 @@ snip_before_you_begin_3 snip_before_you_begin_4 # wait for deployments -#sample_wait_for_deployment default httpbin-v1 -#sample_wait_for_deployment default httpbin-v2 -#sample_wait_for_deployment default sleep +sample_wait_for_deployment default httpbin-v1 +sample_wait_for_deployment default httpbin-v2 +sample_wait_for_deployment default sleep snip_creating_a_default_routing_policy_1 # wait for virtual service #istioctl experimental wait --for=distribution VirtualService httpbin.default -#sleep 5s +sleep 5s -kubectl get all --all-namespaces +_run_and_verify_contains snip_creating_a_default_routing_policy_2 "headers" -#_run_and_verify_contains snip_creating_a_default_routing_policy_2 "headers" - -#_run_and_verify_contains snip_creating_a_default_routing_policy_3 "GET /headers HTTP/1.1" +_run_and_verify_contains snip_creating_a_default_routing_policy_3 "GET /headers HTTP/1.1" +# No point in retrying for "not contains". TODO: some kind of _verify_worked_and_not_contains function #_run_and_verify_not_contains snip_creating_a_default_routing_policy_4 "GET /headers HTTP/1.1" +out=$(snip_creating_a_default_routing_policy_4 2>&1) +_verify_not_contains "$out" "GET /headers HTTP/1.1" "snip_creating_a_default_routing_policy_4" -#snip_mirroring_traffic_to_v2_1 +snip_mirroring_traffic_to_v2_1 # wait for virtual service #istioctl experimental wait --for=distribution VirtualService httpbin.default -#sleep 5s +sleep 5s -#snip_mirroring_traffic_to_v2_2 +# Set environment variables. TODO: why didn't the exports from snip_creating_a_default_routing_policy_2/3/4 take? +export SLEEP_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name}) +export V1_POD=$(kubectl get pod -l app=httpbin,version=v1 -o jsonpath={.items..metadata.name}) +export V2_POD=$(kubectl get pod -l app=httpbin,version=v2 -o jsonpath={.items..metadata.name}) -#_run_and_verify_contains snip_mirroring_traffic_to_v2_3 "GET /headers HTTP/1.1" +snip_mirroring_traffic_to_v2_2 -#_run_and_verify_contains snip_mirroring_traffic_to_v2_3 "GET /headers HTTP/1.1" +# TODO: This should check for 2 lines with the GET request +_run_and_verify_contains snip_mirroring_traffic_to_v2_3 "GET /headers HTTP/1.1" + +_run_and_verify_contains snip_mirroring_traffic_to_v2_3 "GET /headers HTTP/1.1" diff --git a/tests/util/verify.sh b/tests/util/verify.sh index 8192876ec6..5cd9cfac35 100644 --- a/tests/util/verify.sh +++ b/tests/util/verify.sh @@ -40,7 +40,6 @@ __cmp_contains() { local expected=$2 if [[ "$out" != *"$expected"* ]]; then - echo "false" return 1 fi @@ -212,10 +211,13 @@ __verify_with_retry() { # Run the command. out=$($func 2>&1) + $cmp_func "$out" "$expected" + local retval="$?" + # Restore the "errexit" state. eval "$errexit_state" - if $cmp_func "$out" "$expected"; then + if [[ "$retval" -eq 0 ]]; then return fi