Skip to content

Commit 6bf15e4

Browse files
author
Jesus Carrillo
committed
add: e2e tests for port change in service
1 parent d78fb1f commit 6bf15e4

3 files changed

Lines changed: 233 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
2+
apiVersion: chainsaw.kyverno.io/v1alpha1
3+
kind: Test
4+
metadata:
5+
name: lb-update-port
6+
labels:
7+
all:
8+
lke:
9+
spec:
10+
namespace: "lb-update-port"
11+
steps:
12+
- name: Create pods and services
13+
try:
14+
- apply:
15+
file: create-pods-services.yaml
16+
catch:
17+
- describe:
18+
apiVersion: v1
19+
kind: Pod
20+
- describe:
21+
apiVersion: v1
22+
kind: Service
23+
- name: Check that loadbalancer ip is assigned
24+
try:
25+
- assert:
26+
resource:
27+
apiVersion: v1
28+
kind: Service
29+
metadata:
30+
name: svc-test
31+
status:
32+
(loadBalancer.ingress[0].ip != null): true
33+
- name: Fetch loadbalancer ip and check both pods reachable
34+
try:
35+
- script:
36+
content: |
37+
set -e
38+
IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip)
39+
40+
podnames=()
41+
42+
for i in {1..10}; do
43+
if [[ ${#podnames[@]} -lt 2 ]]; then
44+
output=$(curl -s $IP:80 | jq -e .podName || true)
45+
46+
if [[ "$output" == *"test-"* ]]; then
47+
unique=true
48+
for i in "${array[@]}"; do
49+
if [[ "$i" == "$output" ]]; then
50+
unique=false
51+
break
52+
fi
53+
done
54+
if [[ "$unique" == true ]]; then
55+
podnames+=($output)
56+
fi
57+
fi
58+
else
59+
break
60+
fi
61+
sleep 10
62+
done
63+
64+
if [[ ${#podnames[@]} -lt 2 ]]; then
65+
echo "all pods failed to respond"
66+
else
67+
echo "all pods responded"
68+
fi
69+
check:
70+
($error == null): true
71+
(contains($stdout, 'all pods responded')): true
72+
- name: Update service
73+
try:
74+
- apply:
75+
file: update-port-service.yaml
76+
- name: Check pods reachable on new port
77+
try:
78+
- script:
79+
content: |
80+
set -e
81+
#wait for changes to propagate to the LB
82+
sleep 30
83+
IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip)
84+
85+
podnames=()
86+
87+
for i in {1..10}; do
88+
if [[ ${#podnames[@]} -lt 2 ]]; then
89+
output=$(curl -s $IP:8080 | jq -e .podName || true)
90+
91+
if [[ "$output" == *"test-"* ]]; then
92+
unique=true
93+
for i in "${array[@]}"; do
94+
if [[ "$i" == "$output" ]]; then
95+
unique=false
96+
break
97+
fi
98+
done
99+
if [[ "$unique" == true ]]; then
100+
podnames+=($output)
101+
fi
102+
fi
103+
else
104+
break
105+
fi
106+
sleep 10
107+
done
108+
109+
if [[ ${#podnames[@]} -lt 2 ]]; then
110+
echo "all pods failed to respond"
111+
else
112+
echo "all pods responded"
113+
fi
114+
check:
115+
($error == null): true
116+
(contains($stdout, 'all pods responded')): true
117+
- name: Fetch firewall ID and check ports are updated
118+
try:
119+
- script:
120+
content: |
121+
set -e
122+
123+
for i in {1..10}; do
124+
nbid=$(KUBECONFIG=$KUBECONFIG NAMESPACE=$NAMESPACE LINODE_TOKEN=$LINODE_TOKEN ../scripts/get-nb-id.sh)
125+
126+
fw=$(curl -s --request GET \
127+
-H "Authorization: Bearer $LINODE_TOKEN" \
128+
-H "Content-Type: application/json" \
129+
-H "accept: application/json" \
130+
"https://api.linode.com/v4/nodebalancers/${nbid}/firewalls" || true)
131+
132+
if echo "$fw" | jq -e '.data[].rules.inbound[] | select(.ports | contains("8080"))' > /dev/null; then
133+
echo "firewall rule updated with new port"
134+
break
135+
fi
136+
sleep 10
137+
done
138+
check:
139+
($error == null): true
140+
(contains($stdout, 'firewall rule updated with new port')): true
141+
- name: Delete Pods
142+
try:
143+
- delete:
144+
ref:
145+
apiVersion: v1
146+
kind: Pod
147+
- name: Delete Service
148+
try:
149+
- delete:
150+
ref:
151+
apiVersion: v1
152+
kind: Service
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
labels:
6+
app: lb-simple
7+
name: test
8+
spec:
9+
replicas: 2
10+
selector:
11+
matchLabels:
12+
app: lb-simple
13+
template:
14+
metadata:
15+
labels:
16+
app: lb-simple
17+
spec:
18+
affinity:
19+
podAntiAffinity:
20+
preferredDuringSchedulingIgnoredDuringExecution:
21+
- podAffinityTerm:
22+
labelSelector:
23+
matchExpressions:
24+
- key: app
25+
operator: In
26+
values:
27+
- simple-lb
28+
topologyKey: kubernetes.io/hostname
29+
weight: 100
30+
containers:
31+
- image: appscode/test-server:2.3
32+
name: test
33+
ports:
34+
- name: http-1
35+
containerPort: 8080
36+
protocol: TCP
37+
env:
38+
- name: POD_NAME
39+
valueFrom:
40+
fieldRef:
41+
apiVersion: v1
42+
fieldPath: metadata.name
43+
---
44+
apiVersion: v1
45+
kind: Service
46+
metadata:
47+
name: svc-test
48+
labels:
49+
app: lb-simple
50+
spec:
51+
type: LoadBalancer
52+
selector:
53+
app: lb-simple
54+
ports:
55+
- name: http-1
56+
protocol: TCP
57+
port: 80
58+
targetPort: 8080
59+
sessionAffinity: None
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: svc-test
6+
labels:
7+
app: lb-simple
8+
spec:
9+
type: LoadBalancer
10+
selector:
11+
app: lb-simple
12+
ports:
13+
- name: http-1
14+
protocol: TCP
15+
port: 80
16+
targetPort: 8080
17+
- name: http-2
18+
protocol: TCP
19+
port: 8080
20+
targetPort: 8080
21+
sessionAffinity: None
22+
...

0 commit comments

Comments
 (0)