-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdeployment_test.go
399 lines (347 loc) · 11.5 KB
/
deployment_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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package e2e
import (
"fmt"
"io"
"net/http"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
var httpClient *http.Client
type getPodSize func(podName string) float64
func requestPrometheusString() string {
var resp *http.Response
req, err := http.NewRequest("GET", "http://127.0.0.1:9100/metrics", nil)
if err != nil {
panic(err)
}
for {
// Send the request
resp, err = httpClient.Do(req)
if err != nil {
time.Sleep(time.Second * 1)
continue
}
break
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
output := string(body)
err = resp.Body.Close()
if err != nil {
panic(err)
}
return output
}
func CheckValues(ifFound map[string]bool) int {
var status int
for _, value := range ifFound {
if value {
status = 1
continue
}
status = 0
}
return status
}
func checkPrometheus(checkSlice []string, inverse bool) {
var status int
timeout := time.Second * 180
startTime := time.Now()
ifFound := make(map[string]bool)
// Add values to IFound Map
for _, a := range checkSlice {
ifFound[a] = false
}
for {
elapsed := time.Since(startTime)
if elapsed >= timeout {
break
}
status = 0
time.Sleep(1 * time.Second)
output := requestPrometheusString()
// Print the response
for _, a := range checkSlice {
if ifFound[a] {
continue
}
if inverse && !strings.Contains(output, a) {
ifFound[a] = true
} else if !inverse && strings.Contains(output, a) {
ifFound[a] = true
}
status = CheckValues(ifFound)
}
if status == 1 {
break
}
}
for key, value := range ifFound {
if value {
if inverse {
ginkgo.GinkgoWriter.Printf("\nDid not find value: [ %v ] in prometheus exporter\n", key)
} else {
ginkgo.GinkgoWriter.Printf("\nFound value: [ %v ] in prometheus exporter\n", key)
}
continue
}
ginkgo.GinkgoWriter.Printf("\nDid not find value: [ %v ] in prometheus exporter\n", key)
}
gomega.Expect(status).Should(gomega.Equal(1))
}
func WatchContainerPercentage() {
status := 0
re := regexp.MustCompile(`ephemeral_storage_container_limit_percentage{container="grow-test",node_name="minikube".+,pod_namespace="ephemeral-metrics",source="container"}\s+(.+)`)
output := requestPrometheusString()
match := re.FindAllStringSubmatch(output, -1)
gomega.Expect(match).ShouldNot(gomega.BeEmpty())
floatValue, _ := strconv.ParseFloat(match[0][1], 64)
if floatValue < 100.0 {
status = 1
}
gomega.Expect(status).Should(gomega.Equal(1))
}
func WatchContainerVolumePercentage() {
status := 0
timeout := time.Second * 180
startTime := time.Now()
for {
elapsed := time.Since(startTime)
if elapsed >= timeout {
break
}
re := regexp.MustCompile(`ephemeral_storage_container_volume_limit_percentage{container="shrink-test",mount_path="\/cache".+volume_name="cache-volume-1"}\s+(.+)`)
output := requestPrometheusString()
match := re.FindAllStringSubmatch(output, -1)
if match == nil {
continue
}
floatValue, _ := strconv.ParseFloat(match[0][1], 64)
if floatValue < 100.0 {
status = 1
break
}
}
gomega.Expect(status).Should(gomega.Equal(1))
}
func WatchNodePercentage() {
status := 0
re := regexp.MustCompile(`ephemeral_storage_node_percentage\{node_name="minikube"}\s+(.+)`)
output := requestPrometheusString()
match := re.FindAllStringSubmatch(output, -1)
floatValue, _ := strconv.ParseFloat(match[0][1], 64)
if floatValue < 100.0 {
status = 1
}
gomega.Expect(status).Should(gomega.Equal(1))
}
func WatchPollingRate(pollRateUpper float64, pollingRateLower float64, timeout time.Duration) {
var currentPollRate float64
status := 0
startTime := time.Now()
re := regexp.MustCompile(`ephemeral_storage_adjusted_polling_rate\{node_name="minikube"}\s+(.+)`)
for {
elapsed := time.Since(startTime)
if elapsed >= timeout {
ginkgo.GinkgoWriter.Printf("\nFailed: \n\tephemeral_storage_adjusted_polling_rate: %f\n", currentPollRate)
break
}
output := requestPrometheusString()
match := re.FindAllStringSubmatch(output, -1)
currentPollRate, _ = strconv.ParseFloat(match[0][1], 64)
if pollRateUpper >= currentPollRate && pollingRateLower <= currentPollRate {
status = 1
break
}
time.Sleep(5 * time.Second)
}
gomega.Expect(status).Should(gomega.Equal(1))
ginkgo.GinkgoWriter.Printf("\nSuccess: \n\tephemeral_storage_adjusted_polling_rate: %f\n", currentPollRate)
}
func getPodUsageSize(podName string) float64 {
output := requestPrometheusString()
re := regexp.MustCompile(fmt.Sprintf(`ephemeral_storage_pod_usage.+pod_name="%s.+\}\s(.+)`, podName))
match := re.FindAllStringSubmatch(output, 2)
currentPodSize, _ := strconv.ParseFloat(match[0][1], 64)
return currentPodSize
}
func getContainerLimitPercentage(podName string) float64 {
output := requestPrometheusString()
re := regexp.MustCompile(fmt.Sprintf(`ephemeral_storage_container_limit_percentage.+pod_name="%s.+\}\s(.+)`, podName))
match := re.FindAllStringSubmatch(output, 2)
currentPodSize, _ := strconv.ParseFloat(match[0][1], 64)
return currentPodSize
}
func getContainerVolumeLimitPercentage(podName string) float64 {
output := requestPrometheusString()
re := regexp.MustCompile(
fmt.Sprintf(`ephemeral_storage_container_volume_limit_percentage.+container="%s",mount_path="\/cache".+\}\s(.+)`,
podName))
match := re.FindAllStringSubmatch(output, 2)
currentPodSize, _ := strconv.ParseFloat(match[0][1], 64)
return currentPodSize
}
func getContainerVolumeUsage(podName string) float64 {
output := requestPrometheusString()
re := regexp.MustCompile(
fmt.Sprintf(`ephemeral_storage_container_volume_usage.+container="%s",mount_path="\/cache".+\}\s(.+)`,
podName))
match := re.FindAllStringSubmatch(output, 2)
currentPodSize, _ := strconv.ParseFloat(match[0][1], 64)
return currentPodSize
}
func WatchEphemeralSize(podName string, desiredSizeChange float64, timeout time.Duration, getPodSize getPodSize) {
// Watch Prometheus Metrics until the ephemeral storage shrinks or grows to a certain desiredSizeChange.
var currentPodSize float64
var targetSizeChange float64
startTime := time.Now()
status := 0
initSize := getPodSize(podName)
if podName == "grow-test" {
targetSizeChange = initSize + desiredSizeChange
} else if podName == "shrink-test" {
targetSizeChange = initSize - desiredSizeChange
}
for {
elapsed := time.Since(startTime)
if elapsed >= timeout {
ginkgo.GinkgoWriter.Printf("\nWatch for metrics has timed out for pod %s", podName)
break
}
currentPodSize = getPodSize(podName)
if podName == "grow-test" && currentPodSize >= targetSizeChange {
status = 1
} else if podName == "shrink-test" && currentPodSize <= targetSizeChange {
status = 1
}
if status == 1 {
ginkgo.GinkgoWriter.Printf("\nSuccess: \n\tPod name: %s \n\tTarget size: %f \n\tCurrent size: %f\n", podName, targetSizeChange, currentPodSize)
break
}
ginkgo.GinkgoWriter.Printf("\nPending: \n\tPod name: %s \n\tTarget size: %f \n\tCurrent size: %f\n", podName, targetSizeChange, currentPodSize)
time.Sleep(time.Second * 5)
}
gomega.Expect(status).Should(gomega.Equal(1))
}
func scaleUp() {
cmd := exec.Command("make", "minikube_scale_up")
cmd.Dir = "../.."
_, err := cmd.Output()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
func scaleDown() {
cmd := exec.Command("make", "minikube_scale_down")
cmd.Dir = "../.."
_, err := cmd.Output()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
var _ = ginkgo.Describe("Test Metrics\n", func() {
ginkgo.Context("Observe labels\n", func() {
ginkgo.Specify("\nMake sure all metrics are in the exporter", func() {
var checkSlice []string
checkSlice = append(checkSlice, "ephemeral_storage_pod_usage",
"pod_name=\"k8s-ephemeral-storage",
"ephemeral_storage_adjusted_polling_rate",
"node_name=\"minikube",
"ephemeral_storage_node_available",
"ephemeral_storage_node_capacity",
"ephemeral_storage_node_percentage",
"ephemeral_storage_container_limit_percentage",
"ephemeral_storage_container_volume_limit_percentage",
"ephemeral_storage_container_volume_usage",
"ephemeral_storage_inodes",
"ephemeral_storage_inodes_free",
"ephemeral_storage_inodes_used",
)
checkPrometheus(checkSlice, false)
})
})
ginkgo.Context("Test Polling speed\n", func() {
ginkgo.Specify("\nMake sure Adjusted Poll rate is between 5000 - 4000 ms", func() {
WatchPollingRate(5000.0, 4000.0, time.Second*90)
})
})
ginkgo.Context("Observe change in ephemeral_storage_pod_usage metric\n", func() {
ginkgo.Specify("\nWatch Pod grow to 100000 Bytes", func() {
WatchEphemeralSize("grow-test", 100000, time.Second*180, getPodUsageSize)
})
ginkgo.Specify("\nWatch Pod shrink to 100000 Bytes", func() {
// Shrinking of ephemeral_storage reflects slower from Node API up to 5 minutes.
// Wait until it's reporting correctly, and start testing with the minimum of 11mb of data
// since the shrink container adds 12mb then decrements 12k a second.
// Ex. /api/v1/nodes/minikube/proxy/stats/summary
for {
currentPodSize := getPodUsageSize("shrink-test")
if currentPodSize >= 11000000.0 {
break
}
time.Sleep(time.Second * 5)
}
WatchEphemeralSize("shrink-test", 100000, time.Second*180, getPodUsageSize)
})
})
ginkgo.Context("Observe change in ephemeral_storage_container_limit_percentage metric\n", func() {
ginkgo.Specify("\nWatch Pod grow to 0.2 percent", func() {
WatchEphemeralSize("grow-test", 0.2, time.Second*180, getContainerLimitPercentage)
})
ginkgo.Specify("\nWatch Pod shrink to 0.2 percent", func() {
WatchEphemeralSize("shrink-test", 0.2, time.Second*180, getContainerLimitPercentage)
})
})
ginkgo.Context("Observe change in ephemeral_storage_container_volume_limit_percentage metric\n", func() {
ginkgo.Specify("\nWatch Pod grow to 0.2 percent", func() {
WatchEphemeralSize("grow-test", 0.2, time.Second*180, getContainerVolumeLimitPercentage)
})
ginkgo.Specify("\nWatch Pod shrink to 0.2 percent", func() {
WatchEphemeralSize("shrink-test", 0.2, time.Second*180, getContainerVolumeLimitPercentage)
})
})
ginkgo.Context("Observe change in ephemeral_storage_container_volume_usage metric\n", func() {
ginkgo.Specify("\nWatch Pod grow to 0.2 percent", func() {
WatchEphemeralSize("grow-test", 100000, time.Second*180, getContainerVolumeUsage)
})
ginkgo.Specify("\nWatch Pod shrink to 0.2 percent", func() {
WatchEphemeralSize("shrink-test", 100000, time.Second*180, getContainerVolumeUsage)
})
})
ginkgo.Context("\nMake sure percentage is not over 100", func() {
ginkgo.Specify("\nTest ephemeral_storage_node_percentage", func() {
WatchNodePercentage()
})
ginkgo.Specify("\nTest ephemeral_storage_container_limit_percentage", func() {
WatchContainerPercentage()
})
ginkgo.Specify("\nTest ephemeral_storage_container_volume_limit_percentage", func() {
WatchContainerVolumePercentage()
})
})
ginkgo.Context("Test Scaling\n", func() {
checkSlice := []string{
"node_name=\"minikube-m02",
"ephemeral_storage_container_limit_percentage{container=\"kube-proxy\",node_name=\"minikube-m02\"",
}
ginkgo.Specify("\nScale up test to make sure pods and nodes are found", func() {
scaleUp()
checkPrometheus(checkSlice, false)
})
ginkgo.Specify("\nScale Down test to make sure pods and nodes are evicted", func() {
scaleDown()
checkPrometheus(checkSlice, true)
})
})
})
func TestDeployments(t *testing.T) {
// https://onsi.github.io/ginkgo/#ginkgo-and-gomega-patterns
httpClient = &http.Client{}
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Test Metrics")
}