@@ -17,8 +17,8 @@ import (
1717)
1818
1919// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
20- // Autoscaler resource
21- func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscaler (t * testing.T ) {
20+ // Autoscaler resource with both metrics
21+ func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithAllMetrics (t * testing.T ) {
2222 t .Parallel ()
2323 minReplicas := "20"
2424 maxReplicas := "30"
@@ -81,3 +81,110 @@ func TestK8SServiceHorizontalPodAutoscalerCreateFalse(t *testing.T) {
8181 assert .NoError (t , err )
8282 assert .Equal (t , 0 , len (rendered ))
8383}
84+
85+ // Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
86+ // Autoscaler resource with the cpu metric
87+ func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithCpuMetric (t * testing.T ) {
88+ t .Parallel ()
89+ minReplicas := "20"
90+ maxReplicas := "30"
91+ avgCpuUtil := "55"
92+
93+ helmChartPath , err := filepath .Abs (filepath .Join (".." , "charts" , "k8s-service" ))
94+ require .NoError (t , err )
95+
96+ // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
97+ // We then use SetValues to override all the defaults.
98+ options := & helm.Options {
99+ ValuesFiles : []string {filepath .Join (".." , "charts" , "k8s-service" , "linter_values.yaml" )},
100+ SetValues : map [string ]string {
101+ "horizontalPodAutoscaler.enabled" : "true" ,
102+ "horizontalPodAutoscaler.minReplicas" : minReplicas ,
103+ "horizontalPodAutoscaler.maxReplicas" : maxReplicas ,
104+ "horizontalPodAutoscaler.avgCpuUtilization" : avgCpuUtil ,
105+ },
106+ }
107+ out := helm .RenderTemplate (t , options , helmChartPath , []string {"templates/horizontalpodautoscaler.yaml" })
108+
109+ // We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
110+ rendered := map [string ]interface {}{}
111+ err = yaml .Unmarshal ([]byte (out ), & rendered )
112+ assert .NoError (t , err )
113+ assert .NotEqual (t , 0 , len (rendered ))
114+ min , err := strconv .ParseFloat (minReplicas , 64 )
115+ max , err := strconv .ParseFloat (maxReplicas , 64 )
116+ avgCpu , err := strconv .ParseFloat (avgCpuUtil , 64 )
117+ assert .Equal (t , min , rendered ["spec" ].(map [string ]interface {})["minReplicas" ])
118+ assert .Equal (t , max , rendered ["spec" ].(map [string ]interface {})["maxReplicas" ])
119+ assert .Equal (t , avgCpu , rendered ["spec" ].(map [string ]interface {})["metrics" ].([]interface {})[0 ].(map [string ]interface {})["resource" ].(map [string ]interface {})["target" ].(map [string ]interface {})["averageUtilization" ])
120+ }
121+
122+ // Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
123+ // Autoscaler resource with the memory metric
124+ func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithMemoryMetric (t * testing.T ) {
125+ t .Parallel ()
126+ minReplicas := "20"
127+ maxReplicas := "30"
128+ avgMemoryUtil := "65"
129+
130+ helmChartPath , err := filepath .Abs (filepath .Join (".." , "charts" , "k8s-service" ))
131+ require .NoError (t , err )
132+
133+ // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
134+ // We then use SetValues to override all the defaults.
135+ options := & helm.Options {
136+ ValuesFiles : []string {filepath .Join (".." , "charts" , "k8s-service" , "linter_values.yaml" )},
137+ SetValues : map [string ]string {
138+ "horizontalPodAutoscaler.enabled" : "true" ,
139+ "horizontalPodAutoscaler.minReplicas" : minReplicas ,
140+ "horizontalPodAutoscaler.maxReplicas" : maxReplicas ,
141+ "horizontalPodAutoscaler.avgMemoryUtilization" : avgMemoryUtil ,
142+ },
143+ }
144+ out := helm .RenderTemplate (t , options , helmChartPath , []string {"templates/horizontalpodautoscaler.yaml" })
145+
146+ // We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
147+ rendered := map [string ]interface {}{}
148+ err = yaml .Unmarshal ([]byte (out ), & rendered )
149+ assert .NoError (t , err )
150+ assert .NotEqual (t , 0 , len (rendered ))
151+ min , err := strconv .ParseFloat (minReplicas , 64 )
152+ max , err := strconv .ParseFloat (maxReplicas , 64 )
153+ avgMem , err := strconv .ParseFloat (avgMemoryUtil , 64 )
154+ assert .Equal (t , min , rendered ["spec" ].(map [string ]interface {})["minReplicas" ])
155+ assert .Equal (t , max , rendered ["spec" ].(map [string ]interface {})["maxReplicas" ])
156+ assert .Equal (t , avgMem , rendered ["spec" ].(map [string ]interface {})["metrics" ].([]interface {})[0 ].(map [string ]interface {})["resource" ].(map [string ]interface {})["target" ].(map [string ]interface {})["averageUtilization" ])
157+ }
158+
159+ // Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
160+ // Autoscaler resource with the no metrics
161+ func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithNoMetrics (t * testing.T ) {
162+ t .Parallel ()
163+ minReplicas := "20"
164+ maxReplicas := "30"
165+
166+ helmChartPath , err := filepath .Abs (filepath .Join (".." , "charts" , "k8s-service" ))
167+ require .NoError (t , err )
168+
169+ // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
170+ // We then use SetValues to override all the defaults.
171+ options := & helm.Options {
172+ ValuesFiles : []string {filepath .Join (".." , "charts" , "k8s-service" , "linter_values.yaml" )},
173+ SetValues : map [string ]string {
174+ "horizontalPodAutoscaler.enabled" : "true" ,
175+ "horizontalPodAutoscaler.minReplicas" : minReplicas ,
176+ "horizontalPodAutoscaler.maxReplicas" : maxReplicas ,
177+ },
178+ }
179+ out := helm .RenderTemplate (t , options , helmChartPath , []string {"templates/horizontalpodautoscaler.yaml" })
180+
181+ // We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
182+ rendered := map [string ]interface {}{}
183+ err = yaml .Unmarshal ([]byte (out ), & rendered )
184+ assert .NoError (t , err )
185+ assert .NotEqual (t , 0 , len (rendered ))
186+ min , err := strconv .ParseFloat (minReplicas , 64 )
187+ max , err := strconv .ParseFloat (maxReplicas , 64 )
188+ assert .Equal (t , min , rendered ["spec" ].(map [string ]interface {})["minReplicas" ])
189+ assert .Equal (t , max , rendered ["spec" ].(map [string ]interface {})["maxReplicas" ])
190+ }
0 commit comments