From 998198b89952d61eb65a1a0da55aae7eb17f6076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Ma=C5=88=C3=A1k?= Date: Fri, 14 Mar 2025 16:24:06 +0100 Subject: [PATCH] testutils: add ownerReference to resource builders --- .../v1beta1/openstackmachine.go | 24 ++++++++++++------- .../v1beta1/openstackmachine_test.go | 9 +++++++ .../v1beta1/openstackmachinetemplate.go | 8 +++++++ .../v1beta1/openstackmachinetemplate_test.go | 9 +++++++ .../infrastructure/v1beta2/awsmachine.go | 24 ++++++++++++------- .../infrastructure/v1beta2/awsmachine_test.go | 9 +++++++ .../v1beta2/awsmachinetemplate.go | 8 +++++++ .../v1beta2/awsmachinetemplate_test.go | 8 +++++++ .../infrastructure/v1beta2/powervsmachine.go | 24 ++++++++++++------- .../v1beta2/powervsmachine_test.go | 9 +++++++ .../v1beta2/powervsmachinetemplate.go | 8 +++++++ .../v1beta2/powervsmachinetemplate_test.go | 8 +++++++ 12 files changed, 124 insertions(+), 24 deletions(-) diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine.go index 4b7b08997..7ed9594f4 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine.go @@ -34,10 +34,11 @@ func OpenStackMachine() OpenStackMachineBuilder { // OpenStackMachineBuilder is used to build out an OpenStackMachine object. type OpenStackMachineBuilder struct { // ObjectMeta fields. - annotations map[string]string - labels map[string]string - name string - namespace string + annotations map[string]string + labels map[string]string + name string + namespace string + ownerReferences []metav1.OwnerReference // Spec fields. additionalBlockDevices []capov1.AdditionalBlockDevice @@ -75,10 +76,11 @@ func (a OpenStackMachineBuilder) Build() *capov1.OpenStackMachine { Kind: "OpenStackMachine", }, ObjectMeta: metav1.ObjectMeta{ - Name: a.name, - Namespace: a.namespace, - Labels: a.labels, - Annotations: a.annotations, + Name: a.name, + Namespace: a.namespace, + Labels: a.labels, + Annotations: a.annotations, + OwnerReferences: a.ownerReferences, }, Spec: capov1.OpenStackMachineSpec{ AdditionalBlockDevices: a.additionalBlockDevices, @@ -138,6 +140,12 @@ func (a OpenStackMachineBuilder) WithNamespace(namespace string) OpenStackMachin return a } +// WithOwnerReferences sets the OwnerReferences for the machine builder. +func (a OpenStackMachineBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) OpenStackMachineBuilder { + a.ownerReferences = ownerRefs + return a +} + // Spec fields. // WithAdditionalBlockDevices sets the additionalBlockDevices for the OpenStackMachine builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine_test.go index 7e6bba9fd..9f3e34bdd 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachine_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" capov1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" ) @@ -44,6 +45,14 @@ var _ = Describe("OpenStackMachineBuilder", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerReferences := []metav1.OwnerReference{{Name: "machine"}} + openstackMachine := OpenStackMachine().WithOwnerReferences(ownerReferences).Build() + Expect(openstackMachine.OwnerReferences).To(Equal(ownerReferences)) + }) + }) + // Spec fields. Describe("WithAdditionalBlockDevices", func() { diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate.go index a1666be9b..579208ec5 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate.go @@ -37,6 +37,7 @@ type OpenStackMachineTemplateBuilder struct { labels map[string]string name string namespace string + ownerReference []metav1.OwnerReference // Spec fields. additionalBlockDevices []capov1.AdditionalBlockDevice @@ -72,6 +73,7 @@ func (a OpenStackMachineTemplateBuilder) Build() *capov1.OpenStackMachineTemplat Labels: a.labels, Name: a.name, Namespace: a.namespace, + OwnerReferences: a.ownerReference, }, Spec: capov1.OpenStackMachineTemplateSpec{ Template: capov1.OpenStackMachineTemplateResource{ @@ -144,6 +146,12 @@ func (a OpenStackMachineTemplateBuilder) WithNamespace(namespace string) OpenSta return a } +// WithOwnerReferences sets the OwnerReferences for the OpenStackMachineTemplate builder. +func (a OpenStackMachineTemplateBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) OpenStackMachineTemplateBuilder { + a.ownerReference = ownerRefs + return a +} + // Spec fields. // WithAdditionalBlockDevices sets the additionalBlockDevices for the OpenStackMachine builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate_test.go index 0507667ab..0ad4b97e2 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta1/openstackmachinetemplate_test.go @@ -19,6 +19,7 @@ package v1beta1 import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" capov1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" ) @@ -43,6 +44,14 @@ var _ = Describe("OpenStackMachineTemplateBuilder", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerReferences := []metav1.OwnerReference{{Name: "cluster"}} + openstackMachineTemplate := OpenStackMachineTemplate().WithOwnerReferences(ownerReferences).Build() + Expect(openstackMachineTemplate.OwnerReferences).To(Equal(ownerReferences)) + }) + }) + // Spec fields. Describe("WithAdditionalBlockDevices", func() { diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine.go index fbb5ace99..051f7e112 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine.go @@ -30,10 +30,11 @@ func AWSMachine() AWSMachineBuilder { // AWSMachineBuilder is used to build out an AWSMachine object. type AWSMachineBuilder struct { // ObjectMeta fields. - annotations map[string]string - labels map[string]string - name string - namespace string + annotations map[string]string + labels map[string]string + name string + namespace string + ownerReferences []metav1.OwnerReference // Spec fields. additionalSecurityGroups []capav1.AWSResourceReference @@ -83,10 +84,11 @@ func (a AWSMachineBuilder) Build() *capav1.AWSMachine { Kind: "AWSMachine", }, ObjectMeta: metav1.ObjectMeta{ - Name: a.name, - Namespace: a.namespace, - Labels: a.labels, - Annotations: a.annotations, + Name: a.name, + Namespace: a.namespace, + Labels: a.labels, + Annotations: a.annotations, + OwnerReferences: a.ownerReferences, }, Spec: capav1.AWSMachineSpec{ AdditionalSecurityGroups: a.additionalSecurityGroups, @@ -158,6 +160,12 @@ func (a AWSMachineBuilder) WithNamespace(namespace string) AWSMachineBuilder { return a } +// WithOwnerReferences sets the OwnerReferences for the machine builder. +func (a AWSMachineBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) AWSMachineBuilder { + a.ownerReferences = ownerRefs + return a +} + // Spec fields. // WithAdditionalSecurityGroups sets the additionalSecurityGroups for the AWSMachine builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine_test.go index 6d1c37cdd..0a2243482 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachine_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" capav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -68,6 +69,14 @@ var _ = Describe("AWSMachineBuilder", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerRefs := []metav1.OwnerReference{{Name: "machineName"}} + awsMachine := AWSMachine().WithOwnerReferences(ownerRefs).Build() + Expect(awsMachine.OwnerReferences).To(Equal(ownerRefs)) + }) + }) + // Spec fields Describe("WithAdditionalSecurityGroups", func() { It("should return the custom value when specified", func() { diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate.go index 2aa2d7592..cddf5150a 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate.go @@ -37,6 +37,7 @@ type AWSMachineTemplateBuilder struct { labels map[string]string name string namespace string + ownerReferences []metav1.OwnerReference // Spec fields. additionalSecurityGroups []capav1.AWSResourceReference @@ -87,6 +88,7 @@ func (a AWSMachineTemplateBuilder) Build() *capav1.AWSMachineTemplate { Labels: a.labels, Name: a.name, Namespace: a.namespace, + OwnerReferences: a.ownerReferences, }, Spec: capav1.AWSMachineTemplateSpec{ Template: capav1.AWSMachineTemplateResource{ @@ -174,6 +176,12 @@ func (a AWSMachineTemplateBuilder) WithNamespace(namespace string) AWSMachineTem return a } +// WithOwnerReferences sets the OwnerReferences for the AWSMachineTemplate builder. +func (a AWSMachineTemplateBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) AWSMachineTemplateBuilder { + a.ownerReferences = ownerRefs + return a +} + // Spec fields. // WithAdditionalSecurityGroups sets the additionalSecurityGroups for the AWSMachineTemplate builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate_test.go index 46e79a5b4..0337b5ddb 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/awsmachinetemplate_test.go @@ -92,6 +92,14 @@ var _ = Describe("AWSMachineTemplate", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerReferences := []metav1.OwnerReference{{Name: "cluster"}} + awsMachineTemplate := AWSMachineTemplate().WithOwnerReferences(ownerReferences).Build() + Expect(awsMachineTemplate.OwnerReferences).To(Equal(ownerReferences)) + }) + }) + // Spec fields. Describe("WithAdditionalSecurityGroups", func() { diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine.go index 89151d6c9..eaca16c59 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine.go @@ -32,10 +32,11 @@ func PowerVSMachine() PowerVSMachineBuilder { // PowerVSMachineBuilder is used to build out an PowerVSMachine object. type PowerVSMachineBuilder struct { // ObjectMeta fields. - annotations map[string]string - labels map[string]string - name string - namespace string + annotations map[string]string + labels map[string]string + name string + namespace string + ownerReferences []metav1.OwnerReference // Spec fields. image *capibmv1.IBMPowerVSResourceReference @@ -66,10 +67,11 @@ func (p PowerVSMachineBuilder) Build() *capibmv1.IBMPowerVSMachine { APIVersion: capibmv1.GroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ - Name: p.name, - Namespace: p.namespace, - Labels: p.labels, - Annotations: p.annotations, + Name: p.name, + Namespace: p.namespace, + Labels: p.labels, + Annotations: p.annotations, + OwnerReferences: p.ownerReferences, }, Spec: capibmv1.IBMPowerVSMachineSpec{ ServiceInstance: p.serviceInstance, @@ -121,6 +123,12 @@ func (p PowerVSMachineBuilder) WithNamespace(namespace string) PowerVSMachineBui return p } +// WithOwnerReferences sets the OwnerReferences for the machine builder. +func (p PowerVSMachineBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) PowerVSMachineBuilder { + p.ownerReferences = ownerRefs + return p +} + // Spec fields. // WithImage sets the image for the PowerVSMachine builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine_test.go index aa23c0498..0256ce020 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachine_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/utils/ptr" capibmv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2" @@ -70,6 +71,14 @@ var _ = Describe("PowerVSMachine", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerReferences := []metav1.OwnerReference{{Name: "machine"}} + PowerVSMachine := PowerVSMachine().WithOwnerReferences(ownerReferences).Build() + Expect(PowerVSMachine.OwnerReferences).To(Equal(ownerReferences)) + }) + }) + // Spec fields. Describe("WithImage", func() { diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate.go index d639f0fd1..99f2c37df 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate.go @@ -38,6 +38,7 @@ type PowerVSMachineTemplateBuilder struct { labels map[string]string name string namespace string + ownerReferences []metav1.OwnerReference // Spec fields. image *capibmv1.IBMPowerVSResourceReference @@ -70,6 +71,7 @@ func (p PowerVSMachineTemplateBuilder) Build() *capibmv1.IBMPowerVSMachineTempla Labels: p.labels, Name: p.name, Namespace: p.namespace, + OwnerReferences: p.ownerReferences, }, Spec: capibmv1.IBMPowerVSMachineTemplateSpec{ Template: capibmv1.IBMPowerVSMachineTemplateResource{ @@ -137,6 +139,12 @@ func (p PowerVSMachineTemplateBuilder) WithNamespace(namespace string) PowerVSMa return p } +// WithOwnerReferences sets the OwnerReferences for the PowerVSMachineTemplate builder. +func (p PowerVSMachineTemplateBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) PowerVSMachineTemplateBuilder { + p.ownerReferences = ownerRefs + return p +} + // Spec fields. // WithImage sets the image for the PowerVSMachineTemplate builder. diff --git a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate_test.go b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate_test.go index 5075098ef..5170389f0 100644 --- a/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate_test.go +++ b/testutils/resourcebuilder/cluster-api/infrastructure/v1beta2/powervsmachinetemplate_test.go @@ -94,6 +94,14 @@ var _ = Describe("PowerVSMachineTemplate", func() { }) }) + Describe("WithOwnerReferences", func() { + It("should return the custom value when specified", func() { + ownerReferences := []metav1.OwnerReference{{Name: "cluster"}} + powerVSMachineTemplate := PowerVSMachineTemplate().WithOwnerReferences(ownerReferences).Build() + Expect(powerVSMachineTemplate.OwnerReferences).To(Equal(ownerReferences)) + }) + }) + // Spec fields. Describe("WithServiceInstance", func() {