Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 0b3dc49

Browse files
authored
Merge pull request #1244 from adrianludwin/netpol-test
Fix netpol e2e test
2 parents 639dcd6 + 8517427 commit 0b3dc49

File tree

2 files changed

+46
-51
lines changed

2 files changed

+46
-51
lines changed

incubator/hnc/pkg/testutils/testutils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ func runShouldNotContainMultiple(offset int, substrs []string, seconds float64,
202202
}
203203

204204
func MustApplyYAML(s string){
205-
filename := WriteTempFile(s)
206-
defer RemoveFile(filename)
205+
filename := writeTempFile(s)
206+
defer removeFile(filename)
207207
MustRun("kubectl apply -f", filename)
208208
}
209209

@@ -336,15 +336,15 @@ func RecoverHNC() {
336336
CleanupNamespaces(a, b)
337337
}
338338

339-
func WriteTempFile(cxt string) string {
339+
func writeTempFile(cxt string) string {
340340
f, err := ioutil.TempFile(os.TempDir(), "e2e-test-*.yaml")
341341
Expect(err).Should(BeNil())
342342
defer f.Close()
343343
f.WriteString(cxt)
344344
return f.Name()
345345
}
346346

347-
func RemoveFile(path string) {
347+
func removeFile(path string) {
348348
Expect(os.Remove(path)).Should(BeNil())
349349
}
350350

incubator/hnc/test/e2e/quickstart_test.go

+42-47
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
. "github.com/onsi/ginkgo"
8-
. "github.com/onsi/gomega"
98
. "sigs.k8s.io/multi-tenancy/incubator/hnc/pkg/testutils"
109
)
1110

@@ -104,27 +103,30 @@ var _ = Describe("Quickstart", func() {
104103
})
105104

106105
It("Should intergrate hierarchical network policy", func(){
107-
GinkgoT().Log("WARNING: IF THIS TEST FAILS, PLEASE CHECK THAT THE NETWORK POLICY IS ENABLED ON THE TEST CLUSTER")
108-
109106
MustRun("kubectl create ns", nsOrg)
110107
MustRun("kubectl hns create", nsTeamA, "-n", nsOrg)
111108
MustRun("kubectl hns create", nsTeamB, "-n", nsOrg)
112109
MustRun("kubectl hns create", nsService1, "-n", nsTeamA)
113110
MustRun("kubectl hns create", nsService2, "-n", nsTeamA)
114111
// create a web service s2 in namespace service-2, and a client pod client-s1 in namespace service-1 that can access this web service
115112
MustRun("kubectl run s2 -n", nsService2, "--image=nginx --restart=Never --expose --port 80")
116-
clientArgs := "-i --image=alpine --restart=Never --rm -- sh -c"
117-
cmdln := "\"wget -qO- --timeout 2 http://s2.service-2\""
118-
// at least 20 seconds is needed here from experiments
119-
RunShouldContain("Welcome to nginx!", 20,
120-
"kubectl run client -n", nsService1, clientArgs, cmdln)
121-
RunShouldContain("Welcome to nginx!", cleanupTimeout,
122-
"kubectl run client -n", nsTeamA, clientArgs, cmdln)
123-
RunShouldContain("Welcome to nginx!", cleanupTimeout,
124-
"kubectl run client -n", nsTeamB, clientArgs, cmdln)
125-
126-
// create a default network policy that blocks any ingress from other namespaces
127-
policy := `# temp file created by quickstart_test.go
113+
114+
// Ensure that we can access the service from various other namespaces
115+
const (
116+
clientCmd = "kubectl run client -n"
117+
alpineArgs = "-i --image=alpine --restart=Never --rm -- sh -c"
118+
119+
// These need to be separate from alpineArgs because RunCommand only understands quoted args
120+
// if the double-quotes appears at the beginning and end of a single string.
121+
wgetArgs = "\"wget -qO- --timeout 2 http://s2.service-2\""
122+
)
123+
// Up to 20 seconds is needed for the service to first come up from experiments
124+
RunShouldContain("Welcome to nginx!", 20, clientCmd, nsService1, alpineArgs, wgetArgs)
125+
RunShouldContain("Welcome to nginx!", defTimeout, clientCmd, nsTeamA, alpineArgs, wgetArgs)
126+
RunShouldContain("Welcome to nginx!", defTimeout, clientCmd, nsTeamB, alpineArgs, wgetArgs)
127+
128+
// create a default network policy in the root namespace that blocks any ingress from other namespaces
129+
policy := `# quickstart_test.go: netpol to block access across namespaces
128130
kind: NetworkPolicy
129131
apiVersion: networking.k8s.io/v1
130132
metadata:
@@ -137,33 +139,35 @@ spec:
137139
- from:
138140
- podSelector: {}`
139141

140-
filename := WriteTempFile(policy)
141-
defer RemoveFile(filename)
142-
MustRun("kubectl apply -f", filename)
143-
// ensure this policy can be propagated to its descendants
142+
MustApplyYAML(policy)
143+
// Enable propagation for netpols and wait for it to get propagated at least to service-1
144144
MustRun("kubectl hns config set-resource networkpolicies --group networking.k8s.io --mode Propagate --force")
145-
expected := "deny-from-other-namespaces"
146-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsOrg)
147-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsTeamA)
148-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsTeamB)
149-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsService1)
150-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsService2)
145+
RunShouldContain("deny-from-other-namespaces", defTimeout, "kubectl get netpol -n", nsService1)
151146

152147
// Now we’ll see that we can no longer access service-2 from the client in service-1. If we can,
153148
// that probably means that network policies aren't enabled on this cluster (e.g. Kind, GKE by
154149
// default) and we should skip the rest of this test.
155-
netpolTestStdout := ""
156-
Eventually(func() error {
157-
stdout, err := RunCommand("kubectl run client -n", nsService1, clientArgs, cmdln)
158-
netpolTestStdout = stdout
159-
return err
160-
}).Should(Succeed())
161-
if !strings.Contains(netpolTestStdout, "wget: download timed out") {
150+
//
151+
// The standard matching functions won't work here because we're looking for a particular error
152+
// string, but we don't want to fail if we've found it. So use the default timeout (2s) by
153+
// trying up to three times with a 1s gap in between.
154+
netpolWorks := false
155+
for i:=0; !netpolWorks && i<3; i++ {
156+
// This command will return a non-nil error if it works correctly
157+
stdout, _ := RunCommand(clientCmd, nsService1, alpineArgs, wgetArgs)
158+
if strings.Contains(stdout, "wget: download timed out") {
159+
netpolWorks = true
160+
}
161+
time.Sleep(1 * time.Second)
162+
}
163+
if !netpolWorks {
162164
Skip("Basic network policies don't appear to be working; skipping the netpol quickstart")
163165
}
164166

165-
// create a second network policy that will allow all namespaces within team-a to be able to communicate with each other
166-
policy = `# temp file created by quickstart_test.go
167+
// create a second network policy that will allow all namespaces within team-a to be able to
168+
// communicate with each other, and wait for it to be propagated to the descendant we want to
169+
// test.
170+
policy = `# quickstart_test.go: netpol to allow communication within team-a subtree
167171
kind: NetworkPolicy
168172
apiVersion: networking.k8s.io/v1
169173
metadata:
@@ -178,21 +182,12 @@ spec:
178182
matchExpressions:
179183
- key: 'team-a.tree.hnc.x-k8s.io/depth'
180184
operator: Exists`
181-
182-
filename2 := WriteTempFile(policy)
183-
defer RemoveFile(filename2)
184-
MustRun("kubectl apply -f", filename2)
185-
186-
expected = "allow-team-a"
187-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsTeamA)
188-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsService1)
189-
RunShouldContain(expected, defTimeout, "kubectl get netpol -n", nsService2)
185+
MustApplyYAML(policy)
186+
RunShouldContain("allow-team-a", defTimeout, "kubectl get netpol -n", nsService1)
190187

191188
// Now, we can access the service from other namespaces in team-a, but not outside of it:
192-
RunShouldContain("Welcome to nginx!", cleanupTimeout,
193-
"kubectl run client -n", nsService1, clientArgs, cmdln)
194-
RunErrorShouldContain("wget: download timed out", cleanupTimeout,
195-
"kubectl run client -n", nsTeamB, clientArgs, cmdln)
189+
RunShouldContain("Welcome to nginx!", defTimeout, clientCmd, nsService1, alpineArgs, wgetArgs)
190+
RunErrorShouldContain("wget: download timed out", defTimeout, clientCmd, nsTeamB, alpineArgs, wgetArgs)
196191
})
197192

198193
It("Should create and delete subnamespaces", func(){

0 commit comments

Comments
 (0)