Skip to content

Commit 1ad97c4

Browse files
authored
Add additional directories option (#5)
1 parent fa0403c commit 1ad97c4

File tree

7 files changed

+106
-10
lines changed

7 files changed

+106
-10
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ require (
1313
k8s.io/apimachinery v0.22.2
1414
k8s.io/kube-openapi v0.0.0-20211025214626-d9a0cc0561b2
1515
k8s.io/kubectl v0.22.2
16+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 // indirect
1617
sigs.k8s.io/yaml v1.3.0
1718
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,8 @@ k8s.io/utils v0.0.0-20210707171843-4b05e18ac7d9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
18331833
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
18341834
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a h1:8dYfu/Fc9Gz2rNJKB9IQRGgQOh2clmRzNIPPY1xLY5g=
18351835
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
1836+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 h1:ZKMMxTvduyf5WUtREOqg5LiXaN1KO/+0oOQPRFrClpo=
1837+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
18361838
mvdan.cc/gofumpt v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
18371839
mvdan.cc/gofumpt v0.1.1/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
18381840
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed h1:WX1yoOaKQfddO/mLzdV4wptyWgoH/6hwLs7QHTixo0I=

pkg/framework/loader.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package framework
22

33
import (
4+
"k8s.io/utils/strings/slices"
45
"os"
56
"path/filepath"
67
"strings"
@@ -9,20 +10,19 @@ import (
910
)
1011

1112
const (
12-
defaultTestFileGlobPattern = "*.test.yaml"
13+
testFileGlobPattern = "*.test.yaml"
1314
)
1415

1516
// Loader loads a test suite.
1617
type Loader struct {
17-
globPattern string
18-
rootDir string
18+
rootDir string
19+
additionalTestDirs []string
1920
}
2021

2122
// NewLoader returns a a loader and applies options to it.
2223
func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
2324
loader := Loader{
2425
rootDir: rootDir,
25-
globPattern: defaultTestFileGlobPattern,
2626
}
2727

2828
for _, opt := range opts {
@@ -34,10 +34,10 @@ func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
3434
// LoaderOpts allows to set custom options.
3535
type LoaderOpt func(loader *Loader)
3636

37-
// WithCustomFilePattern sets a custom file pattern to load test files.
38-
func WithCustomFilePattern(pattern string) LoaderOpt {
37+
// WithAdditionalTestDirs adds additional test source directories which are scanned for tests.
38+
func WithAdditionalTestDirs(path ...string) LoaderOpt {
3939
return func(loader *Loader) {
40-
loader.globPattern = pattern
40+
loader.additionalTestDirs = append(loader.additionalTestDirs, path...)
4141
}
4242
}
4343

@@ -52,10 +52,9 @@ func (loader *Loader) LoadSuite() (*Test, error) {
5252
suite.Name = strings.TrimRight(loader.rootDir, "/")
5353
}
5454

55-
// Locate test files, if any.
56-
testYAMLFiles, err := filepath.Glob(filepath.Join(loader.rootDir, loader.globPattern))
55+
testYAMLFiles, err := loader.readTestYAMLFiles()
5756
if err != nil {
58-
return nil, errors.Wrap(err, "globbing for .test.yaml files")
57+
return nil, err
5958
}
6059

6160
for _, file := range testYAMLFiles {
@@ -77,3 +76,27 @@ func (loader *Loader) LoadSuite() (*Test, error) {
7776

7877
return &suite, nil
7978
}
79+
80+
// readTestYAMLFiles locates test files, if any.
81+
func (loader *Loader) readTestYAMLFiles() ([]string, error) {
82+
var testYAMLFiles []string
83+
var scannedDirs []string
84+
85+
dirs := append(loader.additionalTestDirs, loader.rootDir)
86+
for _, dir := range dirs {
87+
if slices.Contains(scannedDirs, dir) {
88+
continue
89+
}
90+
91+
dirGlob := filepath.Join(dir, testFileGlobPattern)
92+
93+
yamlFiles, err := filepath.Glob(dirGlob)
94+
if err != nil {
95+
return nil, errors.Wrapf(err, "resolving files using wildcard pattern %s", dirGlob)
96+
}
97+
testYAMLFiles = append(testYAMLFiles, yamlFiles...)
98+
scannedDirs = append(scannedDirs, dir)
99+
}
100+
101+
return testYAMLFiles, nil
102+
}

pkg/framework/loader_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package framework
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
)
8+
9+
func TestLoader(t *testing.T) {
10+
const testdataPath = "testdata/suite"
11+
12+
tests := map[string]struct {
13+
opts []LoaderOpt
14+
expectedFunc func(*testing.T, *Test)
15+
additionalDir string
16+
}{
17+
"With root dir": {
18+
expectedFunc: func(t *testing.T, helmTest *Test) {
19+
assert.Len(t, helmTest.Tests, 2)
20+
},
21+
},
22+
"Loader loads test hierarchy": {
23+
expectedFunc: func(t *testing.T, test *Test) {
24+
require.Len(t, test.Tests[1].Tests, 1)
25+
childTest := test.findFirst([]string{testdataPath, "helm.test.yaml", "test in helm.test.yaml", "with overwrites"})
26+
assert.Equal(t, "with overwrites", childTest.Name)
27+
assert.Equal(t, map[string]interface {}{"testValue":"value overwrite"}, childTest.Values)
28+
},
29+
},
30+
"Loader loads additional dir": {
31+
additionalDir: "testdata/additional_dir",
32+
expectedFunc: func(t *testing.T, test *Test) {
33+
childTest := test.findFirst([]string{testdataPath, "additional.test.yaml"})
34+
require.NotNil(t, test)
35+
assert.Equal(t, "additional.test.yaml", childTest.Name)
36+
},
37+
},
38+
}
39+
40+
for name, tt := range tests {
41+
tt := tt
42+
t.Run(name, func(t *testing.T) {
43+
var opts []LoaderOpt
44+
if tt.additionalDir != "" {
45+
opts = append(opts, WithAdditionalTestDirs(tt.additionalDir))
46+
}
47+
48+
loader := NewLoader(testdataPath, opts...)
49+
helmTests, err := loader.LoadSuite()
50+
require.NoError(t, err)
51+
52+
tt.expectedFunc(t, helmTests)
53+
})
54+
}
55+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
- name: test in additional.test.yaml
3+
values:
4+
additional: additionalValue
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests:
2+
- name: test in helm.test.yaml
3+
values:
4+
testValue: value
5+
6+
tests:
7+
- name: with overwrites
8+
values:
9+
testValue: value overwrite
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tests:
2+
- name: test in suite.yaml

0 commit comments

Comments
 (0)