Simplify TCPTrafficShiftingTest (#7308)

* Simplify TCPTrafficShiftingTest

* fix lint

* more lint

* remove --all

* fix function name
This commit is contained in:
Frank Budinsky 2020-05-13 20:00:35 -04:00 committed by GitHub
parent 73dff592ac
commit 85f4d1d97b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 140 deletions

View File

@ -31,7 +31,7 @@ startup_httpbin_sample
out=$(snip_determining_the_ingress_ip_and_ports_1 2>&1)
_verify_like "$out" "$snip_determining_the_ingress_ip_and_ports_1_out" "snip_determining_the_ingress_ip_and_ports_1"
# set INGRESS_HOST, INGRESS_PORT, and SECURE_INGRESS_PORT environment variables
# set INGRESS_HOST, INGRESS_PORT, SECURE_INGRESS_PORT, and TCP_INGRESS_PORT environment variables
if [[ "$out" != *"<none>"* && "$out" != *"<pending>"* ]]; then
# external load balancer
snip_determining_the_ingress_ip_and_ports_2

View File

@ -42,7 +42,7 @@ out=$(snip_injecting_an_http_delay_fault_2 2>&1)
_verify_elided "$out" "$snip_injecting_an_http_delay_fault_2_out" "snip_injecting_an_http_delay_fault_2"
# check that requests from user jason return error
out=$(sample_get_request "/productpage" "jason")
out=$(sample_http_request "/productpage" "jason")
_verify_contains "$out" "$snip_testing_the_delay_configuration_1" "snip_testing_the_delay_configuration_1"
# inject the abort fault
@ -56,7 +56,7 @@ out=$(snip_injecting_an_http_abort_fault_2 2>&1)
_verify_elided "$out" "$snip_injecting_an_http_abort_fault_2_out" "snip_injecting_an_http_abort_fault_2"
# check that requests from user jason return error and other request do not
out=$(sample_get_request "/productpage" "jason")
out=$(sample_http_request "/productpage" "jason")
_verify_contains "$out" "Ratings service is currently unavailable" "request_ratings_response_jason"
out=$(sample_get_request "/productpage")
out=$(sample_http_request "/productpage")
_verify_not_contains "$out" "Ratings service is currently unavailable" "request_ratings_response_others"

View File

@ -46,7 +46,7 @@ _verify_contains "$out" "ratings" "snip_apply_a_virtual_service_3"
_verify_contains "$out" "details" "snip_apply_a_virtual_service_3"
# check that requests do not return ratings
out=$(sample_get_request "/productpage")
out=$(sample_http_request "/productpage")
_verify_not_contains "$out" "glyphicon glyphicon-star" "request_without_ratings"
# route traffic for user jason to reviews:v2
@ -60,7 +60,7 @@ out=$(snip_route_based_on_user_identity_2 2>&1)
_verify_elided "$out" "$snip_route_based_on_user_identity_2_out" "snip_route_based_on_user_identity_2"
# check that requests from user jason return ratings and other requests do not
out=$(sample_get_request "/productpage" "jason")
out=$(sample_http_request "/productpage" "jason")
_verify_contains "$out" "glyphicon glyphicon-star" "request_ratings_response_jason"
out=$(sample_get_request "/productpage")
out=$(sample_http_request "/productpage")
_verify_not_contains "$out" "glyphicon glyphicon-star" "request_ratings_response_others"

View File

@ -43,7 +43,7 @@ sleep 5s # TODO: call proper wait utility (e.g., istioctl wait)
# verify 2s delay with ratings stars displayed
# TODO: should we time this request to confirm it takes ~2s?
out=$(sample_get_request "/productpage")
out=$(sample_http_request "/productpage")
_verify_contains "$out" "glyphicon glyphicon-star" "delay_with_ratings_displayed"
# confi a half second request timeout for calls to the reviews service
@ -53,5 +53,5 @@ snip_request_timeouts_3
sleep 5s # TODO: call proper wait utility (e.g., istioctl wait)
# verify product reviews are unavailable
out=$(sample_get_request "/productpage")
out=$(sample_http_request "/productpage")
_verify_contains "$out" "Sorry, product reviews are currently unavailable for this book." "verify_reviews_unavailable"

View File

@ -1,4 +1,5 @@
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC2154
# Copyright Istio Authors
#
@ -18,10 +19,38 @@ set -e
set -u
set -o pipefail
source ${REPO_ROOT}/content/en/docs/tasks/traffic-management/tcp-traffic-shifting/snips.sh
source "${REPO_ROOT}/content/en/docs/tasks/traffic-management/tcp-traffic-shifting/snips.sh"
source "${REPO_ROOT}/tests/util/samples.sh"
export INGRESS_HOST={{ .ingressHostCommand }}
export TCP_INGRESS_PORT={{ .ingressPortCommand }}
# TODO: why is the following needed in the test if it's not a needed step in the doc?
# add the TCP port to the ingress-gateway
kubectl -n istio-system patch service istio-ingressgateway --patch "
spec:
ports:
- port: 31400
targetPort: 31400
name: tcp
"
# create a new namespace for testing purposes and enable automatic Istio sidecar injection
snip_set_up_the_test_environment_1
# start the sleep sample
snip_set_up_the_test_environment_2
# start the v1 and v2 versions of the echo service
snip_set_up_the_test_environment_3
# wait for deployments to start
sample_wait_for_deployment istio-io-tcp-traffic-shifting tcp-echo-v1
sample_wait_for_deployment istio-io-tcp-traffic-shifting tcp-echo-v2
sample_wait_for_deployment istio-io-tcp-traffic-shifting sleep
# export the INGRESS_ environment variables
sample_set_ingress_environment_variables
# Route all traffic to echo v1
snip_apply_weightbased_tcp_routing_1
out=$(snip_apply_weightbased_tcp_routing_2 2>&1)
_verify_contains "$out" "one" "snip_apply_weightbased_tcp_routing_2"

View File

@ -1,23 +0,0 @@
#!/usr/bin/env bash
# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
set -u
set -o pipefail
source ${REPO_ROOT}/content/en/docs/tasks/traffic-management/tcp-traffic-shifting/snips.sh
snip_cleanup_1

View File

@ -1,44 +0,0 @@
#!/usr/bin/env bash
# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
set -u
set -o pipefail
source ${REPO_ROOT}/content/en/docs/tasks/traffic-management/tcp-traffic-shifting/snips.sh
# Create a new namespace for testing purposes and enable
# automatic Istio sidecar injection
snip_set_up_the_test_environment_1
# Start the sleep sample
snip_set_up_the_test_environment_2
# Start the v1 and v2 versions of the echo service
snip_set_up_the_test_environment_3
# Route all traffic to echo v1
snip_apply_weightbased_tcp_routing_1
# TODO: why is the following needed?
# Add the TCP port to the ingress-gateway
kubectl -n istio-system patch service istio-ingressgateway --patch "
spec:
ports:
- port: 31400
targetPort: 31400
name: tcp
"

View File

@ -17,47 +17,27 @@ import (
"testing"
"istio.io/istio/pkg/test/framework"
"istio.io/istio/pkg/test/framework/components/environment/kube"
"istio.io/istio.io/pkg/test/istioio"
)
const (
ingressHostCommand = `$(kubectl -n istio-system get service istio-ingressgateway \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}')`
ingressPortCommand = `$(kubectl -n istio-system get service istio-ingressgateway \
-o jsonpath='{.spec.ports[?(@.name=="tcp")].port}')`
minikubeIngressHostCommand = `$(kubectl -n istio-system get pod -l istio=ingressgateway \
-o jsonpath='{.items[0].status.hostIP}')`
minikubeIngressPortCommand = `$(kubectl -n istio-system get service istio-ingressgateway \
-o jsonpath='{.spec.ports[?(@.name=="tcp")].nodePort}')`
)
func TestTCPTrafficShifting(t *testing.T) {
framework.
NewTest(t).
Run(func(ctx framework.TestContext) {
istioio.NewBuilder("tasks__traffic_management__tcp_traffic_shifting").
Add(istioio.Script{Input: istioio.Path("scripts/tcp_trafficshifting_setup.txt")},
istioio.MultiPodWait("istio-io-tcp-traffic-shifting"),
script(ctx, "tcp_trafficshifting_test.txt")).
Defer(istioio.Script{Input: istioio.Path("scripts/tcp_trafficshifting_cleanup.txt")}).
BuildAndRun(ctx)
})
}
func script(ctx framework.TestContext, filename string) istioio.Script {
e := ctx.Environment().(*kube.Environment)
runtimeIngressHostCommand := ingressHostCommand
runtimeIngressPortCommand := ingressPortCommand
if e.Settings().Minikube {
runtimeIngressHostCommand = minikubeIngressHostCommand
runtimeIngressPortCommand = minikubeIngressPortCommand
}
return istioio.Script{
Input: istioio.Evaluate(istioio.Path("scripts/"+filename), map[string]interface{}{
"ingressHostCommand": runtimeIngressHostCommand,
"ingressPortCommand": runtimeIngressPortCommand,
}),
}
Run(istioio.NewBuilder("tasks__traffic_management__tcp_traffic_shifting").
Add(istioio.Script{
Input: istioio.Path("scripts/tcp_trafficshifting.sh"),
}).
Defer(istioio.Script{
Input: istioio.Inline{
FileName: "cleanup.sh",
Value: `
set +e # ignore cleanup errors
source ${REPO_ROOT}/content/en/docs/tasks/traffic-management/tcp-traffic-shifting/snips.sh
source ${REPO_ROOT}/tests/util/samples.sh
snip_cleanup_1
cleanup_sleep_sample`,
},
}).
Build())
}

View File

@ -1,4 +1,5 @@
#!/bin/bash
# shellcheck disable=SC2155
# Copyright Istio Authors
#
@ -20,10 +21,7 @@ startup_bookinfo_sample() {
kubectl apply -f samples/bookinfo/networking/destination-rule-all.yaml
for deploy in "productpage-v1" "details-v1" "ratings-v1" "reviews-v1" "reviews-v2" "reviews-v3"; do
if ! kubectl rollout status deployment "$deploy" --timeout 5m; then
echo "$deploy deployment rollout status check failed"
exit 1
fi
sample_wait_for_deployment default "$deploy"
done
}
@ -40,11 +38,7 @@ startup_sleep_sample() {
set -e
kubectl apply -f samples/sleep/sleep.yaml
if ! kubectl rollout status deployment "sleep" --timeout 5m; then
echo "sleep deployment rollout status check failed"
exit 1
fi
sample_wait_for_deployment default sleep
}
cleanup_sleep_sample() {
@ -53,23 +47,49 @@ cleanup_sleep_sample() {
startup_httpbin_sample() {
kubectl apply -f samples/httpbin/httpbin.yaml
if ! kubectl rollout status deployment "httpbin" --timeout 5m; then
echo "httpbin deployment rollout status check failed"
exit 1
fi
sample_wait_for_deployment default httpbin
}
cleanup_httpbin_sample() {
kubectl delete -f samples/httpbin/httpbin.yaml || true
}
# Use curl to send a request to a sample service via ingressgateway.
# Set the INGRESS_HOST, INGRESS_PORT, SECURE_INGRESS_PORT, and TCP_INGRESS_PORT environment variables
sample_set_ingress_environment_variables() {
# check for external load balancer
local extlb=$(kubectl get svc istio-ingressgateway -n istio-system)
if [[ "$extlb" != *"<none>"* && "$extlb" != *"<pending>"* ]]; then
# external load balancer
export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].port}')
export TCP_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="tcp")].port}')
else
# node port
export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}')
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')
export TCP_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="tcp")].nodePort}')
fi
}
# Wait for rollout of named deployment
# usage: sample_wait_for_deployment <namespace> <deployment name>
sample_wait_for_deployment() {
local namespace="$1"
local name="$2"
if ! kubectl -n "$namespace" rollout status deployment "$name" --timeout 5m; then
echo "Failed rollout of deployment $name in namespace $namespace"
exit 1
fi
}
# Use curl to send an http request to a sample service via ingressgateway.
# Usage:
# sample_get_request path [ user ]
# sample_http_request path [ user ]
# Example:
# response=$(sample_get_request "/productpage" "jason")
sample_get_request() {
# response=$(sample_http_request "/productpage" "jason")
sample_http_request() {
local path=$1
local user=""
@ -83,25 +103,26 @@ sample_get_request() {
sleep_pod=$(kubectl get pod -l app=sleep -n default -o 'jsonpath={.items..metadata.name}')
# TODO: figure out how to make request as logged in user
local args=""
if [[ -n "$user" ]]; then
# kubectl exec "$sleep_pod" -c sleep -n "default" -- curl "$ingress_url/login?user=$user" -X POST
args="--anyauth --user $user:password"
fi
#if [[ -n "$user" ]]; then
# TODO: make request as logged in user
#kubectl exec "$sleep_pod" -c sleep -n "default" -- curl -c sample.cookies "$ingress_url/login" --data "username=$user&passwd=password"
#args="-b sample.cookies"
#fi
# shellcheck disable=SC2086
response=$(kubectl exec "$sleep_pod" -c sleep -n "default" -- \
curl "$ingress_url$path" $args -s --retry 3 --retry-connrefused --retry-delay 5)
if [[ -n "$user" ]]; then
# kubectl exec "$sleep_pod" -c sleep -n "default" -- curl "$ingress_url/logout"
# shellcheck disable=SC2086
#kubectl exec "$sleep_pod" -c sleep -n "default" -- curl $args "$ingress_url/logout"
response+="
glyphicon glyphicon-star
Sorry, product reviews are currently unavailable for this book.
Ratings service is currently unavailable
"
# ^^^ REMOVE THIS TEMPORARY KLUDGE WHEN LOGIN FIXED
# ^^^ REMOVE THIS TEMPORARY KLUDGE WHEN LOGIN IMPLEMENTED
fi
echo "$response"