Skip to content

Commit

Permalink
Merge pull request #76142 from soltysh/fix_createdisk
Browse files Browse the repository at this point in the history
Fix TestCreateDisk by sorting tags
  • Loading branch information
k8s-ci-robot committed Apr 5, 2019
2 parents c644851 + 019d16d commit e9ca60b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/cloudprovider/providers/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"reflect"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -69,6 +70,25 @@ func (m *MockedFakeEC2) DescribeSecurityGroups(request *ec2.DescribeSecurityGrou
}

func (m *MockedFakeEC2) CreateVolume(request *ec2.CreateVolumeInput) (*ec2.Volume, error) {
// mock requires stable input, and in CreateDisk we invoke buildTags which uses
// a map to create tags, which then get converted into an array. This leads to
// unstable sorting order which confuses mock. Sorted tags are not needed in
// regular code, but are a must in tests here:
for i := 0; i < len(request.TagSpecifications); i++ {
if request.TagSpecifications[i] == nil {
continue
}
tags := request.TagSpecifications[i].Tags
sort.Slice(tags, func(i, j int) bool {
if tags[i] == nil && tags[j] != nil {
return false
}
if tags[i] != nil && tags[j] == nil {
return true
}
return *tags[i].Key < *tags[j].Key
})
}
args := m.Called(request)
return args.Get(0).(*ec2.Volume), nil
}
Expand Down Expand Up @@ -1788,6 +1808,8 @@ func TestCreateDisk(t *testing.T) {
Size: aws.Int64(10),
TagSpecifications: []*ec2.TagSpecification{
{ResourceType: aws.String(ec2.ResourceTypeVolume), Tags: []*ec2.Tag{
// CreateVolume from MockedFakeEC2 expects sorted tags, so we need to
// always have these tags sorted:
{Key: aws.String(TagNameKubernetesClusterLegacy), Value: aws.String(TestClusterID)},
{Key: aws.String(fmt.Sprintf("%s%s", TagNameKubernetesClusterPrefix, TestClusterID)), Value: aws.String(ResourceLifecycleOwned)},
}},
Expand Down

0 comments on commit e9ca60b

Please sign in to comment.