Class: ChartTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/kube/helm/chart.rb

Instance Method Summary collapse

Instance Method Details

#test_apply_values_builds_correct_commandObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/kube/helm/chart.rb', line 315

def test_apply_values_builds_correct_command
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({ "replicaCount" => 3 })
    end

    assert_includes captured_cmd, "template"
    assert_includes captured_cmd, "my-app"
    assert_includes captured_cmd, dir
    assert_match(/-f .*helm-values.*\.yaml/, captured_cmd)
  end
end

#test_apply_values_defaults_release_to_chart_nameObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/kube/helm/chart.rb', line 390

def test_apply_values_defaults_release_to_chart_name
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "nginx", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({})
    end

    assert_includes captured_cmd, "nginx"
  end
end

#test_apply_values_raises_without_sourceObject

── apply_values ─────────────────────────────────────────────────────



310
311
312
313
# File 'lib/kube/helm/chart.rb', line 310

def test_apply_values_raises_without_source
  chart = Kube::Helm::Chart.new({ "name" => "my-app" })
  assert_raises(Kube::Error) { chart.apply_values({ "replicaCount" => 3 }) }
end

#test_apply_values_returns_manifestObject



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/kube/helm/chart.rb', line 366

def test_apply_values_returns_manifest
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    stub_yaml = [
      { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } },
      { "kind" => "Service", "apiVersion" => "v1", "metadata" => { "name" => "web" } },
    ].map(&:to_yaml).join("")

    result = nil
    Kube::Helm.stub(:run, ->(_cmd) { stub_yaml }) do
      result = chart.apply_values({ "replicaCount" => 3 })
    end

    assert_instance_of Kube::Schema::Manifest, result
    assert_equal 2, result.count

    kinds = result.map(&:kind)
    assert_includes kinds, "Deployment"
    assert_includes kinds, "Service"
  end
end

#test_apply_values_uses_cluster_helm_instanceObject

── cluster scoping ──────────────────────────────────────────────────



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/kube/helm/chart.rb', line 482

def test_apply_values_uses_cluster_helm_instance
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
    chart = Kube::Helm::Chart.open(dir, cluster: cluster)

    captured_cmd = nil
    stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml

    cluster.connection.helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({ "foo" => "bar" })
    end

    assert_includes captured_cmd, "template"
    assert_includes captured_cmd, "my-app"
  end
end

#test_apply_values_with_custom_releaseObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/kube/helm/chart.rb', line 334

def test_apply_values_with_custom_release
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({}, release: "custom-release")
    end

    assert_includes captured_cmd, "custom-release"
  end
end

#test_apply_values_with_namespaceObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/kube/helm/chart.rb', line 350

def test_apply_values_with_namespace
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({}, namespace: "production")
    end

    assert_includes captured_cmd, "--namespace=production"
  end
end

#test_apply_values_with_path_does_not_add_version_flagObject



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/kube/helm/chart.rb', line 545

def test_apply_values_with_path_does_not_add_version_flag
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "kind" => "Pod", "apiVersion" => "v1" }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.apply_values({})
    end

    refute_includes captured_cmd, "--version"
  end
end

#test_apply_values_with_ref_uses_ref_and_versionObject



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/kube/helm/chart.rb', line 526

def test_apply_values_with_ref_uses_ref_and_version
  chart = Kube::Helm::Chart.new(
    { "name" => "nginx", "version" => "18.1.0" },
    ref: "bitnami/nginx"
  )

  captured_cmd = nil
  stub_yaml = { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "web" } }.to_yaml

  Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
    chart.apply_values({ "replicaCount" => 3 })
  end

  assert_includes captured_cmd, "template"
  assert_includes captured_cmd, "nginx"
  assert_includes captured_cmd, "bitnami/nginx"
  assert_includes captured_cmd, "--version=18.1.0"
end

#test_crds_raises_without_sourceObject



475
476
477
478
# File 'lib/kube/helm/chart.rb', line 475

def test_crds_raises_without_source
  chart = Kube::Helm::Chart.new({ "name" => "my-app" })
  assert_raises(Kube::Error) { chart.crds }
end

#test_crds_returns_custom_resource_definition_objectsObject

── crds ─────────────────────────────────────────────────────────────



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/kube/helm/chart.rb', line 435

