Class: ResourcePresetMiddlewareTest
- Inherits:
-
Minitest::Test
- Object
- Minitest::Test
- ResourcePresetMiddlewareTest
- Defined in:
- lib/kube/cluster/middleware/resource_preset.rb
Constant Summary collapse
- Middleware =
Kube::Cluster::Middleware
Instance Method Summary collapse
- #test_applies_to_all_containers ⇒ Object
- #test_applies_to_statefulset ⇒ Object
- #test_injects_nano_preset ⇒ Object
- #test_injects_small_preset_into_deployment ⇒ Object
- #test_injects_xlarge_preset ⇒ Object
- #test_preserves_existing_container_resources_via_deep_merge ⇒ Object
- #test_raises_on_unknown_size ⇒ Object
- #test_skips_non_pod_bearing_resources ⇒ Object
- #test_skips_resources_without_size_label ⇒ Object
Instance Method Details
#test_applies_to_all_containers ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 135 def test_applies_to_all_containers m = manifest(Kube::Cluster["Deployment"].new { .name = "multi" .labels = { "app.kubernetes.io/size": "micro" } spec.selector.matchLabels = { app: "multi" } spec.template..labels = { app: "multi" } spec.template.spec.containers = [ { name: "app", image: "app:latest" }, { name: "sidecar", image: "sidecar:latest" }, ] }) Middleware::ResourcePreset.new.call(m) containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers) containers.each do |c| assert_equal "250m", c.dig(:resources, :requests, :cpu) assert_equal "256Mi", c.dig(:resources, :requests, :memory) end end |
#test_applies_to_statefulset ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 203 def test_applies_to_statefulset m = manifest(Kube::Cluster["StatefulSet"].new { .name = "db" .labels = { "app.kubernetes.io/size": "medium" } spec.selector.matchLabels = { app: "db" } spec.template..labels = { app: "db" } spec.template.spec.containers = [ { name: "postgres", image: "postgres:16" }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) assert_equal "500m", container.dig(:resources, :requests, :cpu) assert_equal "1024Mi", container.dig(:resources, :requests, :memory) end |
#test_injects_nano_preset ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 97 def test_injects_nano_preset m = manifest(Kube::Cluster["Deployment"].new { .name = "tiny" .labels = { "app.kubernetes.io/size": "nano" } spec.selector.matchLabels = { app: "tiny" } spec.template..labels = { app: "tiny" } spec.template.spec.containers = [ { name: "app", image: "app:latest" }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) assert_equal "100m", container.dig(:resources, :requests, :cpu) assert_equal "128Mi", container.dig(:resources, :requests, :memory) end |
#test_injects_small_preset_into_deployment ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 77 def test_injects_small_preset_into_deployment m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/size": "small" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx:latest" }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) assert_equal "500m", container.dig(:resources, :requests, :cpu) assert_equal "512Mi", container.dig(:resources, :requests, :memory) assert_equal "750m", container.dig(:resources, :limits, :cpu) assert_equal "768Mi", container.dig(:resources, :limits, :memory) end |
#test_injects_xlarge_preset ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 115 def test_injects_xlarge_preset m = manifest(Kube::Cluster["Deployment"].new { .name = "big" .labels = { "app.kubernetes.io/size": "xlarge" } spec.selector.matchLabels = { app: "big" } spec.template..labels = { app: "big" } spec.template.spec.containers = [ { name: "app", image: "app:latest" }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) assert_equal "1", container.dig(:resources, :requests, :cpu) assert_equal "3072Mi", container.dig(:resources, :requests, :memory) assert_equal "3", container.dig(:resources, :limits, :cpu) assert_equal "6144Mi", container.dig(:resources, :limits, :memory) end |
#test_preserves_existing_container_resources_via_deep_merge ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 221 def test_preserves_existing_container_resources_via_deep_merge m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/size": "small" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx:latest", resources: { requests: { cpu: "999m" } }, }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) # The container's explicit value wins over the preset assert_equal "999m", container.dig(:resources, :requests, :cpu) # The preset fills in missing values assert_equal "512Mi", container.dig(:resources, :requests, :memory) end |
#test_raises_on_unknown_size ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 184 def test_raises_on_unknown_size m = manifest(Kube::Cluster["Deployment"].new { .name = "web" .labels = { "app.kubernetes.io/size": "potato" } spec.selector.matchLabels = { app: "web" } spec.template..labels = { app: "web" } spec.template.spec.containers = [ { name: "web", image: "nginx:latest" }, ] }) error = assert_raises(ArgumentError) do Middleware::ResourcePreset.new.call(m) end assert_includes error., "potato" assert_includes error., "Valid sizes" end |
#test_skips_non_pod_bearing_resources ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 156 def test_skips_non_pod_bearing_resources resource = Kube::Cluster["ConfigMap"].new { .name = "config" .labels = { "app.kubernetes.io/size": "small" } } m = manifest(resource) Middleware::ResourcePreset.new.call(m) assert_equal resource.to_h, m.resources.first.to_h end |
#test_skips_resources_without_size_label ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/kube/cluster/middleware/resource_preset.rb', line 168 def test_skips_resources_without_size_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:latest" }, ] }) Middleware::ResourcePreset.new.call(m) container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0) assert_nil container[:resources] end |