@@ -19,26 +19,11 @@ package azure
19
19
import (
20
20
"fmt"
21
21
"testing"
22
- "time"
23
22
24
23
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-08-01/compute"
25
24
"github.com/stretchr/testify/assert"
26
- "go.uber.org/mock/gomock"
27
25
28
26
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
29
-
30
- "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/vmssvmclient/mockvmssvmclient"
31
- )
32
-
33
- var (
34
- ctrl * gomock.Controller
35
- currentTime , expiredTime time.Time
36
- provider * AzureCloudProvider
37
- scaleSet * ScaleSet
38
- mockVMSSVMClient * mockvmssvmclient.MockInterface
39
- expectedVMSSVMs []compute.VirtualMachineScaleSetVM
40
- expectedStates []cloudprovider.InstanceState
41
- instanceCache , expectedInstanceCache []cloudprovider.Instance
42
27
)
43
28
44
29
func testGetInstanceCacheWithStates (t * testing.T , vms []compute.VirtualMachineScaleSetVM ,
@@ -53,3 +38,128 @@ func testGetInstanceCacheWithStates(t *testing.T, vms []compute.VirtualMachineSc
53
38
}
54
39
return instanceCacheTest
55
40
}
41
+
42
+ // Suggestion: could populate all combinations, should reunify with TestInstanceStatusFromProvisioningStateAndPowerState
43
+ func TestInstanceStatusFromVM (t * testing.T ) {
44
+ t .Run ("fast delete enablement = false" , func (t * testing.T ) {
45
+ provider := newTestProvider (t )
46
+ scaleSet := newTestScaleSet (provider .azureManager , "testScaleSet" )
47
+
48
+ t .Run ("provisioning state = failed, power state = starting" , func (t * testing.T ) {
49
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStarting )
50
+
51
+ status := scaleSet .instanceStatusFromVM (vm )
52
+
53
+ assert .NotNil (t , status )
54
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
55
+ })
56
+
57
+ t .Run ("provisioning state = failed, power state = running" , func (t * testing.T ) {
58
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateRunning )
59
+
60
+ status := scaleSet .instanceStatusFromVM (vm )
61
+
62
+ assert .NotNil (t , status )
63
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
64
+ })
65
+
66
+ t .Run ("provisioning state = failed, power state = stopping" , func (t * testing.T ) {
67
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStopping )
68
+
69
+ status := scaleSet .instanceStatusFromVM (vm )
70
+
71
+ assert .NotNil (t , status )
72
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
73
+ })
74
+
75
+ t .Run ("provisioning state = failed, power state = stopped" , func (t * testing.T ) {
76
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStopped )
77
+
78
+ status := scaleSet .instanceStatusFromVM (vm )
79
+
80
+ assert .NotNil (t , status )
81
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
82
+ })
83
+
84
+ t .Run ("provisioning state = failed, power state = deallocated" , func (t * testing.T ) {
85
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateDeallocated )
86
+
87
+ status := scaleSet .instanceStatusFromVM (vm )
88
+
89
+ assert .NotNil (t , status )
90
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
91
+ })
92
+
93
+ t .Run ("provisioning state = failed, power state = unknown" , func (t * testing.T ) {
94
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateUnknown )
95
+
96
+ status := scaleSet .instanceStatusFromVM (vm )
97
+
98
+ assert .NotNil (t , status )
99
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
100
+ })
101
+ })
102
+
103
+ t .Run ("fast delete enablement = true" , func (t * testing.T ) {
104
+ provider := newTestProvider (t )
105
+ scaleSet := newTestScaleSetWithFastDelete (provider .azureManager , "testScaleSet" )
106
+
107
+ t .Run ("provisioning state = failed, power state = starting" , func (t * testing.T ) {
108
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStarting )
109
+
110
+ status := scaleSet .instanceStatusFromVM (vm )
111
+
112
+ assert .NotNil (t , status )
113
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
114
+ })
115
+
116
+ t .Run ("provisioning state = failed, power state = running" , func (t * testing.T ) {
117
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateRunning )
118
+
119
+ status := scaleSet .instanceStatusFromVM (vm )
120
+
121
+ assert .NotNil (t , status )
122
+ assert .Equal (t , cloudprovider .InstanceRunning , status .State )
123
+ })
124
+
125
+ t .Run ("provisioning state = failed, power state = stopping" , func (t * testing.T ) {
126
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStopping )
127
+
128
+ status := scaleSet .instanceStatusFromVM (vm )
129
+
130
+ assert .NotNil (t , status )
131
+ assert .Equal (t , cloudprovider .InstanceCreating , status .State )
132
+ assert .NotNil (t , status .ErrorInfo )
133
+ })
134
+
135
+ t .Run ("provisioning state = failed, power state = stopped" , func (t * testing.T ) {
136
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateStopped )
137
+
138
+ status := scaleSet .instanceStatusFromVM (vm )
139
+
140
+ assert .NotNil (t , status )
141
+ assert .Equal (t , cloudprovider .InstanceCreating , status .State )
142
+ assert .NotNil (t , status .ErrorInfo )
143
+ })
144
+
145
+ t .Run ("provisioning state = failed, power state = deallocated" , func (t * testing.T ) {
146
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateDeallocated )
147
+
148
+ status := scaleSet .instanceStatusFromVM (vm )
149
+
150
+ assert .NotNil (t , status )
151
+ assert .Equal (t , cloudprovider .InstanceCreating , status .State )
152
+ assert .NotNil (t , status .ErrorInfo )
153
+ })
154
+
155
+ t .Run ("provisioning state = failed, power state = unknown" , func (t * testing.T ) {
156
+ vm := newVMObjectWithState (string (compute .GalleryProvisioningStateFailed ), vmPowerStateUnknown )
157
+
158
+ status := scaleSet .instanceStatusFromVM (vm )
159
+
160
+ assert .NotNil (t , status )
161
+ assert .Equal (t , cloudprovider .InstanceCreating , status .State )
162
+ assert .NotNil (t , status .ErrorInfo )
163
+ })
164
+ })
165
+ }
0 commit comments