@@ -26,7 +26,6 @@ import (
2626 "github.com/stretchr/testify/assert"
2727 "github.com/vmware/govmomi/object"
2828 v1 "k8s.io/api/core/v1"
29- "k8s.io/apimachinery/pkg/api/errors"
3029 "k8s.io/apimachinery/pkg/api/resource"
3130 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3231 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -233,55 +232,6 @@ func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsError(t *testing.T) {
233232 })
234233}
235234
236- func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundError (t * testing.T ) {
237-
238- t .Run ("TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundError" , func (t * testing.T ) {
239- testCnsNodeVMBatchAttachment := setupTestCnsNodeVMBatchAttachment ()
240- testCnsNodeVMBatchAttachment .Spec .InstanceUUID = "test-3"
241-
242- r := setTestEnvironment (& testCnsNodeVMBatchAttachment , true )
243-
244- req := reconcile.Request {
245- NamespacedName : types.NamespacedName {
246- Name : testCnsNodeVMBatchAttachmentName ,
247- Namespace : testNamespace ,
248- },
249- }
250-
251- GetVMFromVcenter = MockGetVMFromVcenter
252- commonco .ContainerOrchestratorUtility = & unittestcommon.FakeK8SOrchestrator {}
253-
254- // Override with fake client
255- newClientFunc = func (ctx context.Context ) (kubernetes.Interface , error ) {
256- fakeK8sClient := getClientSetWithPvc ()
257- return fakeK8sClient , nil
258- }
259-
260- res , err := r .Reconcile (context .TODO (), req )
261- if err != nil {
262- t .Fatal ("Unexpected reconcile error" )
263- }
264- expectedReconcileResult := reconcile.Result {}
265- var expectedReconcileError error
266-
267- assert .Equal (t , expectedReconcileResult , res )
268- assert .Equal (t , expectedReconcileError , err )
269-
270- updatedCnsNodeVMBatchAttachment := & v1alpha1.CnsNodeVMBatchAttachment {}
271- err = r .client .Get (context .TODO (), req .NamespacedName , updatedCnsNodeVMBatchAttachment )
272- if err == nil {
273- t .Fatalf ("failed to get cnsnodevmbatchattachemnt instance" )
274- }
275-
276- // Error should be not found
277- if statusErr , ok := err .(* errors.StatusError ); ok {
278- assert .Equal (t , metav1 .StatusReasonNotFound , statusErr .Status ().Reason )
279- } else {
280- t .Fatalf ("Unable to verify CnsNodeVMBatchAttachment error" )
281- }
282- })
283- }
284-
285235func TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundErrorAndInstanceIsNotDeleted (t * testing.T ) {
286236 t .Run ("TestCnsNodeVMBatchAttachmentWhenVmOnVcenterReturnsNotFoundErrorAndInstanceIsNotDeleted" ,
287237 func (t * testing.T ) {
@@ -740,6 +690,96 @@ func TestIsSharedPvc(t *testing.T) {
740690 }
741691}
742692
693+ func TestUpdatePvcStatusEntryName_AddsSuffix (t * testing.T ) {
694+ ctx := context .TODO ()
695+
696+ instance := & v1alpha1.CnsNodeVMBatchAttachment {
697+ Status : v1alpha1.CnsNodeVMBatchAttachmentStatus {
698+ VolumeStatus : []v1alpha1.VolumeStatus {
699+ {
700+ Name : "vol1" ,
701+ PersistentVolumeClaim : v1alpha1.PersistentVolumeClaimStatus {
702+ ClaimName : "pvc-1" ,
703+ },
704+ },
705+ },
706+ },
707+ }
708+
709+ pvcsToDetach := map [string ]string {
710+ "pvc-1" : "" ,
711+ }
712+
713+ updatePvcStatusEntryName (ctx , instance , pvcsToDetach )
714+
715+ got := instance .Status .VolumeStatus [0 ].Name
716+ want := "vol1" + detachSuffix
717+
718+ if got != want {
719+ t .Fatalf ("expected volume name %q, got %q" , want , got )
720+ }
721+ }
722+
723+ func TestUpdatePvcStatusEntryName_SkipsIfAlreadyHasSuffix (t * testing.T ) {
724+ ctx := context .TODO ()
725+
726+ instance := & v1alpha1.CnsNodeVMBatchAttachment {
727+ Status : v1alpha1.CnsNodeVMBatchAttachmentStatus {
728+ VolumeStatus : []v1alpha1.VolumeStatus {
729+ {
730+ Name : "vol1" + detachSuffix ,
731+ PersistentVolumeClaim : v1alpha1.PersistentVolumeClaimStatus {
732+ ClaimName : "pvc-1" ,
733+ },
734+ },
735+ },
736+ },
737+ }
738+
739+ pvcsToDetach := map [string ]string {
740+ "pvc-1" : "" ,
741+ }
742+
743+ updatePvcStatusEntryName (ctx , instance , pvcsToDetach )
744+
745+ got := instance .Status .VolumeStatus [0 ].Name
746+ want := "vol1" + detachSuffix // should remain unchanged
747+
748+ if got != want {
749+ t .Fatalf ("expected volume name to remain %q, got %q" , want , got )
750+ }
751+ }
752+
753+ func TestUpdatePvcStatusEntryName_SkipsNonTargetPVC (t * testing.T ) {
754+ ctx := context .TODO ()
755+
756+ instance := & v1alpha1.CnsNodeVMBatchAttachment {
757+ Status : v1alpha1.CnsNodeVMBatchAttachmentStatus {
758+ VolumeStatus : []v1alpha1.VolumeStatus {
759+ {
760+ Name : "vol1" ,
761+ PersistentVolumeClaim : v1alpha1.PersistentVolumeClaimStatus {
762+ ClaimName : "pvc-1" ,
763+ },
764+ },
765+ },
766+ },
767+ }
768+
769+ pvcsToDetach := map [string ]string {
770+ "some-other-pvc" : "" ,
771+ }
772+
773+ updatePvcStatusEntryName (ctx , instance , pvcsToDetach )
774+
775+ got := instance .Status .VolumeStatus [0 ].Name
776+ want := "vol1" // unchanged
777+
778+ if got != want {
779+ t .Fatalf ("expected volume name %q (unchanged), got %q" , want , got )
780+ }
781+ }
782+
743783func MockGetVMFromVcenter (ctx context.Context , nodeUUID string ,
744784 configInfo config.ConfigurationInfo ) (* cnsvsphere.VirtualMachine , error ) {
745785 var vm * cnsvsphere.VirtualMachine
0 commit comments