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,10 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if __FILE__ == $0
4
- require "bundler/setup"
5
- require "kube/cluster"
6
- end
7
-
3
+ require "bundler/setup"
4
+ require "kube/cluster"
8
5
  require "tempfile"
9
6
  require "yaml"
10
7
 
@@ -153,7 +150,7 @@ module Kube
153
150
  ver = version_flag
154
151
  release_name = name || "crds"
155
152
 
156
- cmd = helm.call { template.(release_name).(source_ref).include_crds(true) }
153
+ cmd = helm.call { template.(release_name).(source_ref).include_crds(true) }
157
154
  cmd = cmd.version(ver) if ver
158
155
  cmd = cmd.set("installCRDs=true")
159
156
 
@@ -207,407 +204,411 @@ module Kube
207
204
  end
208
205
  end
209
206
 
210
- if __FILE__ == $0
211
- require "minitest/autorun"
207
+ test do
208
+ # ── initialization ────────────────────────────────────────────────────
212
209
 
213
- class ChartTest < Minitest::Test
214
- # ── initialization ────────────────────────────────────────────────────
215
-
216
- def test_initializes_with_data_hash
217
- chart = Kube::Helm::Chart.new({ "name" => "my-app", "version" => "1.0.0", "appVersion" => "2.5.0" })
218
- assert_equal "my-app", chart.name
219
- assert_equal "1.0.0", chart.version
220
- assert_equal "2.5.0", chart.app_version
221
- end
222
-
223
- def test_initializes_with_block
224
- chart = Kube::Helm::Chart.new {
225
- self.name = "my-app"
226
- self.version = "1.0.0"
227
- self.appVersion = "2.5.0"
228
- self.description = "A test chart"
229
- }
230
-
231
- assert_equal "my-app", chart.name
232
- assert_equal "1.0.0", chart.version
233
- assert_equal "2.5.0", chart.app_version
234
- assert_equal "A test chart", chart.description
235
- end
236
-
237
- def test_initializes_empty
238
- chart = Kube::Helm::Chart.new
239
- assert_nil chart.name
240
- assert_nil chart.version
241
- assert_nil chart.app_version
242
- assert_nil chart.description
243
- assert_nil chart.type
244
- assert_equal [], chart.dependencies
245
- end
210
+ it "initializes_with_data_hash" do
211
+ chart = Kube::Helm::Chart.new({ "name" => "my-app", "version" => "1.0.0", "appVersion" => "2.5.0" })
212
+ chart.name.should == "my-app"
213
+ end
246
214
 
247
- def test_initializes_with_path
248
- chart = Kube::Helm::Chart.new({ "name" => "x" }, path: "/tmp/charts/x")
249
- assert_equal "/tmp/charts/x", chart.path
250
- end
215
+ it "initializes_with_block" do
216
+ chart = Kube::Helm::Chart.new {
217
+ self.name = "my-app"
218
+ self.version = "1.0.0"
219
+ self.appVersion = "2.5.0"
220
+ self.description = "A test chart"
221
+ }
251
222
 
252
- def test_initializes_with_cluster
253
- cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
254
- chart = Kube::Helm::Chart.new({ "name" => "x" }, cluster: cluster)
255
- assert_equal cluster, chart.cluster
256
- end
223
+ chart.description.should == "A test chart"
224
+ end
257
225
 
258
- # ── Chart.open ───────────────────────────────────────────────────────
259
-
260
- def test_open_reads_chart_yaml
261
- Dir.mktmpdir do |dir|
262
- File.write(File.join(dir, "Chart.yaml"), {
263
- "name" => "test-chart",
264
- "version" => "0.1.0",
265
- "appVersion" => "1.0.0",
266
- "description" => "A test chart",
267
- "type" => "application",
268
- }.to_yaml)
269
-
270
- chart = Kube::Helm::Chart.open(dir)
271
- assert_equal "test-chart", chart.name
272
- assert_equal "0.1.0", chart.version
273
- assert_equal "1.0.0", chart.app_version
274
- assert_equal "A test chart", chart.description
275
- assert_equal "application", chart.type
276
- assert_equal dir, chart.path
277
- end
278
- end
226
+ it "initializes_empty" do
227
+ chart = Kube::Helm::Chart.new
228
+ chart.dependencies.should == []
229
+ end
279
230
 
