|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Smoke test for the ServiceExposure controller (network.cozystack.io). |
| 4 | +# |
| 5 | +# This exercises the externalIPs backend end-to-end on a live cluster: the |
| 6 | +# CRDs are installed, the controller resolves a ServiceExposure to its |
| 7 | +# ExposureClass, renders NO foreign backend CRs for the externalIPs |
| 8 | +# backend, and reports status. The externalIPs path is chosen deliberately |
| 9 | +# so the test creates no MetalLB/Cilium pool — it cannot collide with the |
| 10 | +# cluster's existing LoadBalancer address pools or depend on real L2 |
| 11 | +# reachability, keeping it deterministic. The metallb and cilium rendering |
| 12 | +# paths (pool + announcer CRs, garbage collection, take-over guard) are |
| 13 | +# covered exhaustively by the Go unit tests in |
| 14 | +# internal/controller/serviceexposure. |
| 15 | + |
| 16 | +CLASS=se-e2e-externalips |
| 17 | +SVC=se-e2e-svc |
| 18 | +EXP=se-e2e |
| 19 | +POOL=cozystack-${CLASS} # backend.PoolName = cozystack-<class> (class-level) |
| 20 | + |
| 21 | +setup() { |
| 22 | + kubectl -n tenant-test delete serviceexposure "$EXP" --ignore-not-found --timeout=2m |
| 23 | + kubectl -n tenant-test wait serviceexposure/"$EXP" --for=delete --timeout=2m --ignore-not-found |
| 24 | + kubectl -n tenant-test delete service "$SVC" --ignore-not-found --timeout=1m |
| 25 | + kubectl delete exposureclass "$CLASS" --ignore-not-found --timeout=1m |
| 26 | +} |
| 27 | + |
| 28 | +# teardown runs after every test, so a test that fails mid-way does not |
| 29 | +# leave the cluster-scoped ExposureClass (or the namespaced fixtures) |
| 30 | +# behind for the next test or the next CI run. |
| 31 | +teardown() { |
| 32 | + kubectl -n tenant-test delete serviceexposure "$EXP" --ignore-not-found --timeout=2m |
| 33 | + kubectl -n tenant-test delete service "$SVC" --ignore-not-found --timeout=1m |
| 34 | + kubectl delete exposureclass "$CLASS" --ignore-not-found --timeout=1m |
| 35 | +} |
| 36 | + |
| 37 | +dump_diagnostics() { |
| 38 | + echo "# --- diagnostics ---" >&3 |
| 39 | + kubectl -n tenant-test get serviceexposure,service -o wide >&3 2>&1 || true |
| 40 | + kubectl -n tenant-test describe serviceexposure "$EXP" >&3 2>&1 || true |
| 41 | + kubectl get exposureclass "$CLASS" -o yaml >&3 2>&1 || true |
| 42 | + kubectl -n cozy-system logs -l app=cozystack-controller --tail=100 >&3 2>&1 || true |
| 43 | +} |
| 44 | + |
| 45 | +@test "externalIPs ExposureClass resolves and renders no backend pool" { |
| 46 | + kubectl apply -f- <<EOF |
| 47 | +apiVersion: network.cozystack.io/v1alpha1 |
| 48 | +kind: ExposureClass |
| 49 | +metadata: |
| 50 | + name: ${CLASS} |
| 51 | +spec: |
| 52 | + backend: externalIPs |
| 53 | +--- |
| 54 | +apiVersion: v1 |
| 55 | +kind: Service |
| 56 | +metadata: |
| 57 | + name: ${SVC} |
| 58 | + namespace: tenant-test |
| 59 | +spec: |
| 60 | + type: ClusterIP |
| 61 | + externalIPs: |
| 62 | + - 203.0.113.10 |
| 63 | + ports: |
| 64 | + - port: 80 |
| 65 | + targetPort: 80 |
| 66 | +--- |
| 67 | +apiVersion: network.cozystack.io/v1alpha1 |
| 68 | +kind: ServiceExposure |
| 69 | +metadata: |
| 70 | + name: ${EXP} |
| 71 | + namespace: tenant-test |
| 72 | +spec: |
| 73 | + serviceRef: |
| 74 | + name: ${SVC} |
| 75 | + exposureClassName: ${CLASS} |
| 76 | +EOF |
| 77 | + |
| 78 | + # The controller stamps status.resolvedBackend once it has reconciled. |
| 79 | + timeout 120 sh -ec "until [ \"\$(kubectl -n tenant-test get serviceexposure ${EXP} -o jsonpath='{.status.resolvedBackend}' 2>/dev/null)\" = 'externalIPs' ]; do sleep 2; done" || { dump_diagnostics; false; } |
| 80 | + |
| 81 | + # externalIPs backend must not create a MetalLB pool. |
| 82 | + ! kubectl -n cozy-metallb get ipaddresspool "$POOL" 2>/dev/null || { dump_diagnostics; false; } |
| 83 | + |
| 84 | + # The pinned externalIPs make the exposure Ready. |
| 85 | + kubectl -n tenant-test wait serviceexposure/"$EXP" --for=jsonpath='{.status.conditions[?(@.type=="Ready")].status}'=True --timeout=60s || { dump_diagnostics; false; } |
| 86 | + kubectl -n tenant-test get serviceexposure "$EXP" -o jsonpath='{.status.assignedIPs[0]}' | grep -q '^203.0.113.10$' || { dump_diagnostics; false; } |
| 87 | +} |
| 88 | + |
| 89 | +@test "deleting the ServiceExposure removes its finalizer and the object" { |
| 90 | + # Recreate (setup cleared the previous test's objects). |
| 91 | + kubectl apply -f- <<EOF |
| 92 | +apiVersion: network.cozystack.io/v1alpha1 |
| 93 | +kind: ExposureClass |
| 94 | +metadata: |
| 95 | + name: ${CLASS} |
| 96 | +spec: |
| 97 | + backend: externalIPs |
| 98 | +--- |
| 99 | +apiVersion: v1 |
| 100 | +kind: Service |
| 101 | +metadata: |
| 102 | + name: ${SVC} |
| 103 | + namespace: tenant-test |
| 104 | +spec: |
| 105 | + type: ClusterIP |
| 106 | + externalIPs: |
| 107 | + - 203.0.113.10 |
| 108 | + ports: |
| 109 | + - port: 80 |
| 110 | + targetPort: 80 |
| 111 | +--- |
| 112 | +apiVersion: network.cozystack.io/v1alpha1 |
| 113 | +kind: ServiceExposure |
| 114 | +metadata: |
| 115 | + name: ${EXP} |
| 116 | + namespace: tenant-test |
| 117 | +spec: |
| 118 | + serviceRef: |
| 119 | + name: ${SVC} |
| 120 | + exposureClassName: ${CLASS} |
| 121 | +EOF |
| 122 | + timeout 120 sh -ec "until kubectl -n tenant-test get serviceexposure ${EXP} -o jsonpath='{.status.resolvedBackend}' 2>/dev/null | grep -q externalIPs; do sleep 2; done" || { dump_diagnostics; false; } |
| 123 | + |
| 124 | + # Deletion must complete despite the cleanup finalizer (nothing to GC for |
| 125 | + # the externalIPs backend, so the finalizer is removed promptly). |
| 126 | + kubectl -n tenant-test delete serviceexposure "$EXP" --timeout=2m |
| 127 | + kubectl -n tenant-test wait serviceexposure/"$EXP" --for=delete --timeout=1m --ignore-not-found |
| 128 | + |
| 129 | + # Inline cleanup of the remaining fixtures. |
| 130 | + kubectl -n tenant-test delete service "$SVC" --ignore-not-found --timeout=1m |
| 131 | + kubectl delete exposureclass "$CLASS" --ignore-not-found --timeout=1m |
| 132 | +} |
0 commit comments