kube_cluster 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if __FILE__ == $0
4
- require "bundler/setup"
5
- require "kube/cluster"
6
- end
3
+ require "bundler/setup"
4
+ require "kube/cluster"
7
5
 
8
6
  module Kube
9
7
  module Cluster
@@ -68,185 +66,163 @@ module Kube
68
66
  end
69
67
  end
70
68
 
71
- if __FILE__ == $0
72
- require "minitest/autorun"
73
-
74
- class ResourcePresetMiddlewareTest < Minitest::Test
75
- Middleware = Kube::Cluster::Middleware
76
-
77
- def test_injects_small_preset_into_deployment
78
- m = manifest(Kube::Cluster["Deployment"].new {
79
- metadata.name = "web"
80
- metadata.labels = { "app.kubernetes.io/size": "small" }
81
- spec.selector.matchLabels = { app: "web" }
82
- spec.template.metadata.labels = { app: "web" }
83
- spec.template.spec.containers = [
84
- { name: "web", image: "nginx:latest" },
85
- ]
86
- })
87
-
88
- Middleware::ResourcePreset.new.call(m)
89
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
90
-
91
- assert_equal "500m", container.dig(:resources, :requests, :cpu)
92
- assert_equal "512Mi", container.dig(:resources, :requests, :memory)
93
- assert_equal "750m", container.dig(:resources, :limits, :cpu)
94
- assert_equal "768Mi", container.dig(:resources, :limits, :memory)
95
- end
69
+ test do
70
+ Middleware = Kube::Cluster::Middleware
96
71
 
97
- def test_injects_nano_preset
98
- m = manifest(Kube::Cluster["Deployment"].new {
99
- metadata.name = "tiny"
100
- metadata.labels = { "app.kubernetes.io/size": "nano" }
101
- spec.selector.matchLabels = { app: "tiny" }
102
- spec.template.metadata.labels = { app: "tiny" }
103
- spec.template.spec.containers = [
104
- { name: "app", image: "app:latest" },
105
- ]
106
- })
107
-
108
- Middleware::ResourcePreset.new.call(m)
109
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
110
-
111
- assert_equal "100m", container.dig(:resources, :requests, :cpu)
112
- assert_equal "128Mi", container.dig(:resources, :requests, :memory)
113
- end
72
+ it "injects_small_preset_into_deployment" do
73
+ m = manifest(Kube::Cluster["Deployment"].new {
74
+ metadata.name = "web"
75
+ metadata.labels = { "app.kubernetes.io/size": "small" }
76
+ spec.selector.matchLabels = { app: "web" }
77
+ spec.template.metadata.labels = { app: "web" }
78
+ spec.template.spec.containers = [
79
+ { name: "web", image: "nginx:latest" },
80
+ ]
81
+ })
114
82
 
115
- def test_injects_xlarge_preset
116
- m = manifest(Kube::Cluster["Deployment"].new {
117
- metadata.name = "big"
118
- metadata.labels = { "app.kubernetes.io/size": "xlarge" }
119
- spec.selector.matchLabels = { app: "big" }
120
- spec.template.metadata.labels = { app: "big" }
121
- spec.template.spec.containers = [
122
- { name: "app", image: "app:latest" },
123
- ]
124
- })
125
-
126
- Middleware::ResourcePreset.new.call(m)
127
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
128
-
129
- assert_equal "1", container.dig(:resources, :requests, :cpu)
130
- assert_equal "3072Mi", container.dig(:resources, :requests, :memory)
131
- assert_equal "3", container.dig(:resources, :limits, :cpu)
132
- assert_equal "6144Mi", container.dig(:resources, :limits, :memory)
133
- end
83
+ Middleware::ResourcePreset.new.call(m)
84
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
134
85
 
135
- def test_applies_to_all_containers
136
- m = manifest(Kube::Cluster["Deployment"].new {
137
- metadata.name = "multi"
138
- metadata.labels = { "app.kubernetes.io/size": "micro" }
139
- spec.selector.matchLabels = { app: "multi" }
140
- spec.template.metadata.labels = { app: "multi" }
141
- spec.template.spec.containers = [
142
- { name: "app", image: "app:latest" },
143
- { name: "sidecar", image: "sidecar:latest" },
144
- ]
145
- })
146
-
147
- Middleware::ResourcePreset.new.call(m)
148
- containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
149
-
150
- containers.each do |c|
151
- assert_equal "250m", c.dig(:resources, :requests, :cpu)
152
- assert_equal "256Mi", c.dig(:resources, :requests, :memory)
153
- end
154
- end
86
+ container.dig(:resources, :requests, :cpu).should == "500m"
87
+ end
155
88
 
