From 06ac41e045f42b0ae09e3cab64b9b3ffbedbfa28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 19 Mar 2025 13:13:16 +0100 Subject: [PATCH] chore: add some cel unit tests (#12453) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/cel/libs/globalcontext/impl_test.go | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/cel/libs/globalcontext/impl_test.go b/pkg/cel/libs/globalcontext/impl_test.go index 2533d74de7..11da1aacc4 100644 --- a/pkg/cel/libs/globalcontext/impl_test.go +++ b/pkg/cel/libs/globalcontext/impl_test.go @@ -5,6 +5,8 @@ import ( "testing" "github.com/google/cel-go/cel" + "github.com/google/cel-go/common/types" + "github.com/google/cel-go/common/types/ref" "github.com/kyverno/kyverno/pkg/cel/libs/resource" "github.com/kyverno/kyverno/pkg/globalcontext/store" "github.com/stretchr/testify/assert" @@ -92,3 +94,38 @@ func Test_impl_get_globalreference_string_string(t *testing.T) { }) } } + +func Test_impl_get_string_string(t *testing.T) { + opts := Lib() + base, err := cel.NewEnv(opts) + assert.NoError(t, err) + assert.NotNil(t, base) + tests := []struct { + name string + args []ref.Val + want ref.Val + }{{ + name: "not enough args", + args: nil, + want: types.NewErr("expected 3 arguments, got %d", 0), + }, { + name: "bad arg 1", + args: []ref.Val{types.String("foo"), types.String("foo"), types.String("foo")}, + want: types.NewErr("unsupported native conversion from string to 'globalcontext.Context'"), + }, { + name: "bad arg 2", + args: []ref.Val{base.CELTypeAdapter().NativeToValue(Context{}), types.Bool(false), types.String("foo")}, + want: types.NewErr("type conversion error from bool to 'string'"), + }, { + name: "bad arg 3", + args: []ref.Val{base.CELTypeAdapter().NativeToValue(Context{}), types.String("foo"), types.Bool(false)}, + want: types.NewErr("type conversion error from bool to 'string'"), + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &impl{} + got := c.get_string_string(tt.args...) + assert.Equal(t, tt.want, got) + }) + } +}