diff --git a/pkg/cloudprovider/providers/aws/aws.go b/pkg/cloudprovider/providers/aws/aws.go index ce0c1d5f9e4..eec475e7f7a 100644 --- a/pkg/cloudprovider/providers/aws/aws.go +++ b/pkg/cloudprovider/providers/aws/aws.go @@ -3583,7 +3583,7 @@ var providerIDRegexp = regexp.MustCompile(`^aws://([^/]+)$`) func instanceIDFromProviderID(providerID string) (instanceID string, err error) { matches := providerIDRegexp.FindStringSubmatch(providerID) - if len(matches) != 1 { + if len(matches) != 2 { return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"aws://InstanceID\"", providerID) } diff --git a/pkg/cloudprovider/providers/aws/aws_test.go b/pkg/cloudprovider/providers/aws/aws_test.go index a50eac1fc81..f75c1a571c3 100644 --- a/pkg/cloudprovider/providers/aws/aws_test.go +++ b/pkg/cloudprovider/providers/aws/aws_test.go @@ -322,6 +322,12 @@ func (self *FakeEC2) DescribeInstances(request *ec2.DescribeInstancesInput) ([]* return matches, nil } +func (self *FakeEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) ([]*ec2.Address, error) { + addresses := []*ec2.Address{} + + return addresses, nil +} + type FakeMetadata struct { aws *FakeAWSServices } @@ -1350,3 +1356,37 @@ func TestGetLoadBalancerAdditionalTags(t *testing.T) { } } } + +func TestInstanceIDFromProviderID(t *testing.T) { + testCases := []struct { + providerID string + instanceID string + fail bool + }{ + { + providerID: "aws://i-0194bbdb81a49b169", + instanceID: "i-0194bbdb81a49b169", + fail: false, + }, + { + providerID: "i-0194bbdb81a49b169", + instanceID: "", + fail: true, + }, + } + + for _, test := range testCases { + instanceID, err := instanceIDFromProviderID(test.providerID) + if (err != nil) != test.fail { + t.Errorf("%s yielded `err != nil` as %t. expected %t", test.providerID, (err != nil), test.fail) + } + + if test.fail { + continue + } + + if instanceID != test.instanceID { + t.Errorf("%s yielded %s. expected %s", test.providerID, instanceID, test.instanceID) + } + } +}