forked from cloudfoundry/cf-networking-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoke_test.go
198 lines (164 loc) · 5.21 KB
/
smoke_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
package smoke_test
import (
"fmt"
"io"
"net"
"net/http"
"time"
"code.cloudfoundry.org/cf-pusher/cf_cli_adapter"
"github.com/cloudfoundry/cf-test-helpers/v2/cf"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const Timeout_Short = 10 * time.Second
var _ = Describe("connectivity between containers on the overlay network", func() {
Describe("networking policy", func() {
var (
appProxy string
appSmoke string
appInstances int
prefix string
spaceName string
orgName string
cfCli *cf_cli_adapter.Adapter
)
BeforeEach(func() {
prefix = config.Prefix
if config.SmokeOrg == "" {
orgName = prefix + "org"
Expect(cf.Cf("create-org", orgName).Wait(Timeout_Push)).To(gexec.Exit(0))
} else {
orgName = config.SmokeOrg
}
Expect(cf.Cf("target", "-o", orgName).Wait(Timeout_Push)).To(gexec.Exit(0))
spaceName = prefix + "inter-container-connectivity"
Expect(cf.Cf("create-space", spaceName).Wait(Timeout_Push)).To(gexec.Exit(0))
Expect(cf.Cf("target", "-o", orgName, "-s", spaceName).Wait(Timeout_Push)).To(gexec.Exit(0))
Expect(cf.Cf("set-space-role", config.SmokeUser, orgName, spaceName, "SpaceDeveloper").Wait(Timeout_Push)).To(gexec.Exit(0))
appInstances = config.AppInstances
appProxy = prefix + "proxy"
appSmoke = prefix + "smoke"
cfCli = cf_cli_adapter.NewAdapter()
})
AfterEach(func() {
appReport(appProxy, Timeout_Short)
appReport(appSmoke, Timeout_Short)
Expect(cf.Cf("delete-space", spaceName, "-f").Wait(Timeout_Push)).To(gexec.Exit(0))
})
It("allows the user to configure policies", func() {
pushApp(appProxy, "proxy")
pushApp(appSmoke, "smoke", "--no-start")
setEnv(appSmoke, "PROXY_APP_URL", fmt.Sprintf("http://%s.%s", appProxy, config.AppsDomain))
start(appSmoke)
scaleApp(appSmoke, appInstances)
ports := []int{8080}
appsSmoke := []string{appSmoke}
By("checking that the connection fails")
runWithTimeout("check connection failures", 5*time.Minute, func() {
assertConnectionFails(appSmoke, appInstances)
})
By("creating policies")
createAllPolicies(appProxy, appsSmoke, ports, cfCli)
// we should wait for minimum (pollInterval * 2)
By("waiting for policies to be created on cells")
time.Sleep(10 * time.Second)
By(fmt.Sprintf("checking that %s can reach %s", appProxy, appsSmoke))
runWithTimeout("check connection success", 5*time.Minute, func() {
assertConnectionSucceeds(appSmoke, appInstances)
})
By("deleting policies")
deleteAllPolicies(appProxy, appsSmoke, ports, cfCli)
By(fmt.Sprintf("checking that %s can NOT reach %s", appProxy, appsSmoke))
runWithTimeout("check connection failures, again", 5*time.Minute, func() {
assertConnectionFails(appSmoke, appInstances)
})
})
})
})
func createAllPolicies(sourceApp string, dstList []string, dstPorts []int, cfCli *cf_cli_adapter.Adapter) {
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, cfCli *cf_cli_adapter.Adapter) {
for _, destApp := range dstList {
for _, port := range dstPorts {
err := cfCli.RemoveNetworkPolicy(sourceApp, destApp, port, "tcp")
Expect(err).NotTo(HaveOccurred())
}
}
}
func runWithTimeout(operation string, timeout time.Duration, work func()) {
done := make(chan bool)
go func() {
defer GinkgoRecover()
fmt.Printf("starting %s\n", operation)
work()
done <- true
}()
select {
case <-done:
fmt.Printf("completed %s\n", operation)
return
case <-time.After(timeout):
Fail("timeout on " + operation)
}
}
func assertConnectionSucceeds(sourceApp string, appInstances int) {
for i := 0; i < appInstances; i++ {
assertSingleConnection(sourceApp, true)
}
}
func assertConnectionFails(sourceApp string, appInstances int) {
for i := 0; i < appInstances; i++ {
assertSingleConnection(sourceApp, false)
}
}
func assertSingleConnection(sourceAppName string, shouldSucceed bool) {
if shouldSucceed {
By("eventually smoke should reach itself")
assertResponseContains(sourceAppName, "OK")
} else {
By("eventually smoke should NOT reach itself")
assertResponseContains(sourceAppName, "FAILED")
}
}
func assertResponseContains(sourceAppName string, desiredResponse string) {
proxyTest := func() (string, error) {
resp, err := httpGetBytes(fmt.Sprintf("http://%s.%s/selfproxy", sourceAppName, config.AppsDomain))
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,
},
}
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
}