@@ -349,3 +349,121 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
349349 assert .False (t , shutdown )
350350 })
351351}
352+
353+ // TODO: consider folding all of these tests into the InstanceMetadata tests above
354+ // they have the same setup, we're just testing different properties
355+ func TestMetadataRegion (t * testing.T ) {
356+ ctx := context .TODO ()
357+ ctrl := gomock .NewController (t )
358+ defer ctrl .Finish ()
359+
360+ client := NewMockClient (ctrl )
361+ instances := newInstances (client )
362+
363+ t .Run ("Test region retrieval" , func (t * testing.T ) {
364+ id := 123
365+ region := "us-east"
366+ node := nodeWithProviderID (providerIDPrefix + strconv .Itoa (id ))
367+ client .EXPECT ().GetInstance (gomock .Any (), 123 ).Times (1 ).Return (& linodego.Instance {
368+ ID : 123 ,
369+ Label : "mock" ,
370+ Region : region ,
371+ Type : "g6-standard-2" ,
372+ }, nil )
373+ client .EXPECT ().GetInstanceIPAddresses (gomock .Any (), id ).Times (1 ).Return (& linodego.InstanceIPAddressResponse {
374+ IPv4 : & linodego.InstanceIPv4Response {Public : []* linodego.InstanceIP {{Address : "1.2.3.4" }}},
375+ IPv6 : & linodego.InstanceIPv6Response {},
376+ }, nil )
377+ meta , err := instances .InstanceMetadata (ctx , node )
378+
379+ assert .NoError (t , err )
380+ assert .Equal (t , region , meta .Region )
381+ assert .Empty (t , meta .Zone )
382+ })
383+ }
384+
385+ func TestGetZoneByProviderID (t * testing.T ) {
386+ ctx := context .TODO ()
387+ ctrl := gomock .NewController (t )
388+ defer ctrl .Finish ()
389+
390+ client := NewMockClient (ctrl )
391+ instances := newInstances (client )
392+
393+ t .Run ("fail when providerID is malformed" , func (t * testing.T ) {
394+ providerID := "bogus://123"
395+ node := nodeWithProviderID (providerID )
396+ meta , err := instances .InstanceMetadata (ctx , node )
397+
398+ assert .Error (t , err , invalidProviderIDError {providerID }.Error ())
399+ assert .Nil (t , meta )
400+ })
401+
402+ t .Run ("fail on api error" , func (t * testing.T ) {
403+ id := 29182
404+ providerID := providerIDPrefix + strconv .Itoa (id )
405+ node := nodeWithProviderID (providerID )
406+ getErr := & linodego.Error {Code : http .StatusServiceUnavailable }
407+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (nil , getErr )
408+ meta , err := instances .InstanceMetadata (ctx , node )
409+
410+ assert .ErrorIs (t , err , getErr )
411+ assert .Nil (t , meta )
412+ })
413+
414+ t .Run ("get region when linode exists" , func (t * testing.T ) {
415+ id := 29818
416+ region := "eu-west"
417+ providerID := providerIDPrefix + strconv .Itoa (id )
418+ node := nodeWithProviderID (providerID )
419+ client .EXPECT ().GetInstance (gomock .Any (), id ).Times (1 ).Return (& linodego.Instance {
420+ ID : id , Region : region ,
421+ }, nil )
422+ client .EXPECT ().GetInstanceIPAddresses (gomock .Any (), id ).Times (1 ).Return (& linodego.InstanceIPAddressResponse {
423+ IPv4 : & linodego.InstanceIPv4Response {Public : []* linodego.InstanceIP {{Address : "1.2.3.4" }}},
424+ IPv6 : & linodego.InstanceIPv6Response {},
425+ }, nil )
426+ meta , err := instances .InstanceMetadata (ctx , node )
427+
428+ assert .NoError (t , err )
429+ assert .Equal (t , meta .Region , region )
430+ })
431+ }
432+
433+ func TestGetZoneByNodeName (t * testing.T ) {
434+ ctx := context .TODO ()
435+ ctrl := gomock .NewController (t )
436+ defer ctrl .Finish ()
437+
438+ client := NewMockClient (ctrl )
439+ instances := newInstances (client )
440+
441+ t .Run ("fail on api error" , func (t * testing.T ) {
442+ name := "a-very-nice-linode"
443+ node := nodeWithName (name )
444+ listErr := & linodego.Error {Code : http .StatusInternalServerError }
445+ client .EXPECT ().ListInstances (gomock .Any (), linodeFilterListOptions (name )).Times (1 ).Return (nil , listErr )
446+
447+ meta , err := instances .InstanceMetadata (ctx , node )
448+ assert .ErrorIs (t , err , listErr )
449+ assert .Nil (t , meta )
450+ })
451+
452+ t .Run ("get region when linode exists" , func (t * testing.T ) {
453+ name := "some-linode"
454+ id := 291828
455+ node := nodeWithName (name )
456+ region := "eu-west"
457+ client .EXPECT ().ListInstances (gomock .Any (), linodeFilterListOptions (name )).Times (1 ).Return ([]linodego.Instance {
458+ {ID : id , Label : name , Region : region },
459+ }, nil )
460+ client .EXPECT ().GetInstanceIPAddresses (gomock .Any (), id ).Times (1 ).Return (& linodego.InstanceIPAddressResponse {
461+ IPv4 : & linodego.InstanceIPv4Response {Public : []* linodego.InstanceIP {{Address : "1.2.3.4" }}},
462+ IPv6 : & linodego.InstanceIPv6Response {},
463+ }, nil )
464+ meta , err := instances .InstanceMetadata (ctx , node )
465+
466+ assert .NoError (t , err )
467+ assert .Equal (t , region , meta .Region )
468+ })
469+ }
0 commit comments