@@ -10,6 +10,7 @@ import (
1010 "testing"
1111
1212 "github.com/stretchr/testify/require"
13+ "github.com/stretchr/testify/assert"
1314 kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1415 "sigs.k8s.io/kustomize/kyaml/copyutil"
1516)
@@ -626,3 +627,138 @@ func copyTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced)
626627
627628 require .NoError (t , copyutil .CopyDir (th .GetFSys (), chartDir , thDir ))
628629}
630+
631+ func TestHelmChartInflationGeneratorWithSameChartMultipleVersions (t * testing.T ) {
632+ th := kusttest_test .MakeEnhancedHarnessWithTmpRoot (t ).
633+ PrepBuiltin ("HelmChartInflationGenerator" )
634+ defer th .Reset ()
635+ if err := th .ErrIfNoHelm (); err != nil {
636+ t .Skip ("skipping: " + err .Error ())
637+ }
638+ tests := []struct {
639+ name string
640+ chartName string
641+ repo string
642+ version string
643+ releaseName string
644+ }{
645+ {
646+ name : "terraform chart with no version grabs latest" ,
647+ chartName : "terraform" ,
648+ repo : "https://helm.releases.hashicorp.com" ,
649+ version : "" ,
650+ releaseName : "terraform-latest" ,
651+ },
652+ {
653+ name : "terraform chart with version 1.1.1" ,
654+ chartName : "terraform" ,
655+ repo : "https://helm.releases.hashicorp.com" ,
656+ version : "1.1.1" ,
657+ releaseName : "terraform-1.1.1" ,
658+ },
659+ {
660+ name : "terraform chart with version 1.1.1 again" ,
661+ chartName : "terraform" ,
662+ repo : "https://helm.releases.hashicorp.com" ,
663+ version : "1.1.1" ,
664+ releaseName : "terraform-1.1.1-1" ,
665+ },
666+ {
667+ name : "terraform chart with version 1.1.2" ,
668+ chartName : "terraform" ,
669+ repo : "https://helm.releases.hashicorp.com" ,
670+ version : "1.1.2" ,
671+ releaseName : "terraform-1.1.2" ,
672+ },
673+ }
674+
675+ for _ , tt := range tests {
676+ t .Run (tt .name , func (t * testing.T ) {
677+ config := fmt .Sprintf (`
678+ apiVersion: builtin
679+ kind: HelmChartInflationGenerator
680+ metadata:
681+ name: %s
682+ name: %s
683+ version: %s
684+ repo: %s
685+ releaseName: %s
686+ ` , tt .chartName , tt .chartName , tt .version , tt .repo , tt .releaseName )
687+
688+ rm := th .LoadAndRunGenerator (config )
689+ assert .True (t , len (rm .Resources ()) > 0 )
690+
691+ var chartDir string
692+ if tt .version != "" {
693+ chartDir = fmt .Sprintf ("charts/%s-%s/%s" , tt .chartName , tt .version , tt .chartName )
694+ } else {
695+ chartDir = fmt .Sprintf ("charts/%s" , tt .chartName )
696+ }
697+
698+ d , err := th .GetFSys ().ReadFile (filepath .Join (th .GetRoot (), chartDir , "Chart.yaml" ))
699+ if err != nil {
700+ t .Fatal (err )
701+ }
702+
703+ assert .Contains (t , string (d ), fmt .Sprintf ("name: %s" , tt .chartName ))
704+ if tt .version != "" {
705+ assert .Contains (t , string (d ), fmt .Sprintf ("version: %s" , tt .version ))
706+ }
707+ })
708+ }
709+ }
710+
711+ // Test that verifies +1 instances of same chart with different versions
712+ // https://github.com/kubernetes-sigs/kustomize/issues/4813
713+ func TestHelmChartInflationGeneratorWithMultipleInstancesSameChartDifferentVersions (t * testing.T ) {
714+ th := kusttest_test .MakeEnhancedHarnessWithTmpRoot (t ).
715+ PrepBuiltin ("HelmChartInflationGenerator" )
716+ defer th .Reset ()
717+ if err := th .ErrIfNoHelm (); err != nil {
718+ t .Skip ("skipping: " + err .Error ())
719+ }
720+
721+ podinfo1 := th .LoadAndRunGenerator (`
722+ apiVersion: builtin
723+ kind: HelmChartInflationGenerator
724+ metadata:
725+ name: podinfo
726+ name: podinfo
727+ version: 6.2.1
728+ repo: https://stefanprodan.github.io/podinfo
729+ releaseName: podinfo1
730+ ` )
731+
732+ podinfo2 := th .LoadAndRunGenerator (`
733+ apiVersion: builtin
734+ kind: HelmChartInflationGenerator
735+ metadata:
736+ name: podinfo
737+ name: podinfo
738+ version: 6.1.8
739+ repo: https://stefanprodan.github.io/podinfo
740+ releaseName: podinfo2
741+ ` )
742+
743+ podinfo1Img , err := podinfo1 .Resources ()[1 ].GetFieldValue ("spec.template.spec.containers.0.image" )
744+ assert .NoError (t , err )
745+ assert .Equal (t , "ghcr.io/stefanprodan/podinfo:6.2.1" , podinfo1Img )
746+
747+ podinfo2Img , err := podinfo2 .Resources ()[1 ].GetFieldValue ("spec.template.spec.containers.0.image" )
748+ assert .NoError (t , err )
749+ assert .Equal (t , "ghcr.io/stefanprodan/podinfo:6.1.8" , podinfo2Img )
750+
751+ podinfo1ChartsDir := filepath .Join (th .GetRoot (), "charts/podinfo-6.2.1/podinfo" )
752+ assert .True (t , th .GetFSys ().Exists (podinfo1ChartsDir ))
753+
754+ podinfo2ChartsDir := filepath .Join (th .GetRoot (), "charts/podinfo-6.1.8/podinfo" )
755+ assert .True (t , th .GetFSys ().Exists (podinfo2ChartsDir ))
756+
757+ podinfo1ChartContents , err := th .GetFSys ().ReadFile (filepath .Join (podinfo1ChartsDir , "Chart.yaml" ))
758+ assert .NoError (t , err )
759+ assert .Contains (t , string (podinfo1ChartContents ), "version: 6.2.1" )
760+
761+ podinfo2ChartContents , err := th .GetFSys ().ReadFile (filepath .Join (podinfo2ChartsDir , "Chart.yaml" ))
762+ assert .NoError (t , err )
763+ assert .Contains (t , string (podinfo2ChartContents ), "version: 6.1.8" )
764+ }
0 commit comments