@@ -2,17 +2,47 @@ package tests
22
33import (
44 "flag"
5+ "maps"
56 "os"
7+ "path"
68 "regexp"
9+ "slices"
710 "testing"
811
912 "github.com/gruntwork-io/terratest/modules/helm"
1013 "github.com/gruntwork-io/terratest/modules/k8s"
1114 "github.com/stretchr/testify/require"
15+ "github.com/stretchr/testify/suite"
16+ "helm.sh/helm/v3/pkg/chartutil"
17+ )
18+
19+ const (
20+ chartsRootDir = "../../."
1221)
1322
1423var update = flag .Bool ("update-golden" , false , "update golden test output files" )
1524
25+ type helmRepoSuite struct {
26+ suite.Suite
27+ }
28+
29+ func TestHelmRepository (t * testing.T ) {
30+ suite .Run (t , new (helmRepoSuite ))
31+ }
32+
33+ func (s * helmRepoSuite ) SetupSuite () {
34+ // Run the helm repo add command
35+ // helm repo add wiz-sec https://wiz-sec.github.io/charts
36+ if _ , err := helm .RunHelmCommandAndGetOutputE (s .T (), & helm.Options {}, "repo" , "add" , "wiz-chart-test" , "https://wiz-sec.github.io/charts" ); err != nil {
37+ s .Failf ("Failed to add helm repository" , "error is %s" , err )
38+ }
39+
40+ //// Run the helm repo update command
41+ if _ , err := helm .RunHelmCommandAndGetOutputE (s .T (), & helm.Options {}, "repo" , "update" ); err != nil {
42+ s .Failf ("Failed to update helm repository" , "error is %s" , err )
43+ }
44+ }
45+
1646type goldenHelmTest struct {
1747 ChartPath string
1848 Release string
@@ -64,3 +94,47 @@ func runGoldenHelmTest(t *testing.T, testCase *goldenHelmTest) {
6494 r .NoError (err , "Golden file doesn't exist or was not readable" )
6595 r .Equal (string (expected ), output , "Rendered output does not match golden file. Please run tests with -update-golden flag to update the golden files." )
6696}
97+
98+ func (s * helmRepoSuite ) getChartDirectory (chartName string ) string {
99+ chartDir := path .Join (chartsRootDir , chartName )
100+ if _ , err := os .Stat (path .Join (chartDir , "Chart.yaml" )); os .IsNotExist (err ) {
101+ // fail the test
102+ s .Fail ("Chart.yaml file not found in %s" , chartDir )
103+ }
104+
105+ isChartDir , err := chartutil .IsChartDir (chartDir )
106+ s .NoError (err )
107+ s .True (isChartDir )
108+
109+ return chartDir
110+ }
111+
112+ func (s * helmRepoSuite ) getChartsInDirectory (dir string ) []string {
113+ // There's no need to add directories to this list, but this is for extra care, to ensure we don't miss these charts
114+ charts := map [string ]struct {}{
115+ "wiz-broker" : {},
116+ "wiz-sensor" : {},
117+ "wiz-admission-controller" : {},
118+ "wiz-kubernetes-connector" : {},
119+ "wiz-kubernetes-integration" : {},
120+ }
121+
122+ files , err := os .ReadDir (dir )
123+ s .NoError (err )
124+
125+ for _ , fileInfo := range files {
126+ if ! fileInfo .IsDir () {
127+ continue
128+ }
129+
130+ chartName := fileInfo .Name ()
131+ chartFilePath := path .Join (path .Join (chartsRootDir , chartName ), "Chart.yaml" )
132+ if _ , err := os .Stat (chartFilePath ); os .IsNotExist (err ) {
133+ continue
134+ }
135+
136+ charts [chartName ] = struct {}{}
137+ }
138+
139+ return slices .Collect (maps .Keys (charts ))
140+ }
0 commit comments