280
- def test_open_raises_without_chart_yaml
281
- Dir.mktmpdir do |dir|
282
- assert_raises(Kube::Error) { Kube::Helm::Chart.open(dir) }
283
- end
284
- end
231
+ it "initializes_with_path" do
232
+ chart = Kube::Helm::Chart.new({ "name" => "x" }, path: "/tmp/charts/x")
233
+ chart.path.should == "/tmp/charts/x"
234
+ end
285
235
 
286
- def test_open_with_cluster
287
- Dir.mktmpdir do |dir|
288
- File.write(File.join(dir, "Chart.yaml"), { "name" => "x", "version" => "1.0.0" }.to_yaml)
289
- cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
236
+ it "initializes_with_cluster" do
237
+ cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
238
+ chart = Kube::Helm::Chart.new({ "name" => "x" }, cluster: cluster)
239
+ chart.cluster.should == cluster
240
+ end
290
241
 
291
- chart = Kube::Helm::Chart.open(dir, cluster: cluster)
292
- assert_equal cluster, chart.cluster
293
- end
294
- end
242
+ # ── Chart.open ───────────────────────────────────────────────────────
295
243
 
296
- # ── to_s ──────────────────────────────────────────────────────────────
244
+ it "open_reads_chart_yaml" do
245
+ Dir.mktmpdir do |dir|
246
+ File.write(File.join(dir, "Chart.yaml"), {
247
+ "name" => "test-chart",
248
+ "version" => "0.1.0",
249
+ "appVersion" => "1.0.0",
250
+ "description" => "A test chart",
251
+ "type" => "application",
252
+ }.to_yaml)
297
253
 
298
- def test_to_s_with_version
299
- chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" })
300
- assert_equal "nginx:18.1.0", chart.to_s
254
+ chart = Kube::Helm::Chart.open(dir)
255
+ chart.name.should == "test-chart"
301
256
  end
257
+ end
302
258
 
303
- def test_to_s_without_version
304
- chart = Kube::Helm::Chart.new({ "name" => "nginx" })
305
- assert_equal "nginx", chart.to_s
259
+ it "open_raises_without_chart_yaml" do
260
+ Dir.mktmpdir do |dir|
261
+ lambda { Kube::Helm::Chart.open(dir) }.should.raise Kube::Error
306
262
  end
263
+ end
307
264
 
308
- # ── apply_values ─────────────────────────────────────────────────────
265
+ it "open_with_cluster" do
266
+ Dir.mktmpdir do |dir|
267
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "x", "version" => "1.0.0" }.to_yaml)
268
+ cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
309
269
 
310
- def test_apply_values_raises_without_source
311
- chart = Kube::Helm::Chart.new({ "name" => "my-app" })
312
- assert_raises(Kube::Error) { chart.apply_values({ "replicaCount" => 3 }) }
270
+ chart = Kube::Helm::Chart.open(dir, cluster: cluster)
271
+ chart.cluster.should == cluster
313
272
  end
273
+ end
314
274
 
315
- def test_apply_values_builds_correct_command
316
- Dir.mktmpdir do |dir|
317
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
318
- chart = Kube::Helm::Chart.open(dir)
275
+ # ── to_s ──────────────────────────────────────────────────────────────
319
276
 
320
- captured_cmd = nil
321
- stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml
277
+ it "to_s_with_version" do
278
+ chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" })
279
+ chart.to_s.should == "nginx:18.1.0"
280
+ end
322
281
 
323
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
324
- chart.apply_values({ "replicaCount" => 3 })
325
- end
282
+ it "to_s_without_version" do
283
+ chart = Kube::Helm::Chart.new({ "name" => "nginx" })
284
+ chart.to_s.should == "nginx"
285
+ end
326
286
 
