Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support distinguishing flavours #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion pkg/framework/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
}
20 changes: 13 additions & 7 deletions pkg/framework/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
13 changes: 13 additions & 0 deletions pkg/framework/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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,
Expand Down