Skip to content

Commit

Permalink
feat: add tests runner
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Oct 12, 2023
1 parent 7c6c695 commit 54befcb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/commands/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/config"
"github.com/kyverno/chainsaw/pkg/runner"
flagutils "github.com/kyverno/chainsaw/pkg/utils/flag"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -92,6 +93,7 @@ func Command() *cobra.Command {
// run tests
fmt.Fprintln(out, "Running tests...")
fmt.Fprintln(out, "- TODO")
runner.Run()
// done
fmt.Fprintln(out, "Done.")
return nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/runner/corpus_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package runner

// corpusEntry is from the public go testing which references an internal structure.
// corpusEntry is an alias to the same type as internal/fuzz.CorpusEntry.
// We use a type alias because we don't want to export this type, and we can't
// import internal/fuzz from testing.
type corpusEntry = struct {
Parent string
Path string
Data []byte
Values []interface{}
Generation int
IsSeed bool
}
36 changes: 36 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package runner

import (
"flag"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
)

func Run(tests ...v1alpha1.Test) int {
if len(tests) == 0 {
return 0
}
flag.Parse()
testing.Init()
// TODO: flags
var testDeps testDeps
m := testing.MainStart(
&testDeps,
[]testing.InternalTest{{
Name: "chainsaw",
F: func(t *testing.T) {
t.Helper()
run(t, tests...)
},
}},
nil,
nil,
nil,
)
return m.Run()
}

func run(t *testing.T, tests ...v1alpha1.Test) {
t.Helper()
}
71 changes: 71 additions & 0 deletions pkg/runner/test_deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package runner

import (
"io"
"reflect"
"regexp"
"runtime/pprof"
"time"
)

// testDeps implements the testDeps interface for MainStart.
type testDeps struct {
matchPat string
matchRe *regexp.Regexp
}

func (d *testDeps) MatchString(pat, str string) (bool, error) {
if d.matchRe == nil || d.matchPat != pat {
d.matchPat = pat
matchRe, err := regexp.Compile(d.matchPat)
if err != nil {
return false, err
}
d.matchRe = matchRe
}
return d.matchRe.MatchString(str), nil
}

func (*testDeps) SetPanicOnExit0(bool) {}

func (*testDeps) StartCPUProfile(w io.Writer) error {
return pprof.StartCPUProfile(w)
}

func (*testDeps) StopCPUProfile() {
pprof.StopCPUProfile()
}

func (*testDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
return pprof.Lookup(name).WriteTo(w, debug)
}

func (*testDeps) ImportPath() string {
return ""
}

func (*testDeps) StartTestLog(w io.Writer) {}

func (*testDeps) StopTestLog() error {
return nil
}

func (*testDeps) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error {
return nil
}

func (*testDeps) RunFuzzWorker(func(corpusEntry) error) error {
return nil
}

func (*testDeps) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) {
return nil, nil
}

func (*testDeps) CheckCorpus([]interface{}, []reflect.Type) error {
return nil
}

func (*testDeps) ResetCoverage() {}

func (*testDeps) SnapshotCoverage() {}

0 comments on commit 54befcb

Please sign in to comment.