Class: HPAForDeploymentMiddlewareTest
- Inherits:
-
Minitest::Test
- Object
- Minitest::Test
- HPAForDeploymentMiddlewareTest
- Defined in:
- lib/kube/cluster/middleware/hpa_for_deployment.rb
Constant Summary collapse
- Middleware =
Kube::Cluster::Middleware
Instance Method Summary collapse
- #test_custom_cpu_and_memory_targets ⇒ Object
- #test_generates_hpa_from_deployment ⇒ Object
- #test_raises_on_invalid_range_format ⇒ Object
- #test_raises_on_invalid_range_values ⇒ Object
- #test_skips_deployment_without_autoscale_label ⇒ Object
- #test_skips_non_pod_bearing_resources ⇒ Object
- #test_strips_autoscale_label_from_hpa ⇒ Object
- #test_works_with_statefulset ⇒ Object
Instance Method Details
#test_custom_cpu_and_memory_targets ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 166 def test_custom_cpu_and_memory_targets m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/autoscale": "1-3" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) Middleware::HPAForDeployment.new(cpu: 60, memory: 70).call(m) hpa = m.resources.last.to_h assert_equal 60, hpa.dig(:spec, :metrics, 0, :resource, :target, :averageUtilization) assert_equal 70, hpa.dig(:spec, :metrics, 1, :resource, :target, :averageUtilization) end |
#test_generates_hpa_from_deployment ⇒ Object
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 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 124 def test_generates_hpa_from_deployment m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .namespace = "production" .labels = { "app.kubernetes.io/name": "web", "app.kubernetes.io/autoscale": "2-10", } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) Middleware::HPAForDeployment.new.call(m) assert_equal 2, m.resources.size deploy, hpa = m.resources assert_equal "Deployment", deploy.to_h[:kind] assert_equal "HorizontalPodAutoscaler", hpa.to_h[:kind] hh = hpa.to_h assert_equal "web", hh.dig(:metadata, :name) assert_equal "production", hh.dig(:metadata, :namespace) assert_equal 2, hh.dig(:spec, :minReplicas) assert_equal 10, hh.dig(:spec, :maxReplicas) ref = hh.dig(:spec, :scaleTargetRef) assert_equal "apps/v1", ref[:apiVersion] assert_equal "Deployment", ref[:kind] assert_equal "web", ref[:name] metrics = hh.dig(:spec, :metrics) assert_equal 2, metrics.size assert_equal "cpu", metrics[0].dig(:resource, :name) assert_equal 75, metrics[0].dig(:resource, :target, :averageUtilization) assert_equal "memory", metrics[1].dig(:resource, :name) assert_equal 80, metrics[1].dig(:resource, :target, :averageUtilization) end |
#test_raises_on_invalid_range_format ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 231 def test_raises_on_invalid_range_format m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/autoscale": "bad" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) error = assert_raises(ArgumentError) do Middleware::HPAForDeployment.new.call(m) end assert_includes error., "Invalid autoscale label" end |
#test_raises_on_invalid_range_values ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 249 def test_raises_on_invalid_range_values m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/autoscale": "5-2" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) error = assert_raises(ArgumentError) do Middleware::HPAForDeployment.new.call(m) end assert_includes error., "Invalid autoscale range" end |
#test_skips_deployment_without_autoscale_label ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 205 def test_skips_deployment_without_autoscale_label m = manifest(Kube::Cluster["Deployment"].new { .name = "web" spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) Middleware::HPAForDeployment.new.call(m) assert_equal 1, m.resources.size end |
#test_skips_non_pod_bearing_resources ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 220 def test_skips_non_pod_bearing_resources m = manifest(Kube::Cluster["ConfigMap"].new { .name = "config" .labels = { "app.kubernetes.io/autoscale": "1-5" } }) Middleware::HPAForDeployment.new.call(m) assert_equal 1, m.resources.size end |
#test_strips_autoscale_label_from_hpa ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 184 def test_strips_autoscale_label_from_hpa m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/name": "web", "app.kubernetes.io/autoscale": "1-5", } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx" }, ] }) Middleware::HPAForDeployment.new.call(m) hpa_labels = m.resources.last.to_h.dig(:metadata, :labels) assert_nil hpa_labels[:"app.kubernetes.io/autoscale"] assert_equal "web", hpa_labels[:"app.kubernetes.io/name"] end |
#test_works_with_statefulset ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/kube/cluster/middleware/hpa_for_deployment.rb', line 267 def test_works_with_statefulset m = manifest(Kube::Cluster["StatefulSet"].new { .name = "db" .labels = { "app.kubernetes.io/autoscale": "1-3" } spec.selector.matchLabels = { app: "db" } spec.template..labels = { app: "db" } spec.template.spec.containers = [ { name: "postgres", image: "postgres:16" }, ] }) Middleware::HPAForDeployment.new.call(m) assert_equal 2, m.resources.size hpa = m.resources.last.to_h assert_equal "StatefulSet", hpa.dig(:spec, :scaleTargetRef, :kind) end |