def test_crds_returns_custom_resource_definition_objects
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "cert-manager", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    stub_yaml = [
      { "kind" => "Deployment", "apiVersion" => "apps/v1", "metadata" => { "name" => "cm" } },
      {
        "kind" => "CustomResourceDefinition",
        "apiVersion" => "apiextensions.k8s.io/v1",
        "metadata" => { "name" => "clusterissuers.cert-manager.io" },
        "spec" => {
          "group" => "cert-manager.io",
          "names" => { "kind" => "ClusterIssuer" },
          "versions" => [
            {
              "name" => "v1",
              "schema" => {
                "openAPIV3Schema" => {
                  "type" => "object",
                  "properties" => { "spec" => { "type" => "object" } },
                },
              },
            },
          ],
        },
      },
    ].map(&:to_yaml).join("")

    result = nil
    Kube::Helm.stub(:run, ->(_cmd) { stub_yaml }) do
      result = chart.crds
    end

    assert_equal 1, result.length
    assert_equal "CustomResourceDefinition", result.first.kind
    assert result.first.respond_to?(:to_json_schema)
  end
end

#test_crds_with_refObject



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/kube/helm/chart.rb', line 580

def test_crds_with_ref
  chart = Kube::Helm::Chart.new(
    { "name" => "cert-manager", "version" => "1.17.2" },
    ref: "jetstack/cert-manager"
  )

  stub_yaml = [
    {
      "kind" => "CustomResourceDefinition",
      "apiVersion" => "apiextensions.k8s.io/v1",
      "metadata" => { "name" => "issuers.cert-manager.io" },
      "spec" => {
        "group" => "cert-manager.io",
        "names" => { "kind" => "Issuer" },
        "versions" => [
          { "name" => "v1", "schema" => { "openAPIV3Schema" => { "type" => "object" } } },
        ],
      },
    },
  ].map(&:to_yaml).join("")

  captured_cmd = nil
  Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
    result = chart.crds
    assert_equal 1, result.length
  end

  assert_includes captured_cmd, "show"
  assert_includes captured_cmd, "crds"
  assert_includes captured_cmd, "jetstack/cert-manager"
  assert_includes captured_cmd, "--version=1.17.2"
end

#test_initializes_emptyObject



237
238
239
240
241
242
243
244
245
# File 'lib/kube/helm/chart.rb', line 237

def test_initializes_empty
  chart = Kube::Helm::Chart.new
  assert_nil chart.name
  assert_nil chart.version
  assert_nil chart.app_version
  assert_nil chart.description
  assert_nil chart.type
  assert_equal [], chart.dependencies
end

#test_initializes_with_blockObject



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/kube/helm/chart.rb', line 223

def test_initializes_with_block
  chart = Kube::Helm::Chart.new {
    self.name = "my-app"
    self.version = "1.0.0"
    self.appVersion = "2.5.0"
    self.description = "A test chart"
  }

  assert_equal "my-app", chart.name
  assert_equal "1.0.0", chart.version
  assert_equal "2.5.0", chart.app_version
  assert_equal "A test chart", chart.description
end

#test_initializes_with_clusterObject



252
253
254
255
256
# File 'lib/kube/helm/chart.rb', line 252

def test_initializes_with_cluster
  cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
  chart = Kube::Helm::Chart.new({ "name" => "x" }, cluster: cluster)
  assert_equal cluster, chart.cluster
end

#test_initializes_with_data_hashObject

── initialization ────────────────────────────────────────────────────



216
217
218
219
220
221
# File 'lib/kube/helm/chart.rb', line 216

def test_initializes_with_data_hash
  chart = Kube::Helm::Chart.new({ "name" => "my-app", "version" => "1.0.0", "appVersion" => "2.5.0" })
  assert_equal "my-app", chart.name
  assert_equal "1.0.0", chart.version
  assert_equal "2.5.0", chart.app_version
end

#test_initializes_with_pathObject



247
248
249
250
# File 'lib/kube/helm/chart.rb', line 247

def test_initializes_with_path
  chart = Kube::Helm::Chart.new({ "name" => "x" }, path: "/tmp/charts/x")
  assert_equal "/tmp/charts/x", chart.path
end

#test_initializes_with_refObject

── remote chart (ref-based) ─────────────────────────────────────────



520
521
522
523
524
# File 'lib/kube/helm/chart.rb', line 520

def test_initializes_with_ref
  chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" }, ref: "bitnami/nginx")
  assert_equal "bitnami/nginx", chart.ref
  assert_nil chart.path
