@@ -314,7 +314,56 @@ func TestAddSSHKeyToAllInstances(t *testing.T) {
314314}
315315
316316func TestInstanceShutdownByProviderID (t * testing.T ) {
317- instances := newInstances (nil )
318- _ , err := instances .InstanceShutdownByProviderID (context .TODO (), providerIDPrefix + "12345" )
319- assert .ErrorIs (t , err , cloudprovider .NotImplemented )
317+ ctx := context .TODO ()
318+ ctrl := gomock .NewController (t )
319+ defer ctrl .Finish ()
320+
321+ client := NewMockClient (ctrl )
322+ instances := newInstances (client )
323+
324+ t .Run ("fails when instance not found" , func (t * testing.T ) {
325+ id := 12345
326+ providerID := providerIDPrefix + strconv .Itoa (id )
327+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (nil , linodego.Error {Code : http .StatusNotFound })
328+ shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
329+
330+ assert .Error (t , err )
331+ assert .False (t , shutdown )
332+ })
333+
334+ t .Run ("returns true when instance is shut down" , func (t * testing.T ) {
335+ id := 12345
336+ providerID := providerIDPrefix + strconv .Itoa (id )
337+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
338+ ID : id , Label : "offline-linode" , Status : linodego .InstanceOffline ,
339+ }, nil )
340+ shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
341+
342+ assert .NoError (t , err )
343+ assert .True (t , shutdown )
344+ })
345+
346+ t .Run ("returns true when instance is shutting down" , func (t * testing.T ) {
347+ id := 12345
348+ providerID := providerIDPrefix + strconv .Itoa (id )
349+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
350+ ID : id , Label : "shutting-down-linode" , Status : linodego .InstanceShuttingDown ,
351+ }, nil )
352+ shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
353+
354+ assert .NoError (t , err )
355+ assert .True (t , shutdown )
356+ })
357+
358+ t .Run ("returns false when instance is running" , func (t * testing.T ) {
359+ id := 12345
360+ providerID := providerIDPrefix + strconv .Itoa (id )
361+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
362+ ID : id , Label : "running-linode" , Status : linodego .InstanceRunning ,
363+ }, nil )
364+ shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
365+
366+ assert .NoError (t , err )
367+ assert .False (t , shutdown )
368+ })
320369}
0 commit comments