327
- assert_includes captured_cmd, "template"
328
- assert_includes captured_cmd, "my-app"
329
- assert_includes captured_cmd, dir
330
- assert_match(/-f .*helm-values.*\.yaml/, captured_cmd)
331
- end
332
- end
287
+ # ── apply_values ─────────────────────────────────────────────────────
333
288
 
334
- def test_apply_values_with_custom_release
335
- Dir.mktmpdir do |dir|
336
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
337
- chart = Kube::Helm::Chart.open(dir)
289
+ it "apply_values_raises_without_source" do
290
+ chart = Kube::Helm::Chart.new({ "name" => "my-app" })
291
+ lambda { chart.apply_values({ "replicaCount" => 3 }) }.should.raise Kube::Error
292
+ end
338
293
 
339
- captured_cmd = nil
340
- stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
294
+ it "apply_values_builds_correct_command" do
295
+ Dir.mktmpdir do |dir|
296
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
297
+ chart = Kube::Helm::Chart.open(dir)
341
298
 
342
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
343
- chart.apply_values({}, release: "custom-release")
344
- end
299
+ captured_cmd = nil
300
+ stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml
345
301
 
346
- assert_includes captured_cmd, "custom-release"
302
+ original = Kube::Helm.method(:run)
303
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
304
+ begin
305
+ chart.apply_values({ "replicaCount" => 3 })
306
+ ensure
307
+ Kube::Helm.define_singleton_method(:run, original)
347
308
  end
348
- end
349
-
350
- def test_apply_values_with_namespace
351
- Dir.mktmpdir do |dir|
352
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
353
- chart = Kube::Helm::Chart.open(dir)
354
-
355
- captured_cmd = nil
356
- stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
357
-
358
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
359
- chart.apply_values({}, namespace: "production")
360
- end
361
309
 
362
- assert_includes captured_cmd, "--namespace=production"
363
- end
310
+ captured_cmd.should.include dir
364
311
  end
312
+ end
365
313
 
366
- def test_apply_values_returns_manifest
367
- Dir.mktmpdir do |dir|
368
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
369
- chart = Kube::Helm::Chart.open(dir)
314
+ it "apply_values_with_custom_release" do
315
+ Dir.mktmpdir do |dir|
316
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
317
+ chart = Kube::Helm::Chart.open(dir)
370
318
 
371
- stub_yaml = [
372
- { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } },
373
- { "kind" => "Service", "apiVersion" => "v1", "metadata" => { "name" => "web" } },
374
- ].map(&:to_yaml).join("")
319
+ captured_cmd = nil
320
+ stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
321
+
322
+ original = Kube::Helm.method(:run)
323
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
324
+ begin
325
+ chart.apply_values({}, release: "custom-release")
326
+ ensure
327
+ Kube::Helm.define_singleton_method(:run, original)
328
+ end
375
329
 
376
- result = nil
377
- Kube::Helm.stub(:run, ->(_cmd) { stub_yaml }) do
378
- result = chart.apply_values({ "replicaCount" => 3 })
379
- end
330
+ captured_cmd.should.include "custom-release"
331
+ end
332
+ end
380
333
 
381
- assert_instance_of Kube::Schema::Manifest, result
382
- assert_equal 2, result.count
334
+ it "apply_values_with_namespace" do
335
+ Dir.mktmpdir do |dir|
336
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
337
+ chart = Kube::Helm::Chart.open(dir)
383
338
 
384
- kinds = result.map(&:kind)
385
- assert_includes kinds, "Deployment"
386
- assert_includes kinds, "Service"
339
+ captured_cmd = nil
340
+ stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
341
+
342
+ original = Kube::Helm.method(:run)
343
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
344
+ begin
345
+ chart.apply_values({}, namespace: "production")
346
+ ensure
347
+ Kube::Helm.define_singleton_method(:run, original)
387
348
  end
388
- end
389
349
 
390
- def test_apply_values_defaults_release_to_chart_name
391
- Dir.mktmpdir do |dir|
392
- File.write(File.join(dir, "Chart.yaml"), { "name" => "nginx", "version" => "1.0.0" }.to_yaml)
393
- chart = Kube::Helm::Chart.open(dir)
350
+ captured_cmd.should.include "--namespace=production"
351
+ end
352
+ end
394
353
 