156
- def test_skips_non_pod_bearing_resources
157
- resource = Kube::Cluster["ConfigMap"].new {
158
- metadata.name = "config"
159
- metadata.labels = { "app.kubernetes.io/size": "small" }
160
- }
161
- m = manifest(resource)
89
+ it "injects_nano_preset" do
90
+ m = manifest(Kube::Cluster["Deployment"].new {
91
+ metadata.name = "tiny"
92
+ metadata.labels = { "app.kubernetes.io/size": "nano" }
93
+ spec.selector.matchLabels = { app: "tiny" }
94
+ spec.template.metadata.labels = { app: "tiny" }
95
+ spec.template.spec.containers = [
96
+ { name: "app", image: "app:latest" },
97
+ ]
98
+ })
99
+
100
+ Middleware::ResourcePreset.new.call(m)
101
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
102
+
103
+ container.dig(:resources, :requests, :cpu).should == "100m"
104
+ end
162
105
 
163
- Middleware::ResourcePreset.new.call(m)
106
+ it "injects_xlarge_preset" do
107
+ m = manifest(Kube::Cluster["Deployment"].new {
108
+ metadata.name = "big"
109
+ metadata.labels = { "app.kubernetes.io/size": "xlarge" }
110
+ spec.selector.matchLabels = { app: "big" }
111
+ spec.template.metadata.labels = { app: "big" }
112
+ spec.template.spec.containers = [
113
+ { name: "app", image: "app:latest" },
114
+ ]
115
+ })
116
+
117
+ Middleware::ResourcePreset.new.call(m)
118
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
119
+
120
+ container.dig(:resources, :limits, :cpu).should == "3"
121
+ end
164
122
 
165
- assert_equal resource.to_h, m.resources.first.to_h
166
- end
123
+ it "applies_to_all_containers" do
124
+ m = manifest(Kube::Cluster["Deployment"].new {
125
+ metadata.name = "multi"
126
+ metadata.labels = { "app.kubernetes.io/size": "micro" }
127
+ spec.selector.matchLabels = { app: "multi" }
128
+ spec.template.metadata.labels = { app: "multi" }
129
+ spec.template.spec.containers = [
130
+ { name: "app", image: "app:latest" },
131
+ { name: "sidecar", image: "sidecar:latest" },
132
+ ]
133
+ })
134
+
135
+ Middleware::ResourcePreset.new.call(m)
136
+ containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
137
+
138
+ containers.last.dig(:resources, :requests, :cpu).should == "250m"
139
+ end
167
140
 
168
- def test_skips_resources_without_size_label
169
- m = manifest(Kube::Cluster["Deployment"].new {
170
- metadata.name = "web"
171
- spec.selector.matchLabels = { app: "web" }
172
- spec.template.metadata.labels = { app: "web" }
173
- spec.template.spec.containers = [
174
- { name: "web", image: "nginx:latest" },
175
- ]
176
- })
141
+ it "skips_non_pod_bearing_resources" do
142
+ resource = Kube::Cluster["ConfigMap"].new {
143
+ metadata.name = "config"
144
+ metadata.labels = { "app.kubernetes.io/size": "small" }
145
+ }
146
+ m = manifest(resource)
177
147
 
178
- Middleware::ResourcePreset.new.call(m)
179
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
148
+ Middleware::ResourcePreset.new.call(m)
180
149
 
181
- assert_nil container[:resources]
182
- end
150
+ m.resources.first.to_h.should == resource.to_h
151
+ end
183
152
 
184
- def test_raises_on_unknown_size
185
- m = manifest(Kube::Cluster["Deployment"].new {
186
- metadata.name = "web"
187
- metadata.labels = { "app.kubernetes.io/size": "potato" }
188
- spec.selector.matchLabels = { app: "web" }
189
- spec.template.metadata.labels = { app: "web" }
190
- spec.template.spec.containers = [
191
- { name: "web", image: "nginx:latest" },
192
- ]
193
- })
194
-
195
- error = assert_raises(ArgumentError) do
196
- Middleware::ResourcePreset.new.call(m)
197
- end
153
+ it "skips_resources_without_size_label" do
154
+ m = manifest(Kube::Cluster["Deployment"].new {
155
+ metadata.name = "web"
156
+ spec.selector.matchLabels = { app: "web" }
157
+ spec.template.metadata.labels = { app: "web" }
158
+ spec.template.spec.containers = [
159
+ { name: "web", image: "nginx:latest" },
160
+ ]
161
+ })
198
162
 
