@@ -172,17 +172,6 @@ var _ = Describe("Reconcile", func() {
172172 Expect (fn ).To (PanicWith ("configSpec is nil" ))
173173 })
174174 })
175- When ("restClient is nil" , func () {
176- JustBeforeEach (func () {
177- ctx = context .Background ()
178- })
179- It ("should panic" , func () {
180- fn := func () {
181- _ = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
182- }
183- Expect (fn ).To (PanicWith ("restClient is nil" ))
184- })
185- })
186175 })
187176
188177 When ("no panic is expected" , func () {
@@ -590,19 +579,10 @@ var _ = Describe("Reconcile", func() {
590579 err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
591580 Expect (err ).ToNot (HaveOccurred ())
592581
593- // Verify that the policy tag was detached
594- attachedTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
595- Expect (err ).ToNot (HaveOccurred ())
596- tagIDs := make ([]string , len (attachedTags ))
597- for i , tag := range attachedTags {
598- tagIDs [i ] = tag .ID
599- }
600- // Should only have the non-policy tag
601- Expect (tagIDs ).To (ContainElement (vcSimCtx .TagID ))
602- Expect (tagIDs ).ToNot (ContainElement (policyTag3ID ))
582+ // Verify configSpec has a remove TagSpec for the policy tag
583+ Expect (configSpec .TagSpecs ).To (ConsistOf (
584+ tagSpec (vimtypes .ArrayUpdateOperationRemove , policyTag3ID )))
603585
604- // BMV: I think the test setup for this is a little funky: policyTag3ID
605- // does not look to have a PolicyEval for it, so that's why it isn't here.
606586 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
607587 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
608588 Expect (ok ).To (BeTrue ())
@@ -659,27 +639,11 @@ var _ = Describe("Reconcile", func() {
659639 })
660640
661641 It ("should not modify the VM's tag associations" , func () {
662- // Record initial state
663- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
664- Expect (err ).ToNot (HaveOccurred ())
665- initialTagCount := len (initialTags )
666-
667- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
668- Expect (err ).ToNot (HaveOccurred ())
669-
670- // Verify no changes were made
671- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
642+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
672643 Expect (err ).ToNot (HaveOccurred ())
673- Expect (len (finalTags )).To (Equal (initialTagCount ))
674-
675- // Verify specific tags still exist
676- finalTagIDs := make ([]string , len (finalTags ))
677- for i , tag := range finalTags {
678- finalTagIDs [i ] = tag .ID
679- }
680- Expect (finalTagIDs ).To (ContainElement (vcSimCtx .TagID ))
681- Expect (finalTagIDs ).To (ContainElement (policyTag3ID ))
682644
645+ // No tag changes needed
646+ Expect (configSpec .TagSpecs ).To (BeEmpty ())
683647 Expect (configSpec .ExtraConfig ).To (BeEmpty ())
684648 })
685649 })
@@ -727,34 +691,14 @@ var _ = Describe("Reconcile", func() {
727691 })
728692
729693 It ("should associate the policy's tags with the vm" , func () {
730- // Record initial tags
731- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
694+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
732695 Expect (err ).ToNot (HaveOccurred ())
733- initialTagIDs := make ([]string , len (initialTags ))
734- for i , tag := range initialTags {
735- initialTagIDs [i ] = tag .ID
736- }
737696
738- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
739- Expect (err ).ToNot (HaveOccurred ())
740-
741- // Verify policy tags were attached
742- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
743- Expect (err ).ToNot (HaveOccurred ())
744- finalTagIDs := make ([]string , len (finalTags ))
745- for i , tag := range finalTags {
746- finalTagIDs [i ] = tag .ID
747- }
697+ // Verify configSpec has add TagSpecs for policy tags
698+ Expect (configSpec .TagSpecs ).To (ConsistOf (
699+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag1ID ),
700+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag2ID )))
748701
749- // Should have all original tags plus new policy tags
750- for _ , tagID := range initialTagIDs {
751- Expect (finalTagIDs ).To (ContainElement (tagID ))
752- }
753- // Should have the new policy tags
754- Expect (finalTagIDs ).To (ContainElement (policyTag1ID ))
755- Expect (finalTagIDs ).To (ContainElement (policyTag2ID ))
756-
757- // Should have new policy tags added
758702 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
759703 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
760704 Expect (ok ).To (BeTrue ())
@@ -807,34 +751,14 @@ var _ = Describe("Reconcile", func() {
807751 })
808752
809753 It ("should associate the policies' tags with the vm" , func () {
810- // VM currently has policyTag3ID and vcSimCtx.TagID
811- // But policies require policyTag1ID and policyTag2ID (from computePolicy1)
812- // So it should add the required tags
813-
814- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
815- Expect (err ).ToNot (HaveOccurred ())
816- initialCount := len (initialTags )
817-
818- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
819- Expect (err ).ToNot (HaveOccurred ())
820-
821- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
754+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
822755 Expect (err ).ToNot (HaveOccurred ())
823- finalTagIDs := make ([]string , len (finalTags ))
824- for i , tag := range finalTags {
825- finalTagIDs [i ] = tag .ID
826- }
827756
828- // Should have more tags than initially (new policy tags added)
829- Expect (len (finalTags )).To (BeNumerically (">=" , initialCount ))
830- // Should contain non-policy tag
831- Expect (finalTagIDs ).To (ContainElement (vcSimCtx .TagID ))
832- // Should contain all required policy tags
833- Expect (finalTagIDs ).To (ContainElement (policyTag1ID ))
834- Expect (finalTagIDs ).To (ContainElement (policyTag2ID ))
835- Expect (finalTagIDs ).To (ContainElement (policyTag3ID ))
757+ // Verify configSpec has add TagSpecs for the missing policy tags
758+ Expect (configSpec .TagSpecs ).To (ConsistOf (
759+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag1ID ),
760+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag2ID )))
836761
837- // Should have new policy tags added
838762 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
839763 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
840764 Expect (ok ).To (BeTrue ())
@@ -888,30 +812,13 @@ var _ = Describe("Reconcile", func() {
888812 })
889813
890814 It ("should associate the missing tags with the vm" , func () {
891- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
892- Expect (err ).ToNot (HaveOccurred ())
893- initialCount := len (initialTags )
894-
895- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
815+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
896816 Expect (err ).ToNot (HaveOccurred ())
897817
898- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
899- Expect (err ).ToNot (HaveOccurred ())
900- finalTagIDs := make ([]string , len (finalTags ))
901- for i , tag := range finalTags {
902- finalTagIDs [i ] = tag .ID
903- }
904-
905- // Should have all original tags plus missing policy tags
906- Expect (len (finalTags )).To (BeNumerically (">=" , initialCount ))
907- // Should still have the original policy tag
908- Expect (finalTagIDs ).To (ContainElement (policyTag1ID ))
909- // Should have the missing policy tags
910- Expect (finalTagIDs ).To (ContainElement (policyTag2ID ))
911- Expect (finalTagIDs ).To (ContainElement (policyTag3ID ))
818+ // Only missing tags should be added via TagSpecs
819+ Expect (configSpec .TagSpecs ).To (ConsistOf (
820+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag2ID )))
912821
913- // Should have existing policyTag1ID, and the two newly added
914- // policyTag2ID and policyTag3ID policies
915822 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
916823 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
917824 Expect (ok ).To (BeTrue ())
@@ -1035,36 +942,25 @@ var _ = Describe("Reconcile", func() {
1035942 })
1036943
1037944 It ("should associate the policy's tags with the vm" , func () {
1038- // Ensure VM starts with no tags
1039- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
945+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
1040946 Expect (err ).ToNot (HaveOccurred ())
1041- Expect (initialTags ).To (BeEmpty ())
1042947
1043- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
1044- Expect (err ).ToNot (HaveOccurred ())
1045-
1046- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
1047- Expect (err ).ToNot (HaveOccurred ())
1048- finalTagIDs := make ([]string , len (finalTags ))
1049- for i , tag := range finalTags {
1050- finalTagIDs [i ] = tag .ID
1051- }
948+ // Verify configSpec has add TagSpecs for policy tags
949+ Expect (configSpec .TagSpecs ).To (ConsistOf (
950+ tagSpec (vimtypes .ArrayUpdateOperationAdd , policyTag1ID )))
1052951
1053952 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
1054953 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
1055954 Expect (ok ).To (BeTrue ())
1056955 Expect (strings .Split (ecTags , "," )).To (ConsistOf (policyTag1ID ))
1057956
1058- // Should now have policy tags
1059- Expect (len (finalTags )).To (BeNumerically (">" , 0 ))
1060- Expect (finalTagIDs ).To (ContainElement (policyTag1ID ))
1061-
1062957 // Ensure the VM status was not updated yet.
1063958 Expect (vm .Status .Policies ).To (HaveLen (0 ))
1064959
1065960 // Reconcile again so the VM status is updated.
961+ configSpec = & vimtypes.VirtualMachineConfigSpec {}
1066962 err = vmconfpolicy .Reconcile (
1067- pkgctx .WithVMTags (ctx , finalTagIDs ),
963+ pkgctx .WithVMTags (ctx , [] string { policyTag1ID } ),
1068964 k8sClient ,
1069965 vimClient ,
1070966 vm ,
@@ -1142,26 +1038,14 @@ var _ = Describe("Reconcile", func() {
11421038 })
11431039
11441040 It ("should associate the policies' tags with the vm" , func () {
1145- // Ensure VM starts with no tags
1146- initialTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
1147- Expect (err ).ToNot (HaveOccurred ())
1148- Expect (initialTags ).To (BeEmpty ())
1149-
1150- err = vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
1151- Expect (err ).ToNot (HaveOccurred ())
1152-
1153- finalTags , err := tagMgr .GetAttachedTags (ctx , moVM .Self )
1041+ err := vmconfpolicy .Reconcile (ctx , k8sClient , vimClient , vm , moVM , configSpec )
11541042 Expect (err ).ToNot (HaveOccurred ())
1155- finalTagIDs := make ([]string , len (finalTags ))
1156- for i , tag := range finalTags {
1157- finalTagIDs [i ] = tag .ID
1158- }
11591043
1160- // Should have tags from multiple policies
1161- Expect (len ( finalTags )) .To (BeNumerically ( ">" , 0 ))
1162- Expect ( finalTagIDs ). To ( ContainElement ( policyTag1ID ))
1163- Expect ( finalTagIDs ). To ( ContainElement ( policyTag2ID ))
1164- Expect ( finalTagIDs ). To ( ContainElement ( policyTag3ID ))
1044+ // Verify configSpec has add TagSpecs for all policy tags
1045+ Expect (configSpec . TagSpecs ) .To (ConsistOf (
1046+ tagSpec ( vimtypes . ArrayUpdateOperationAdd , policyTag1ID ),
1047+ tagSpec ( vimtypes . ArrayUpdateOperationAdd , policyTag2ID ),
1048+ tagSpec ( vimtypes . ArrayUpdateOperationAdd , policyTag3ID ) ))
11651049
11661050 ecTags , ok := object .OptionValueList (configSpec .ExtraConfig ).
11671051 GetString (vmconfpolicy .ExtraConfigPolicyTagsKey )
@@ -1173,8 +1057,9 @@ var _ = Describe("Reconcile", func() {
11731057 Expect (vm .Status .Policies ).To (HaveLen (0 ))
11741058
11751059 // Reconcile again so the VM status is updated.
1060+ configSpec = & vimtypes.VirtualMachineConfigSpec {}
11761061 err = vmconfpolicy .Reconcile (
1177- pkgctx .WithVMTags (ctx , finalTagIDs ),
1062+ pkgctx .WithVMTags (ctx , [] string { policyTag1ID , policyTag2ID , policyTag3ID } ),
11781063 k8sClient ,
11791064 vimClient ,
11801065 vm ,
0 commit comments