Skip to content

Commit

Permalink
Add test cover port-forward logic
Browse files Browse the repository at this point in the history
Co-authored-by: Nikola Jokic <jokicnikola07@gmail.com>
  • Loading branch information
wzshiming and nikola-jokic committed Feb 23, 2023
1 parent 1085a04 commit 2a42fd9
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ jobs:
run: |
./hack/e2e-test.sh kwokctl/kwokctl_${{ matrix.kwokctl-runtime }}_restart
- name: Test Port Forward
shell: bash
run: |
./hack/e2e-test.sh kwokctl/kwokctl_${{ matrix.kwokctl-runtime }}_port_forward
- name: Test Benchmark
if: ${{ matrix.os == 'ubuntu-latest' && matrix.kwokctl-runtime != 'kind' }}
shell: bash
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ integration-test:
.PHONY: e2e-test
e2e-test:
@./hack/requirements.sh kubectl buildx kind
@./hack/e2e-test.sh --skip=nerdctl --skip=kind
@./hack/e2e-test.sh \
--skip=nerdctl \
--skip=kind \
--skip=kwokctl_binary_port_forward

## help: Show this help message
.PHONY: help
Expand Down
14 changes: 14 additions & 0 deletions test/kwokctl/fake-pod-in-other-ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Namespace
metadata:
name: other
---
apiVersion: v1
kind: Pod
metadata:
name: fake-pod
namespace: other
spec:
containers:
- name: fake-pod
image: fake
30 changes: 30 additions & 0 deletions test/kwokctl/kwokctl_binary_port_forward.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes 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.

DIR="$(dirname "${BASH_SOURCE[0]}")"

DIR="$(realpath "${DIR}")"

source "${DIR}/helper.sh"

function main() {
local all_releases=("${@}")

test_all "binary" "port_forward" "${all_releases[@]}" || exit 1
}

requirements_for_binary

main $(supported_releases)
30 changes: 30 additions & 0 deletions test/kwokctl/kwokctl_docker_port_forward.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes 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.

DIR="$(dirname "${BASH_SOURCE[0]}")"

DIR="$(realpath "${DIR}")"

source "${DIR}/helper.sh"

function main() {
local all_releases=("${@}")

test_all "docker" "port_forward" "${all_releases[@]}" || exit 1
}

requirements

main $(supported_releases)
30 changes: 30 additions & 0 deletions test/kwokctl/kwokctl_kind_port_forward.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes 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.

DIR="$(dirname "${BASH_SOURCE[0]}")"

DIR="$(realpath "${DIR}")"

source "${DIR}/helper.sh"

function main() {
local all_releases=("${@}")

test_all "kind" "port_forward" "${all_releases[@]}" || exit 1
}

requirements

main $(supported_releases)
30 changes: 30 additions & 0 deletions test/kwokctl/kwokctl_nerdctl_port_forward.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes 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.

DIR="$(dirname "${BASH_SOURCE[0]}")"

DIR="$(realpath "${DIR}")"

source "${DIR}/helper.sh"

function main() {
local all_releases=("${@}")

test_all "nerdctl" "port_forward" "${all_releases[@]}" || exit 1
}

requirements_for_nerdctl

main $(supported_releases)
149 changes: 149 additions & 0 deletions test/kwokctl/kwokctl_port_forward_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes 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.

DIR="$(dirname "${BASH_SOURCE[0]}")"

DIR="$(realpath "${DIR}")"

RELEASES=()

function usage() {
echo "Usage: $0 <kube-version...>"
echo " <kube-version> is the version of kubernetes to test against."
}

