|
1 | 1 | #!/usr/bin/env bats |
2 | 2 |
|
3 | | -@test "Create a tenant Kubernetes control plane" { |
| 3 | +run_kubernetes_test() { |
| 4 | + local version_expr="$1" |
| 5 | + local test_name="$2" |
| 6 | + local port="$3" |
| 7 | + local k8s_version=$(yq "$version_expr" packages/apps/kubernetes/files/versions.yaml) |
| 8 | + |
4 | 9 | kubectl apply -f - <<EOF |
5 | 10 | apiVersion: apps.cozystack.io/v1alpha1 |
6 | 11 | kind: Kubernetes |
7 | 12 | metadata: |
8 | | - name: test |
| 13 | + name: "${test_name}" |
9 | 14 | namespace: tenant-test |
10 | 15 | spec: |
11 | 16 | addons: |
@@ -60,13 +65,49 @@ spec: |
60 | 65 | roles: |
61 | 66 | - ingress-nginx |
62 | 67 | storageClass: replicated |
| 68 | + version: "${k8s_version}" |
63 | 69 | EOF |
| 70 | + # Wait for the tenant-test namespace to be active |
64 | 71 | kubectl wait namespace tenant-test --timeout=20s --for=jsonpath='{.status.phase}'=Active |
65 | | - timeout 10 sh -ec 'until kubectl get kamajicontrolplane -n tenant-test kubernetes-test; do sleep 1; done' |
66 | | - kubectl wait --for=condition=TenantControlPlaneCreated kamajicontrolplane -n tenant-test kubernetes-test --timeout=4m |
67 | | - kubectl wait tcp -n tenant-test kubernetes-test --timeout=2m --for=jsonpath='{.status.kubernetesResources.version.status}'=Ready |
68 | | - kubectl wait deploy --timeout=4m --for=condition=available -n tenant-test kubernetes-test kubernetes-test-cluster-autoscaler kubernetes-test-kccm kubernetes-test-kcsi-controller |
69 | | - kubectl wait machinedeployment kubernetes-test-md0 -n tenant-test --timeout=1m --for=jsonpath='{.status.replicas}'=2 |
70 | | - kubectl wait machinedeployment kubernetes-test-md0 -n tenant-test --timeout=10m --for=jsonpath='{.status.v1beta2.readyReplicas}'=2 |
71 | | - kubectl -n tenant-test delete kuberneteses.apps.cozystack.io test |
| 72 | + |
| 73 | + # Wait for the Kamaji control plane to be created (retry for up to 10 seconds) |
| 74 | + timeout 10 sh -ec 'until kubectl get kamajicontrolplane -n tenant-test kubernetes-'"${test_name}"'; do sleep 1; done' |
| 75 | + |
| 76 | + # Wait for the tenant control plane to be fully created (timeout after 4 minutes) |
| 77 | + kubectl wait --for=condition=TenantControlPlaneCreated kamajicontrolplane -n tenant-test kubernetes-${test_name} --timeout=4m |
| 78 | + |
| 79 | + # Wait for Kubernetes resources to be ready (timeout after 2 minutes) |
| 80 | + kubectl wait tcp -n tenant-test kubernetes-${test_name} --timeout=2m --for=jsonpath='{.status.kubernetesResources.version.status}'=Ready |
| 81 | + |
| 82 | + # Wait for all required deployments to be available (timeout after 4 minutes) |
| 83 | + kubectl wait deploy --timeout=4m --for=condition=available -n tenant-test kubernetes-${test_name} kubernetes-${test_name}-cluster-autoscaler kubernetes-${test_name}-kccm kubernetes-${test_name}-kcsi-controller |
| 84 | + |
| 85 | + # Wait for the machine deployment to scale to 2 replicas (timeout after 1 minute) |
| 86 | + kubectl wait machinedeployment kubernetes-${test_name}-md0 -n tenant-test --timeout=1m --for=jsonpath='{.status.replicas}'=2 |
| 87 | + |
| 88 | + # Get the admin kubeconfig and save it to a file |
| 89 | + kubectl get secret kubernetes-${test_name}-admin-kubeconfig -ojsonpath='{.data.super-admin\.conf}' -n tenant-test | base64 -d > tenantkubeconfig |
| 90 | + |
| 91 | + # Update the kubeconfig to use localhost for the API server |
| 92 | + yq -i ".clusters[0].cluster.server = \"https://localhost:${port}\"" tenantkubeconfig |
| 93 | + |
| 94 | + # Set up port forwarding to the Kubernetes API server for a 40 second timeout |
| 95 | + bash -c 'timeout 40s kubectl port-forward service/kubernetes-'"${test_name}"' -n tenant-test '"${port}"':6443 > /dev/null 2>&1 &' |
| 96 | + |
| 97 | + # Verify the Kubernetes version matches what we expect (retry for up to 20 seconds) |
| 98 | + timeout 20 sh -ec 'until kubectl --kubeconfig tenantkubeconfig version 2>/dev/null | grep -Fq "Server Version: ${k8s_version}"; do sleep 5; done' |
| 99 | + |
| 100 | + # Wait for all machine deployment replicas to be ready (timeout after 10 minutes) |
| 101 | + kubectl wait machinedeployment kubernetes-${test_name}-md0 -n tenant-test --timeout=10m --for=jsonpath='{.status.v1beta2.readyReplicas}'=2 |
| 102 | + |
| 103 | + # Clean up by deleting the Kubernetes resource |
| 104 | + kubectl -n tenant-test delete kuberneteses.apps.cozystack.io $test_name |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +@test "Create a tenant Kubernetes control plane with latest version" { |
| 109 | + run_kubernetes_test 'keys | sort_by(.) | .[-1]' 'test-latest-version' '59991' |
| 110 | +} |
| 111 | +@test "Create a tenant Kubernetes control plane with previous version" { |
| 112 | + run_kubernetes_test 'keys | sort_by(.) | .[-2]' 'test-previous-version' '59992' |
72 | 113 | } |
0 commit comments