395
- captured_cmd = nil
396
- stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
354
+ it "apply_values_returns_manifest" do
355
+ Dir.mktmpdir do |dir|
356
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
357
+ chart = Kube::Helm::Chart.open(dir)
397
358
 
398
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
399
- chart.apply_values({})
400
- end
359
+ stub_yaml = [
360
+ { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } },
361
+ { "kind" => "Service", "apiVersion" => "v1", "metadata" => { "name" => "web" } },
362
+ ].map(&:to_yaml).join("")
401
363
 
402
- assert_includes captured_cmd, "nginx"
364
+ original = Kube::Helm.method(:run)
365
+ Kube::Helm.define_singleton_method(:run) { |_cmd| stub_yaml }
366
+ begin
367
+ result = chart.apply_values({ "replicaCount" => 3 })
368
+ ensure
369
+ Kube::Helm.define_singleton_method(:run, original)
403
370
  end
404
- end
405
-
406
- # ── show_values ──────────────────────────────────────────────────────
407
371
 
408
- def test_show_values
409
- Dir.mktmpdir do |dir|
410
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
411
- chart = Kube::Helm::Chart.open(dir)
412
-
413
- captured_cmd = nil
414
- stub_yaml = { "replicaCount" => 1, "service" => { "type" => "ClusterIP" } }.to_yaml
372
+ result.should.be.instance_of Kube::Schema::Manifest
373
+ end
374
+ end
415
375
 
416
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
417
- result = chart.show_values
418
- assert_equal 1, result["replicaCount"]
419
- assert_equal "ClusterIP", result.dig("service", "type")
420
- end
376
+ it "apply_values_defaults_release_to_chart_name" do
377
+ Dir.mktmpdir do |dir|
378
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "nginx", "version" => "1.0.0" }.to_yaml)
379
+ chart = Kube::Helm::Chart.open(dir)
421
380
 
422
- assert_includes captured_cmd, "show"
423
- assert_includes captured_cmd, "values"
424
- assert_includes captured_cmd, dir
381
+ captured_cmd = nil
382
+ stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
383
+
384
+ original = Kube::Helm.method(:run)
385
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
386
+ begin
387
+ chart.apply_values({})
388
+ ensure
389
+ Kube::Helm.define_singleton_method(:run, original)
425
390
  end
426
- end
427
391
 
428
- def test_show_values_raises_without_source
429
- chart = Kube::Helm::Chart.new({ "name" => "my-app" })
430
- assert_raises(Kube::Error) { chart.show_values }
392
+ captured_cmd.should.include "nginx"
431
393
  end
394
+ end
432
395
 
433
- # ── crds ─────────────────────────────────────────────────────────────
434
-
435
- def test_crds_returns_custom_resource_definition_objects
436
- Dir.mktmpdir do |dir|
437
- File.write(File.join(dir, "Chart.yaml"), { "name" => "cert-manager", "version" => "1.0.0" }.to_yaml)
438
- chart = Kube::Helm::Chart.open(dir)
439
-
440
- stub_yaml = [
441
- { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "cm" } },
442
- {
443
- "kind" => "CustomResourceDefinition",
444
- "apiVersion" => "apiextensions.k8s.io/v1",
445
- "metadata" => { "name" => "clusterissuers.cert-manager.io" },
446
- "spec" => {
447
- "group" => "cert-manager.io",
448
- "names" => { "kind" => "ClusterIssuer" },
449
- "versions" => [
450
- {
451
- "name" => "v1",
452
- "schema" => {
453
- "openAPIV3Schema" => {
454
- "type" => "object",
455
- "properties" => { "spec" => { "type" => "object" } },
456
- },
457
- },
458
- },
459
- ],
460
- },
461
- },
462
- ].map(&:to_yaml).join("")
396
+ # ── show_values ──────────────────────────────────────────────────────
463
397
 
