Skip to content

Commit ae0344b

Browse files
committed
Make NetworkPolicy tests use pod IPs rather than service IPs (temporarily)
1 parent bdf4a01 commit ae0344b

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

test/extended/networking/networkpolicy.go

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
kube #36673).
3535
3. Ported to use IPs rather than DNS names in tests since our extended
3636
networking tests don't run with DNS.
37+
4. Changed to use pod IPs rather than service IPs in tests since our
38+
NetworkPolicy implementation doesn't currently support services
39+
correctly in Namespaces with NetworkPolicies with PodSelectors.
3740
*/
3841

3942

@@ -68,19 +71,21 @@ var _ = Describe("NetworkPolicy", func() {
6871
}
6972
}()
7073
framework.Logf("Waiting for Server to come up.")
71-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, podServer)
74+
err := f.WaitForAnEndpoint(service.Name)
75+
Expect(err).NotTo(HaveOccurred())
76+
podServer, err = f.ClientSet.Core().Pods(ns.Name).Get(podServer.Name)
7277
Expect(err).NotTo(HaveOccurred())
7378

7479
// Create a pod with name 'client-a', which should be able to communicate with server.
7580
By("Creating client which will be able to contact the server since isolation is off.")
76-
testCanConnect(f, ns, "client-can-connect", service, 80)
81+
testCanConnect(f, ns, "client-can-connect", service, podServer.Status.PodIP, 80)
7782

7883
framework.Logf("Enabling network isolation.")
7984
setNamespaceIsolation(f, ns, "DefaultDeny")
8085

8186
// Create a pod with name 'client-b', which will attempt to comunicate with the server,
8287
// but should not be able to now that isolation is on.
83-
testCannotConnect(f, ns, "client-cannot-connect", service, 80)
88+
testCannotConnect(f, ns, "client-cannot-connect", service, podServer.Status.PodIP, 80)
8489
})
8590

