@@ -17,9 +17,14 @@ limitations under the License.
1717package cloudprovider
1818
1919import (
20+ "context"
2021 "testing"
22+ "time"
2123
24+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
25+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v7"
2226 . "github.com/onsi/gomega"
27+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2328)
2429
2530func TestGenerateNodeClaimName (t * testing.T ) {
@@ -54,3 +59,68 @@ func TestGenerateNodeClaimName(t *testing.T) {
5459 })
5560 }
5661}
62+
63+ func TestVmInstanceToNodeClaim_NilProperties (t * testing.T ) {
64+ tests := []struct {
65+ name string
66+ vm * armcompute.VirtualMachine
67+ expectFallbackToNow bool
68+ expectExactTime * time.Time
69+ }{
70+ {
71+ name : "nil Properties - fallback to time.Now()" ,
72+ vm : & armcompute.VirtualMachine {
73+ Name : to .Ptr ("aks-test-vm" ),
74+ ID : to .Ptr ("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm" ),
75+ },
76+ expectFallbackToNow : true ,
77+ },
78+ {
79+ name : "nil TimeCreated - fallback to time.Now()" ,
80+ vm : & armcompute.VirtualMachine {
81+ Name : to .Ptr ("aks-test-vm" ),
82+ ID : to .Ptr ("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm" ),
83+ Properties : & armcompute.VirtualMachineProperties {},
84+ },
85+ expectFallbackToNow : true ,
86+ },
87+ {
88+ name : "valid TimeCreated - use exact time" ,
89+ vm : & armcompute.VirtualMachine {
90+ Name : to .Ptr ("aks-test-vm" ),
91+ ID : to .Ptr ("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm" ),
92+ Properties : & armcompute.VirtualMachineProperties {
93+ TimeCreated : to .Ptr (time .Date (2024 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
94+ },
95+ },
96+ expectExactTime : to .Ptr (time .Date (2024 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
97+ },
98+ }
99+
100+ for _ , tt := range tests {
101+ t .Run (tt .name , func (t * testing.T ) {
102+ g := NewWithT (t )
103+ ctx := context .Background ()
104+
105+ cp := & CloudProvider {}
106+ before := time .Now ()
107+ nodeClaim , err := cp .vmInstanceToNodeClaim (ctx , tt .vm , nil )
108+ after := time .Now ()
109+
110+ g .Expect (err ).ToNot (HaveOccurred ())
111+ g .Expect (nodeClaim ).ToNot (BeNil ())
112+ g .Expect (nodeClaim .CreationTimestamp ).ToNot (Equal (metav1.Time {}))
113+
114+ if tt .expectFallbackToNow {
115+ // When TimeCreated is unavailable, should fallback to time.Now() for GC safety
116+ g .Expect (nodeClaim .CreationTimestamp .Time ).To (BeTemporally (">=" , before ))
117+ g .Expect (nodeClaim .CreationTimestamp .Time ).To (BeTemporally ("<=" , after ))
118+ }
119+
120+ if tt .expectExactTime != nil {
121+ // When TimeCreated is available, should use the exact time from VM
122+ g .Expect (nodeClaim .CreationTimestamp .Time ).To (Equal (* tt .expectExactTime ))
123+ }
124+ })
125+ }
126+ }
0 commit comments