Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

[Feature] : possibility to check warnings when applying resources #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions config/crd/bases/kuttl.dev_teststeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ spec:
type: string
shouldFail:
type: boolean
warnings:
items:
description: ExpectedOutput defines the criteria that command
output should meet.
properties:
expected:
description: Value is the expected value or pattern that should
be matched against the command's output.
type: string
match:
description: MatchType is the type of match that should be
applied for validation. This could be "Equals", "Contains",
or "Wildcard".
type: string
required:
- expected
- match
type: object
type: array
type: object
type: array
assert:
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/testharness/v1beta1/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ func (c *CommandOutput) ValidateCommandOutput(stdoutOutput, stderrOutput strings
var errs []string

if c.Stdout != nil {
if err := c.Stdout.validateOutput("stdout", stdoutOutput.String()); err != nil {
if err := c.Stdout.ValidateOutput("stdout", stdoutOutput.String()); err != nil {
errs = append(errs, err.Error())
}
}
if c.Stderr != nil {
if err := c.Stderr.validateOutput("stderr", stderrOutput.String()); err != nil {
if err := c.Stderr.ValidateOutput("stderr", stderrOutput.String()); err != nil {
errs = append(errs, err.Error())
}
}
Expand All @@ -26,7 +26,7 @@ func (c *CommandOutput) ValidateCommandOutput(stdoutOutput, stderrOutput strings
return nil
}

func (e *ExpectedOutput) validateOutput(outputType string, actualValue string) error {
func (e *ExpectedOutput) ValidateOutput(outputType string, actualValue string) error {
expectedValue := e.ExpectedValue
matchType := e.MatchType
if matchType == "" {
Expand Down
11 changes: 7 additions & 4 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ type TestSuite struct {

// Apply holds infos for an apply statement
type Apply struct {
File string `json:"file,omitempty"`
ShouldFail bool `json:"shouldFail,omitempty"`
File string `json:"file,omitempty"`
ShouldFail bool `json:"shouldFail,omitempty"`
Warnings *[]ExpectedOutput `json:"warnings,omitempty"`
}

// UnmarshalJSON implements the json.Unmarshaller interface.
Expand All @@ -113,14 +114,16 @@ func (apply *Apply) UnmarshalJSON(value []byte) error {
return json.Unmarshal(value, &apply.File)
}
data := struct {
File string `json:"file,omitempty"`
ShouldFail bool `json:"shouldFail,omitempty"`
File string `json:"file,omitempty"`
ShouldFail bool `json:"shouldFail,omitempty"`
Warnings *[]ExpectedOutput `json:"warnings,omitempty"`
}{}
if err := json.Unmarshal(value, &data); err != nil {
return err
}
apply.File = data.File
apply.ShouldFail = data.ShouldFail
apply.Warnings = data.Warnings
return nil
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var fileNameRegex = regexp.MustCompile(`^(?:\d+-)?([^-\.]+)(-[^\.]+)?(?:\.yaml)?
type apply struct {
object client.Object
shouldFail bool
warnings *[]harness.ExpectedOutput
}

type asserts struct {
Expand Down Expand Up @@ -250,7 +251,7 @@ func (s *Step) Create(test *testing.T, namespace string) []error {

for _, apply := range s.Apply {
err := doApply(test, s.SkipDelete, s.Logger, s.Timeout, dClient, cl, apply.object, namespace)
if err != nil && !apply.shouldFail {
if err != nil && !apply.shouldFail && !expectedWarnings(err.Error(), apply.warnings) {
errs = append(errs, err)
}
// if there was no error but we expected one
Expand All @@ -263,6 +264,19 @@ func (s *Step) Create(test *testing.T, namespace string) []error {
return errs
}

func expectedWarnings(actualWarning string, warnings *[]harness.ExpectedOutput) bool {
if warnings == nil {
return false
}

for _, warning := range *warnings {
if err := warning.ValidateOutput("", actualWarning); err != nil {
return false
}
}
return true
}

// GetTimeout gets the timeout defined for the test step.
func (s *Step) GetTimeout() int {
timeout := s.Timeout
Expand Down Expand Up @@ -649,7 +663,7 @@ func (s *Step) LoadYAML(file string) error {
return fmt.Errorf("step %q apply path %s: %w", s.Name, exApply, err)
}
for _, a := range aa {
applies = append(applies, apply{object: a, shouldFail: applyPath.ShouldFail})
applies = append(applies, apply{object: a, shouldFail: applyPath.ShouldFail, warnings: applyPath.Warnings})
}
}
// process configured step asserts
Expand Down