Skip to content

Commit

Permalink
chore: add unit tests (#2236)
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 13, 2024
1 parent f121618 commit d8845f1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 8 deletions.
14 changes: 14 additions & 0 deletions pkg/runner/names/step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package names

import (
"fmt"

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

func Step(step v1alpha1.TestStep, i int) string {
if step.Name != "" {
return step.Name
}
return fmt.Sprintf("step-%d", i+1)
}
37 changes: 37 additions & 0 deletions pkg/runner/names/step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package names

import (
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
)

func TestStep(t *testing.T) {
tests := []struct {
name string
step v1alpha1.TestStep
i int
want string
}{{
name: "no name",
step: v1alpha1.TestStep{
Name: "",
},
i: 10,
want: "step-11",
}, {
name: "with name",
step: v1alpha1.TestStep{
Name: "foo",
},
i: 10,
want: "foo",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Step(tt.step, tt.i)
assert.Equal(t, tt.want, got)
})
}
}
8 changes: 0 additions & 8 deletions pkg/runner/names/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"

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

Expand All @@ -26,13 +25,6 @@ func Test(test discovery.Test, full bool) (string, error) {
return helpTest(test, nil, nil, nil)
}

func Step(step v1alpha1.TestStep, i int) string {
if step.Name != "" {
return step.Name
}
return fmt.Sprintf("step-%d", i+1)
}

func helpTest(test discovery.Test, workingDir workignDirInterface, absolutePath absolutePathInterface, relativePath relativePathInterface) (string, error) {
if workingDir == nil {
workingDir = os.Getwd
Expand Down
125 changes: 125 additions & 0 deletions pkg/runner/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package runner

import (
"context"
"testing"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

func Test_buildNamespace(t *testing.T) {
tests := []struct {
name string
compilers compilers.Compilers
nsName string
nsTemplate *v1alpha1.Projection
bindings apis.Bindings
want *corev1.Namespace
wantErr bool
}{{
name: "simple",
nsName: "foo",
want: &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
},
}, {
name: "with template",
compilers: apis.DefaultCompilers,
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")),
nsName: "foo",
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"foo": "bar",
},
},
})),
want: &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
},
},
},
}, {
name: "with nil template",
compilers: apis.DefaultCompilers,
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")),
nsName: "foo",
nsTemplate: ptr.To(v1alpha1.NewProjection(nil)),
want: &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
},
}, {
name: "with template",
compilers: apis.DefaultCompilers,
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")),
nsName: "foo",
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"foo": "($bar)",
},
},
})),
want: &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
},
},
},
}, {
name: "with bad template",
compilers: apis.DefaultCompilers,
bindings: apis.NewBindings().Register("$bar", apis.NewBinding("bar")),
nsName: "foo",
nsTemplate: ptr.To(v1alpha1.NewProjection(map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"foo": "($flop)",
},
},
})),
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildNamespace(context.Background(), tt.compilers, tt.nsName, tt.nsTemplate, tt.bindings)
assert.Equal(t, tt.want, got)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit d8845f1

Please sign in to comment.