Skip to content

Commit

Permalink
chore: add unit tests (#2240)
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 authored Dec 16, 2024
1 parent 9035ce6 commit bc40b1f
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 44 deletions.
4 changes: 2 additions & 2 deletions pkg/engine/clusters/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type fromConfig struct {
config *rest.Config
}

func NewClusterFromConfig(config *rest.Config) (Cluster, error) {
func NewClusterFromConfig(config *rest.Config) Cluster {
return &fromConfig{
config: config,
}, nil
}
}

func (c *fromConfig) Config() (*rest.Config, error) {
Expand Down
23 changes: 8 additions & 15 deletions pkg/engine/clusters/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (

func TestNewClusterFromConfig(t *testing.T) {
tests := []struct {
name string
config *rest.Config
want Cluster
wantErr bool
name string
config *rest.Config
want Cluster
}{{
name: "nil",
want: &fromConfig{
Expand All @@ -27,17 +26,11 @@ func TestNewClusterFromConfig(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewClusterFromConfig(tt.config)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
got, err := got.Config()
assert.NoError(t, err)
assert.Same(t, tt.config, got)
}
got := NewClusterFromConfig(tt.config)
assert.Equal(t, tt.want, got)
config, err := got.Config()
assert.NoError(t, err)
assert.Same(t, tt.config, config)
})
}
}
Expand Down
34 changes: 18 additions & 16 deletions pkg/runner/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kyverno/kyverno-json/pkg/core/compilers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/utils/clock"
)

type TestContext struct {
Expand All @@ -34,21 +35,22 @@ type TestContext struct {
timeouts v1alpha1.DefaultTimeouts
}

func MakeContext(bindings apis.Bindings, registry clusters.Registry) TestContext {
func MakeContext(clock clock.PassiveClock, bindings apis.Bindings, registry clusters.Registry) TestContext {
return TestContext{
Summary: &model.Summary{},
Report: &model.Report{
Name: "chainsaw-report",
StartTime: time.Now(),
StartTime: clock.Now(),
},
bindings: bindings,
clusters: registry,
compilers: apis.DefaultCompilers,
bindings: bindings,
clusters: registry,
compilers: apis.DefaultCompilers,
deletionPropagation: metav1.DeletePropagationBackground,
}
}

func EmptyContext() TestContext {
return MakeContext(apis.NewBindings(), clusters.NewRegistry(nil))
func EmptyContext(clock clock.PassiveClock) TestContext {
return MakeContext(clock, apis.NewBindings(), clusters.NewRegistry(nil))
}

func (tc *TestContext) Bindings() apis.Bindings {
Expand All @@ -59,10 +61,6 @@ func (tc *TestContext) Catch() []v1alpha1.CatchFinally {
return tc.catch
}

func (tc *TestContext) Compilers() compilers.Compilers {
return tc.compilers
}

func (tc *TestContext) Cluster(name string) clusters.Cluster {
return tc.clusters.Lookup(name)
}
Expand All @@ -71,6 +69,10 @@ func (tc *TestContext) Clusters() clusters.Registry {
return tc.clusters
}

func (tc *TestContext) Compilers() compilers.Compilers {
return tc.compilers
}

func (tc *TestContext) CurrentCluster() clusters.Cluster {
return tc.cluster
}
Expand Down Expand Up @@ -129,11 +131,6 @@ func (tc TestContext) WithCatch(catch ...v1alpha1.CatchFinally) TestContext {
return tc
}

func (tc TestContext) WithDefaultCompiler(name string) TestContext {
tc.compilers = tc.compilers.WithDefaultCompiler(name)
return tc
}

func (tc TestContext) WithCluster(name string, cluster clusters.Cluster) TestContext {
tc.clusters = tc.clusters.Register(name, cluster)
return tc
Expand All @@ -144,6 +141,11 @@ func (tc TestContext) WithCurrentCluster(name string) TestContext {
return tc
}

func (tc TestContext) WithDefaultCompiler(name string) TestContext {
tc.compilers = tc.compilers.WithDefaultCompiler(name)
return tc
}

func (tc TestContext) WithDelayBeforeCleanup(delayBeforeCleanup *time.Duration) TestContext {
tc.delayBeforeCleanup = delayBeforeCleanup
return tc
Expand Down
Loading

0 comments on commit bc40b1f

Please sign in to comment.