-
Notifications
You must be signed in to change notification settings - Fork 433
/
autoscaling_groups_test.go
136 lines (114 loc) · 3.63 KB
/
autoscaling_groups_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ec2_test
import (
"context"
"errors"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
"github.com/stretchr/testify/assert"
"github.com/tailwarden/komiser/mocks"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers/aws/ec2"
)
func TestDiscoverReturnsNoErrorIfHappyPath(t *testing.T) {
// Arrange
ctx := context.Background()
var queryInput autoscaling.DescribeAutoScalingGroupsInput
asgClient := mocks.NewAutoScalingGroupClient(t)
asgClient.On(
"DescribeAutoScalingGroups",
ctx,
&queryInput,
).Return(&autoscaling.DescribeAutoScalingGroupsOutput{}, nil)
discoverer := ec2.ASGDiscoverer{
Client: asgClient,
Ctx: ctx,
AccountName: "test",
Region: "us-east-1",
}
// Act
_, err := discoverer.Discover()
// Assert
assert.NoError(t, err)
}
func TestDiscoverReturnsResourcesAsExpectedWhenDiscovered(t *testing.T) {
// Arrange
asg1 := newASG("foo")
asg2 := newASG("bar")
ctx := context.Background()
var queryInput autoscaling.DescribeAutoScalingGroupsInput
asgClient := mocks.NewAutoScalingGroupClient(t)
asgClient.On(
"DescribeAutoScalingGroups",
ctx,
&queryInput,
).Return(&autoscaling.DescribeAutoScalingGroupsOutput{
AutoScalingGroups: []types.AutoScalingGroup{asg1, asg2},
}, nil)
discoverer := ec2.ASGDiscoverer{
Client: asgClient,
Ctx: ctx,
AccountName: "my-account",
Region: "my-region",
}
// Act
resources, err := discoverer.Discover()
// Assert
expectedTags := make([]models.Tag, 0)
expectedTags = append(expectedTags, models.Tag{
Key: "Name",
Value: "my-asg-foo",
})
assert.NoError(t, err)
assert.Len(t, resources, 2)
assert.Equal(t, "AWS", resources[0].Provider)
assert.Equal(t, "my-account", resources[0].Account)
assert.Equal(t, "AutoScalingGroup", resources[0].Service)
assert.Equal(t, "arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:12345678-1234-1234-1234-123456789012:autoScalingGroupName/my-asg-foo", resources[0].ResourceId)
assert.Equal(t, float64(0), resources[0].Cost)
assert.Equal(t, expectedTags, resources[0].Tags)
assert.Equal(t, "https://my-region.console.aws.amazon.com/ec2/home?region=my-region#AutoScalingGroupDetails:id=my-asg-foo", resources[0].Link)
}
func TestDiscoverReturnsErrorIfCannotDiscoverASGs(t *testing.T) {
// Arrange
ctx := context.Background()
var queryInput autoscaling.DescribeAutoScalingGroupsInput
asgClient := mocks.NewAutoScalingGroupClient(t)
asgClient.On(
"DescribeAutoScalingGroups",
ctx,
&queryInput,
).Return(&autoscaling.DescribeAutoScalingGroupsOutput{}, errors.New("Could not discover ASGs"))
discoverer := ec2.ASGDiscoverer{
Client: asgClient,
Ctx: ctx,
AccountName: "my-account",
Region: "my-region",
}
// Act
resources, err := discoverer.Discover()
// Assert
assert.Error(t, err)
assert.ErrorContains(t, err, "Could not discover ASGs")
assert.Len(t, resources, 0)
}
func getStringPointer(str string) *string {
return &str
}
func newASG(seed string) types.AutoScalingGroup {
tag := types.TagDescription{
Key: getStringPointer("Name"),
Value: getStringPointer("my-asg-" + seed),
}
return types.AutoScalingGroup{
AutoScalingGroupName: getStringPointer("my-asg-" + seed),
AvailabilityZones: []string{"us-east-1a", "us-east-1b"},
CreatedTime: aws.Time(time.Now()),
Tags: []types.TagDescription{
tag,
},
AutoScalingGroupARN: getStringPointer("arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:12345678-1234-1234-1234-123456789012:autoScalingGroupName/my-asg-" + seed),
}
}