Skip to content

Commit

Permalink
Add additional directories option (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer authored and Malte Isberner committed Feb 3, 2022
1 parent b0fe82b commit 17b5d4d
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
k8s.io/apimachinery v0.22.2
k8s.io/kube-openapi v0.0.0-20211025214626-d9a0cc0561b2
k8s.io/kubectl v0.22.2
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 // indirect
sigs.k8s.io/yaml v1.3.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,8 @@ k8s.io/utils v0.0.0-20210707171843-4b05e18ac7d9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a h1:8dYfu/Fc9Gz2rNJKB9IQRGgQOh2clmRzNIPPY1xLY5g=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 h1:ZKMMxTvduyf5WUtREOqg5LiXaN1KO/+0oOQPRFrClpo=
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
mvdan.cc/gofumpt v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
mvdan.cc/gofumpt v0.1.1/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed h1:WX1yoOaKQfddO/mLzdV4wptyWgoH/6hwLs7QHTixo0I=
Expand Down
43 changes: 33 additions & 10 deletions pkg/framework/loader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package framework

import (
"k8s.io/utils/strings/slices"
"os"
"path/filepath"
"strings"
Expand All @@ -9,20 +10,19 @@ import (
)

const (
defaultTestFileGlobPattern = "*.test.yaml"
testFileGlobPattern = "*.test.yaml"
)

// Loader loads a test suite.
type Loader struct {
globPattern string
rootDir string
rootDir string
additionalTestDirs []string
}

// NewLoader returns a a loader and applies options to it.
func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
loader := Loader{
rootDir: rootDir,
globPattern: defaultTestFileGlobPattern,
}

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

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

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

// Locate test files, if any.
testYAMLFiles, err := filepath.Glob(filepath.Join(loader.rootDir, loader.globPattern))
testYAMLFiles, err := loader.readTestYAMLFiles()
if err != nil {
return nil, errors.Wrap(err, "globbing for .test.yaml files")
return nil, err
}

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

return &suite, nil
}

// readTestYAMLFiles locates test files, if any.
func (loader *Loader) readTestYAMLFiles() ([]string, error) {
var testYAMLFiles []string
var scannedDirs []string

dirs := append(loader.additionalTestDirs, loader.rootDir)
for _, dir := range dirs {
if slices.Contains(scannedDirs, dir) {
continue
}

dirGlob := filepath.Join(dir, testFileGlobPattern)

yamlFiles, err := filepath.Glob(dirGlob)
if err != nil {
return nil, errors.Wrapf(err, "resolving files using wildcard pattern %s", dirGlob)
}
testYAMLFiles = append(testYAMLFiles, yamlFiles...)
scannedDirs = append(scannedDirs, dir)
}

return testYAMLFiles, nil
}
55 changes: 55 additions & 0 deletions pkg/framework/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package framework

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestLoader(t *testing.T) {
const testdataPath = "testdata/suite"

tests := map[string]struct {
opts []LoaderOpt
expectedFunc func(*testing.T, *Test)
additionalDir string
}{
"With root dir": {
expectedFunc: func(t *testing.T, helmTest *Test) {
assert.Len(t, helmTest.Tests, 2)
},
},
"Loader loads test hierarchy": {
expectedFunc: func(t *testing.T, test *Test) {
require.Len(t, test.Tests[1].Tests, 1)
childTest := test.findFirst([]string{testdataPath, "helm.test.yaml", "test in helm.test.yaml", "with overwrites"})
assert.Equal(t, "with overwrites", childTest.Name)
assert.Equal(t, map[string]interface {}{"testValue":"value overwrite"}, childTest.Values)
},
},
"Loader loads additional dir": {
additionalDir: "testdata/additional_dir",
expectedFunc: func(t *testing.T, test *Test) {
childTest := test.findFirst([]string{testdataPath, "additional.test.yaml"})
require.NotNil(t, test)
assert.Equal(t, "additional.test.yaml", childTest.Name)
},
},
}

for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
var opts []LoaderOpt
if tt.additionalDir != "" {
opts = append(opts, WithAdditionalTestDirs(tt.additionalDir))
}

loader := NewLoader(testdataPath, opts...)
helmTests, err := loader.LoadSuite()
require.NoError(t, err)

tt.expectedFunc(t, helmTests)
})
}
}
4 changes: 4 additions & 0 deletions pkg/framework/testdata/additional_dir/additional.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests:
- name: test in additional.test.yaml
values:
additional: additionalValue
9 changes: 9 additions & 0 deletions pkg/framework/testdata/suite/helm.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests:
- name: test in helm.test.yaml
values:
testValue: value

tests:
- name: with overwrites
values:
testValue: value overwrite
2 changes: 2 additions & 0 deletions pkg/framework/testdata/suite/suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests:
- name: test in suite.yaml

0 comments on commit 17b5d4d

Please sign in to comment.