forked from gruntwork-io/helm-kubernetes-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_service_horizontal_pod_autoscaler_template_test.go
238 lines (207 loc) · 11.2 KB
/
k8s_service_horizontal_pod_autoscaler_template_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
//go:build all || tpl
// +build all tpl
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
// run just the template tests. See the test README for more information.
package test
import (
"path/filepath"
"strconv"
"testing"
"github.com/ghodss/yaml"
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
// Autoscaler resource with both metrics
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithAllMetrics(t *testing.T) {
t.Parallel()
minReplicas := "20"
maxReplicas := "30"
avgCpuUtil := "55"
avgMemoryUtil := "65"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
"horizontalPodAutoscaler.minReplicas": minReplicas,
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
"horizontalPodAutoscaler.avgCpuUtilization": avgCpuUtil,
"horizontalPodAutoscaler.avgMemoryUtilization": avgMemoryUtil,
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"})
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
min, err := strconv.ParseFloat(minReplicas, 64)
max, err := strconv.ParseFloat(maxReplicas, 64)
avgCpu, err := strconv.ParseFloat(avgCpuUtil, 64)
avgMem, err := strconv.ParseFloat(avgMemoryUtil, 64)
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
assert.Equal(t, avgCpu, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[0].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
assert.Equal(t, avgMem, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[1].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
}
// Test that setting horizontalPodAutoscaler.enabled = false will cause the helm template to not render the Horizontal
// Pod Autoscaler resource
func TestK8SServiceHorizontalPodAutoscalerCreateFalse(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "false",
},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"})
require.Error(t, err)
}
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
// Autoscaler resource with the cpu metric
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithCpuMetric(t *testing.T) {
t.Parallel()
minReplicas := "20"
maxReplicas := "30"
avgCpuUtil := "55"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
"horizontalPodAutoscaler.minReplicas": minReplicas,
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
"horizontalPodAutoscaler.avgCpuUtilization": avgCpuUtil,
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"})
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
min, err := strconv.ParseFloat(minReplicas, 64)
max, err := strconv.ParseFloat(maxReplicas, 64)
avgCpu, err := strconv.ParseFloat(avgCpuUtil, 64)
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
assert.Equal(t, avgCpu, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[0].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
}
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
// Autoscaler resource with the memory metric
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithMemoryMetric(t *testing.T) {
t.Parallel()
minReplicas := "20"
maxReplicas := "30"
avgMemoryUtil := "65"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
"horizontalPodAutoscaler.minReplicas": minReplicas,
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
"horizontalPodAutoscaler.avgMemoryUtilization": avgMemoryUtil,
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"})
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
min, err := strconv.ParseFloat(minReplicas, 64)
max, err := strconv.ParseFloat(maxReplicas, 64)
avgMem, err := strconv.ParseFloat(avgMemoryUtil, 64)
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
assert.Equal(t, avgMem, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[0].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
}
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
// Autoscaler resource with the no metrics
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithNoMetrics(t *testing.T) {
t.Parallel()
minReplicas := "20"
maxReplicas := "30"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
"horizontalPodAutoscaler.minReplicas": minReplicas,
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"})
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
min, err := strconv.ParseFloat(minReplicas, 64)
max, err := strconv.ParseFloat(maxReplicas, 64)
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
}
// Test that the apiVersion of the Horizontal Pod Autoscaler is correct for Kubernetes < 1.23
func TestK8SServiceHorizontalPodAutoscalerDisplaysBetaApiVersion(t *testing.T) {
t.Parallel()
expectedApiVersion := "autoscaling/v2beta2"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"}, "--kube-version", "1.22")
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
assert.Equal(t, expectedApiVersion, rendered["apiVersion"])
}
// Test that the apiVersion of the Horizontal Pod Autoscaler is correct for Kubernetes >= 1.23
func TestK8SServiceHorizontalPodAutoscalerDisplaysStableApiVersion(t *testing.T) {
t.Parallel()
expectedApiVersion := "autoscaling/v2"
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{
"horizontalPodAutoscaler.enabled": "true",
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "hpa", []string{"templates/horizontalpodautoscaler.yaml"}, "--kube-version", "1.23", "--api-versions", "autoscaling/v2")
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
assert.Equal(t, expectedApiVersion, rendered["apiVersion"])
}