199
- assert_includes error.message, "potato"
200
- assert_includes error.message, "Valid sizes"
201
- end
163
+ Middleware::ResourcePreset.new.call(m)
164
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
202
165
 
203
- def test_applies_to_statefulset
204
- m = manifest(Kube::Cluster["StatefulSet"].new {
205
- metadata.name = "db"
206
- metadata.labels = { "app.kubernetes.io/size": "medium" }
207
- spec.selector.matchLabels = { app: "db" }
208
- spec.template.metadata.labels = { app: "db" }
209
- spec.template.spec.containers = [
210
- { name: "postgres", image: "postgres:16" },
211
- ]
212
- })
213
-
214
- Middleware::ResourcePreset.new.call(m)
215
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
216
-
217
- assert_equal "500m", container.dig(:resources, :requests, :cpu)
218
- assert_equal "1024Mi", container.dig(:resources, :requests, :memory)
219
- end
166
+ container[:resources].should.be.nil
167
+ end
220
168
 
221
- def test_preserves_existing_container_resources_via_deep_merge
222
- m = manifest(Kube::Cluster["Deployment"].new {
223
- metadata.name = "web"
224
- metadata.labels = { "app.kubernetes.io/size": "small" }
225
- spec.selector.matchLabels = { app: "web" }
226
- spec.template.metadata.labels = { app: "web" }
227
- spec.template.spec.containers = [
228
- {
229
- name: "web", image: "nginx:latest",
230
- resources: { requests: { cpu: "999m" } },
231
- },
232
- ]
233
- })
234
-
235
- Middleware::ResourcePreset.new.call(m)
236
- container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
237
-
238
- # The container's explicit value wins over the preset
239
- assert_equal "999m", container.dig(:resources, :requests, :cpu)
240
- # The preset fills in missing values
241
- assert_equal "512Mi", container.dig(:resources, :requests, :memory)
242
- end
169
+ it "raises_on_unknown_size" do
170
+ m = manifest(Kube::Cluster["Deployment"].new {
171
+ metadata.name = "web"
172
+ metadata.labels = { "app.kubernetes.io/size": "potato" }
173
+ spec.selector.matchLabels = { app: "web" }
174
+ spec.template.metadata.labels = { app: "web" }
175
+ spec.template.spec.containers = [
176
+ { name: "web", image: "nginx:latest" },
177
+ ]
178
+ })
179
+
180
+ lambda { Middleware::ResourcePreset.new.call(m) }.should.raise ArgumentError
181
+ end
243
182
 
244
- private
183
+ it "applies_to_statefulset" do
184
+ m = manifest(Kube::Cluster["StatefulSet"].new {
185
+ metadata.name = "db"
186
+ metadata.labels = { "app.kubernetes.io/size": "medium" }
187
+ spec.selector.matchLabels = { app: "db" }
188
+ spec.template.metadata.labels = { app: "db" }
189
+ spec.template.spec.containers = [
190
+ { name: "postgres", image: "postgres:16" },
191
+ ]
192
+ })
193
+
194
+ Middleware::ResourcePreset.new.call(m)
195
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
196
+
197
+ container.dig(:resources, :requests, :cpu).should == "500m"
198
+ end
245
199
 
246
- def manifest(*resources)
247
- m = Kube::Cluster::Manifest.new
248
- resources.each { |r| m << r }
249
- m
250
- end
200
+ it "preserves_existing_container_resources_via_deep_merge" do
201
+ m = manifest(Kube::Cluster["Deployment"].new {
202
+ metadata.name = "web"
203
+ metadata.labels = { "app.kubernetes.io/size": "small" }
204
+ spec.selector.matchLabels = { app: "web" }
205
+ spec.template.metadata.labels = { app: "web" }
206
+ spec.template.spec.containers = [
207
+ {
208
+ name: "web", image: "nginx:latest",
209
+ resources: { requests: { cpu: "999m" } },
210
+ },
211
+ ]
212
+ })
213
+
214
+ Middleware::ResourcePreset.new.call(m)
215
+ container = m.resources.first.to_h.dig(:spec, :template, :spec, :containers, 0)
216
+
217
+ # The container's explicit value wins over the preset
218
+ container.dig(:resources, :requests, :cpu).should == "999m"
251
219
  end
