forked from cloudfoundry/cf-networking-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinter_container_connectivity_test.go
272 lines (227 loc) · 8.09 KB
/
inter_container_connectivity_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package acceptance_test
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"time"
"github.com/cloudfoundry/cf-test-helpers/v2/cf"
helpers "github.com/cloudfoundry/cf-test-helpers/v2/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const Timeout_Short = 10 * time.Second
const PolicyWaitTime = 7 * time.Second
var ports []int
var _ = Describe("connectivity between containers on the overlay network", func() {
Describe("networking policy", func() {
var (
appsProxy []string
appRegistry string
appsTest []string
appInstances int
applications int
proxyApplications int
proxyInstances int
prefix string
orgName string
)
BeforeEach(func() {
prefix = testConfig.Prefix
orgName = prefix + "org" // cf-pusher expects this name
Expect(cf.Cf("create-org", orgName).Wait(Timeout_Push)).To(gexec.Exit(0))
Expect(cf.Cf("target", "-o", orgName).Wait(Timeout_Push)).To(gexec.Exit(0))
spaceName := prefix + "space" // cf-pusher expects this name
Expect(cf.Cf("create-space", spaceName, "-o", orgName).Wait(Timeout_Push)).To(gexec.Exit(0))
Expect(cf.Cf("target", "-o", orgName, "-s", spaceName).Wait(Timeout_Push)).To(gexec.Exit(0))
appInstances = testConfig.AppInstances
applications = testConfig.Applications
proxyApplications = testConfig.ProxyApplications
proxyInstances = testConfig.ProxyInstances
for i := 0; i < proxyApplications; i++ {
appsProxy = append(appsProxy, fmt.Sprintf(prefix+"proxy-%d", i))
}
appRegistry = prefix + "registry"
for i := 0; i < applications; i++ {
appsTest = append(appsTest, fmt.Sprintf(prefix+"tick-%d", i))
}
ports = []int{8080}
for i := 0; i < testConfig.ExtraListenPorts; i++ {
ports = append(ports, 7000+i)
}
})
AfterEach(func() {
appsProxy = nil
appsTest = nil
ports = []int{8080}
Expect(cf.Cf("delete-org", orgName, "-f").Wait(Timeout_Push)).To(gexec.Exit(0))
_, err := cfCLI.CleanupStaleNetworkPolicies()
Expect(err).NotTo(HaveOccurred())
})
It("allows policies to whitelist traffic between applications", func(ctx SpecContext) {
cmd := exec.Command("go", "run", "../../cf-pusher/cmd/cf-pusher/main.go", "--config", helpers.ConfigPath())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Println("\n----- cf-pusher start ------")
err := cmd.Run()
fmt.Println("\n----- cf-pusher done -------")
Expect(err).NotTo(HaveOccurred())
By("checking that all test app instances have registered themselves")
checkRegistry(appRegistry, 60*time.Second, 500*time.Millisecond, len(appsTest)*appInstances)
appIPs := getAppIPs(appRegistry)
By("checking that the connection fails")
for _, appProxy := range appsProxy {
By(fmt.Sprintf("checking that %s can NOT reach %s", appProxy, appsTest))
assertConnectionsFail(appProxy, appIPs, ports, proxyInstances)
}
By("creating policies")
for _, appProxy := range appsProxy {
createAllPolicies(appProxy, appsTest, ports)
}
// we should wait for minimum (pollInterval * 2)
By(fmt.Sprintf("waiting %s for policies to be created on cells", PolicyWaitTime))
time.Sleep(PolicyWaitTime)
By("checking that the connection succeeds")
for _, appProxy := range appsProxy {
By(fmt.Sprintf("checking that %s can reach %s", appProxy, appsTest))
assertConnectionsSucceed(appProxy, appIPs, ports, proxyInstances)
}
By("deleting policies")
for _, appProxy := range appsProxy {
deleteAllPolicies(appProxy, appsTest, ports)
}
By(fmt.Sprintf("waiting %s for policies to be deleted on cells", PolicyWaitTime))
time.Sleep(PolicyWaitTime)
By("checking that the connection fails, again")
for _, appProxy := range appsProxy {
By(fmt.Sprintf("checking that %s can NOT reach %s", appProxy, appsTest))
assertConnectionsFail(appProxy, appIPs, ports, proxyInstances)
}
By("checking that the registry updates when apps are scaled")
scaleApps(appsTest, 1 /* instances */)
checkRegistry(appRegistry, 60*time.Second, 500*time.Millisecond, len(appsTest))
scaleApps(appsTest, appInstances /* instances */)
checkRegistry(appRegistry, 60*time.Second, 500*time.Millisecond, len(appsTest)*appInstances)
}, SpecTimeout(30*time.Minute))
})
})
func createAllPolicies(sourceApp string, dstList []string, dstPorts []int) {
for _, destApp := range dstList {
for _, port := range dstPorts {
err := cfCLI.AddNetworkPolicy(sourceApp, destApp, port, "tcp")
Expect(err).NotTo(HaveOccurred())
}
}
}
func deleteAllPolicies(sourceApp string, dstList []string, dstPorts []int) {
for _, destApp := range dstList {
for _, port := range dstPorts {
err := cfCLI.RemoveNetworkPolicy(sourceApp, destApp, port, "tcp")
Expect(err).NotTo(HaveOccurred())
}
}
}
type RegistryInstancesResponse struct {
Instances []struct {
ServiceName string `json:"service_name"`
Endpoint struct {
Value string `json:"value"`
} `json:"endpoint"`
} `json:"instances"`
}
func getInstancesFromA8(registry string) (*RegistryInstancesResponse, error) {
resp, err := httpGetBytes(fmt.Sprintf("http://%s.%s/api/v1/instances", registry, config.AppsDomain))
if err != nil {
return nil, err
}
Expect(resp.StatusCode).To(Equal(http.StatusOK))
var instancesResponse RegistryInstancesResponse
err = json.Unmarshal(resp.Body, &instancesResponse)
if err != nil {
return nil, err
}
return &instancesResponse, nil
}
func checkRegistry(registry string, timeout, pollingInterval time.Duration, totalInstances int) {
registeredApps := func() (int, error) {
instancesResponse, err := getInstancesFromA8(registry)
if err != nil {
return 0, err
}
return len(instancesResponse.Instances), nil
}
Eventually(registeredApps, timeout, pollingInterval).Should(Equal(totalInstances))
}
func getAppIPs(registry string) []string {
instancesResponse, err := getInstancesFromA8(registry)
Expect(err).NotTo(HaveOccurred())
ips := []string{}
for _, instance := range instancesResponse.Instances {
ip, _, err := net.SplitHostPort(instance.Endpoint.Value)
Expect(err).NotTo(HaveOccurred())
ips = append(ips, ip)
}
return ips
}
func assertConnectionsSucceed(sourceApp string, destApps []string, ports []int, nProxies int) {
assertConnections(sourceApp, destApps, ports, nProxies, true)
}
func assertConnectionsFail(sourceApp string, destApps []string, ports []int, nProxies int) {
assertConnections(sourceApp, destApps, ports, nProxies, false)
}
func assertConnections(sourceApp string, destApps []string, ports []int, nProxies int, shouldSucceed bool) {
for _, appIP := range destApps {
for _, port := range ports {
assertSingleConnection(appIP, port, sourceApp, shouldSucceed)
}
}
}
func assertSingleConnection(destIP string, port int, sourceAppName string, shouldSucceed bool) {
if shouldSucceed {
By(fmt.Sprintf("eventually proxy should reach %s at port %d", destIP, port))
assertResponseContains(destIP, port, sourceAppName, "application_name")
} else {
By(fmt.Sprintf("eventually proxy should NOT reach %s at port %d", destIP, port))
assertResponseContains(destIP, port, sourceAppName, "request failed")
}
}
func assertResponseContains(destIP string, port int, sourceAppName string, desiredResponse string) {
proxyTest := func() (string, error) {
resp, err := httpGetBytes(fmt.Sprintf("http://%s.%s/proxy/%s:%d", sourceAppName, config.AppsDomain, destIP, port))
if err != nil {
return "", err
}
return string(resp.Body), nil
}
Eventually(proxyTest, 10*time.Second, 500*time.Millisecond).Should(ContainSubstring(desiredResponse))
}
var httpClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
Dial: (&net.Dialer{
Timeout: 4 * time.Second,
KeepAlive: 0,
}).Dial,
},
Timeout: 20 * time.Second,
}
type httpResp struct {
StatusCode int
Body []byte
}
func httpGetBytes(url string) (httpResp, error) {
resp, err := httpClient.Get(url)
if err != nil {
return httpResp{}, err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return httpResp{}, err
}
return httpResp{resp.StatusCode, respBytes}, nil
}