function args() {
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
while [[ $# -gt 0 ]]; do
RELEASES+=("${1}")
shift
done
}

function test_create_cluster() {
local release="${1}"
local name="${2}"

KWOK_CONTROLLER_PORT=10247 KWOK_KUBE_VERSION="${release}" kwokctl -v=-4 create cluster --name "${name}" --timeout 10m --wait 10m --quiet-pull --config="${DIR}/port-forward.yaml"
if [[ $? -ne 0 ]]; then
echo "Error: Cluster ${name} creation failed"
exit 1
fi
}

function test_port_forward() {
local name="${1}"
local namespace="${2}"
local target="${3}"
local port="${4}"
local pid
local result
kwokctl --name "${name}" kubectl -n "${namespace}" port-forward "${target}" 8888:"${port}" 2>&1 > /dev/null &
pid=$!

# allow some time for port forward to start
sleep 5
result=$(curl localhost:8888/healthz 2>/dev/null)
kill -INT $pid
if [[ ! "${result}" == "ok" ]]; then
echo "Error: failed to port-forward to ${2}"
return 1
fi
echo "${result}"
}

function test_port_forward_failed() {
local name="${1}"
local namespace="${2}"
local target="${3}"
local port="${4}"
local pid
local result
kwokctl --name "${name}" kubectl -n "${namespace}" port-forward "${target}" 8888:"${port}" 2>&1 > /dev/null &
pid=$!

# allow some time for port forward to start
sleep 5
if curl localhost:8888/healthz 2>/dev/null; then
echo "Error: port-forward to ${2} should fail"
return 1
fi
}

function test_apply_node_and_pod() {
kwokctl --name "${name}" kubectl apply -f "${DIR}/fake-node.yaml"
if [[ $? -ne 0 ]]; then
echo "Error: fake-node apply failed"
exit 1
fi
kwokctl --name "${name}" kubectl apply -f "${DIR}/fake-deployment.yaml"
if [[ $? -ne 0 ]]; then
echo "Error: fake-deployment apply failed"
exit 1
fi
kwokctl --name "${name}" kubectl apply -f "${DIR}/fake-pod-in-other-ns.yaml"
if [[ $? -ne 0 ]]; then
echo "Error: fake-pod apply failed"
exit 1
fi
kwokctl --name "${name}" kubectl wait pod -A --all --for=condition=Ready --timeout=30s
if [[ $? -ne 0 ]]; then
echo "Error: fake-pod wait failed"
echo kubectl get pod -A --all
kubectl get pod -A --all
exit 1
fi
}

function test_delete_cluster() {
local release="${1}"
local name="${2}"
kwokctl delete cluster --name "${name}"
}

function main() {
local failed=()
for release in "${RELEASES[@]}"; do
echo "------------------------------"
echo "Testing port-forward on ${KWOK_RUNTIME} for ${release}"
name="port-forward-cluster-${KWOK_RUNTIME}-${release//./-}"
test_create_cluster "${release}" "${name}" || failed+=("create_cluster_${name}")
test_apply_node_and_pod || failed+=("apply_node_and_pod")
test_port_forward "${name}" other pod/fake-pod "8001" || failed+=("${name}_target_forward")
test_port_forward "${name}" other pod/fake-pod "8002" || failed+=("${name}_command_forward")
test_port_forward_failed "${name}" other pod/fake-pod "8003" || failed+=("${name}_failed")
test_port_forward "${name}" default deploy/fake-pod "8004" || failed+=("${name}_cluster_default_forward")
test_port_forward_failed "${name}" default deploy/fake-pod "8005" || failed+=("${name}_cluster_failed")
test_delete_cluster "${release}" "${name}" || failed+=("delete_cluster_${name}")
done

if [[ "${#failed[@]}" -ne 0 ]]; then
echo "------------------------------"
echo "Error: Some tests failed"
for test in "${failed[@]}"; do
echo " - ${test}"
done
echo kubectl get pod -A --all
kubectl get pod -A --all
exit 1
fi
}

args "$@"

main
43 changes: 43 additions & 0 deletions test/kwokctl/port-forward.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
kind: PortForward
apiVersion: kwok.x-k8s.io/v1alpha1
metadata:
name: fake-pod
namespace: other
spec:
forwards:
# match the port 8001 forwards to the targetAddress:targetPort
- ports:
- 8001
target:
port: 10247
address: localhost

# match the port 8002 forwards with the stdin/stdout of nc
- ports:
- 8002
command:
- nc
- localhost
- "10247"

---

kind: ClusterPortForward
apiVersion: kwok.x-k8s.io/v1alpha1
metadata:
name: cluster-port-forward-rules
spec:
selector:
matchNamespaces:
- default
forwards:
# invalid port
- ports:
- 8005
target:
port: 0
address: localhost

- target:
port: 10247
address: localhost

0 comments on commit 2a42fd9

Please sign in to comment.