220
+
221
+ private
222
+
223
+ def manifest(*resources)
224
+ m = Kube::Cluster::Manifest.new
225
+ resources.each { |r| m << r }
226
+ m
227
+ end
252
228
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if __FILE__ == $0
4
- require "bundler/setup"
5
- require "kube/cluster"
6
- end
3
+ require "bundler/setup"
4
+ require "kube/cluster"
7
5
 
8
6
  module Kube
9
7
  module Cluster
@@ -88,167 +86,142 @@ module Kube
88
86
  end
89
87
  end
90
88
 
91
- if __FILE__ == $0
92
- require "minitest/autorun"
93
-
94
- class SecurityContextMiddlewareTest < Minitest::Test
95
- Middleware = Kube::Cluster::Middleware
96
-
97
- def test_applies_restricted_profile_by_default
98
- m = manifest(Kube::Cluster["Deployment"].new {
99
- metadata.name = "web"
100
- spec.selector.matchLabels = { app: "web" }
101
- spec.template.metadata.labels = { app: "web" }
102
- spec.template.spec.containers = [
103
- { name: "web", image: "nginx:latest" },
104
- ]
105
- })
106
-
107
- Middleware::SecurityContext.new.call(m)
108
- h = m.resources.first.to_h
109
- pod_sc = h.dig(:spec, :template, :spec, :securityContext)
110
- container_sc = h.dig(:spec, :template, :spec, :containers, 0, :securityContext)
111
-
112
- assert_equal true, pod_sc[:runAsNonRoot]
113
- assert_equal 1000, pod_sc[:runAsUser]
114
- assert_equal 1000, pod_sc[:fsGroup]
115
- assert_equal({ type: "RuntimeDefault" }, pod_sc[:seccompProfile])
116
-
117
- assert_equal false, container_sc[:allowPrivilegeEscalation]
118
- assert_equal true, container_sc[:readOnlyRootFilesystem]
119
- assert_equal({ drop: ["ALL"] }, container_sc[:capabilities])
120
- end
89
+ test do
90
+ Middleware = Kube::Cluster::Middleware
121
91
 
122
- def test_applies_baseline_profile_via_label
123
- m = manifest(Kube::Cluster["Deployment"].new {
124
- metadata.name = "web"
125
- metadata.labels = { "app.kubernetes.io/security": "baseline" }
126
- spec.selector.matchLabels = { app: "web" }
127
- spec.template.metadata.labels = { app: "web" }
128
- spec.template.spec.containers = [
129
- { name: "web", image: "nginx:latest" },
130
- ]
131
- })
132
-
133
- Middleware::SecurityContext.new.call(m)
134
- h = m.resources.first.to_h
135
- pod_sc = h.dig(:spec, :template, :spec, :securityContext)
136
- container_sc = h.dig(:spec, :template, :spec, :containers, 0, :securityContext)
137
-
138
- assert_equal true, pod_sc[:runAsNonRoot]
139
- assert_nil pod_sc[:seccompProfile]
140
-
141
- assert_equal false, container_sc[:allowPrivilegeEscalation]
142
- assert_nil container_sc[:readOnlyRootFilesystem]
143
- end
92
+ it "applies_restricted_profile_by_default" do
93
+ m = manifest(Kube::Cluster["Deployment"].new {
94
+ metadata.name = "web"
95
+ spec.selector.matchLabels = { app: "web" }
96
+ spec.template.metadata.labels = { app: "web" }
97
+ spec.template.spec.containers = [
98
+ { name: "web", image: "nginx:latest" },
99
+ ]
100
+ })
144
101
 
145
- def test_applies_baseline_profile_via_constructor_default
146
- m = manifest(Kube::Cluster["Deployment"].new {
147
- metadata.name = "web"
148
- spec.selector.matchLabels = { app: "web" }
149
- spec.template.metadata.labels = { app: "web" }
150
- spec.template.spec.containers = [
151
- { name: "web", image: "nginx:latest" },
152
- ]
153
- })
154
-
155
- Middleware::SecurityContext.new(default: :baseline).call(m)
156
- h = m.resources.first.to_h
157
- pod_sc = h.dig(:spec, :template, :spec, :securityContext)
158
-
159
- assert_nil pod_sc[:seccompProfile]
160
- end
102
+ Middleware::SecurityContext.new.call(m)
103
+ h = m.resources.first.to_h
104
+ pod_sc = h.dig(:spec, :template, :spec, :securityContext)
161
105
 
