@@ -12,6 +12,8 @@ import (
12
12
"sort"
13
13
"testing"
14
14
15
+ "helm.sh/helm/v3/pkg/chartutil"
16
+
15
17
"helm.sh/helm/v3/pkg/chart"
16
18
17
19
"github.com/google/go-containerregistry/pkg/name"
@@ -736,3 +738,73 @@ func TestGroupChangesByChart(t *testing.T) {
736
738
t .Errorf ("got=%v; want=%v" , got , want )
737
739
}
738
740
}
741
+
742
+ // Check that a relocated Helm Chart does not contain information about their dependencies
743
+ func TestStripDependencyRefs (t * testing.T ) {
744
+ testChart , err := loader .Load (filepath .Join (fixturesRoot , "3-levels-chart" ))
745
+ if err != nil {
746
+ t .Fatal (err )
747
+ }
748
+
749
+ if got , want := len (testChart .Metadata .Dependencies ), 2 ; got != want {
750
+ t .Errorf ("invalid number of dependencies, got=%d, want=%d" , got , want )
751
+ }
752
+
753
+ if testChart .Lock == nil {
754
+ t .Error ("Chart.lock file expected " )
755
+ }
756
+
757
+ firstLevelDeps := testChart .Dependencies ()
758
+ // Sort dependencies since they come in arbitrary order
759
+ sortCharts (firstLevelDeps )
760
+
761
+ // Subchart1
762
+ if got , want := len (firstLevelDeps [0 ].Metadata .Dependencies ), 1 ; got != want {
763
+ t .Errorf ("invalid number of dependencies, got=%d, want=%d" , got , want )
764
+ }
765
+
766
+ if firstLevelDeps [0 ].Lock == nil {
767
+ t .Error ("Chart.lock file expected " )
768
+ }
769
+
770
+ // Strip dependencies and re-package chart
771
+ if err := stripDependencyRefs (testChart ); err != nil {
772
+ t .Fatal (err )
773
+ }
774
+
775
+ tmpDir , err := os .MkdirTemp ("" , "external-tests-*" )
776
+ if err != nil {
777
+ t .Fatal (err )
778
+ }
779
+
780
+ // Repackage chart
781
+ filename , err := chartutil .Save (testChart , tmpDir )
782
+ if err != nil {
783
+ t .Fatal (err )
784
+ }
785
+
786
+ // Load from re-packaged version
787
+ modifiedChart , err := loader .Load (filename )
788
+ if err != nil {
789
+ t .Fatal (err )
790
+ }
791
+
792
+ firstLevelDeps = modifiedChart .Dependencies ()
793
+ sortCharts (firstLevelDeps )
794
+
795
+ for _ , c := range []* chart.Chart {modifiedChart , firstLevelDeps [0 ]} {
796
+ if got := len (c .Metadata .Dependencies ); got != 0 {
797
+ t .Errorf ("expected no dependencies got=%d" , got )
798
+ }
799
+
800
+ if c .Lock != nil {
801
+ t .Error ("Chart.lock file unexpected " )
802
+ }
803
+ }
804
+ }
805
+
806
+ func sortCharts (charts []* chart.Chart ) {
807
+ sort .Slice (charts , func (i , j int ) bool {
808
+ return charts [i ].Name () < charts [j ].Name ()
809
+ })
810
+ }
0 commit comments