464
- result = nil
465
- Kube::Helm.stub(:run, ->(_cmd) { stub_yaml }) do
466
- result = chart.crds
467
- end
398
+ it "show_values" do
399
+ Dir.mktmpdir do |dir|
400
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
401
+ chart = Kube::Helm::Chart.open(dir)
468
402
 
469
- assert_equal 1, result.length
470
- assert_equal "CustomResourceDefinition", result.first.kind
471
- assert result.first.respond_to?(:to_json_schema)
403
+ captured_cmd = nil
404
+ stub_yaml = { "replicaCount" => 1, "service" => { "type" => "ClusterIP" } }.to_yaml
405
+
406
+ original = Kube::Helm.method(:run)
407
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
408
+ begin
409
+ result = chart.show_values
410
+ result["replicaCount"].should == 1
411
+ ensure
412
+ Kube::Helm.define_singleton_method(:run, original)
472
413
  end
473
414
  end
415
+ end
474
416
 
475
- def test_crds_raises_without_source
476
- chart = Kube::Helm::Chart.new({ "name" => "my-app" })
477
- assert_raises(Kube::Error) { chart.crds }
478
- end
479
-
480
- # ── cluster scoping ──────────────────────────────────────────────────
417
+ it "show_values_raises_without_source" do
418
+ chart = Kube::Helm::Chart.new({ "name" => "my-app" })
419
+ lambda { chart.show_values }.should.raise Kube::Error
420
+ end
481
421
 
482
- def test_apply_values_uses_cluster_helm_instance
483
- Dir.mktmpdir do |dir|
484
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
485
- cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
486
- chart = Kube::Helm::Chart.open(dir, cluster: cluster)
422
+ # ── crds ─────────────────────────────────────────────────────────────
487
423
 
488
- captured_cmd = nil
489
- stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
424
+ it "crds_returns_custom_resource_definition_objects" do
425
+ Dir.mktmpdir do |dir|
426
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "cert-manager", "version" => "1.0.0" }.to_yaml)
427
+ chart = Kube::Helm::Chart.open(dir)
490
428
 
491
- cluster.connection.helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
492
- chart.apply_values({ "foo" => "bar" })
493
- end
429
+ stub_yaml = [
430
+ { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "cm" } },
431
+ {
432
+ "kind" => "CustomResourceDefinition",
433
+ "apiVersion" => "apiextensions.k8s.io/v1",
434
+ "metadata" => { "name" => "clusterissuers.cert-manager.io" },
435
+ "spec" => {
436
+ "group" => "cert-manager.io",
437
+ "names" => { "kind" => "ClusterIssuer" },
438
+ "versions" => [
439
+ {
440
+ "name" => "v1",
441
+ "schema" => {
442
+ "openAPIV3Schema" => {
443
+ "type" => "object",
444
+ "properties" => { "spec" => { "type" => "object" } },
445
+ },
446
+ },
447
+ },
448
+ ],
449
+ },
450
+ },
451
+ ].map(&:to_yaml).join("")
494
452
 
495
- assert_includes captured_cmd, "template"
496
- assert_includes captured_cmd, "my-app"
453
+ original = Kube::Helm.method(:run)
454
+ Kube::Helm.define_singleton_method(:run) { |_cmd| stub_yaml }
455
+ begin
456
+ result = chart.crds
457
+ ensure
458
+ Kube::Helm.define_singleton_method(:run, original)
497
459
  end
460
+
461
+ result.length.should == 1
498
462
  end
463
+ end
499
464
 
500
- def test_show_values_uses_cluster_helm_instance
501
- Dir.mktmpdir do |dir|
502
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
503
- cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
504
- chart = Kube::Helm::Chart.open(dir, cluster: cluster)
465
+ it "crds_raises_without_source" do
466
+ chart = Kube::Helm::Chart.new({ "name" => "my-app" })
467
+ lambda { chart.crds }.should.raise Kube::Error
468
+ end
505
469
 
506
- captured_cmd = nil
507
- stub_yaml = { "replicaCount" => 1 }.to_yaml
470
+ # ── cluster scoping ──────────────────────────────────────────────────
508
471
 
