Skip to content

Commit 0a77cbf

Browse files
committed
some refactors
1 parent 71dad5a commit 0a77cbf

File tree

4 files changed

+138
-134
lines changed

4 files changed

+138
-134
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"path"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func (s *helmRepoSuite) TestChartTemplatesWithCustomValues() {
11+
testFiles, err := os.ReadDir("testfiles")
12+
s.NoError(err)
13+
14+
for _, testFile := range testFiles {
15+
testFileName := testFile.Name()
16+
17+
s.Run(testFileName, func() {
18+
chartName := strings.Split(strings.Split(testFileName, ".")[0], "_")[0]
19+
chartDir := s.getChartDirectory(chartName)
20+
21+
chartDirFullPath, err := filepath.Abs(chartDir)
22+
s.NoError(err)
23+
24+
valuesFilePath := path.Join("testfiles", testFileName)
25+
runGoldenHelmTest(s.T(), &goldenHelmTest{
26+
ChartPath: chartDirFullPath,
27+
Release: "release-test",
28+
Namespace: "release-helm-namespace",
29+
// remove .yaml from the test file name
30+
GoldenFileName: strings.TrimSuffix(testFileName, ".yaml"),
31+
ValuesFile: valuesFilePath,
32+
GoldenSubDirectory: "custom",
33+
})
34+
})
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tests
2+
3+
import (
4+
"path"
5+
"path/filepath"
6+
)
7+
8+
func (s *helmRepoSuite) TestChartTemplateWithDefaultValues() {
9+
charts := s.getChartsInDirectory(chartsRootDir)
10+
11+
for _, chartName := range charts {
12+
s.Run(chartName, func() {
13+
chartDir := s.getChartDirectory(chartName)
14+
chartDirFullPath, err := filepath.Abs(chartDir)
15+
s.NoError(err)
16+
defaultValuesFilePath := path.Join(chartDir, "values.yaml")
17+
18+
runGoldenHelmTest(s.T(), &goldenHelmTest{
19+
ChartPath: chartDirFullPath,
20+
Release: "release-test",
21+
Namespace: "release-helm-namespace",
22+
GoldenFileName: chartName,
23+
ValuesFile: defaultValuesFilePath,
24+
GoldenSubDirectory: "default",
25+
})
26+
})
27+
}
28+
}

.circleci/tests/golden.go renamed to .circleci/tests/helm_repository_suite_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,47 @@ package tests
22

33
import (
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

1423
var 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+
1646
type 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+
}

.circleci/tests/helm_repository_test.go

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)