From 17b5d4d91f690a0a5fc8051c76feb5ca4aafa670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=A4umer?= Date: Tue, 18 Jan 2022 11:08:12 +0100 Subject: [PATCH] Add additional directories option (#5) --- go.mod | 1 + go.sum | 2 + pkg/framework/loader.go | 43 +++++++++++---- pkg/framework/loader_test.go | 55 +++++++++++++++++++ .../additional_dir/additional.test.yaml | 4 ++ pkg/framework/testdata/suite/helm.test.yaml | 9 +++ pkg/framework/testdata/suite/suite.yaml | 2 + 7 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 pkg/framework/loader_test.go create mode 100644 pkg/framework/testdata/additional_dir/additional.test.yaml create mode 100644 pkg/framework/testdata/suite/helm.test.yaml create mode 100644 pkg/framework/testdata/suite/suite.yaml diff --git a/go.mod b/go.mod index 7b3c9c6..dbdefb7 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index acac0db..49161bb 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/framework/loader.go b/pkg/framework/loader.go index 0b310bf..db1fc99 100644 --- a/pkg/framework/loader.go +++ b/pkg/framework/loader.go @@ -1,6 +1,7 @@ package framework import ( + "k8s.io/utils/strings/slices" "os" "path/filepath" "strings" @@ -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 { @@ -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...) } } @@ -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 { @@ -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 +} diff --git a/pkg/framework/loader_test.go b/pkg/framework/loader_test.go new file mode 100644 index 0000000..4be5adb --- /dev/null +++ b/pkg/framework/loader_test.go @@ -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) + }) + } +} diff --git a/pkg/framework/testdata/additional_dir/additional.test.yaml b/pkg/framework/testdata/additional_dir/additional.test.yaml new file mode 100644 index 0000000..4b680e1 --- /dev/null +++ b/pkg/framework/testdata/additional_dir/additional.test.yaml @@ -0,0 +1,4 @@ +tests: + - name: test in additional.test.yaml + values: + additional: additionalValue diff --git a/pkg/framework/testdata/suite/helm.test.yaml b/pkg/framework/testdata/suite/helm.test.yaml new file mode 100644 index 0000000..6c7e7be --- /dev/null +++ b/pkg/framework/testdata/suite/helm.test.yaml @@ -0,0 +1,9 @@ +tests: + - name: test in helm.test.yaml + values: + testValue: value + + tests: + - name: with overwrites + values: + testValue: value overwrite diff --git a/pkg/framework/testdata/suite/suite.yaml b/pkg/framework/testdata/suite/suite.yaml new file mode 100644 index 0000000..f0693eb --- /dev/null +++ b/pkg/framework/testdata/suite/suite.yaml @@ -0,0 +1,2 @@ +tests: + - name: test in suite.yaml