Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Extend test GenerateName support to Unstructureds #420

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions testing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions testing/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
Loading