Skip to content

Commit

Permalink
feat: introduce model package (#1664)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Jul 18, 2024
1 parent 83562ff commit c2ff8bb
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 122 deletions.
5 changes: 3 additions & 2 deletions pkg/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -22,7 +23,7 @@ func TestDiscoverTests(t *testing.T) {
paths: []string{"../../testdata/discovery/test"},
want: []Test{{
BasePath: "../../testdata/discovery/test",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestDiscoverTests(t *testing.T) {
paths: []string{"../../testdata/discovery/manifests"},
want: []Test{{
BasePath: "../../testdata/discovery/manifests",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down
13 changes: 7 additions & 6 deletions pkg/discovery/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestLoadTest(t *testing.T) {
path: filepath.Join(basePath, "test"),
want: []Test{{
BasePath: "../../testdata/discovery/test",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestLoadTest(t *testing.T) {
path: filepath.Join(basePath, "test"),
want: []Test{{
BasePath: "../../testdata/discovery/test",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestLoadTest(t *testing.T) {
path: filepath.Join(basePath, "test-yml"),
want: []Test{{
BasePath: "../../testdata/discovery/test-yml",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -180,7 +181,7 @@ func TestLoadTest(t *testing.T) {
path: filepath.Join(basePath, "manifests"),
want: []Test{{
BasePath: "../../testdata/discovery/manifests",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -235,7 +236,7 @@ func TestLoadTest(t *testing.T) {
path: filepath.Join(basePath, "multiple-tests"),
want: []Test{{
BasePath: "../../testdata/discovery/multiple-tests",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down Expand Up @@ -277,7 +278,7 @@ func TestLoadTest(t *testing.T) {
},
}, {
BasePath: "../../testdata/discovery/multiple-tests",
Test: &v1alpha1.Test{
Test: &model.Test{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "Test",
Expand Down
4 changes: 2 additions & 2 deletions pkg/discovery/test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package discovery

import (
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/model"
)

type Test struct {
Test *v1alpha1.Test
Test *model.Test
BasePath string
Err error
}
11 changes: 11 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import (
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
)

type (
Configuration = v1alpha2.ConfigurationSpec
Test = v1alpha1.Test
)
4 changes: 2 additions & 2 deletions pkg/runner/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package flags
import (
"strconv"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/model"
)

func GetFlags(config v1alpha2.ConfigurationSpec) map[string]string {
func GetFlags(config model.Configuration) map[string]string {
flags := map[string]string{
"test.v": "true",
"test.paniconexit0": "true",
Expand Down
13 changes: 7 additions & 6 deletions pkg/runner/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"
)

func TestGetFlags(t *testing.T) {
tests := []struct {
name string
config v1alpha2.ConfigurationSpec
config model.Configuration
want map[string]string
}{{
name: "default",
config: v1alpha2.ConfigurationSpec{},
config: model.Configuration{},
want: map[string]string{
"test.v": "true",
"test.paniconexit0": "true",
Expand All @@ -25,7 +26,7 @@ func TestGetFlags(t *testing.T) {
},
}, {
name: "include",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
IncludeTestRegex: "^.*$",
},
Expand All @@ -39,7 +40,7 @@ func TestGetFlags(t *testing.T) {
},
}, {
name: "exclude",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
ExcludeTestRegex: "^.*$",
},
Expand All @@ -53,7 +54,7 @@ func TestGetFlags(t *testing.T) {
},
}, {
name: "parallel",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Execution: v1alpha2.ExecutionOptions{
Parallel: ptr.To(10),
},
Expand All @@ -68,7 +69,7 @@ func TestGetFlags(t *testing.T) {
},
}, {
name: "repeat count",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Execution: v1alpha2.ExecutionOptions{
RepeatCount: ptr.To(10),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"flag"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/kyverno/chainsaw/pkg/runner/flags"
)

func SetupFlags(config v1alpha2.ConfigurationSpec) error {
func SetupFlags(config model.Configuration) error {
testing.Init()
for k, v := range flags.GetFlags(config) {
if err := flag.Set(k, v); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/names/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/discovery"
"github.com/kyverno/chainsaw/pkg/model"
)

type (
Expand All @@ -16,7 +16,7 @@ type (
relativePathInterface = func(string, string) (string, error)
)

func Test(config v1alpha2.ConfigurationSpec, test discovery.Test) (string, error) {
func Test(config model.Configuration, test discovery.Test) (string, error) {
if test.Test == nil {
return "", errors.New("test must not be nil")
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/runner/names/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path/filepath"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/discovery"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -18,13 +18,13 @@ func TestTest(t *testing.T) {
assert.NoError(t, err)
tests := []struct {
name string
config v1alpha2.ConfigurationSpec
config model.Configuration
test discovery.Test
want string
wantErr bool
}{{
name: "nil test",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
FullName: false,
},
Expand All @@ -36,14 +36,14 @@ func TestTest(t *testing.T) {
wantErr: true,
}, {
name: "no full name",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
FullName: false,
},
},
test: discovery.Test{
BasePath: cwd,
Test: &v1alpha1.Test{
Test: &model.Test{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Expand All @@ -53,14 +53,14 @@ func TestTest(t *testing.T) {
want: "foo",
}, {
name: "full name",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
FullName: true,
},
},
test: discovery.Test{
BasePath: cwd,
Test: &v1alpha1.Test{
Test: &model.Test{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Expand All @@ -70,14 +70,14 @@ func TestTest(t *testing.T) {
want: ".[foo]",
}, {
name: "full name",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
FullName: true,
},
},
test: discovery.Test{
BasePath: filepath.Join(cwd, "..", "dir", "dir"),
Test: &v1alpha1.Test{
Test: &model.Test{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Expand All @@ -87,14 +87,14 @@ func TestTest(t *testing.T) {
want: "../dir/dir[foo]",
}, {
name: "full name",
config: v1alpha2.ConfigurationSpec{
config: model.Configuration{
Discovery: v1alpha2.DiscoveryOptions{
FullName: true,
},
},
test: discovery.Test{
BasePath: filepath.Join(cwd, "dir", "dir"),
Test: &v1alpha1.Test{
Test: &model.Test{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestHelpTest(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
test := discovery.Test{
BasePath: "/some/path",
Test: &v1alpha1.Test{
Test: &model.Test{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/discovery"
"github.com/kyverno/chainsaw/pkg/loaders/resource"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/kyverno/chainsaw/pkg/report"
apibindings "github.com/kyverno/chainsaw/pkg/runner/bindings"
"github.com/kyverno/chainsaw/pkg/runner/cleanup"
Expand Down Expand Up @@ -50,7 +50,7 @@ type StepProcessor interface {
}

func NewStepProcessor(
config v1alpha2.ConfigurationSpec,
config model.Configuration,
clusters clusters.Registry,
namespacer namespacer.Namespacer,
clock clock.PassiveClock,
Expand All @@ -71,7 +71,7 @@ func NewStepProcessor(
}

type stepProcessor struct {
config v1alpha2.ConfigurationSpec
config model.Configuration
clusters clusters.Registry
namespacer namespacer.Namespacer
clock clock.PassiveClock
Expand Down
Loading

0 comments on commit c2ff8bb

Please sign in to comment.