Skip to content

Commit f9ab640

Browse files
authored
Merge pull request #4118 from AkihiroSuda/test-k8s
CI: test Kubernetes port forwarder
2 parents 1c53f93 + f636521 commit f9ab640

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,19 @@ jobs:
354354
template: templates/default.yaml
355355
- name: "Run BATS integration tests"
356356
run: make bats
357+
- name: Cache image used by templates/k8s.yaml
358+
uses: ./.github/actions/setup_cache_for_template
359+
with:
360+
template: templates/k8s.yaml
361+
- name: "Run BATS k8s tests"
362+
# Wish we could use BATS_TEST_RETRIES=3 as an environment variable here,
363+
# but bats does not seem to support it.
364+
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
365+
with:
366+
timeout_minutes: 30
367+
retry_on: error
368+
max_attempts: 3
369+
command: ./hack/bats/lib/bats-core/bin/bats --timing ./hack/bats/extras/k8s.bats
357370

358371
colima:
359372
name: "Colima tests (QEMU, Linux host)"

hack/bats/extras/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Extra tests
2+
3+
The extra tests located in this directory are not automatically executed via `make bats`.
4+
5+
Some tests are executed on the CI, some ones are not.
6+
Refer to the configuration of the GitHub Actions to see what tests are executed.

hack/bats/extras/k8s.bats

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This test verifies that a Kubernetes cluster can be started and that the single node is ready.
5+
6+
load "../helpers/load"
7+
8+
: "${TEMPLATE:=k8s}"
9+
10+
# Instance names are "${NAME}-0", "${NAME}-1", ...
11+
NAME="k8s"
12+
13+
get_num_nodes() {
14+
local nodes=0
15+
for tag in "${BATS_TEST_TAGS[@]}"; do
16+
if [[ $tag =~ ^nodes:([0-9]+)$ ]]; then
17+
nodes="${BASH_REMATCH[1]}"
18+
fi
19+
done
20+
if [[ $nodes -eq 0 ]]; then
21+
echo >&2 "nodes:N tag is required"
22+
exit 1
23+
fi
24+
echo "$nodes"
25+
}
26+
27+
local_setup() {
28+
local nodes=$(get_num_nodes)
29+
for ((i=0; i<nodes; i++)); do
30+
limactl delete --force "${NAME}-$i" || :
31+
limactl start --tty=false --name "${NAME}-$i" "template:${TEMPLATE}" 3>&- 4>&-
32+
# NOTE: No support for multi-node clusters yet.
33+
done
34+
for node in $(k get node -o name); do
35+
k wait --timeout=5m --for=condition=ready "${node}"
36+
done
37+
}
38+
39+
local_teardown() {
40+
local nodes=$(get_num_nodes)
41+
for ((i=0; i<nodes; i++)); do
42+
limactl delete --force "${NAME}-$i" || :
43+
done
44+
}
45+
46+
k() {
47+
# The host home directory is not mounted in the case of k8s.
48+
limactl shell --workdir=/ "${NAME}-0" -- kubectl "$@"
49+
}
50+
51+
# bats test_tags=nodes:1
52+
@test 'Single-node' {
53+
k create deployment nginx --image="${TEST_CONTAINER_IMAGES["nginx"]}"
54+
k rollout status deployment nginx --timeout 60s
55+
k create service nodeport nginx --node-port=31080 --tcp=80:80
56+
run curl --fail --silent --show-error --retry 30 --retry-all-errors http://localhost:31080
57+
assert_success
58+
assert_output --partial "Welcome to nginx"
59+
# TODO: support UDP
60+
k delete service nginx
61+
k delete deployment nginx
62+
}
63+
64+
# TODO: add a test for multi-node

hack/bats/helpers/load.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ teardown() {
6565
assert_output_lines_count() {
6666
assert_equal "${#lines[@]}" "$1"
6767
}
68+
69+
# Use GHCR and ECR to avoid hitting Docker Hub rate limit.
70+
# NOTE: keep this list in sync with hack/test-templates.sh .
71+
declare -A -g TEST_CONTAINER_IMAGES=(
72+
["nginx"]="ghcr.io/stargz-containers/nginx:1.19-alpine-org"
73+
)

templates/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Container orchestration:
5757
- [`faasd`](./faasd.yaml): [Faasd](https://docs.openfaas.com/deployment/edge/)
5858
- [`k0s`](./k0s.yaml): [k0s](https://k0sproject.io/) Zero Friction Kubernetes
5959
- [`k3s`](./k3s.yaml): Kubernetes via k3s
60-
- [`k8s`](./k8s.yaml): Kubernetes via kubeadm
60+
- [`k8s`](./k8s.yaml): Kubernetes via kubeadm
6161
- [`experimental/u7s`](./experimental/u7s.yaml): [Usernetes](https://github.com/rootless-containers/usernetes): Rootless Kubernetes
6262

6363
Optional feature enablers:

0 commit comments

Comments
 (0)