8691
It("should enforce policy based on PodSelector [Feature:NetworkPolicy]", func() {
@@ -102,7 +107,9 @@ var _ = Describe("NetworkPolicy", func() {
102107
}
103108
}()
104109
framework.Logf("Waiting for Server to come up.")
105-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
110+
err := f.WaitForAnEndpoint(service.Name)
111+
Expect(err).NotTo(HaveOccurred())
112+
serverPod, err = f.ClientSet.Core().Pods(ns.Name).Get(serverPod.Name)
106113
Expect(err).NotTo(HaveOccurred())
107114

108115
By("Creating a network policy for the server which allows traffic from the pod 'client-a'.")
@@ -142,8 +149,8 @@ var _ = Describe("NetworkPolicy", func() {
142149
}()
143150

144151
By("Creating client-a which should be able to contact the server.")
145-
testCanConnect(f, ns, "client-a", service, 80)
146-
testCannotConnect(f, ns, "client-b", service, 80)
152+
testCanConnect(f, ns, "client-a", service, serverPod.Status.PodIP, 80)
153+
testCannotConnect(f, ns, "client-b", service, serverPod.Status.PodIP, 80)
147154
})
148155

149156
It("should enforce policy based on Ports [Feature:NetworkPolicy]", func() {
@@ -165,18 +172,20 @@ var _ = Describe("NetworkPolicy", func() {
165172
}
166173
}()
167174
framework.Logf("Waiting for Server to come up.")
168-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
175+
err := f.WaitForAnEndpoint(service.Name)
176+
Expect(err).NotTo(HaveOccurred())
177+
serverPod, err = f.ClientSet.Core().Pods(ns.Name).Get(serverPod.Name)
169178
Expect(err).NotTo(HaveOccurred())
170179

171180
By("Testing pods can connect to both ports when isolation is off.")
172-
testCanConnect(f, ns, "basecase-reachable-80", service, 80)
173-
testCanConnect(f, ns, "basecase-reachable-81", service, 81)
181+
testCanConnect(f, ns, "basecase-reachable-80", service, serverPod.Status.PodIP, 80)
182+
testCanConnect(f, ns, "basecase-reachable-81", service, serverPod.Status.PodIP, 81)
174183

175184
setNamespaceIsolation(f, ns, "DefaultDeny")
176185

177186
By("Testing pods cannot by default when isolation is turned on.")
178-
testCannotConnect(f, ns, "basecase-unreachable-80", service, 80)
179-
testCannotConnect(f, ns, "basecase-unreachable-81", service, 81)
187+
testCannotConnect(f, ns, "basecase-unreachable-80", service, serverPod.Status.PodIP, 80)
188+
testCannotConnect(f, ns, "basecase-unreachable-81", service, serverPod.Status.PodIP, 81)
180189

181190
By("Creating a network policy for the Service which allows traffic only to one port.")
182191
policy := extensions.NetworkPolicy{
@@ -208,8 +217,8 @@ var _ = Describe("NetworkPolicy", func() {
208217
}
209218
}()
210219

211-
testCannotConnect(f, ns, "client-a", service, 80)
212-
testCanConnect(f, ns, "client-b", service, 81)
220+
testCannotConnect(f, ns, "client-a", service, serverPod.Status.PodIP, 80)
221+
testCanConnect(f, ns, "client-b", service, serverPod.Status.PodIP, 81)
213222
})
214223

215224
It("shouldn't enforce policy when isolation is off [Feature:NetworkPolicy]", func() {
@@ -231,12 +240,14 @@ var _ = Describe("NetworkPolicy", func() {
231240
}
232241
}()
233242
framework.Logf("Waiting for Server to come up.")
234-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
243+
err := f.WaitForAnEndpoint(service.Name)
244+
Expect(err).NotTo(HaveOccurred())
245+
serverPod, err = f.ClientSet.Core().Pods(ns.Name).Get(serverPod.Name)
235246
Expect(err).NotTo(HaveOccurred())
236247

237248
By("Testing pods can connect to both ports when isolation is off and no policy is defined.")
238-
testCanConnect(f, ns, "basecase-reachable-a", service, 80)
239-
testCanConnect(f, ns, "basecase-reachable-b", service, 81)
249+
testCanConnect(f, ns, "basecase-reachable-a", service, serverPod.Status.PodIP, 80)
250+
testCanConnect(f, ns, "basecase-reachable-b", service, serverPod.Status.PodIP, 81)
240251

241252
By("Creating a network policy for the Service which allows traffic only to one port.")
242253
policy := extensions.NetworkPolicy{
@@ -268,8 +279,8 @@ var _ = Describe("NetworkPolicy", func() {
268279
}
269280
}()
270281

271-
testCanConnect(f, ns, "client-a", service, 80)
272-
testCanConnect(f, ns, "client-b", service, 81)
282+
testCanConnect(f, ns, "client-a", service, serverPod.Status.PodIP, 80)
283+
testCanConnect(f, ns, "client-b", service, serverPod.Status.PodIP, 81)
273284
})
274285

275286
It("should enforce multiple, stacked policies with overlapping podSelectors [Feature:NetworkPolicy]", func() {
@@ -291,18 +302,20 @@ var _ = Describe("NetworkPolicy", func() {
291302
}
292303
}()
293304
framework.Logf("Waiting for Server to come up.")
294-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
305+
err := f.WaitForAnEndpoint(service.Name)
306+
Expect(err).NotTo(HaveOccurred())
307+
serverPod, err = f.ClientSet.Core().Pods(ns.Name).Get(serverPod.Name)
295308
Expect(err).NotTo(HaveOccurred())
296309

297310
By("Testing pods can connect to both ports when isolation is off.")
298-
testCanConnect(f, ns, "test-a", service, 80)
299-
testCanConnect(f, ns, "test-b", service, 81)
311+
testCanConnect(f, ns, "test-a", service, serverPod.Status.PodIP, 80)
312+
testCanConnect(f, ns, "test-b", service, serverPod.Status.PodIP, 81)
300313

301314
setNamespaceIsolation(f, ns, "DefaultDeny")
302315

303316
By("Testing pods cannot connect to either port when no policy is defined.")
304-
testCannotConnect(f, ns, "test-a-2", service, 80)
305-
testCannotConnect(f, ns, "test-b-2", service, 81)
317+
testCannotConnect(f, ns, "test-a-2", service, serverPod.Status.PodIP, 80)
318+
testCannotConnect(f, ns, "test-b-2", service, serverPod.Status.PodIP, 81)
306319

307320
By("Creating a network policy for the Service which allows traffic only to one port.")
308321
policy := extensions.NetworkPolicy{
@@ -364,8 +377,8 @@ var _ = Describe("NetworkPolicy", func() {
364377
}
365378
}()
366379

367-
testCanConnect(f, ns, "client-a", service, 80)
368-
testCanConnect(f, ns, "client-b", service, 81)
380+
testCanConnect(f, ns, "client-a", service, serverPod.Status.PodIP, 80)
381+
testCanConnect(f, ns, "client-b", service, serverPod.Status.PodIP, 81)
369382
})
370383

371384
It("should support allow-all policy [Feature:NetworkPolicy]", func() {
@@ -387,18 +400,20 @@ var _ = Describe("NetworkPolicy", func() {
387400
}
388401
}()
389402
framework.Logf("Waiting for Server to come up.")
390-
err := framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
403+
err := f.WaitForAnEndpoint(service.Name)
404+
Expect(err).NotTo(HaveOccurred())
405+
serverPod, err = f.ClientSet.Core().Pods(ns.Name).Get(serverPod.Name)
391406
Expect(err).NotTo(HaveOccurred())
392407

393408
By("Testing pods can connect to both ports when isolation is off.")
394-
testCanConnect(f, ns, "test-a", service, 80)
395-
testCanConnect(f, ns, "test-b", service, 81)
409+
testCanConnect(f, ns, "test-a", service, serverPod.Status.PodIP, 80)
410+
testCanConnect(f, ns, "test-b", service, serverPod.Status.PodIP, 81)
396411

397412
setNamespaceIsolation(f, ns, "DefaultDeny")
398413

399414
By("Testing pods cannot connect to either port when isolation is on.")
400-
testCannotConnect(f, ns, "test-a", service, 80)
401-
testCannotConnect(f, ns, "test-b", service, 81)
415+
testCannotConnect(f, ns, "test-a", service, serverPod.Status.PodIP, 80)
416+
testCannotConnect(f, ns, "test-b", service, serverPod.Status.PodIP, 81)
402417

403418
By("Creating a network policy which allows all traffic.")
404419
policy := extensions.NetworkPolicy{
@@ -423,8 +438,8 @@ var _ = Describe("NetworkPolicy", func() {
423438
}
424439
}()
425440

426-
testCanConnect(f, ns, "client-a", service, 80)
427-
testCanConnect(f, ns, "client-b", service, 81)
441+
testCanConnect(f, ns, "client-a", service, serverPod.Status.PodIP, 80)
442+
testCanConnect(f, ns, "client-b", service, serverPod.Status.PodIP, 81)
428443
})
429444

430445
It("should enforce policy based on NamespaceSelector [Feature:NetworkPolicy]", func() {
@@ -455,7 +470,9 @@ var _ = Describe("NetworkPolicy", func() {
455470
}
456471
}()
457472
framework.Logf("Waiting for server to come up.")
458-
err = framework.WaitForPodRunningInNamespace(f.ClientSet, serverPod)
473+
err = f.WaitForAnEndpoint(service.Name)
474+
Expect(err).NotTo(HaveOccurred())
475+
serverPod, err = f.ClientSet.Core().Pods(nsA.Name).Get(serverPod.Name)
459476
Expect(err).NotTo(HaveOccurred())
460477

461478
// Create Policy for that service that allows traffic only via namespace B
@@ -493,15 +510,15 @@ var _ = Describe("NetworkPolicy", func() {
493510
}
494511
}()
495512

496-
testCannotConnect(f, nsA, "client-a", service, 80)
497-
testCanConnect(f, nsB, "client-b", service, 80)
513+
testCannotConnect(f, nsA, "client-a", service, serverPod.Status.PodIP, 80)
514+
testCanConnect(f, nsB, "client-b", service, serverPod.Status.PodIP, 80)
498515
})
499516
})
500517
})
501518

502-
func testCanConnect(f *framework.Framework, ns *api.Namespace, podName string, service *api.Service, targetPort int) {
519+
func testCanConnect(f *framework.Framework, ns *api.Namespace, podName string, service *api.Service, target string, targetPort int) {
503520
By(fmt.Sprintf("Creating client pod %s that should successfully connect to %s.", podName, service.Name))
504-
podClient := createNetworkClientPod(f, ns, podName, service.Spec.ClusterIP, targetPort)
521+
podClient := createNetworkClientPod(f, ns, podName, target, targetPort)
505522
defer func() {
506523
By(fmt.Sprintf("Cleaning up the pod %s", podName))
507524
if err := f.ClientSet.Core().Pods(ns.Name).Delete(podClient.Name, nil); err != nil {
@@ -518,9 +535,9 @@ func testCanConnect(f *framework.Framework, ns *api.Namespace, podName string, s
518535
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("checking %s could communicate with server.", podClient.Name))
519536
}
520537

521-
func testCannotConnect(f *framework.Framework, ns *api.Namespace, podName string, service *api.Service, targetPort int) {
538+
func testCannotConnect(f *framework.Framework, ns *api.Namespace, podName string, service *api.Service, target string, targetPort int) {
522539
By(fmt.Sprintf("Creating client pod %s that should not be able to connect to %s.", podName, service.Name))
523-
podClient := createNetworkClientPod(f, ns, podName, service.Spec.ClusterIP, targetPort)
540+
podClient := createNetworkClientPod(f, ns, podName, target, targetPort)
524541
defer func() {
525542
By(fmt.Sprintf("Cleaning up the pod %s", podName))
526543
if err := f.ClientSet.Core().Pods(ns.Name).Delete(podClient.Name, nil); err != nil {

0 commit comments

Comments
 (0)