Skip to content

Commit

Permalink
chore: add unit tests
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 Dec 16, 2024
1 parent bc40b1f commit 4028cbd
Show file tree
Hide file tree
Showing 12 changed files with 525 additions and 653 deletions.
11 changes: 9 additions & 2 deletions pkg/commands/assert/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ package assert
import (
"bytes"
"context"
"errors"
"io"
"os"
"path"
"path/filepath"
"testing"
"time"

"github.com/kyverno/chainsaw/pkg/client"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
"github.com/kyverno/chainsaw/pkg/commands/root"
fakeNamespacer "github.com/kyverno/chainsaw/pkg/engine/namespacer/testing"
"github.com/kyverno/chainsaw/pkg/testing"
"github.com/spf13/cobra"
testify "github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type errReader struct{}

func (e *errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("error reading from stdin")
}

func Test_Execute(t *testing.T) {
basePath := path.Join("..", "..", "..", "testdata", "commands", "assert")
tests := []struct {
Expand Down Expand Up @@ -320,7 +327,7 @@ data:
cmd.Args = cobra.RangeArgs(0, 1)
cmd.SilenceUsage = true
cmd.SetOut(bytes.NewBufferString(""))
cmd.SetIn(&testing.ErrReader{})
cmd.SetIn(&errReader{})
return cmd
},
opts: options{
Expand Down
74 changes: 0 additions & 74 deletions pkg/runner/context.go

This file was deleted.

73 changes: 73 additions & 0 deletions pkg/runner/context/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package context

import (
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ContextData struct {
BasePath string
Catch []v1alpha1.CatchFinally
Cluster *string
Clusters v1alpha1.Clusters
DelayBeforeCleanup *metav1.Duration
DeletionPropagation *metav1.DeletionPropagation
DryRun *bool
SkipDelete *bool
Templating *bool
TerminationGrace *metav1.Duration
Timeouts *v1alpha1.Timeouts
}

func SetupContext(tc TestContext, data ContextData) (TestContext, error) {
if len(data.Catch) > 0 {
tc = tc.WithCatch(data.Catch...)
}
if data.DryRun != nil {
tc = tc.WithDryRun(*data.DryRun)
}
if data.DelayBeforeCleanup != nil {
tc = tc.WithDelayBeforeCleanup(&data.DelayBeforeCleanup.Duration)
}
if data.DeletionPropagation != nil {
tc = tc.WithDeletionPropagation(*data.DeletionPropagation)
}
if data.SkipDelete != nil {
tc = tc.WithSkipDelete(*data.SkipDelete)
}
if data.Templating != nil {
tc = tc.WithTemplating(*data.Templating)
}
if data.TerminationGrace != nil {
tc = tc.WithTerminationGrace(&data.TerminationGrace.Duration)
}
if data.Timeouts != nil {
tc = tc.WithTimeouts(*data.Timeouts)
}
tc = WithClusters(tc, data.BasePath, data.Clusters)
if data.Cluster != nil {
if _tc, err := WithCurrentCluster(tc, *data.Cluster); err != nil {
return tc, err
} else {
tc = _tc
}

Check warning on line 53 in pkg/runner/context/legacy.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/context/legacy.go#L22-L53

Added lines #L22 - L53 were not covered by tests
}
return tc, nil

Check warning on line 55 in pkg/runner/context/legacy.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/context/legacy.go#L55

Added line #L55 was not covered by tests
}

func SetupBindings(tc TestContext, bindings ...v1alpha1.Binding) (TestContext, error) {
if _tc, err := WithBindings(tc, bindings...); err != nil {
return tc, err
} else {
tc = _tc
}
return tc, nil

Check warning on line 64 in pkg/runner/context/legacy.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/context/legacy.go#L58-L64

Added lines #L58 - L64 were not covered by tests
}

func SetupContextAndBindings(tc TestContext, data ContextData, bindings ...v1alpha1.Binding) (TestContext, error) {
if tc, err := SetupContext(tc, data); err != nil {
return tc, err
} else {
return SetupBindings(tc, bindings...)
}

Check warning on line 72 in pkg/runner/context/legacy.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/context/legacy.go#L67-L72

Added lines #L67 - L72 were not covered by tests
}
Loading

0 comments on commit 4028cbd

Please sign in to comment.