forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_resolver_test.go
289 lines (240 loc) · 8.76 KB
/
auto_resolver_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package ami_test
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
. "github.com/weaveworks/eksctl/pkg/ami"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)
type returnAmi struct {
imageID string
state ec2types.ImageState
createdDate string
}
var _ = Describe("AMI Auto Resolution", func() {
Describe("When resolving an AMI to use", func() {
var (
p *mockprovider.MockProvider
err error
region string
version string
instanceType string
imageFamily string
resolvedAmi string
expectedAmi string
imageState ec2types.ImageState
)
Context("setting proper AWS Account IDs based on instance families", func() {
It("should return the AWS Account ID for AL2 images", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyAmazonLinux2, region)
Expect(ownerAccount).To(BeEquivalentTo("602401143452"))
Expect(err).NotTo(HaveOccurred())
})
It("should return the AWS Account ID for AL2 images in ap-east-1", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyAmazonLinux2, "ap-east-1")
Expect(ownerAccount).To(BeEquivalentTo("800184023465"))
Expect(err).NotTo(HaveOccurred())
})
It("should return the AWS Account ID for Ubuntu images in ap-east-1", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyUbuntu1804, "ap-east-1")
Expect(ownerAccount).To(BeEquivalentTo("099720109477"))
Expect(err).NotTo(HaveOccurred())
})
It("should return the Ubuntu Account ID for Ubuntu images", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyUbuntu1804, region)
Expect(ownerAccount).To(BeEquivalentTo("099720109477"))
Expect(err).NotTo(HaveOccurred())
})
It("should return the Ubuntu Account ID for Ubuntu images", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyUbuntu2204, region)
Expect(ownerAccount).To(BeEquivalentTo("099720109477"))
Expect(err).NotTo(HaveOccurred())
})
It("should return the Windows Account ID for Windows Server images", func() {
ownerAccount, err := OwnerAccountID(api.NodeImageFamilyWindowsServer2022CoreContainer, region)
Expect(ownerAccount).To(BeEquivalentTo("801119661308"))
Expect(err).NotTo(HaveOccurred())
})
})
Context("with a valid region and N instance type", func() {
BeforeEach(func() {
region = "eu-west-1"
version = "1.15"
expectedAmi = "ami-12345"
})
Context("and non-gpu instance type", func() {
BeforeEach(func() {
instanceType = "t2.medium"
imageFamily = "AmazonLinux2"
})
Context("and ami is available", func() {
BeforeEach(func() {
imageState = ec2types.ImageStateAvailable
p = mockprovider.NewMockProvider()
addMockDescribeImages(p, "amazon-eks-node-1.15-v*", expectedAmi, imageState, "2018-08-20T23:25:53.000Z", api.NodeImageFamilyAmazonLinux2)
resolver := NewAutoResolver(p.MockEC2())
resolvedAmi, err = resolver.Resolve(context.Background(), region, version, instanceType, imageFamily)
})
It("should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have called AWS EC2 DescribeImages", func() {
Expect(p.MockEC2().AssertNumberOfCalls(GinkgoT(), "DescribeImages", 1)).To(BeTrue())
})
It("should have returned an ami id", func() {
Expect(resolvedAmi).To(BeEquivalentTo(expectedAmi))
})
})
Context("and ami is available", func() {
BeforeEach(func() {
imageState = ec2types.ImageStateAvailable
imageFamily = "Ubuntu1804"
p = mockprovider.NewMockProvider()
addMockDescribeImages(p, "ubuntu-eks/k8s_1.15/images/*18.04*", expectedAmi, imageState, "2018-08-20T23:25:53.000Z", api.NodeImageFamilyUbuntu1804)
resolver := NewAutoResolver(p.MockEC2())
resolvedAmi, err = resolver.Resolve(context.Background(), region, version, instanceType, imageFamily)
})
It("should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have called AWS EC2 DescribeImages", func() {
Expect(p.MockEC2().AssertNumberOfCalls(GinkgoT(), "DescribeImages", 1)).To(BeTrue())
})
It("should have returned an ami id", func() {
Expect(resolvedAmi).To(BeEquivalentTo(expectedAmi))
})
})
Context("and ami is NOT available", func() {
BeforeEach(func() {
imageState = ec2types.ImageStatePending
p = mockprovider.NewMockProvider()
addMockDescribeImagesMultiple(p, "amazon-eks-node-1.15-v*", []returnAmi{})
resolver := NewAutoResolver(p.MockEC2())
resolvedAmi, err = resolver.Resolve(context.Background(), region, version, instanceType, imageFamily)
})
It("should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have called AWS EC2 DescribeImages", func() {
Expect(p.MockEC2().AssertNumberOfCalls(GinkgoT(), "DescribeImages", 1)).To(BeTrue())
})
It("should NOT have returned an ami id", func() {
Expect(resolvedAmi).To(BeEquivalentTo(""))
})
})
Context("and there are 2 ami's available", func() {
BeforeEach(func() {
imageState = "available"
expectedAmi = "ami-5678"
p = mockprovider.NewMockProvider()
images := []returnAmi{
{
createdDate: "2018-08-20T23:25:53.000Z",
imageID: "ami-1234",
state: "available",
},
{
createdDate: "2018-09-12T22:21:11.000Z",
imageID: expectedAmi,
state: "available",
},
}
addMockDescribeImagesMultiple(p, "amazon-eks-node-1.15-v*", images)
resolver := NewAutoResolver(p.MockEC2())
resolvedAmi, err = resolver.Resolve(context.Background(), region, version, instanceType, imageFamily)
})
It("should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have called AWS EC2 DescribeImages", func() {
Expect(p.MockEC2().AssertNumberOfCalls(GinkgoT(), "DescribeImages", 1)).To(BeTrue())
})
It("should have returned an ami id", func() {
Expect(resolvedAmi).To(BeEquivalentTo(expectedAmi))
})
})
})
Context("and gpu instance type", func() {
BeforeEach(func() {
instanceType = "p2.xlarge"
})
Context("and ami is available", func() {
BeforeEach(func() {
imageState = "available"
p = mockprovider.NewMockProvider()
addMockDescribeImages(p, "amazon-eks-gpu-node-1.15-*", expectedAmi, imageState, "2018-08-20T23:25:53.000Z", api.NodeImageFamilyAmazonLinux2)
resolver := NewAutoResolver(p.MockEC2())
resolvedAmi, err = resolver.Resolve(context.Background(), region, version, instanceType, imageFamily)
})
It("should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have called AWS EC2 DescribeImages", func() {
Expect(p.MockEC2().AssertNumberOfCalls(GinkgoT(), "DescribeImages", 1)).To(BeTrue())
})
It("should have returned an ami id", func() {
Expect(resolvedAmi).To(BeEquivalentTo(expectedAmi))
})
})
})
})
})
})
func addMockDescribeImages(p *mockprovider.MockProvider, expectedNamePattern string, amiID string, amiState ec2types.ImageState, createdDate string, instanceFamily string) {
p.MockEC2().On("DescribeImages",
mock.Anything,
mock.MatchedBy(func(input *ec2.DescribeImagesInput) bool {
for _, filter := range input.Filters {
if *filter.Name == "name" {
if len(filter.Values) > 0 {
if filter.Values[0] == expectedNamePattern {
return true
}
}
}
}
return false
}),
).Return(&ec2.DescribeImagesOutput{
Images: []ec2types.Image{
{
ImageId: aws.String(amiID),
State: amiState,
CreationDate: aws.String(createdDate),
Description: aws.String(instanceFamily),
},
},
}, nil)
}
func addMockDescribeImagesMultiple(p *mockprovider.MockProvider, expectedNamePattern string, returnAmis []returnAmi) {
images := make([]ec2types.Image, len(returnAmis))
for i, ami := range returnAmis {
images[i] = ec2types.Image{
ImageId: aws.String(ami.imageID),
State: ami.state,
CreationDate: aws.String(ami.createdDate),
}
}
p.MockEC2().On("DescribeImages",
mock.Anything,
mock.MatchedBy(func(input *ec2.DescribeImagesInput) bool {
for _, filter := range input.Filters {
if *filter.Name == "name" {
if len(filter.Values) > 0 {
if filter.Values[0] == expectedNamePattern {
return true
}
}
}
}
return false
}),
).Return(&ec2.DescribeImagesOutput{
Images: images,
}, nil)
}