509
- cluster.connection.helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
510
- chart.show_values
511
- end
472
+ it "apply_values_uses_cluster_helm_instance" do
473
+ Dir.mktmpdir do |dir|
474
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
475
+ cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
476
+ chart = Kube::Helm::Chart.open(dir, cluster: cluster)
512
477
 
513
- assert_includes captured_cmd, "show"
514
- assert_includes captured_cmd, "values"
478
+ captured_cmd = nil
479
+ stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
480
+
481
+ helm = cluster.connection.helm
482
+ original = helm.method(:run)
483
+ helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
484
+ begin
485
+ chart.apply_values({ "foo" => "bar" })
486
+ ensure
487
+ helm.define_singleton_method(:run, original)
515
488
  end
516
- end
517
-
518
- # ── remote chart (ref-based) ─────────────────────────────────────────
519
489
 
520
- def test_initializes_with_ref
521
- chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" }, ref: "bitnami/nginx")
522
- assert_equal "bitnami/nginx", chart.ref
523
- assert_nil chart.path
490
+ captured_cmd.should.include "template"
524
491
  end
492
+ end
525
493
 
526
- def test_apply_values_with_ref_uses_ref_and_version
527
- chart = Kube::Helm::Chart.new(
528
- { "name" => "nginx", "version" => "18.1.0" },
529
- ref: "bitnami/nginx"
530
- )
494
+ it "show_values_uses_cluster_helm_instance" do
495
+ Dir.mktmpdir do |dir|
496
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
497
+ cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
498
+ chart = Kube::Helm::Chart.open(dir, cluster: cluster)
531
499
 
532
500
  captured_cmd = nil
533
- stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml
501
+ stub_yaml = { "replicaCount" => 1 }.to_yaml
534
502
 
535
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
536
- chart.apply_values({ "replicaCount" => 3 })
503
+ helm = cluster.connection.helm
504
+ original = helm.method(:run)
505
+ helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
506
+ begin
507
+ chart.show_values
508
+ ensure
509
+ helm.define_singleton_method(:run, original)
537
510
  end
538
511
 
539
- assert_includes captured_cmd, "template"
540
- assert_includes captured_cmd, "nginx"
541
- assert_includes captured_cmd, "bitnami/nginx"
542
- assert_includes captured_cmd, "--version=18.1.0"
512
+ captured_cmd.should.include "show"
543
513
  end
514
+ end
544
515
 
545
- def test_apply_values_with_path_does_not_add_version_flag
546
- Dir.mktmpdir do |dir|
547
- File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
548
- chart = Kube::Helm::Chart.open(dir)
549
-
550
- captured_cmd = nil
551
- stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
516
+ # ── remote chart (ref-based) ─────────────────────────────────────────
552
517
 
553
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
554
- chart.apply_values({})
555
- end
518
+ it "initializes_with_ref" do
519
+ chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" }, ref: "bitnami/nginx")
520
+ chart.ref.should == "bitnami/nginx"
521
+ end
556
522
 
557
- refute_includes captured_cmd, "--version"
558
- end
523
+ it "apply_values_with_ref_uses_ref_and_version" do
524
+ chart = Kube::Helm::Chart.new(
525
+ { "name" => "nginx", "version" => "18.1.0" },
526
+ ref: "bitnami/nginx"
527
+ )
528
+
529
+ captured_cmd = nil
530
+ stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml
531
+
532
+ original = Kube::Helm.method(:run)
533
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
534
+ begin
535
+ chart.apply_values({ "replicaCount" => 3 })
536
+ ensure
537
+ Kube::Helm.define_singleton_method(:run, original)
559
538
  end
560
539
 
561
- def test_show_values_with_ref
562
- chart = Kube::Helm::Chart.new(
563
- { "name" => "nginx", "version" => "18.1.0" },
564
- ref: "bitnami/nginx"
565
- )
540
+ captured_cmd.should.include "--version=18.1.0"
541
+ end
566
542
 
