From 6399bbcc44302a1b93323ac83a3c47edccf03ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 13 Oct 2023 18:14:21 +0200 Subject: [PATCH] fix: add pet namespace if necessary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/client/client.go | 2 ++ pkg/runner/runner.go | 16 +++++++++++++--- testdata/tests/configmap.yaml | 1 - 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index f9e1a6a75..592733ee7 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1,6 +1,7 @@ package client import ( + "k8s.io/apimachinery/pkg/runtime" _ "k8s.io/client-go/plugin/pkg/client/auth" // package needed for auth providers like GCP "k8s.io/client-go/rest" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -9,6 +10,7 @@ import ( type Client interface { ctrlclient.Reader ctrlclient.Writer + IsObjectNamespaced(obj runtime.Object) (bool, error) } func New(cfg *rest.Config) (Client, error) { diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 54082f3dc..656ea3e07 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -73,12 +73,12 @@ func runTest(t *testing.T, cfg *rest.Config, test discovery.Test) { step := test.Spec.Steps[i] t.Run(fmt.Sprintf("step-%d", i+1), func(t *testing.T) { t.Helper() - executeStep(t, test.BasePath, step, c) + executeStep(t, test.BasePath, namespace.Name, step, c) }) } } -func executeStep(t *testing.T, basePath string, step v1alpha1.TestStepSpec, c client.Client) { +func executeStep(t *testing.T, basePath string, namespace string, step v1alpha1.TestStepSpec, c client.Client) { t.Helper() for _, apply := range step.Apply { resources, err := resource.Load(filepath.Join(basePath, apply.File)) @@ -86,7 +86,17 @@ func executeStep(t *testing.T, basePath string, step v1alpha1.TestStepSpec, c cl t.Fatal(err) } for i := range resources { - _, err := client.CreateOrUpdate(context.Background(), c, &resources[i]) + resource := &resources[i] + if resource.GetNamespace() == "" { + namespaced, err := c.IsObjectNamespaced(resource) + if err != nil { + t.Fatal(err) + } + if namespaced { + resource.SetNamespace(namespace) + } + } + _, err := client.CreateOrUpdate(context.Background(), c, resource) if err != nil { t.Fatal(err) } diff --git a/testdata/tests/configmap.yaml b/testdata/tests/configmap.yaml index 30f558871..381941858 100644 --- a/testdata/tests/configmap.yaml +++ b/testdata/tests/configmap.yaml @@ -2,6 +2,5 @@ apiVersion: v1 kind: ConfigMap metadata: name: chainsaw-quick-start - namespace: default data: foo: bar