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

[gk-test] Run case #1446

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/gktest/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ var (
// ErrCreatingClient indicates an error instantiating the Client which compiles
// Constraints and runs validation.
ErrCreatingClient = errors.New("creating client")
// ErrInvalidCase indicates a Case cannot be run due to not being configured properly.
ErrInvalidCase = errors.New("invalid Case")
// ErrUnexpectedAllow indicates a Case failed because it was expected to get
// violations, but did not get any.
ErrUnexpectedAllow = errors.New("got no violations")
// ErrUnexpectedDeny indicates a Case failed because it got violations, but
// was not expected to get any.
ErrUnexpectedDeny = errors.New("got violations")
)
24 changes: 10 additions & 14 deletions pkg/gktest/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ type Filter struct{}
// - Test: "require-foo-label-and-bar-annotation", Case: "missing-label-and-annotation"
//
// 2) NewFilter("missing-label")
// Matches tests which either have a name containing "missing-label" or which
// are in a constraint test containing "missing-label". Matches the following:
// Matches cases which either have a name containing "missing-label" or which
// are in a test named "missing-label". Matches the following:
// - Test: "forbid-missing-label", Case: "with-foo-label"
// - Test: "required-labels", Case: "missing-label"
//
// 3) NewFilter("^require-foo-label$//")
// Matches tests in constraints which exactly match "require-foo-label". Matches the
// following:
// Matches tests which exactly match "require-foo-label". Matches the following:
// - Test: "require-foo-label", Case: "with-foo-label"
// - Test: "require-foo-label", Case: "no-labels"
//
Expand All @@ -39,21 +38,18 @@ func NewFilter(run string) (Filter, error) {
return Filter{}, nil
}

// MatchesTest filters the set of constraint tests to run by constraint name
// and the tests contained in the constraint. Returns true if tests in the constraint
// should be run.
// MatchesTest filters the set of tests to run by test name
// and the cases contained in the test. Returns true if the Test should be run.
//
// If a constraint regex was specified, returns true if the constraint regex
// matches `constraint`.
// If a constraint regex was not specified but a test regex was, returns true if
// at least one test in `tests` matches the test regex.
// If a test regex was not specified but a case regex was, returns true if
// at least one case in `c` matches the case regex.
func (f Filter) MatchesTest(c Test) bool {
return true
}

// MatchesCase filters the set of tests to run by name.
// MatchesCase filters Cases to run by name.
//
// Returns true if the test regex matches test.
func (f Filter) MatchesCase(t Case) bool {
// Returns true if the case regex matches c.
func (f Filter) MatchesCase(c Case) bool {
return true
}
16 changes: 16 additions & 0 deletions pkg/gktest/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gktest

// Printer knows how to print the results of running a Suite.
type Printer interface {
// Print formats and writes SuiteResult to w. If verbose it true, prints more
// extensive output (behavior is specific to the printer). Returns an error
// if there is a problem writing to w.
Print(w StringWriter, r []SuiteResult, verbose bool) error
}

// StringWriter knows how to write a string to a Writer.
//
// Note: StringBuffer meets this interface.
type StringWriter interface {
WriteString(s string) (int, error)
}
116 changes: 116 additions & 0 deletions pkg/gktest/printer_go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package gktest

import (
"errors"
"fmt"
)

type PrinterGo struct{}

var _ Printer = PrinterGo{}

// ErrWritingString means there was a problem writing output to the writer
// passed to Print.
var ErrWritingString = errors.New("writing output")

func (p PrinterGo) Print(w StringWriter, r []SuiteResult, verbose bool) error {
fail := false
for i := range r {
err := p.PrintSuite(w, &r[i], verbose)
if err != nil {
return err
}

if r[i].IsFailure() {
fail = true
}
}

if fail {
_, err := w.WriteString("FAIL\n")
if err != nil {
return err
}
} else {
_, err := w.WriteString("PASS\n")
if err != nil {
return err
}
}

return nil
}

func (p PrinterGo) PrintSuite(w StringWriter, r *SuiteResult, verbose bool) error {
for i := range r.TestResults {
err := p.PrintTest(w, &r.TestResults[i], verbose)
if err != nil {
return err
}
}

if r.IsFailure() {
_, err := w.WriteString(fmt.Sprintf("FAIL\t%s\t%v\n", r.Path, r.Runtime))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
} else {
_, err := w.WriteString(fmt.Sprintf("ok\t%s\t%v\n", r.Path, r.Runtime))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}
return nil
}

func (p PrinterGo) PrintTest(w StringWriter, r *TestResult, verbose bool) error {
if verbose {
_, err := w.WriteString(fmt.Sprintf("=== RUN %s\n", r.Name))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}

for i := range r.CaseResults {
err := p.PrintCase(w, &r.CaseResults[i], verbose)
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}

if r.IsFailure() {
_, err := w.WriteString(fmt.Sprintf("--- FAIL: %s\t(%v)\n", r.Name, r.Runtime))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
} else if verbose {
_, err := w.WriteString(fmt.Sprintf("--- PASS: %s\t(%v)\n", r.Name, r.Runtime))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}
return nil
}

func (p PrinterGo) PrintCase(w StringWriter, r *CaseResult, verbose bool) error {
if verbose {
_, err := w.WriteString(fmt.Sprintf(" === RUN %s\n", r.Name))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}

if r.Error != nil {
_, err := w.WriteString(fmt.Sprintf(" --- FAIL: %s\t(%v)\n %v\n", r.Name, r.Runtime, r.Error))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
} else if verbose {
_, err := w.WriteString(fmt.Sprintf(" --- PASS: %s\t(%v)\n", r.Name, r.Runtime))
if err != nil {
return fmt.Errorf("%w: %v", ErrWritingString, err)
}
}

return nil
}