end

#test_open_raises_without_chart_yamlObject



280
281
282
283
284
# File 'lib/kube/helm/chart.rb', line 280

def test_open_raises_without_chart_yaml
  Dir.mktmpdir do |dir|
    assert_raises(Kube::Error) { Kube::Helm::Chart.open(dir) }
  end
end

#test_open_reads_chart_yamlObject

── Chart.open ───────────────────────────────────────────────────────



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/kube/helm/chart.rb', line 260

def test_open_reads_chart_yaml
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), {
      "name" => "test-chart",
      "version" => "0.1.0",
      "appVersion" => "1.0.0",
      "description" => "A test chart",
      "type" => "application",
    }.to_yaml)

    chart = Kube::Helm::Chart.open(dir)
    assert_equal "test-chart", chart.name
    assert_equal "0.1.0", chart.version
    assert_equal "1.0.0", chart.app_version
    assert_equal "A test chart", chart.description
    assert_equal "application", chart.type
    assert_equal dir, chart.path
  end
end

#test_open_with_clusterObject



286
287
288
289
290
291
292
293
294
# File 'lib/kube/helm/chart.rb', line 286

def test_open_with_cluster
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "x", "version" => "1.0.0" }.to_yaml)
    cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")

    chart = Kube::Helm::Chart.open(dir, cluster: cluster)
    assert_equal cluster, chart.cluster
  end
end

#test_show_valuesObject

── show_values ──────────────────────────────────────────────────────



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/kube/helm/chart.rb', line 408

def test_show_values
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    chart = Kube::Helm::Chart.open(dir)

    captured_cmd = nil
    stub_yaml = { "replicaCount" => 1, "service" => { "type" => "ClusterIP" } }.to_yaml

    Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      result = chart.show_values
      assert_equal 1, result["replicaCount"]
      assert_equal "ClusterIP", result.dig("service", "type")
    end

    assert_includes captured_cmd, "show"
    assert_includes captured_cmd, "values"
    assert_includes captured_cmd, dir
  end
end

#test_show_values_raises_without_sourceObject



428
429
430
431
# File 'lib/kube/helm/chart.rb', line 428

def test_show_values_raises_without_source
  chart = Kube::Helm::Chart.new({ "name" => "my-app" })
  assert_raises(Kube::Error) { chart.show_values }
end

#test_show_values_uses_cluster_helm_instanceObject



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/kube/helm/chart.rb', line 500

def test_show_values_uses_cluster_helm_instance
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "Chart.yaml"), { "name" => "my-app", "version" => "1.0.0" }.to_yaml)
    cluster = Kube::Cluster.connect(kubeconfig: "/tmp/test-kubeconfig")
    chart = Kube::Helm::Chart.open(dir, cluster: cluster)

    captured_cmd = nil
    stub_yaml = { "replicaCount" => 1 }.to_yaml

    cluster.connection.helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
      chart.show_values
    end

    assert_includes captured_cmd, "show"
    assert_includes captured_cmd, "values"
  end
end

#test_show_values_with_refObject



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/kube/helm/chart.rb', line 561

def test_show_values_with_ref
  chart = Kube::Helm::Chart.new(
    { "name" => "nginx", "version" => "18.1.0" },
    ref: "bitnami/nginx"
  )

  captured_cmd = nil
  stub_yaml = { "replicaCount" => 1 }.to_yaml

  Kube::Helm.stub(:run, ->(cmd) { captured_cmd = cmd; stub_yaml }) do
    chart.show_values
  end

  assert_includes captured_cmd, "show"
  assert_includes captured_cmd, "values"
  assert_includes captured_cmd, "bitnami/nginx"
  assert_includes captured_cmd, "--version=18.1.0"
end

#test_to_s_with_versionObject

── to_s ──────────────────────────────────────────────────────────────



298
299
300
301
# File 'lib/kube/helm/chart.rb', line 298

def test_to_s_with_version
  chart = Kube::Helm::Chart.new({ "name" => "nginx", "version" => "18.1.0" })
  assert_equal "nginx:18.1.0", chart.to_s
end

#test_to_s_without_versionObject



303
304
305
306
# File 'lib/kube/helm/chart.rb', line 303

def test_to_s_without_version
  chart = Kube::Helm::Chart.new({ "name" => "nginx" })
  assert_equal "nginx", chart.to_s
end