diff --git a/pkg/framework/loader.go b/pkg/framework/loader.go index 0b310bf..c8591ac 100644 --- a/pkg/framework/loader.go +++ b/pkg/framework/loader.go @@ -21,7 +21,7 @@ type Loader struct { // NewLoader returns a a loader and applies options to it. func NewLoader(rootDir string, opts ...LoaderOpt) *Loader { loader := Loader{ - rootDir: rootDir, + rootDir: rootDir, globPattern: defaultTestFileGlobPattern, } @@ -77,3 +77,45 @@ func (loader *Loader) LoadSuite() (*Test, error) { return &suite, nil } + +// LoadSuiteWithFlavour loads a helmtest suite from the given directory with a specific flavour pre-configured. +func (loader *Loader) LoadSuiteWithFlavour(defaultFlavour string) (*Test, error) { + var suite Test + + if err := unmarshalYamlFromFileStrict(filepath.Join(loader.rootDir, "suite.yaml"), &suite); err != nil && !os.IsNotExist(err) { + return nil, errors.Wrap(err, "loading suite specification") + } + + if suite.Flavour == "" { + suite.Flavour = defaultFlavour + } + + if suite.Name == "" { + suite.Name = strings.TrimRight(loader.rootDir, "/") + } + + // Locate test files, if any. + testYAMLFiles, err := filepath.Glob(filepath.Join(loader.rootDir, loader.globPattern)) + if err != nil { + return nil, errors.Wrap(err, "globbing for .test.yaml files") + } + + for _, file := range testYAMLFiles { + test := Test{ + parent: &suite, + } + if err := unmarshalYamlFromFileStrict(file, &test); err != nil { + return nil, errors.Wrapf(err, "loading test specification from file %s", file) + } + if test.Name == "" { + test.Name = filepath.Base(file) + } + suite.Tests = append(suite.Tests, &test) + } + + if err := suite.initialize(); err != nil { + return nil, err + } + + return &suite, nil +} diff --git a/pkg/framework/spec.go b/pkg/framework/spec.go index 51c2315..190f134 100644 --- a/pkg/framework/spec.go +++ b/pkg/framework/spec.go @@ -12,13 +12,14 @@ type Test struct { Name string `json:"name,omitempty"` - Values map[string]interface{} `json:"values,omitempty"` - Set map[string]interface{} `json:"set,omitempty"` - - Defs string `json:"defs,omitempty"` - Release *ReleaseSpec `json:"release,omitempty"` - Server *ServerSpec `json:"server,omitempty"` - Capabilities *CapabilitiesSpec `json:"capabilities,omitempty"` + Values map[string]interface{} `json:"values,omitempty"` + Set map[string]interface{} `json:"set,omitempty"` + Flavour string `json:"flavour,omitempty"` + Condition Condition `json:"condition,omitempty"` + Defs string `json:"defs,omitempty"` + Release *ReleaseSpec `json:"release,omitempty"` + Server *ServerSpec `json:"server,omitempty"` + Capabilities *CapabilitiesSpec `json:"capabilities,omitempty"` Expect string `json:"expect,omitempty"` ExpectError *bool `json:"expectError,omitempty"` @@ -32,6 +33,11 @@ type Test struct { predicates []*gojq.Query } +// Condition +type Condition struct { + IfFlavour string `json:"ifFlavour,omitempty"` +} + // ReleaseSpec specifies how the release options for Helm will be constructed. type ReleaseSpec struct { Name string `json:"name,omitempty"` diff --git a/pkg/framework/test.go b/pkg/framework/test.go index 8cfc5bc..d1b1e08 100644 --- a/pkg/framework/test.go +++ b/pkg/framework/test.go @@ -88,9 +88,17 @@ func (t *Test) initialize() error { for i, subTest := range t.Tests { subTest.parent = t + if subTest.Flavour == "" { + subTest.Flavour = t.Flavour + } if subTest.Name == "" { subTest.Name = fmt.Sprintf("#%d", i) } + + if subTest.Flavour != "" { + subTest.Name = fmt.Sprintf("%s(%s)", subTest.Name, subTest.Flavour) + } + if err := subTest.initialize(); err != nil { return errors.Wrapf(err, "initializing %q", subTest.Name) } @@ -117,6 +125,11 @@ func (t *Test) DoRun(testingT *testing.T, tgt *Target) { return } + if t.Condition.IfFlavour != "" && t.Condition.IfFlavour != t.Flavour { + // We skip the test. + return + } + // leaf case runner := &runner{ t: testingT,