162
- def test_label_overrides_constructor_default
163
- m = manifest(Kube::Cluster["Deployment"].new {
164
- metadata.name = "web"
165
- metadata.labels = { "app.kubernetes.io/security": "restricted" }
166
- spec.selector.matchLabels = { app: "web" }
167
- spec.template.metadata.labels = { app: "web" }
168
- spec.template.spec.containers = [
169
- { name: "web", image: "nginx:latest" },
170
- ]
171
- })
172
-
173
- Middleware::SecurityContext.new(default: :baseline).call(m)
174
- h = m.resources.first.to_h
175
- pod_sc = h.dig(:spec, :template, :spec, :securityContext)
176
-
177
- assert_equal({ type: "RuntimeDefault" }, pod_sc[:seccompProfile])
178
- end
106
+ pod_sc[:runAsNonRoot].should == true
107
+ end
179
108
 
180
- def test_applies_to_all_containers
181
- m = manifest(Kube::Cluster["Deployment"].new {
182
- metadata.name = "web"
183
- spec.selector.matchLabels = { app: "web" }
184
- spec.template.metadata.labels = { app: "web" }
185
- spec.template.spec.containers = [
186
- { name: "app", image: "app:latest" },
187
- { name: "sidecar", image: "sidecar:latest" },
188
- ]
189
- })
190
-
191
- Middleware::SecurityContext.new.call(m)
192
- containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
193
-
194
- containers.each do |c|
195
- assert_equal false, c.dig(:securityContext, :allowPrivilegeEscalation)
196
- end
197
- end
109
+ it "applies_baseline_profile_via_label" do
110
+ m = manifest(Kube::Cluster["Deployment"].new {
111
+ metadata.name = "web"
112
+ metadata.labels = { "app.kubernetes.io/security": "baseline" }
113
+ spec.selector.matchLabels = { app: "web" }
114
+ spec.template.metadata.labels = { app: "web" }
115
+ spec.template.spec.containers = [
116
+ { name: "web", image: "nginx:latest" },
117
+ ]
118
+ })
119
+
120
+ Middleware::SecurityContext.new.call(m)
121
+ h = m.resources.first.to_h
122
+ pod_sc = h.dig(:spec, :template, :spec, :securityContext)
123
+
124
+ pod_sc[:runAsNonRoot].should == true
125
+ end
198
126
 
199
- def test_skips_non_pod_bearing_resources
200
- resource = Kube::Cluster["ConfigMap"].new { metadata.name = "config" }
201
- m = manifest(resource)
127
+ it "applies_baseline_profile_via_constructor_default" do
128
+ m = manifest(Kube::Cluster["Deployment"].new {
129
+ metadata.name = "web"
130
+ spec.selector.matchLabels = { app: "web" }
131
+ spec.template.metadata.labels = { app: "web" }
132
+ spec.template.spec.containers = [
133
+ { name: "web", image: "nginx:latest" },
134
+ ]
135
+ })
136
+
137
+ Middleware::SecurityContext.new(default: :baseline).call(m)
138
+ h = m.resources.first.to_h
139
+ pod_sc = h.dig(:spec, :template, :spec, :securityContext)
140
+
141
+ pod_sc[:seccompProfile].should.be.nil
142
+ end
202
143
 
203
- Middleware::SecurityContext.new.call(m)
144
+ it "label_overrides_constructor_default" do
145
+ m = manifest(Kube::Cluster["Deployment"].new {
146
+ metadata.name = "web"
147
+ metadata.labels = { "app.kubernetes.io/security": "restricted" }
148
+ spec.selector.matchLabels = { app: "web" }
149
+ spec.template.metadata.labels = { app: "web" }
150
+ spec.template.spec.containers = [
151
+ { name: "web", image: "nginx:latest" },
152
+ ]
153
+ })
154
+
155
+ Middleware::SecurityContext.new(default: :baseline).call(m)
156
+ h = m.resources.first.to_h
157
+ pod_sc = h.dig(:spec, :template, :spec, :securityContext)
158
+
159
+ pod_sc[:seccompProfile].should == { type: "RuntimeDefault" }
160
+ end
204
161
 
