@@ -23,6 +23,7 @@ import (
23
23
"net/http/httptest"
24
24
"reflect"
25
25
"strconv"
26
+ "strings"
26
27
"testing"
27
28
"time"
28
29
@@ -92,9 +93,12 @@ func testPod(namespace string, id int, nPorts int, isReady bool, ipFamilies []v1
92
93
for _ , family := range ipFamilies {
93
94
var ip string
94
95
if family == v1 .IPv4Protocol {
95
- ip = fmt .Sprintf ("1.2.3.%d" , 4 + id )
96
+ // if id=1, ip=1.2.3.4
97
+ // if id=2, ip=1.2.3.5
98
+ // if id=999, ip=1.2.6.235
99
+ ip = fmt .Sprintf ("1.2.%d.%d" , 3 + ((4 + id )/ 256 ), (4 + id )% 256 )
96
100
} else {
97
- ip = fmt .Sprintf ("2000::%d " , 4 + id )
101
+ ip = fmt .Sprintf ("2000::%x " , 4 + id )
98
102
}
99
103
p .Status .PodIPs = append (p .Status .PodIPs , v1.PodIP {IP : ip })
100
104
}
@@ -111,6 +115,13 @@ func addPods(store cache.Store, namespace string, nPods int, nPorts int, nNotRea
111
115
}
112
116
}
113
117
118
+ func addBadIPPod (store cache.Store , namespace string , ipFamilies []v1.IPFamily ) {
119
+ pod := testPod (namespace , 0 , 1 , true , ipFamilies )
120
+ pod .Status .PodIPs [0 ].IP = "0" + pod .Status .PodIPs [0 ].IP
121
+ pod .Status .PodIP = pod .Status .PodIPs [0 ].IP
122
+ store .Add (pod )
123
+ }
124
+
114
125
func addNotReadyPodsWithSpecifiedRestartPolicyAndPhase (store cache.Store , namespace string , nPods int , nPorts int , restartPolicy v1.RestartPolicy , podPhase v1.PodPhase ) {
115
126
for i := 0 ; i < nPods ; i ++ {
116
127
p := & v1.Pod {
@@ -1476,7 +1487,9 @@ func TestPodToEndpointAddressForService(t *testing.T) {
1476
1487
t .Run (tc .name , func (t * testing.T ) {
1477
1488
podStore := cache .NewStore (cache .DeletionHandlingMetaNamespaceKeyFunc )
1478
1489
ns := "test"
1479
- addPods (podStore , ns , 1 , 1 , 0 , tc .ipFamilies )
1490
+ // We use addBadIPPod to test that podToEndpointAddressForService
1491
+ // fixes up the bad IP.
1492
+ addBadIPPod (podStore , ns , tc .ipFamilies )
1480
1493
pods := podStore .List ()
1481
1494
if len (pods ) != 1 {
1482
1495
t .Fatalf ("podStore size: expected: %d, got: %d" , 1 , len (pods ))
@@ -1499,6 +1512,9 @@ func TestPodToEndpointAddressForService(t *testing.T) {
1499
1512
if utilnet .IsIPv6String (epa .IP ) != (tc .expectedEndpointFamily == ipv6 ) {
1500
1513
t .Fatalf ("IP: expected %s, got: %s" , tc .expectedEndpointFamily , epa .IP )
1501
1514
}
1515
+ if strings .HasPrefix (epa .IP , "0" ) {
1516
+ t .Fatalf ("IP: expected valid, got: %s" , epa .IP )
1517
+ }
1502
1518
if * (epa .NodeName ) != pod .Spec .NodeName {
1503
1519
t .Fatalf ("NodeName: expected: %s, got: %s" , pod .Spec .NodeName , * (epa .NodeName ))
1504
1520
}
0 commit comments