diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index c61cd9b57..9b0f1b67e 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -36,6 +36,8 @@ jobs: with: go-version-file: go.mod cache-dependency-path: go.sum + - name: Create test cluster + run: make kind-cluster - name: Run tests run: make tests - name: Upload Report to Codecov 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