github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/k8s-port-forward.bats (about)

     1  #!/usr/bin/env bats
     2  #
     3  # Copyright (c) 2019 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  
     7  load "${BATS_TEST_DIRNAME}/../../.ci/lib.sh"
     8  load "${BATS_TEST_DIRNAME}/tests_common.sh"
     9  source "/etc/os-release" || source "/usr/lib/os-release"
    10  
    11  issue="https://github.com/kata-containers/runtime/issues/1834"
    12  
    13  setup() {
    14  	skip "test not working see: ${issue}"
    15  	get_pod_config_dir
    16  }
    17  
    18  @test "Port forwarding" {
    19  	skip "test not working see: ${issue}"
    20  	deployment_name="redis-master"
    21  
    22  	# Create deployment
    23  	kubectl apply -f "${pod_config_dir}/redis-master-deployment.yaml"
    24  
    25  	# Check deployment
    26  	kubectl wait --for=condition=Available --timeout=$timeout deployment/"$deployment_name"
    27  	kubectl expose deployment/"$deployment_name"
    28  
    29  	# Get pod name
    30  	pod_name=$(kubectl get pods --output=jsonpath={.items..metadata.name})
    31  	kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
    32  
    33  	# View replicaset
    34  	kubectl get rs
    35  
    36  	# Create service
    37  	kubectl apply -f "${pod_config_dir}/redis-master-service.yaml"
    38  
    39  	# Check service
    40  	kubectl get svc | grep redis
    41  
    42  	# Check redis service
    43  	port_redis=$(kubectl get pods $pod_name --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}')
    44  
    45  	# Verify that redis is running in the pod and listening on port
    46  	port=6379
    47  	[ "$port_redis" -eq "$port" ]
    48  
    49  	# Forward a local port to a port on the pod
    50  	(2&>1 kubectl port-forward "$pod_name" 7000:"$port"> /dev/null) &
    51  
    52  	# Run redis-cli
    53  	retries="10"
    54  	ok="0"
    55  
    56  	for _ in $(seq 1 "$retries"); do
    57  		if sudo -E redis-cli -p 7000 ping | grep -q "PONG" ; then
    58  			ok="1"
    59  			break;
    60  		fi
    61  		sleep 1
    62  	done
    63  
    64  	[ "$ok" -eq "1" ]
    65  }
    66  
    67  teardown() {
    68  	skip "test not working see: ${issue}"
    69  	kubectl delete -f "${pod_config_dir}/redis-master-deployment.yaml"
    70  	kubectl delete -f "${pod_config_dir}/redis-master-service.yaml"
    71  }