Skip to content

Commit

Permalink
chore: add functions unit tests (#1107)
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 Mar 13, 2024
1 parent a49c9ff commit 259168e
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/runner/functions/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"context"
"errors"

"github.com/kyverno/chainsaw/pkg/client"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -135,6 +136,9 @@ func jpKubernetesServerVersion(arguments []any) (any, error) {
if err := getArg(arguments, 0, &config); err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("rest config is nil")
}
client, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
Expand Down
174 changes: 174 additions & 0 deletions pkg/runner/functions/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"testing"

"github.com/kyverno/chainsaw/pkg/client"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
restutils "github.com/kyverno/chainsaw/pkg/utils/rest"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

Expand All @@ -20,6 +22,21 @@ func Test_jpKubernetesResourceExists(t *testing.T) {
want any
wantErr bool
}{{
name: "nil",
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, nil},
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, "", nil},
wantErr: true,
}, {
name: "bad group",
args: []any{client, "foo/v1/bar", "Baz"},
wantErr: true,
}, {
name: "pods",
args: []any{
client,
Expand Down Expand Up @@ -71,6 +88,25 @@ func Test_jpKubernetesExists(t *testing.T) {
want any
wantErr bool
}{{
name: "nil",
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, nil},
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, "", nil},
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, "", "", nil},
wantErr: true,
}, {
name: "not enough args",
args: []any{&tclient.FakeClient{}, "", "", "", nil},
wantErr: true,
}, {
name: "exist",
args: []any{
client,
Expand Down Expand Up @@ -138,3 +174,141 @@ func Test_jpKubernetesExists(t *testing.T) {
})
}
}

func Test_jpKubernetesGet(t *testing.T) {
config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
assert.NoError(t, err)
client, err := client.New(config)
assert.NoError(t, err)
tests := []struct {
name string
arguments []any
want any
wantErr bool
}{{
name: "nil",
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, nil},
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, "", nil},
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, "", "", nil},
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, "", "", "", nil},
wantErr: true,
}, {
name: "kube-apiserver-kind-control-plane",
arguments: []any{client, "v1", "Pod", "kube-system", "kube-apiserver-kind-control-plane"},
wantErr: false,
}, {
name: "foo",
arguments: []any{client, "v1", "Foo", "kube-system", "kube-apiserver-kind-control-plane"},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := jpKubernetesGet(tt.arguments)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}

func Test_jpKubernetesList(t *testing.T) {
config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
assert.NoError(t, err)
client, err := client.New(config)
assert.NoError(t, err)
tests := []struct {
name string
arguments []any
want any
wantErr bool
}{{
name: "nil",
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, nil},
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, "", nil},
wantErr: true,
}, {
name: "not enough args",
arguments: []any{&tclient.FakeClient{}, "", "", nil},
wantErr: true,
}, {
name: "nodes",
arguments: []any{client, "v1", "Node"},
wantErr: false,
}, {
name: "pods",
arguments: []any{client, "v1", "Pod", "kube-system"},
wantErr: false,
}, {
name: "foos",
arguments: []any{client, "v1", "Foo", "kube-system"},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := jpKubernetesList(tt.arguments)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}

func Test_jpKubernetesServerVersion(t *testing.T) {
var nilConfig *rest.Config
config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
assert.NoError(t, err)
tests := []struct {
name string
arguments []any
wantErr bool
}{{
name: "nil",
wantErr: true,
}, {
name: "nil config",
arguments: []any{nilConfig},
wantErr: true,
}, {
name: "config",
arguments: []any{config},
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := jpKubernetesServerVersion(tt.arguments)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/runner/functions/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package functions

import (
"errors"
"fmt"
)

Expand All @@ -17,7 +18,7 @@ func getArg[T any](arguments []any, index int, out *T) error {
return fmt.Errorf("index out of range (%d / %d)", index, len(arguments))
}
if value, ok := arguments[index].(T); !ok {
return fmt.Errorf("invalid type")
return errors.New("invalid type")
} else {
*out = value
return nil
Expand Down

0 comments on commit 259168e

Please sign in to comment.