From 34d979dd1931e6f7d745b56e2e52d080cee934ed Mon Sep 17 00:00:00 2001 From: Dustin Lish Date: Wed, 22 Mar 2023 16:53:22 -0600 Subject: [PATCH] Add test for issue #4813 --- .../HelmChartInflationGenerator_test.go | 149 +++++++++++------- 1 file changed, 95 insertions(+), 54 deletions(-) diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go index 3fa84acf2ed..c633d21bd6a 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go @@ -590,75 +590,61 @@ func TestHelmChartInflationGeneratorWithSameChartMultipleVersions(t *testing.T) } tests := []struct { - name string - version string - config string + name string + chartName string + repo string + version string + releaseName string }{ { - name: "terraform chart with no version grabs latest", - version: "", - config: ` -apiVersion: builtin -kind: HelmChartInflationGenerator -metadata: - name: terraform -name: terraform -repo: https://helm.releases.hashicorp.com -releaseName: terraforming-mars -`, + name: "terraform chart with no version grabs latest", + chartName: "terraform", + repo: "https://helm.releases.hashicorp.com", + version: "", + releaseName: "terraform-latest", }, { - name: "terraform chart with version 1.1.1", - version: "1.1.1", - config: ` -apiVersion: builtin -kind: HelmChartInflationGenerator -metadata: - name: terraform -name: terraform -version: 1.1.1 -repo: https://helm.releases.hashicorp.com -releaseName: terraforming-mars-1 -`, + name: "terraform chart with version 1.1.1", + chartName: "terraform", + repo: "https://helm.releases.hashicorp.com", + version: "1.1.1", + releaseName: "terraform-1.1.1", }, { - name: "terraform chart with version 1.1.1 again", - version: "1.1.1", - config: ` -apiVersion: builtin -kind: HelmChartInflationGenerator -metadata: - name: terraform -name: terraform -version: 1.1.1 -repo: https://helm.releases.hashicorp.com -releaseName: terraforming-mars-2 -`, + name: "terraform chart with version 1.1.1 again", + chartName: "terraform", + repo: "https://helm.releases.hashicorp.com", + version: "1.1.1", + releaseName: "terraform-1.1.1-1", }, { - name: "terraform chart with version 1.1.2", - version: "1.1.2", - config: ` -apiVersion: builtin -kind: HelmChartInflationGenerator -metadata: - name: terraform -name: terraform -version: 1.1.2 -repo: https://helm.releases.hashicorp.com -releaseName: terraforming-mars-3 -`, + name: "terraform chart with version 1.1.2", + chartName: "terraform", + repo: "https://helm.releases.hashicorp.com", + version: "1.1.2", + releaseName: "terraform-1.1.2", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - rm := th.LoadAndRunGenerator(tt.config) + config := fmt.Sprintf(` +apiVersion: builtin +kind: HelmChartInflationGenerator +metadata: + name: %s +name: %s +version: %s +repo: %s +releaseName: %s +`, tt.chartName, tt.chartName, tt.version, tt.repo, tt.releaseName) + + rm := th.LoadAndRunGenerator(config) assert.True(t, len(rm.Resources()) > 0) - chartDir := "charts/terraform" + chartDir := fmt.Sprintf("charts/%s", tt.chartName) if tt.version != "" { - chartDir = fmt.Sprintf("%s-%s/terraform", chartDir, tt.version) + chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName) } d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml")) @@ -666,10 +652,65 @@ releaseName: terraforming-mars-3 t.Fatal(err) } - assert.Contains(t, string(d), "name: terraform") + assert.Contains(t, string(d), fmt.Sprintf("name: %s", tt.chartName)) if tt.version != "" { assert.Contains(t, string(d), fmt.Sprintf("version: %s", tt.version)) } }) } } + +// Test that verifies +1 instances of same chart with different versions +// https://github.com/kubernetes-sigs/kustomize/issues/4813 +func TestHelmChartInflationGeneratorWithMultipleInstancesSameChartDifferentVersions(t *testing.T) { + th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t). + PrepBuiltin("HelmChartInflationGenerator") + defer th.Reset() + if err := th.ErrIfNoHelm(); err != nil { + t.Skip("skipping: " + err.Error()) + } + + podinfo1 := th.LoadAndRunGenerator(` +apiVersion: builtin +kind: HelmChartInflationGenerator +metadata: + name: podinfo +name: podinfo +version: 6.2.1 +repo: https://stefanprodan.github.io/podinfo +releaseName: podinfo1 +`) + + podinfo2 := th.LoadAndRunGenerator(` +apiVersion: builtin +kind: HelmChartInflationGenerator +metadata: + name: podinfo +name: podinfo +version: 6.1.8 +repo: https://stefanprodan.github.io/podinfo +releaseName: podinfo2 +`) + + podinfo1Img, err := podinfo1.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image") + assert.NoError(t, err) + assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.2.1", podinfo1Img) + + podinfo2Img, err := podinfo2.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image") + assert.NoError(t, err) + assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.1.8", podinfo2Img) + + podinfo1ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.2.1/podinfo") + assert.True(t, th.GetFSys().Exists(podinfo1ChartsDir)) + + podinfo2ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.1.8/podinfo") + assert.True(t, th.GetFSys().Exists(podinfo2ChartsDir)) + + podinfo1ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo1ChartsDir, "Chart.yaml")) + assert.NoError(t, err) + assert.Contains(t, string(podinfo1ChartContents), "version: 6.2.1") + + podinfo2ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo2ChartsDir, "Chart.yaml")) + assert.NoError(t, err) + assert.Contains(t, string(podinfo2ChartContents), "version: 6.1.8") +}