From 2c1bed209aa96cbee886a428a18993b3200e8c58 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Wed, 30 Aug 2023 18:58:00 -0400 Subject: [PATCH] Extend test GenerateName support to Unstructureds Unstructured resources do not implement ObjectMetaAccessor, we should cast the object to metav1.Object instead. Signed-off-by: Scott Andrews --- testing/client.go | 3 +-- testing/config_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/testing/client.go b/testing/client.go index 6f5e960..56dabd5 100644 --- a/testing/client.go +++ b/testing/client.go @@ -51,8 +51,7 @@ func NewFakeClientWrapper(client client.Client) *clientWrapper { c.AddReactor("create", "*", func(action Action) (bool, runtime.Object, error) { if createAction, ok := action.(CreateAction); ok && action.GetSubresource() == "" { obj := createAction.GetObject() - if accessor, ok := obj.(metav1.ObjectMetaAccessor); ok { - objmeta := accessor.GetObjectMeta() + if objmeta, ok := obj.(metav1.Object); ok { if objmeta.GetName() == "" && objmeta.GetGenerateName() != "" { c.genCount++ // mutate the existing obj diff --git a/testing/config_test.go b/testing/config_test.go index 7d25e2d..71a33e8 100644 --- a/testing/config_test.go +++ b/testing/config_test.go @@ -18,6 +18,7 @@ import ( "github.com/vmware-labs/reconciler-runtime/reconcilers" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" @@ -321,6 +322,53 @@ func TestExpectConfig(t *testing.T) { `Missing create for config "test": `, }, }, + "generate name": { + config: ExpectConfig{ + ExpectCreates: []client.Object{ + &resources.TestResource{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns, + GenerateName: "resource-", + }, + }, + }, + }, + operation: func(t *testing.T, ctx context.Context, c reconcilers.Config) { + r := &resources.TestResource{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns, + GenerateName: "resource-", + }, + } + + c.Create(ctx, r) + }, + }, + "generate name unstructured": { + config: ExpectConfig{ + ExpectCreates: []client.Object{ + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "testing.reconciler.runtime/v1", + "kind": "TestResource", + "metadata": map[string]interface{}{ + "namespace": ns, + "generateName": "resource-", + }, + }, + }, + }, + }, + operation: func(t *testing.T, ctx context.Context, c reconcilers.Config) { + u := &unstructured.Unstructured{} + u.SetAPIVersion("testing.reconciler.runtime/v1") + u.SetKind("TestResource") + u.SetNamespace(ns) + u.SetGenerateName("resource-") + + c.Create(ctx, u) + }, + }, "expected update": { config: ExpectConfig{