205
- assert_equal resource.to_h, m.resources.first.to_h
206
- end
162
+ it "applies_to_all_containers" do
163
+ m = manifest(Kube::Cluster["Deployment"].new {
164
+ metadata.name = "web"
165
+ spec.selector.matchLabels = { app: "web" }
166
+ spec.template.metadata.labels = { app: "web" }
167
+ spec.template.spec.containers = [
168
+ { name: "app", image: "app:latest" },
169
+ { name: "sidecar", image: "sidecar:latest" },
170
+ ]
171
+ })
172
+
173
+ Middleware::SecurityContext.new.call(m)
174
+ containers = m.resources.first.to_h.dig(:spec, :template, :spec, :containers)
175
+
176
+ containers.last.dig(:securityContext, :allowPrivilegeEscalation).should == false
177
+ end
207
178
 
208
- def test_raises_on_unknown_profile
209
- m = manifest(Kube::Cluster["Deployment"].new {
210
- metadata.name = "web"
211
- metadata.labels = { "app.kubernetes.io/security": "yolo" }
212
- spec.selector.matchLabels = { app: "web" }
213
- spec.template.metadata.labels = { app: "web" }
214
- spec.template.spec.containers = [
215
- { name: "web", image: "nginx:latest" },
216
- ]
217
- })
218
-
219
- error = assert_raises(ArgumentError) do
220
- Middleware::SecurityContext.new.call(m)
221
- end
179
+ it "skips_non_pod_bearing_resources" do
180
+ resource = Kube::Cluster["ConfigMap"].new { metadata.name = "config" }
181
+ m = manifest(resource)
222
182
 
223
- assert_includes error.message, "yolo"
224
- end
183
+ Middleware::SecurityContext.new.call(m)
225
184
 
226
- def test_preserves_existing_pod_security_context
227
- m = manifest(Kube::Cluster["Deployment"].new {
228
- metadata.name = "web"
229
- spec.selector.matchLabels = { app: "web" }
230
- spec.template.metadata.labels = { app: "web" }
231
- spec.template.spec.securityContext = { runAsUser: 9999 }
232
- spec.template.spec.containers = [
233
- { name: "web", image: "nginx:latest" },
234
- ]
235
- })
236
-
237
- Middleware::SecurityContext.new.call(m)
238
- pod_sc = m.resources.first.to_h.dig(:spec, :template, :spec, :securityContext)
239
-
240
- # Existing value wins
241
- assert_equal 9999, pod_sc[:runAsUser]
242
- # Middleware fills in missing values
243
- assert_equal true, pod_sc[:runAsNonRoot]
244
- end
185
+ m.resources.first.to_h.should == resource.to_h
186
+ end
245
187
 
246
- private
188
+ it "raises_on_unknown_profile" do
189
+ m = manifest(Kube::Cluster["Deployment"].new {
190
+ metadata.name = "web"
191
+ metadata.labels = { "app.kubernetes.io/security": "yolo" }
192
+ spec.selector.matchLabels = { app: "web" }
193
+ spec.template.metadata.labels = { app: "web" }
194
+ spec.template.spec.containers = [
195
+ { name: "web", image: "nginx:latest" },
196
+ ]
197
+ })
198
+
199
+ lambda { Middleware::SecurityContext.new.call(m) }.should.raise ArgumentError
200
+ end
247
201
 
248
- def manifest(*resources)
249
- m = Kube::Cluster::Manifest.new
250
- resources.each { |r| m << r }
251
- m
252
- end
202
+ it "preserves_existing_pod_security_context" do
203
+ m = manifest(Kube::Cluster["Deployment"].new {
204
+ metadata.name = "web"
205
+ spec.selector.matchLabels = { app: "web" }
206
+ spec.template.metadata.labels = { app: "web" }
207
+ spec.template.spec.securityContext = { runAsUser: 9999 }
208
+ spec.template.spec.containers = [
209
+ { name: "web", image: "nginx:latest" },
210
+ ]
211
+ })
212
+
213
+ Middleware::SecurityContext.new.call(m)
214
+ pod_sc = m.resources.first.to_h.dig(:spec, :template, :spec, :securityContext)
215
+
216
+ # Existing value wins
217
+ pod_sc[:runAsUser].should == 9999
253
218
  end
219
+
220
+ private
221
+
222
+ def manifest(*resources)
223
+ m = Kube::Cluster::Manifest.new
224
+ resources.each { |r| m << r }
225
+ m
226
+ end
254
227
  end