@@ -24,6 +24,19 @@ import (
24
24
"net"
25
25
"net/http"
26
26
"testing"
27
+
28
+ "k8s.io/apimachinery/pkg/util/sets"
29
+ cloudprovider "k8s.io/cloud-provider"
30
+ "k8s.io/legacy-cloud-providers/azure/clients/vmclient/mockvmclient"
31
+
32
+ "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
33
+ "github.com/Azure/go-autorest/autorest/to"
34
+ "github.com/golang/mock/gomock"
35
+ "github.com/stretchr/testify/assert"
36
+ )
37
+
38
+ const (
39
+ testAvailabilitySetNodeProviderID = "azure:///subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm-0"
27
40
)
28
41
29
42
func TestIsAvailabilityZone (t * testing.T ) {
@@ -91,6 +104,8 @@ func TestGetZone(t *testing.T) {
91
104
location string
92
105
faultDomain string
93
106
expected string
107
+ isNilResp bool
108
+ expectedErr error
94
109
}{
95
110
{
96
111
name : "GetZone should get real zone if only node's zone is set" ,
@@ -117,6 +132,19 @@ func TestGetZone(t *testing.T) {
117
132
zone : "1" ,
118
133
expected : "eastus-1" ,
119
134
},
135
+ {
136
+ name : "GetZone should report an error if there is no `Compute` in the response" ,
137
+ isNilResp : true ,
138
+ expectedErr : fmt .Errorf ("failure of getting compute information from instance metadata" ),
139
+ },
140
+ {
141
+ name : "GetZone should report an error if the zone is invalid" ,
142
+ zone : "a" ,
143
+ location : "eastus" ,
144
+ faultDomain : "99" ,
145
+ expected : "" ,
146
+ expectedErr : fmt .Errorf ("failed to parse zone ID \" a\" : strconv.Atoi: parsing \" a\" : invalid syntax" ),
147
+ },
120
148
}
121
149
122
150
for _ , test := range testcases {
@@ -125,9 +153,13 @@ func TestGetZone(t *testing.T) {
125
153
t .Errorf ("Test [%s] unexpected error: %v" , test .name , err )
126
154
}
127
155
156
+ respString := fmt .Sprintf (`{"compute":{"zone":"%s", "platformFaultDomain":"%s", "location":"%s"}}` , test .zone , test .faultDomain , test .location )
157
+ if test .isNilResp {
158
+ respString = "{}"
159
+ }
128
160
mux := http .NewServeMux ()
129
161
mux .Handle ("/" , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
130
- fmt .Fprint (w , fmt . Sprintf ( `{"compute":{"zone":"%s", "platformFaultDomain":"%s", "location":"%s"}}` , test . zone , test . faultDomain , test . location ) )
162
+ fmt .Fprint (w , respString )
131
163
}))
132
164
go func () {
133
165
http .Serve (listener , mux )
@@ -141,13 +173,72 @@ func TestGetZone(t *testing.T) {
141
173
142
174
zone , err := cloud .GetZone (context .Background ())
143
175
if err != nil {
144
- t .Errorf ("Test [%s] unexpected error: %v" , test .name , err )
176
+ if test .expectedErr == nil {
177
+ t .Errorf ("Test [%s] unexpected error: %v" , test .name , err )
178
+ } else {
179
+ assert .Equal (t , test .expectedErr , err )
180
+ }
145
181
}
146
182
if zone .FailureDomain != test .expected {
147
183
t .Errorf ("Test [%s] unexpected zone: %s, expected %q" , test .name , zone .FailureDomain , test .expected )
148
184
}
149
- if zone .Region != cloud .Location {
185
+ if err == nil && zone .Region != cloud .Location {
150
186
t .Errorf ("Test [%s] unexpected region: %s, expected: %s" , test .name , zone .Region , cloud .Location )
151
187
}
152
188
}
153
189
}
190
+
191
+ func TestMakeZone (t * testing.T ) {
192
+ az := & Cloud {}
193
+ zone := az .makeZone ("EASTUS" , 2 )
194
+ assert .Equal (t , "eastus-2" , zone )
195
+ }
196
+
197
+ func TestGetZoneByProviderID (t * testing.T ) {
198
+ ctrl := gomock .NewController (t )
199
+ defer ctrl .Finish ()
200
+
201
+ az := GetTestCloud (ctrl )
202
+
203
+ zone , err := az .GetZoneByProviderID (context .Background (), "" )
204
+ assert .Equal (t , errNodeNotInitialized , err )
205
+ assert .Equal (t , cloudprovider.Zone {}, zone )
206
+
207
+ zone , err = az .GetZoneByProviderID (context .Background (), "invalid/id" )
208
+ assert .NoError (t , err )
209
+ assert .Equal (t , cloudprovider.Zone {}, zone )
210
+
211
+ mockVMClient := az .VirtualMachinesClient .(* mockvmclient.MockInterface )
212
+ mockVMClient .EXPECT ().Get (gomock .Any (), az .ResourceGroup , "vm-0" , gomock .Any ()).Return (compute.VirtualMachine {
213
+ Zones : & []string {"1" },
214
+ Location : to .StringPtr ("eastus" ),
215
+ }, nil )
216
+ zone , err = az .GetZoneByProviderID (context .Background (), testAvailabilitySetNodeProviderID )
217
+ assert .NoError (t , err )
218
+ assert .Equal (t , cloudprovider.Zone {
219
+ FailureDomain : "eastus-1" ,
220
+ Region : "eastus" ,
221
+ }, zone )
222
+ }
223
+
224
+ func TestAvailabilitySetGetZoneByNodeName (t * testing.T ) {
225
+ az := & Cloud {
226
+ unmanagedNodes : sets.String {"vm-0" : sets.Empty {}},
227
+ nodeInformerSynced : func () bool {
228
+ return true
229
+ },
230
+ }
231
+ zone , err := az .GetZoneByNodeName (context .Background (), "vm-0" )
232
+ assert .NoError (t , err )
233
+ assert .Equal (t , cloudprovider.Zone {}, zone )
234
+
235
+ az = & Cloud {
236
+ unmanagedNodes : sets.String {"vm-0" : sets.Empty {}},
237
+ nodeInformerSynced : func () bool {
238
+ return false
239
+ },
240
+ }
241
+ zone , err = az .GetZoneByNodeName (context .Background (), "vm-0" )
242
+ assert .Equal (t , fmt .Errorf ("node informer is not synced when trying to GetUnmanagedNodes" ), err )
243
+ assert .Equal (t , cloudprovider.Zone {}, zone )
244
+ }
0 commit comments