bugfix for ProviderID parsing & corresponding unit test

This commit is contained in:
ublubu 2017-06-08 23:12:28 -04:00
parent bc9d2e8832
commit c261f98a60
2 changed files with 41 additions and 1 deletions

View File

@ -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)
}

View File

@ -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)
}
}
}