Skip to content

Commit

Permalink
Merge branch 'main' into load-testStep
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycharly authored Oct 12, 2023
2 parents a78a759 + 75c3388 commit f28c289
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 5 deletions.
29 changes: 28 additions & 1 deletion 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 @@ -89,9 +90,35 @@ func Command() *cobra.Command {
if flagutils.IsSet(flags, "skipTestRegex") {
configuration.Spec.SkipTestRegex = options.skipTestRegex
}
// loading tests
fmt.Fprintln(out, "Loading tests...")
fmt.Fprintln(out, "- TODO")
// TODO: load tests
test := v1alpha1.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
},
Spec: v1alpha1.TestSpec{
Steps: []v1alpha1.TestStepSpec{{
Apply: []v1alpha1.Apply{{
File: "foo.yaml",
}},
}, {
Assert: []v1alpha1.Assert{{
File: "bar.yaml",
}},
}},
},
}
// run tests
fmt.Fprintln(out, "Running tests...")
fmt.Fprintln(out, "- TODO")
if _, err := runner.Run(test); err != nil {
return err
}
// 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
}
59 changes: 59 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package runner

import (
"flag"
"fmt"
"testing"

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

func Run(tests ...v1alpha1.Test) (int, error) {
if len(tests) == 0 {
return 0, nil
}
flag.Parse()
testing.Init()
// Set the verbose test flag to true since we are not using the regular go test CLI.
if err := flag.Set("test.v", "true"); err != nil {
return 0, err
}
// 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(), nil
}

func run(t *testing.T, tests ...v1alpha1.Test) {
t.Helper()
for _, test := range tests {
func(t *testing.T, test v1alpha1.Test) {
t.Helper()
t.Run(test.Name, func(t *testing.T) {
t.Helper()
t.Parallel()
for i, step := range test.Spec.Steps {
func(t *testing.T, test v1alpha1.TestStepSpec) {
t.Helper()
t.Run(fmt.Sprintf("step-%d", i+1), func(t *testing.T) {
t.Helper()
// TODO: execute step
})
}(t, step)
}
})
}(t, test)
}
}
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() {}
3 changes: 2 additions & 1 deletion testdata/commands/test/config_all_fields.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Loading config (../../../testdata/commands/test/config/config_all_fields.yaml)...
Running tests...
Loading tests...
- TODO
Running tests...
Done.
3 changes: 2 additions & 1 deletion testdata/commands/test/default.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Running without configuration
Running tests...
Loading tests...
- TODO
Running tests...
Done.
3 changes: 2 additions & 1 deletion testdata/commands/test/valid_config.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Loading config (../../../testdata/commands/test/config/empty_config.yaml)...
Running tests...
Loading tests...
- TODO
Running tests...
Done.
3 changes: 2 additions & 1 deletion testdata/commands/test/without_config.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Running without configuration
Running tests...
Loading tests...
- TODO
Running tests...
Done.

0 comments on commit f28c289

Please sign in to comment.