Skip to content

Commit a152be0

Browse files
xiaojiangao123Geaus
authored andcommitted
fix ipv6 bug in ipsec test
Signed-off-by: xiaojiangao123 <[email protected]>
1 parent 75eba28 commit a152be0

File tree

1 file changed

+20
-76
lines changed

1 file changed

+20
-76
lines changed

test/e2e/ipsec_test.go

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -87,53 +87,6 @@ spec:
8787
optional: true
8888
`
8989

90-
var httpbinYaml = `
91-
apiVersion: v1
92-
kind: ServiceAccount
93-
metadata:
94-
name: httpbin
95-
---
96-
apiVersion: v1
97-
kind: Service
98-
metadata:
99-
name: httpbin
100-
labels:
101-
app: httpbin
102-
service: httpbin
103-
spec:
104-
ports:
105-
- name: http
106-
port: 8000
107-
targetPort: 80
108-
selector:
109-
app: httpbin
110-
---
111-
apiVersion: apps/v1
112-
kind: Deployment
113-
metadata:
114-
name: httpbin
115-
spec:
116-
replicas: 1
117-
selector:
118-
matchLabels:
119-
app: httpbin
120-
version: v1
121-
template:
122-
metadata:
123-
labels:
124-
app: httpbin
125-
version: v1
126-
spec:
127-
serviceAccountName: httpbin
128-
nodeName: kmesh-testing-worker
129-
containers:
130-
- image: docker.io/kong/httpbin
131-
imagePullPolicy: IfNotPresent
132-
name: httpbin
133-
ports:
134-
- containerPort: 80
135-
`
136-
13790
func deployServices(t framework.TestContext) error {
13891
t.Logf("Labeling namespace...")
13992
cmd := exec.Command("kubectl", "label", "namespace", "default", "istio.io/dataplane-mode=Kmesh")
@@ -152,15 +105,6 @@ func deployServices(t framework.TestContext) error {
152105
return err
153106
}
154107

155-
t.Logf("Applying httpbin resources...")
156-
cmd = exec.Command("kubectl", "apply", "-f", "-")
157-
cmd.Stdin = bytes.NewBufferString(httpbinYaml)
158-
out, err = cmd.CombinedOutput()
159-
if err != nil {
160-
t.Fatalf("Failed to apply httpbin resources: %s\n%s", err, string(out))
161-
return err
162-
}
163-
164108
return nil
165109
}
166110

@@ -174,15 +118,6 @@ func deleteServices(t framework.TestContext) error {
174118
return err
175119
}
176120

177-
t.Logf("Deleting httpbin resources...")
178-
cmd = exec.Command("kubectl", "delete", "-f", "-")
179-
cmd.Stdin = bytes.NewBufferString(httpbinYaml)
180-
out, err = cmd.CombinedOutput()
181-
if err != nil {
182-
t.Fatalf("Failed to delete httpbin resources: %s\n%s", err, string(out))
183-
return err
184-
}
185-
186121
t.Logf("Removing label from namespace default...")
187122
cmd = exec.Command("kubectl", "label", "namespace", "default", "istio.io/dataplane-mode-")
188123
out, err = cmd.CombinedOutput()
@@ -194,9 +129,9 @@ func deleteServices(t framework.TestContext) error {
194129
return nil
195130
}
196131

197-
func getPodNameAndIP(t framework.TestContext, appLabel string) (string, string, error) {
132+
func getPodNameAndIP(t framework.TestContext, namespace string, appLabel string) (string, string, error) {
198133
for {
199-
cmd := exec.Command("kubectl", "get", "pods", "-l", "app="+appLabel, "-o", "wide")
134+
cmd := exec.Command("kubectl", "get", "pods", "-n", namespace, "-l", "app="+appLabel, "-o", "wide")
200135
out, err := cmd.CombinedOutput()
201136
if err != nil {
202137
t.Logf("kubectl get pods failed: %v\n%s", err, string(out))
@@ -314,8 +249,17 @@ func runTcpdumpESP(t framework.TestContext, containerName string, duration int)
314249
return string(out), nil
315250
}
316251

317-
func curlFromSleepToHttpbin(t framework.TestContext, sleepPod, httpbinIP string) (string, error) {
318-
cmd := exec.Command("kubectl", "exec", sleepPod, "--", "curl", fmt.Sprintf("http://%s:80", httpbinIP))
252+
func curlFromSleepToEcho(t framework.TestContext, sleepPod, echoIP string) (string, error) {
253+
t.Logf("Curling from sleep pod %s to echo pod IP %s...", sleepPod, echoIP)
254+
var url string
255+
if strings.Contains(echoIP, ":") {
256+
// IPv6 address
257+
url = fmt.Sprintf("http://[%s]:18080", echoIP)
258+
} else {
259+
// IPv4 address
260+
url = fmt.Sprintf("http://%s:18080", echoIP)
261+
}
262+
cmd := exec.Command("kubectl", "exec", sleepPod, "--", "curl", url)
319263
out, err := cmd.CombinedOutput()
320264
if err != nil {
321265
t.Fatalf("curl failed: %v\n%s", err, string(out))
@@ -379,18 +323,18 @@ func TestIPsecAuthorization(t *testing.T) {
379323

380324
t.Logf("Resources applied. You can now continue with IPSec validation.")
381325

382-
sleepPodName, sleepPodIP, err := getPodNameAndIP(t, "sleep")
326+
sleepPodName, sleepPodIP, err := getPodNameAndIP(t, "default", "sleep")
383327
if err != nil {
384328
return
385329
} else {
386330
t.Logf("sleep pod name: %s, ip: %s", sleepPodName, sleepPodIP)
387331
}
388332

389-
httpbinPodName, httpbinPodIP, err := getPodNameAndIP(t, "httpbin")
333+
echoPodName, echoPodIP, err := getPodNameAndIP(t, apps.Namespace.Name(), "enrolled-to-kmesh")
390334
if err != nil {
391335
return
392336
} else {
393-
t.Logf("httpbin pod name: %s, ip: %s", httpbinPodName, httpbinPodIP)
337+
t.Logf("echo pod name: %s, ip: %s", echoPodName, echoPodIP)
394338
}
395339

396340
if err = downloadTcpdump(t, "kmesh-testing-control-plane"); err != nil {
@@ -408,8 +352,8 @@ func TestIPsecAuthorization(t *testing.T) {
408352
tcpdumpCh <- out
409353
}()
410354
time.Sleep(2 * time.Second)
411-
t.Logf("Curling from sleep pod to httpbin pod...")
412-
_, err = curlFromSleepToHttpbin(t, sleepPodName, httpbinPodIP)
355+
t.Logf("Curling from sleep pod to echo pod...")
356+
_, err = curlFromSleepToEcho(t, sleepPodName, echoPodIP)
413357
if err != nil {
414358
<-tcpdumpCh
415359
deleteServices(t)
@@ -436,8 +380,8 @@ func TestIPsecAuthorization(t *testing.T) {
436380
tcpdumpCh <- out
437381
}()
438382
time.Sleep(2 * time.Second)
439-
t.Logf("Curling from sleep pod to httpbin pod after key rotation...")
440-
_, err = curlFromSleepToHttpbin(t, sleepPodName, httpbinPodIP)
383+
t.Logf("Curling from sleep pod to echo pod after key rotation...")
384+
_, err = curlFromSleepToEcho(t, sleepPodName, echoPodIP)
441385
if err != nil {
442386
<-tcpdumpCh
443387
deleteServices(t)

0 commit comments

Comments
 (0)