Skip to content

Commit d54a86a

Browse files
committed
Further simplify tests
1 parent 5ada764 commit d54a86a

1 file changed

Lines changed: 50 additions & 223 deletions

File tree

cloud/linode/instances_test.go

Lines changed: 50 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ func TestInstanceExists(t *testing.T) {
9696
})
9797
}
9898

99-
func TestNodeAddresses(t *testing.T) {
99+
func TestMetadataRetrieval(t *testing.T) {
100100
ctx := context.TODO()
101101
ctrl := gomock.NewController(t)
102102
defer ctrl.Finish()
103103

104104
client := NewMockClient(ctrl)
105105
instances := newInstances(client)
106106

107-
t.Run("errors when linode does not exist", func(t *testing.T) {
107+
t.Run("errors when linode does not exist (by name)", func(t *testing.T) {
108108
name := "does-not-exist"
109109
node := nodeWithName(name)
110110
filter := linodeFilterListOptions(name)
@@ -115,6 +115,18 @@ func TestNodeAddresses(t *testing.T) {
115115
assert.Nil(t, meta)
116116
})
117117

118+
t.Run("fails when linode does not exist (by provider)", func(t *testing.T) {
119+
id := 456302
120+
providerID := providerIDPrefix + strconv.Itoa(id)
121+
node := nodeWithProviderID(providerID)
122+
getInstanceErr := &linodego.Error{Code: http.StatusNotFound}
123+
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, getInstanceErr)
124+
meta, err := instances.InstanceMetadata(ctx, node)
125+
126+
assert.ErrorIs(t, err, getInstanceErr)
127+
assert.Nil(t, meta)
128+
})
129+
118130
t.Run("errors when linode does not have any ips", func(t *testing.T) {
119131
id := 29392
120132
name := "an-instance"
@@ -135,15 +147,17 @@ func TestNodeAddresses(t *testing.T) {
135147
assert.Nil(t, meta)
136148
})
137149

138-
t.Run("should return addresses when linode is found", func(t *testing.T) {
150+
t.Run("should return data when linode is found (by name)", func(t *testing.T) {
139151
id := 123
140152
name := "mock-instance"
141153
node := nodeWithName(name)
142154
publicIPv4 := "45.76.101.25"
143155
privateIPv4 := "192.168.133.65"
156+
linodeType := "g6-standard-1"
157+
region := "us-east"
144158
filter := linodeFilterListOptions(name)
145159
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return([]linodego.Instance{
146-
{ID: id, Label: name},
160+
{ID: id, Label: name, Type: linodeType, Region: region},
147161
}, nil)
148162
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
149163
IPv4: &linodego.InstanceIPv4Response{
@@ -154,6 +168,8 @@ func TestNodeAddresses(t *testing.T) {
154168

155169
meta, err := instances.InstanceMetadata(ctx, node)
156170
assert.NoError(t, err)
171+
assert.Equal(t, region, meta.Region)
172+
assert.Equal(t, linodeType, meta.InstanceType)
157173
assert.Equal(t, meta.NodeAddresses, []v1.NodeAddress{
158174
{
159175
Type: v1.NodeHostName,
@@ -169,54 +185,18 @@ func TestNodeAddresses(t *testing.T) {
169185
},
170186
})
171187
})
172-
}
173-
174-
func TestNodeAddressesByProviderID(t *testing.T) {
175-
ctx := context.TODO()
176-
ctrl := gomock.NewController(t)
177-
defer ctrl.Finish()
178-
179-
client := NewMockClient(ctrl)
180-
instances := newInstances(client)
181-
182-
t.Run("fails on malformed providerID", func(t *testing.T) {
183-
providerID := "bogus://bogus"
184-
node := nodeWithProviderID(providerID)
185-
meta, err := instances.InstanceMetadata(ctx, node)
186-
assert.ErrorIs(t, err, invalidProviderIDError{providerID})
187-
assert.Nil(t, meta)
188-
})
189188

190-
t.Run("fails on non-numeric providerID", func(t *testing.T) {
191-
providerID := providerIDPrefix + "abc"
192-
node := nodeWithProviderID(providerID)
193-
meta, err := instances.InstanceMetadata(ctx, node)
194-
195-
assert.ErrorIs(t, err, invalidProviderIDError{providerID})
196-
assert.Nil(t, meta)
197-
})
198-
199-
t.Run("fails when linode does not exist", func(t *testing.T) {
200-
id := 456302
201-
providerID := providerIDPrefix + strconv.Itoa(id)
202-
node := nodeWithProviderID(providerID)
203-
getInstanceErr := &linodego.Error{Code: http.StatusNotFound}
204-
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, getInstanceErr)
205-
meta, err := instances.InstanceMetadata(ctx, node)
206-
207-
assert.ErrorIs(t, err, getInstanceErr)
208-
assert.Nil(t, meta)
209-
})
210-
211-
t.Run("should return addresses when linode is found", func(t *testing.T) {
189+
t.Run("should return addresses when linode is found (by provider)", func(t *testing.T) {
212190
id := 192910
213191
name := "my-instance"
214192
providerID := providerIDPrefix + strconv.Itoa(id)
215193
node := nodeWithProviderID(providerID)
216194
publicIPv4 := "32.74.121.25"
217195
privateIPv4 := "192.168.121.42"
196+
linodeType := "g6-standard-1"
197+
region := "us-east"
218198
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
219-
ID: id, Label: name,
199+
ID: id, Label: name, Type: linodeType, Region: region,
220200
}, nil)
221201
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
222202
IPv4: &linodego.InstanceIPv4Response{
@@ -228,6 +208,8 @@ func TestNodeAddressesByProviderID(t *testing.T) {
228208
meta, err := instances.InstanceMetadata(ctx, node)
229209

230210
assert.NoError(t, err)
211+
assert.Equal(t, region, meta.Region)
212+
assert.Equal(t, linodeType, meta.InstanceType)
231213
assert.Equal(t, meta.NodeAddresses, []v1.NodeAddress{
232214
{
233215
Type: v1.NodeHostName,
@@ -245,92 +227,41 @@ func TestNodeAddressesByProviderID(t *testing.T) {
245227
})
246228
}
247229

248-
func TestInstanceType(t *testing.T) {
230+
func TestMalformedProviders(t *testing.T) {
249231
ctx := context.TODO()
250232
ctrl := gomock.NewController(t)
251233
defer ctrl.Finish()
252234

253235
client := NewMockClient(ctrl)
254236
instances := newInstances(client)
255237

256-
t.Run("fails when instance not found", func(t *testing.T) {
257-
name := "bogus"
258-
node := nodeWithName(name)
259-
filter := linodeFilterListOptions(name)
260-
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return([]linodego.Instance{}, nil)
261-
meta, err := instances.InstanceMetadata(ctx, node)
262-
263-
assert.ErrorIs(t, err, cloudprovider.InstanceNotFound)
264-
assert.Nil(t, meta)
265-
})
266-
267-
t.Run("succeeds when instance exists", func(t *testing.T) {
268-
id := 39399
269-
name := "my-nanode"
270-
node := nodeWithName(name)
271-
linodeType := "g6-standard-1"
272-
filter := linodeFilterListOptions(name)
273-
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return([]linodego.Instance{
274-
{ID: id, Label: name, Type: linodeType},
275-
}, nil)
276-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
277-
IPv4: &linodego.InstanceIPv4Response{Public: []*linodego.InstanceIP{{Address: "1.2.3.4"}}},
278-
IPv6: &linodego.InstanceIPv6Response{},
279-
}, nil)
280-
meta, err := instances.InstanceMetadata(ctx, node)
281-
282-
assert.NoError(t, err)
283-
assert.Equal(t, linodeType, meta.InstanceType)
284-
})
285-
}
286-
287-
func TestInstanceTypeByProviderID(t *testing.T) {
288-
ctx := context.TODO()
289-
ctrl := gomock.NewController(t)
290-
defer ctrl.Finish()
291-
292-
client := NewMockClient(ctrl)
293-
instances := newInstances(client)
294-
295-
t.Run("fails when instance not found", func(t *testing.T) {
296-
id := 39281
297-
providerID := providerIDPrefix + strconv.Itoa(id)
238+
t.Run("fails on malformed providerID", func(t *testing.T) {
239+
providerID := "bogus://bogus"
298240
node := nodeWithProviderID(providerID)
299-
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, linodego.Error{Code: http.StatusNotFound})
300241
meta, err := instances.InstanceMetadata(ctx, node)
301-
302-
assert.Error(t, err)
242+
assert.ErrorIs(t, err, invalidProviderIDError{providerID})
303243
assert.Nil(t, meta)
304244
})
305245

306-
t.Run("succeeds when instance exists", func(t *testing.T) {
307-
id := 39281
308-
providerID := providerIDPrefix + strconv.Itoa(id)
246+
t.Run("fails on non-numeric providerID", func(t *testing.T) {
247+
providerID := providerIDPrefix + "abc"
309248
node := nodeWithProviderID(providerID)
310-
linodeType := "g6-standard-2"
311-
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
312-
ID: id, Label: "some-linode", Type: linodeType,
313-
}, nil)
314-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
315-
IPv4: &linodego.InstanceIPv4Response{Public: []*linodego.InstanceIP{{Address: "1.2.3.4"}}},
316-
IPv6: &linodego.InstanceIPv6Response{},
317-
}, nil)
318249
meta, err := instances.InstanceMetadata(ctx, node)
319250

320-
assert.NoError(t, err)
321-
assert.Equal(t, linodeType, meta.InstanceType)
251+
assert.ErrorIs(t, err, invalidProviderIDError{providerID})
252+
assert.Nil(t, meta)
322253
})
323254
}
324255

325-
func TestInstanceShutdownByProviderID(t *testing.T) {
256+
func TestInstanceShutdown(t *testing.T) {
326257
ctx := context.TODO()
327258
ctrl := gomock.NewController(t)
328259
defer ctrl.Finish()
329260

330261
client := NewMockClient(ctrl)
331262
instances := newInstances(client)
332263

333-
t.Run("fails when instance not found", func(t *testing.T) {
264+
t.Run("fails when instance not found (by provider)", func(t *testing.T) {
334265
id := 12345
335266
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
336267
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, linodego.Error{Code: http.StatusNotFound})
@@ -340,6 +271,20 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
340271
assert.False(t, shutdown)
341272
})
342273

274+
t.Run("fails when instance not found (by name)", func(t *testing.T) {
275+
name := "some-name"
276+
node := nodeWithName(name)
277+
notFound := &linodego.Error{
278+
Code: http.StatusNotFound,
279+
}
280+
filter := linodeFilterListOptions(name)
281+
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return(nil, notFound)
282+
shutdown, err := instances.InstanceShutdown(ctx, node)
283+
284+
assert.Error(t, err)
285+
assert.False(t, shutdown)
286+
})
287+
343288
t.Run("returns true when instance is shut down", func(t *testing.T) {
344289
id := 12345
345290
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
@@ -376,121 +321,3 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
376321
assert.False(t, shutdown)
377322
})
378323
}
379-
380-
// TODO: consider folding all of these tests into the InstanceMetadata tests above
381-
// they have the same setup, we're just testing different properties
382-
func TestMetadataRegion(t *testing.T) {
383-
ctx := context.TODO()
384-
ctrl := gomock.NewController(t)
385-
defer ctrl.Finish()
386-
387-
client := NewMockClient(ctrl)
388-
instances := newInstances(client)
389-
390-
t.Run("Test region retrieval", func(t *testing.T) {
391-
id := 123
392-
region := "us-east"
393-
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
394-
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(&linodego.Instance{
395-
ID: 123,
396-
Label: "mock",
397-
Region: region,
398-
Type: "g6-standard-2",
399-
}, nil)
400-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
401-
IPv4: &linodego.InstanceIPv4Response{Public: []*linodego.InstanceIP{{Address: "1.2.3.4"}}},
402-
IPv6: &linodego.InstanceIPv6Response{},
403-
}, nil)
404-
meta, err := instances.InstanceMetadata(ctx, node)
405-
406-
assert.NoError(t, err)
407-
assert.Equal(t, region, meta.Region)
408-
assert.Empty(t, meta.Zone)
409-
})
410-
}
411-
412-
func TestGetZoneByProviderID(t *testing.T) {
413-
ctx := context.TODO()
414-
ctrl := gomock.NewController(t)
415-
defer ctrl.Finish()
416-
417-
client := NewMockClient(ctrl)
418-
instances := newInstances(client)
419-
420-
t.Run("fail when providerID is malformed", func(t *testing.T) {
421-
providerID := "bogus://123"
422-
node := nodeWithProviderID(providerID)
423-
meta, err := instances.InstanceMetadata(ctx, node)
424-
425-
assert.Error(t, err, invalidProviderIDError{providerID}.Error())
426-
assert.Nil(t, meta)
427-
})
428-
429-
t.Run("fail on api error", func(t *testing.T) {
430-
id := 29182
431-
providerID := providerIDPrefix + strconv.Itoa(id)
432-
node := nodeWithProviderID(providerID)
433-
getErr := &linodego.Error{Code: http.StatusServiceUnavailable}
434-
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, getErr)
435-
meta, err := instances.InstanceMetadata(ctx, node)
436-
437-
assert.ErrorIs(t, err, getErr)
438-
assert.Nil(t, meta)
439-
})
440-
441-
t.Run("get region when linode exists", func(t *testing.T) {
442-
id := 29818
443-
region := "eu-west"
444-
providerID := providerIDPrefix + strconv.Itoa(id)
445-
node := nodeWithProviderID(providerID)
446-
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
447-
ID: id, Region: region,
448-
}, nil)
449-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
450-
IPv4: &linodego.InstanceIPv4Response{Public: []*linodego.InstanceIP{{Address: "1.2.3.4"}}},
451-
IPv6: &linodego.InstanceIPv6Response{},
452-
}, nil)
453-
meta, err := instances.InstanceMetadata(ctx, node)
454-
455-
assert.NoError(t, err)
456-
assert.Equal(t, meta.Region, region)
457-
})
458-
}
459-
460-
func TestGetZoneByNodeName(t *testing.T) {
461-
ctx := context.TODO()
462-
ctrl := gomock.NewController(t)
463-
defer ctrl.Finish()
464-
465-
client := NewMockClient(ctrl)
466-
instances := newInstances(client)
467-
468-
t.Run("fail on api error", func(t *testing.T) {
469-
name := "a-very-nice-linode"
470-
node := nodeWithName(name)
471-
listErr := &linodego.Error{Code: http.StatusInternalServerError}
472-
client.EXPECT().ListInstances(gomock.Any(), linodeFilterListOptions(name)).Times(1).Return(nil, listErr)
473-
474-
meta, err := instances.InstanceMetadata(ctx, node)
475-
assert.ErrorIs(t, err, listErr)
476-
assert.Nil(t, meta)
477-
})
478-
479-
t.Run("get region when linode exists", func(t *testing.T) {
480-
name := "some-linode"
481-
id := 291828
482-
node := nodeWithName(name)
483-
region := "eu-west"
484-
client.EXPECT().ListInstances(gomock.Any(), linodeFilterListOptions(name)).Times(1).Return([]linodego.Instance{
485-
{ID: id, Label: name, Region: region},
486-
}, nil)
487-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
488-
IPv4: &linodego.InstanceIPv4Response{Public: []*linodego.InstanceIP{{Address: "1.2.3.4"}}},
489-
IPv6: &linodego.InstanceIPv6Response{},
490-
}, nil)
491-
meta, err := instances.InstanceMetadata(ctx, node)
492-
493-
assert.NoError(t, err)
494-
assert.Equal(t, region, meta.Region)
495-
})
496-
}

0 commit comments

Comments
 (0)