forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_port_exclusion_test.go
200 lines (164 loc) · 6.51 KB
/
e2e_port_exclusion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package e2e
import (
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/openservicemesh/osm/tests/framework"
)
var _ = OSMDescribe("Tests traffic via port exclusion",
OSMDescribeInfo{
Tier: 2,
Bucket: 7,
},
func() {
Context("Test global port exclusion", func() {
testGlobalPortExclusion()
})
Context("Test pod level port exclusion", func() {
testPodLevelPortExclusion()
})
})
func testGlobalPortExclusion() {
const sourceName = "client"
const destName = "server"
var ns = []string{sourceName, destName}
It("Tests HTTP traffic to external server via global port exclusion", func() {
// Install OSM
installOpts := Td.GetOSMInstallOpts()
installOpts.EnablePermissiveMode = false // explicitly set to false to demonstrate port exclusion
Expect(Td.InstallOSM(installOpts)).To(Succeed())
meshConfig, _ := Td.GetMeshConfig(Td.OsmNamespace)
// Create Test NS
for _, n := range ns {
Expect(Td.CreateNs(n, nil)).To(Succeed())
}
// Only add source namespace to the mesh, destination is simulating an external cluster
Expect(Td.AddNsToMesh(true, sourceName)).To(Succeed())
// Set up the destination HTTP server. It is not part of the mesh
svcAccDef, podDef, svcDef, err := Td.SimplePodApp(
SimplePodAppDef{
PodName: destName,
Namespace: destName,
Image: "kennethreitz/httpbin",
Ports: []int{80},
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateServiceAccount(destName, &svcAccDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreatePod(destName, podDef)
Expect(err).NotTo(HaveOccurred())
dstSvc, err := Td.CreateService(destName, svcDef)
Expect(err).NotTo(HaveOccurred())
// Expect it to be up and running in it's receiver namespace
Expect(Td.WaitForPodsRunningReady(destName, 90*time.Second, 1, nil)).To(Succeed())
// The destination port will be programmed as an global port exclusion
destinationPort := int(dstSvc.Spec.Ports[0].Port)
meshConfig.Spec.Traffic.OutboundPortExclusionList = []int{destinationPort}
_, err = Td.UpdateOSMConfig(meshConfig)
Expect(err).NotTo(HaveOccurred())
srcPod := setupSource(sourceName, false)
By("Using global port exclusion to access destination")
// All ready. Expect client to reach server
clientToServer := HTTPRequestDef{
SourceNs: sourceName,
SourcePod: srcPod.Name,
SourceContainer: srcPod.Name,
Destination: fmt.Sprintf("%s.%s", dstSvc.Name, dstSvc.Namespace),
}
srcToDestStr := fmt.Sprintf("%s -> %s",
fmt.Sprintf("%s/%s", sourceName, srcPod.Name),
clientToServer.Destination)
cond := Td.WaitForRepeatedSuccess(func() bool {
result := Td.HTTPRequest(clientToServer)
if result.Err != nil || result.StatusCode != 200 {
Td.T.Logf("> (%s) HTTP Req failed %d %v",
srcToDestStr, result.StatusCode, result.Err)
return false
}
Td.T.Logf("> (%s) HTTP Req succeeded: %d", srcToDestStr, result.StatusCode)
return true
}, 5, 90*time.Second)
Expect(cond).To(BeTrue(), "Failed testing HTTP traffic from source pod %s to destination with port %s", srcPod.Name, destinationPort)
})
}
func testPodLevelPortExclusion() {
// XXX(3755): use a different namespace due to test pollution
const sourceName = "client1"
const destName = "server1"
var ns = []string{sourceName, destName}
It("Tests HTTP traffic to external server via pod level port exclusion", func() {
// Install OSM
installOpts := Td.GetOSMInstallOpts()
installOpts.EnablePermissiveMode = false // explicitly set to false to demonstrate port exclusion
Expect(Td.InstallOSM(installOpts)).To(Succeed())
// Create Test NS
for _, n := range ns {
Expect(Td.CreateNs(n, nil)).To(Succeed())
}
// Only add source namespace to the mesh, destination is simulating an external cluster
Expect(Td.AddNsToMesh(true, sourceName)).To(Succeed())
// Set up the destination HTTP server. It is not part of the mesh
svcAccDef, podDef, svcDef, err := Td.SimplePodApp(
SimplePodAppDef{
PodName: destName,
Namespace: destName,
Image: "kennethreitz/httpbin",
Ports: []int{80},
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateServiceAccount(destName, &svcAccDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreatePod(destName, podDef)
Expect(err).NotTo(HaveOccurred())
dstSvc, err := Td.CreateService(destName, svcDef)
Expect(err).NotTo(HaveOccurred())
// Expect it to be up and running in it's receiver namespace
Expect(Td.WaitForPodsRunningReady(destName, 90*time.Second, 1, nil)).To(Succeed())
// Set up the source curl client. It will be a part of the mesh
svcAccDef, podDef, svcDef, err = Td.SimplePodApp(SimplePodAppDef{
PodName: sourceName,
Namespace: sourceName,
Command: []string{"sleep", "365d"},
Image: "curlimages/curl",
Ports: []int{80},
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateServiceAccount(sourceName, &svcAccDef)
Expect(err).NotTo(HaveOccurred())
// The destination port will be programmed as an annotation on the source pod for port exclusion
destinationPort := fmt.Sprintf("%v", dstSvc.Spec.Ports[0].Port)
podDef.Annotations = map[string]string{"openservicemesh.io/outbound-port-exclusion-list": destinationPort}
srcPod, err := Td.CreatePod(sourceName, podDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateService(sourceName, svcDef)
Expect(err).NotTo(HaveOccurred())
// Expect it to be up and running in it's receiver namespace
Expect(Td.WaitForPodsRunningReady(sourceName, 90*time.Second, 1, nil)).To(Succeed())
By("Using pod level port exclusion to access destination")
// All ready. Expect client to reach server
clientToServer := HTTPRequestDef{
SourceNs: sourceName,
SourcePod: srcPod.Name,
SourceContainer: srcPod.Name,
Destination: fmt.Sprintf("%s.%s", dstSvc.Name, dstSvc.Namespace),
}
srcToDestStr := fmt.Sprintf("%s -> %s",
fmt.Sprintf("%s/%s", sourceName, srcPod.Name),
clientToServer.Destination)
cond := Td.WaitForRepeatedSuccess(func() bool {
result := Td.HTTPRequest(clientToServer)
if result.Err != nil || result.StatusCode != 200 {
Td.T.Logf("> (%s) HTTP Req failed %d %v",
srcToDestStr, result.StatusCode, result.Err)
return false
}
Td.T.Logf("> (%s) HTTP Req succeeded: %d", srcToDestStr, result.StatusCode)
return true
}, 5, 90*time.Second)
Expect(cond).To(BeTrue(), "Failed testing HTTP traffic from source pod %s to destination with port %s", srcPod.Name, destinationPort)
})
}