Skip to content

Commit

Permalink
Merge pull request #1494 from Jeffwan/aws-flakinng-ut-fix-1.3
Browse files Browse the repository at this point in the history
Cherry-pick of #1485 to 1.3 : Fix aws flaking Unit Tests
  • Loading branch information
k8s-ci-robot authored Dec 10, 2018
2 parents 58ffbde + 2160cce commit a9ef826
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions cluster-autoscaler/cloudprovider/aws/aws_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package aws

import (
"fmt"
"reflect"
"sort"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -167,21 +170,21 @@ func TestFetchExplicitAsgs(t *testing.T) {
validateAsg(t, asgs[0], groupname, min, max)
}

/* Disabled due to flakiness. See https://github.com/kubernetes/autoscaler/issues/608
func TestFetchAutoAsgs(t *testing.T) {
min, max := 1, 10
groupname, tags := "coolasg", []string{"tag", "anothertag"}

s := &AutoScalingMock{}
// Lookup groups associated with tags
s.On("DescribeTagsPages",
&autoscaling.DescribeTagsInput{
Filters: []*autoscaling.Filter{
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[0]})},
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[1]})},
},
MaxRecords: aws.Int64(maxRecordsReturnedByAPI),
expectedTagsInput := &autoscaling.DescribeTagsInput{
Filters: []*autoscaling.Filter{
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[0]})},
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[1]})},
},
MaxRecords: aws.Int64(maxRecordsReturnedByAPI),
}
// Use MatchedBy pattern to avoid list order issue https://github.com/kubernetes/autoscaler/issues/1346
s.On("DescribeTagsPages", mock.MatchedBy(tagsMatcher(expectedTagsInput)),
mock.AnythingOfType("func(*autoscaling.DescribeTagsOutput, bool) bool"),
).Run(func(args mock.Arguments) {
fn := args.Get(1).(func(*autoscaling.DescribeTagsOutput, bool) bool)
Expand Down Expand Up @@ -218,27 +221,38 @@ func TestFetchAutoAsgs(t *testing.T) {
m, err := createAWSManagerInternal(nil, do, &autoScalingWrapper{s})
assert.NoError(t, err)

asgs := m.asgCache.get()
asgs := m.asgCache.Get()
assert.Equal(t, 1, len(asgs))
validateAsg(t, asgs[0].config, groupname, min, max)
validateAsg(t, asgs[0], groupname, min, max)

// Simulate the previously discovered ASG disappearing
s.On("DescribeTagsPages",
&autoscaling.DescribeTagsInput{
Filters: []*autoscaling.Filter{
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[0]})},
{Name: aws.String("key"), Values: aws.StringSlice([]string{tags[1]})},
},
MaxRecords: aws.Int64(maxRecordsReturnedByAPI),
},
s.On("DescribeTagsPages", mock.MatchedBy(tagsMatcher(expectedTagsInput)),
mock.AnythingOfType("func(*autoscaling.DescribeTagsOutput, bool) bool"),
).Run(func(args mock.Arguments) {
fn := args.Get(1).(func(*autoscaling.DescribeTagsOutput, bool) bool)
fn(&autoscaling.DescribeTagsOutput{Tags: []*autoscaling.TagDescription{}}, false)
}).Return(nil).Once()

err = m.fetchAutoAsgs()
err = m.asgCache.regenerate()
assert.NoError(t, err)
assert.Empty(t, m.asgCache.get())
assert.Empty(t, m.asgCache.Get())
}

func tagsMatcher(expected *autoscaling.DescribeTagsInput) func(*autoscaling.DescribeTagsInput) bool {
return func(actual *autoscaling.DescribeTagsInput) bool {
expectedTags := flatTagSlice(expected.Filters)
actualTags := flatTagSlice(actual.Filters)

return *expected.MaxRecords == *actual.MaxRecords && reflect.DeepEqual(expectedTags, actualTags)
}
}

func flatTagSlice(filters []*autoscaling.Filter) []string {
tags := []string{}
for _, filter := range filters {
tags = append(tags, aws.StringValueSlice(filter.Values)...)
}
// Sort slice for compare
sort.Strings(tags)
return tags
}
*/

0 comments on commit a9ef826

Please sign in to comment.