567
- captured_cmd = nil
568
- stub_yaml = { "replicaCount" => 1 }.to_yaml
543
+ it "apply_values_with_path_does_not_add_version_flag" do
544
+ Dir.mktmpdir do |dir|
545
+ File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
546
+ chart = Kube::Helm::Chart.open(dir)
569
547
 
570
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
571
- chart.show_values
548
+ captured_cmd = nil
549
+ stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml
550
+
551
+ original = Kube::Helm.method(:run)
552
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
553
+ begin
554
+ chart.apply_values({})
555
+ ensure
556
+ Kube::Helm.define_singleton_method(:run, original)
572
557
  end
573
558
 
574
- assert_includes captured_cmd, "show"
575
- assert_includes captured_cmd, "values"
576
- assert_includes captured_cmd, "bitnami/nginx"
577
- assert_includes captured_cmd, "--version=18.1.0"
559
+ captured_cmd.should.not.include "--version"
578
560
  end
561
+ end
579
562
 
580
- def test_crds_with_ref
581
- chart = Kube::Helm::Chart.new(
582
- { "name" => "cert-manager", "version" => "1.17.2" },
583
- ref: "jetstack/cert-manager"
584
- )
585
-
586
- stub_yaml = [
587
- {
588
- "kind" => "CustomResourceDefinition",
589
- "apiVersion" => "apiextensions.k8s.io/v1",
590
- "metadata" => { "name" => "issuers.cert-manager.io" },
591
- "spec" => {
592
- "group" => "cert-manager.io",
593
- "names" => { "kind" => "Issuer" },
594
- "versions" => [
595
- { "name" => "v1", "schema" => { "openAPIV3Schema" => { "type" => "object" } } },
596
- ],
597
- },
598
- },
599
- ].map(&:to_yaml).join("")
563
+ it "show_values_with_ref" do
564
+ chart = Kube::Helm::Chart.new(
565
+ { "name" => "nginx", "version" => "18.1.0" },
566
+ ref: "bitnami/nginx"
567
+ )
568
+
569
+ captured_cmd = nil
570
+ stub_yaml = { "replicaCount" => 1 }.to_yaml
571
+
572
+ original = Kube::Helm.method(:run)
573
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
574
+ begin
575
+ chart.show_values
576
+ ensure
577
+ Kube::Helm.define_singleton_method(:run, original)
578
+ end
600
579
 
601
- captured_cmd = nil
602
- Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
603
- result = chart.crds
604
- assert_equal 1, result.length
605
- end
580
+ captured_cmd.should.include "--version=18.1.0"
581
+ end
606
582
 
607
- assert_includes captured_cmd, "show"
608
- assert_includes captured_cmd, "crds"
609
- assert_includes captured_cmd, "jetstack/cert-manager"
610
- assert_includes captured_cmd, "--version=1.17.2"
583
+ it "crds_with_ref" do
584
+ chart = Kube::Helm::Chart.new(
585
+ { "name" => "cert-manager", "version" => "1.17.2" },
586
+ ref: "jetstack/cert-manager"
587
+ )
588
+
589
+ stub_yaml = [
590
+ {
591
+ "kind" => "CustomResourceDefinition",
592
+ "apiVersion" => "apiextensions.k8s.io/v1",
593
+ "metadata" => { "name" => "issuers.cert-manager.io" },
594
+ "spec" => {
595
+ "group" => "cert-manager.io",
596
+ "names" => { "kind" => "Issuer" },
597
+ "versions" => [
598
+ { "name" => "v1", "schema" => { "openAPIV3Schema" => { "type" => "object" } } },
599
+ ],
600
+ },
601
+ },
602
+ ].map(&:to_yaml).join("")
603
+
604
+ captured_cmd = nil
605
+ original = Kube::Helm.method(:run)
606
+ Kube::Helm.define_singleton_method(:run) { |cmd| captured_cmd = cmd; stub_yaml }
607
+ begin
608
+ result = chart.crds
609
+ result.length.should == 1
610
+ ensure
611
+ Kube::Helm.define_singleton_method(:run, original)
611
612
  end
612
613
  end
613
614
  end