@@ -15,6 +15,12 @@ import (
1515 cloudprovider "k8s.io/cloud-provider"
1616)
1717
18+ func nodeWithProviderID (providerID string ) * v1.Node {
19+ return & v1.Node {Spec : v1.NodeSpec {
20+ ProviderID : providerID ,
21+ }}
22+ }
23+
1824func TestInstanceExistsByProviderID (t * testing.T ) {
1925 ctx := context .TODO ()
2026 ctrl := gomock .NewController (t )
@@ -24,9 +30,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
2430 instances := newInstances (client )
2531
2632 t .Run ("should propagate generic api error" , func (t * testing.T ) {
27- node := & v1.Node {Spec : v1.NodeSpec {
28- ProviderID : providerIDPrefix + "123" ,
29- }}
33+ node := nodeWithProviderID (providerIDPrefix + "123" )
3034 expectedErr := errors .New ("some error" )
3135 client .EXPECT ().GetInstance (gomock .Any (), 123 ).Times (1 ).Return (nil , expectedErr )
3236
@@ -36,9 +40,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
3640 })
3741
3842 t .Run ("should return false if linode does not exist" , func (t * testing.T ) {
39- node := & v1.Node {Spec : v1.NodeSpec {
40- ProviderID : providerIDPrefix + "123" ,
41- }}
43+ node := nodeWithProviderID (providerIDPrefix + "123" )
4244 client .EXPECT ().GetInstance (gomock .Any (), 123 ).Times (1 ).Return (nil , & linodego.Error {
4345 Code : http .StatusNotFound ,
4446 })
@@ -49,9 +51,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
4951 })
5052
5153 t .Run ("should return true if linode exists" , func (t * testing.T ) {
52- node := & v1.Node {Spec : v1.NodeSpec {
53- ProviderID : providerIDPrefix + "123" ,
54- }}
54+ node := nodeWithProviderID (providerIDPrefix + "123" )
5555 client .EXPECT ().GetInstance (gomock .Any (), 123 ).Times (1 ).Return (& linodego.Instance {
5656 ID : 123 ,
5757 Label : "mock" ,
@@ -329,45 +329,45 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
329329
330330 t .Run ("fails when instance not found" , func (t * testing.T ) {
331331 id := 12345
332- providerID := providerIDPrefix + strconv .Itoa (id )
332+ node := nodeWithProviderID ( providerIDPrefix + strconv .Itoa (id ) )
333333 client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (nil , linodego.Error {Code : http .StatusNotFound })
334- shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
334+ shutdown , err := instances .InstanceShutdown (ctx , node )
335335
336336 assert .Error (t , err )
337337 assert .False (t , shutdown )
338338 })
339339
340340 t .Run ("returns true when instance is shut down" , func (t * testing.T ) {
341341 id := 12345
342- providerID := providerIDPrefix + strconv .Itoa (id )
342+ node := nodeWithProviderID ( providerIDPrefix + strconv .Itoa (id ) )
343343 client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
344344 ID : id , Label : "offline-linode" , Status : linodego .InstanceOffline ,
345345 }, nil )
346- shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
346+ shutdown , err := instances .InstanceShutdown (ctx , node )
347347
348348 assert .NoError (t , err )
349349 assert .True (t , shutdown )
350350 })
351351
352352 t .Run ("returns true when instance is shutting down" , func (t * testing.T ) {
353353 id := 12345
354- providerID := providerIDPrefix + strconv .Itoa (id )
354+ node := nodeWithProviderID ( providerIDPrefix + strconv .Itoa (id ) )
355355 client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
356356 ID : id , Label : "shutting-down-linode" , Status : linodego .InstanceShuttingDown ,
357357 }, nil )
358- shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
358+ shutdown , err := instances .InstanceShutdown (ctx , node )
359359
360360 assert .NoError (t , err )
361361 assert .True (t , shutdown )
362362 })
363363
364364 t .Run ("returns false when instance is running" , func (t * testing.T ) {
365365 id := 12345
366- providerID := providerIDPrefix + strconv .Itoa (id )
366+ node := nodeWithProviderID ( providerIDPrefix + strconv .Itoa (id ) )
367367 client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
368368 ID : id , Label : "running-linode" , Status : linodego .InstanceRunning ,
369369 }, nil )
370- shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
370+ shutdown , err := instances .InstanceShutdown (ctx , node )
371371
372372 assert .NoError (t , err )
373373 assert .False (t , shutdown )
0 commit comments