From 85bff79fb00e55f4acdff22591fb9635c8d1895e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 1 Mar 2024 23:26:13 +0100 Subject: [PATCH] fix: jpKubernetesExists not setting apiVersion/kind MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/runner/functions/k8s.go | 5 +- pkg/runner/functions/k8s_test.go | 85 +++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/pkg/runner/functions/k8s.go b/pkg/runner/functions/k8s.go index c70d84051..2a5a45c5d 100644 --- a/pkg/runner/functions/k8s.go +++ b/pkg/runner/functions/k8s.go @@ -57,7 +57,10 @@ func jpKubernetesExists(arguments []any) (any, error) { if err := getArg(arguments, 4, &key.Name); err != nil { return nil, err } - err := client.Get(context.TODO(), key, &unstructured.Unstructured{}) + var obj unstructured.Unstructured + obj.SetAPIVersion(apiVersion) + obj.SetKind(kind) + err := client.Get(context.TODO(), key, &obj) if err == nil { return true, nil } diff --git a/pkg/runner/functions/k8s_test.go b/pkg/runner/functions/k8s_test.go index b4cf28b08..c44e6729a 100644 --- a/pkg/runner/functions/k8s_test.go +++ b/pkg/runner/functions/k8s_test.go @@ -10,8 +10,10 @@ import ( ) func Test_jpKubernetesResourceExists(t *testing.T) { - config, _ := restutils.DefaultConfig(clientcmd.ConfigOverrides{}) - client, _ := client.New(config) + config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{}) + assert.NoError(t, err) + client, err := client.New(config) + assert.NoError(t, err) tests := []struct { name string args []any @@ -57,3 +59,82 @@ func Test_jpKubernetesResourceExists(t *testing.T) { }) } } + +func Test_jpKubernetesExists(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 + args []any + want any + wantErr bool + }{{ + name: "exist", + args: []any{ + client, + "v1", + "Pod", + "kube-system", + "kube-apiserver-kind-control-plane", + }, + want: true, + wantErr: false, + }, { + name: "namespace not exists", + args: []any{ + client, + "v1", + "Pod", + "foo", + "kube-apiserver-kind-control-plane", + }, + want: false, + wantErr: false, + }, { + name: "name not exists", + args: []any{ + client, + "v1", + "Pod", + "kube-system", + "foo", + }, + want: false, + wantErr: false, + }, { + name: "kind not exists", + args: []any{ + client, + "v1", + "Foo", + "kube-system", + "foo", + }, + want: nil, + wantErr: true, + }, { + name: "apiVersion not exists", + args: []any{ + client, + "v1alpha1", + "Pod", + "kube-system", + "kube-apiserver-kind-control-plane", + }, + want: nil, + wantErr: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := jpKubernetesExists(tt.args) + assert.Equal(t, tt.want, got) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +}