Skip to content

Commit 81e667d

Browse files
authored
Merge pull request #76 from zeeke/us/end-port-e2e
e2e: test for port ranges
2 parents c0f06e2 + d1d84a6 commit 81e667d

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

e2e/tests/port-range.bats

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bats
2+
3+
setup() {
4+
cd $BATS_TEST_DIRNAME
5+
load "common"
6+
pod_a_net1=$(get_net1_ip "test-port-range" "pod-a")
7+
pod_b_net1=$(get_net1_ip "test-port-range" "pod-b")
8+
}
9+
10+
@test "setup environments" {
11+
# create test manifests
12+
kubectl create -f port-range.yml
13+
14+
# verify all pods are available
15+
run kubectl -n test-port-range wait --for=condition=ready -l app=test-port-range pod --timeout=${kubewait_timeout}
16+
[ "$status" -eq "0" ]
17+
18+
sleep 3
19+
}
20+
21+
@test "test-port-range check pod-a -> pod-b 5555 OK" {
22+
# nc should succeed from client-a to server by policy
23+
run kubectl -n test-port-range exec pod-a -- sh -c "echo x | nc -w 1 ${pod_b_net1} 5555"
24+
[ "$status" -eq "0" ]
25+
}
26+
27+
@test "test-port-range check pod-a -> pod-b 6666 KO" {
28+
# nc should succeed from client-a to server by policy
29+
run kubectl -n test-port-range exec pod-a -- sh -c "echo x | nc -w 1 ${pod_b_net1} 6666"
30+
[ "$status" -eq "1" ]
31+
}
32+
33+
@test "test-port-range check pod-b -> pod-a 5555 KO" {
34+
# nc should succeed from client-a to server by policy
35+
run kubectl -n test-port-range exec pod-b -- sh -c "echo x | nc -w 1 ${pod_a_net1} 5555"
36+
[ "$status" -eq "1" ]
37+
}
38+
39+
@test "test-port-range check pod-b -> pod-a 6666 OK" {
40+
# nc should succeed from client-a to server by policy
41+
run kubectl -n test-port-range exec pod-b -- sh -c "echo x | nc -w 1 ${pod_a_net1} 6666"
42+
[ "$status" -eq "0" ]
43+
}
44+
45+
@test "cleanup environments" {
46+
# remove test manifests
47+
kubectl delete -f port-range.yml
48+
run kubectl -n test-port-range wait --for=delete -l app=test-port-range pod --timeout=${kubewait_timeout}
49+
[ "$status" -eq "0" ]
50+
}

e2e/tests/port-range.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
apiVersion: "k8s.cni.cncf.io/v1"
3+
kind: NetworkAttachmentDefinition
4+
metadata:
5+
namespace: default
6+
name: macvlan1-simple
7+
spec:
8+
config: '{
9+
"cniVersion": "0.3.1",
10+
"name": "macvlan1-simple",
11+
"plugins": [
12+
{
13+
"type": "macvlan",
14+
"mode": "bridge",
15+
"ipam":{
16+
"type":"host-local",
17+
"subnet":"2.2.6.0/24",
18+
"rangeStart":"2.2.6.8",
19+
"rangeEnd":"2.2.6.67"
20+
}
21+
}]
22+
}'
23+
---
24+
# namespace for MultiNetworkPolicy
25+
apiVersion: v1
26+
kind: Namespace
27+
metadata:
28+
name: test-port-range
29+
---
30+
# Pods
31+
apiVersion: v1
32+
kind: Pod
33+
metadata:
34+
name: pod-a
35+
namespace: test-port-range
36+
annotations:
37+
k8s.v1.cni.cncf.io/networks: default/macvlan1-simple
38+
labels:
39+
app: test-port-range
40+
name: pod-a
41+
spec:
42+
containers:
43+
- name: netcat-tcp-5555
44+
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
45+
command: ["nc", "-klp", "5555"]
46+
securityContext:
47+
privileged: true
48+
- name: netcat-tcp-6666
49+
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
50+
command: ["nc", "-klp", "6666"]
51+
securityContext:
52+
privileged: true
53+
---
54+
apiVersion: v1
55+
kind: Pod
56+
metadata:
57+
name: pod-b
58+
namespace: test-port-range
59+
annotations:
60+
k8s.v1.cni.cncf.io/networks: default/macvlan1-simple
61+
labels:
62+
app: test-port-range
63+
name: pod-b
64+
spec:
65+
containers:
66+
- name: netcat-tcp-5555
67+
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
68+
command: ["nc", "-klp", "5555"]
69+
securityContext:
70+
privileged: true
71+
- name: netcat-tcp-6666
72+
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
73+
command: ["nc", "-klp", "6666"]
74+
securityContext:
75+
privileged: true
76+
---
77+
# MultiNetworkPolicies
78+
apiVersion: k8s.cni.cncf.io/v1beta1
79+
kind: MultiNetworkPolicy
80+
metadata:
81+
name: test-multinetwork-policy-simple-1
82+
namespace: test-port-range
83+
annotations:
84+
k8s.v1.cni.cncf.io/policy-for: default/macvlan1-simple
85+
spec:
86+
podSelector:
87+
matchLabels:
88+
name: pod-a
89+
policyTypes:
90+
- Egress
91+
- Ingress
92+
egress:
93+
- ports:
94+
- port: 5000
95+
endPort: 5900
96+
ingress:
97+
- ports:
98+
- port: 6000
99+
endPort: 6900

0 commit comments

Comments
 (0)