44 "fmt"
55
66 . "github.com/onsi/ginkgo/v2"
7+ . "github.com/onsi/gomega"
78 configv1 "github.com/openshift/api/config/v1"
89 mapiv1beta1 "github.com/openshift/api/machine/v1beta1"
910 mapiframework "github.com/openshift/cluster-api-actuator-pkg/pkg/framework"
@@ -12,6 +13,7 @@ import (
1213
1314 awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
1415 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
16+ "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
1517)
1618
1719var _ = Describe ("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Machine Migration CAPI Authoritative Tests" , Ordered , func () {
@@ -298,4 +300,149 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Ma
298300 })
299301 })*/
300302 })
303+
304+ var _ = Describe ("Update CAPI Machine" , Ordered , func () {
305+ var capiUpdateTestName = "machine-update-capi-test"
306+ var newMapiMachine * mapiv1beta1.Machine
307+ var newCapiMachine * clusterv1.Machine
308+ var previousState * MachineState
309+ Context ("with spec.authoritativeAPI: ClusterAPI (both MAPI and CAPI machine exist)" , func () {
310+ BeforeAll (func () {
311+ newMapiMachine = createMAPIMachineWithAuthority (ctx , cl , capiUpdateTestName , mapiv1beta1 .MachineAuthorityClusterAPI )
312+ newCapiMachine = capiframework .GetMachine (cl , newMapiMachine .Name , capiframework .CAPINamespace )
313+ verifyMachineRunning (cl , newCapiMachine )
314+
315+ DeferCleanup (func () {
316+ By ("Cleaning up machine resources" )
317+ cleanupMachineResources (
318+ ctx ,
319+ cl ,
320+ []* clusterv1.Machine {newCapiMachine },
321+ []* mapiv1beta1.Machine {newMapiMachine },
322+ )
323+ })
324+ })
325+
326+ It ("should add labels/annotations on CAPI machine" , func () {
327+ // Capture state before the update
328+ previousState = captureMachineStateBeforeUpdate (cl , newMapiMachine .Name )
329+ Eventually (komega .Update (newCapiMachine , func () {
330+ if newCapiMachine .Labels == nil {
331+ newCapiMachine .Labels = make (map [string ]string )
332+ }
333+ if newCapiMachine .Annotations == nil {
334+ newCapiMachine .Annotations = make (map [string ]string )
335+ }
336+ newCapiMachine .Labels ["test-capi-label" ] = "test-capi-label-value"
337+ newCapiMachine .Annotations ["test-capi-annotation" ] = "test-capi-annotation-value"
338+ }), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to add CAPI Machine labels/annotations" )
339+ })
340+
341+ It ("should synchronize added labels/annotations to both metadata and spec on MAPI" , func () {
342+ Eventually (komega .Object (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (
343+ SatisfyAll (
344+ // Check metadata
345+ WithTransform (func (m * mapiv1beta1.Machine ) string {
346+ return m .Labels ["test-capi-label" ]
347+ }, Equal ("test-capi-label-value" )),
348+ WithTransform (func (m * mapiv1beta1.Machine ) string {
349+ return m .Annotations ["test-capi-annotation" ]
350+ }, Equal ("test-capi-annotation-value" )),
351+ // Check spec template metadata
352+ WithTransform (func (m * mapiv1beta1.Machine ) string {
353+ return m .Spec .ObjectMeta .Labels ["test-capi-label" ]
354+ }, Equal ("test-capi-label-value" )),
355+ WithTransform (func (m * mapiv1beta1.Machine ) string {
356+ return m .Spec .ObjectMeta .Annotations ["test-capi-annotation" ]
357+ }, Equal ("test-capi-annotation-value" )),
358+ ),
359+ "Added labels should be copied to both metadata and spec on MAPI Machine" ,
360+ )
361+ })
362+
363+ It ("should verify MAPI generation changed and synchronized time changed after syncing labels to spec" , func () {
364+ Eventually (komega .Get (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to get updated MAPI Machine" )
365+
366+ verifyMachineStateChanged (cl , newMapiMachine , previousState , mapiv1beta1 .MachineAuthorityClusterAPI , "adding labels/annotations" )
367+ })
368+
369+ It ("should modify existing labels/annotations on CAPI machine" , func () {
370+ // Capture state before the update
371+ previousState = captureMachineStateBeforeUpdate (cl , newMapiMachine .Name )
372+ Eventually (komega .Update (newCapiMachine , func () {
373+ newCapiMachine .Labels ["test-capi-label" ] = "modified-capi-label-value"
374+ newCapiMachine .Annotations ["test-capi-annotation" ] = "modified-capi-annotation-value"
375+ }), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to modify CAPI Machine labels/annotations" )
376+ })
377+
378+ It ("should synchronize modified labels/annotations to both metadata and spec on MAPI" , func () {
379+ Eventually (komega .Object (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (
380+ SatisfyAll (
381+ // Check metadata
382+ WithTransform (func (m * mapiv1beta1.Machine ) string {
383+ return m .Labels ["test-capi-label" ]
384+ }, Equal ("modified-capi-label-value" )),
385+ WithTransform (func (m * mapiv1beta1.Machine ) string {
386+ return m .Annotations ["test-capi-annotation" ]
387+ }, Equal ("modified-capi-annotation-value" )),
388+ // Check spec template metadata
389+ WithTransform (func (m * mapiv1beta1.Machine ) string {
390+ return m .Spec .ObjectMeta .Labels ["test-capi-label" ]
391+ }, Equal ("modified-capi-label-value" )),
392+ WithTransform (func (m * mapiv1beta1.Machine ) string {
393+ return m .Spec .ObjectMeta .Annotations ["test-capi-annotation" ]
394+ }, Equal ("modified-capi-annotation-value" )),
395+ ),
396+ "Modified labels/annotations should be synchronized to both metadata and spec on MAPI Machine" ,
397+ )
398+ })
399+
400+ It ("should verify MAPI generation changed and synchronized time changed after syncing modified labels to spec" , func () {
401+ Eventually (komega .Get (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to get updated MAPI Machine" )
402+
403+ verifyMachineStateChanged (cl , newMapiMachine , previousState , mapiv1beta1 .MachineAuthorityClusterAPI , "modifying labels/annotations" )
404+ })
405+
406+ It ("should delete labels/annotations on CAPI machine" , func () {
407+ // Capture state before the update
408+ previousState = captureMachineStateBeforeUpdate (cl , newMapiMachine .Name )
409+ Eventually (komega .Update (newCapiMachine , func () {
410+ delete (newCapiMachine .Labels , "test-capi-label" )
411+ delete (newCapiMachine .Annotations , "test-capi-annotation" )
412+ }), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to delete CAPI Machine labels/annotations" )
413+ })
414+
415+ It ("should synchronize label/annotation deletion to both metadata and spec on MAPI" , func () {
416+ Eventually (komega .Object (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (
417+ SatisfyAll (
418+ // Check metadata deletion
419+ WithTransform (func (m * mapiv1beta1.Machine ) bool {
420+ _ , exists := m .Labels ["test-capi-label" ]
421+ return exists
422+ }, BeFalse ()),
423+ WithTransform (func (m * mapiv1beta1.Machine ) bool {
424+ _ , exists := m .Annotations ["test-capi-annotation" ]
425+ return exists
426+ }, BeFalse ()),
427+ // Verify deleted labels are also removed from spec
428+ WithTransform (func (m * mapiv1beta1.Machine ) bool {
429+ _ , exists := m .Spec .ObjectMeta .Labels ["test-capi-label" ]
430+ return exists
431+ }, BeFalse ()),
432+ WithTransform (func (m * mapiv1beta1.Machine ) bool {
433+ _ , exists := m .Spec .ObjectMeta .Annotations ["test-capi-annotation" ]
434+ return exists
435+ }, BeFalse ()),
436+ ),
437+ "Deleted labels/annotations should be synchronized to both metadata and spec on MAPI Machine" ,
438+ )
439+ })
440+
441+ It ("should verify MAPI generation changed and synchronized time changed after syncing deleted labels to spec" , func () {
442+ Eventually (komega .Get (newMapiMachine ), capiframework .WaitMedium , capiframework .RetryMedium ).Should (Succeed (), "Failed to get updated MAPI Machine" )
443+
444+ verifyMachineStateChanged (cl , newMapiMachine , previousState , mapiv1beta1 .MachineAuthorityClusterAPI , "deleting labels/annotations" )
445+ })
446+ })
447+ })
301448})
0 commit comments