-
Notifications
You must be signed in to change notification settings - Fork 849
/
describeinstances_test.go
181 lines (162 loc) · 6.28 KB
/
describeinstances_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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package batcher_test
import (
"fmt"
"sync"
"sync/atomic"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/karpenter-provider-aws/pkg/batcher"
"github.com/aws/karpenter-provider-aws/pkg/fake"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("DescribeInstances Batcher", func() {
var cfb *batcher.DescribeInstancesBatcher
BeforeEach(func() {
fakeEC2API.Reset()
cfb = batcher.NewDescribeInstancesBatcher(ctx, fakeEC2API)
})
It("should batch input into a single call", func() {
instanceIDs := []string{"i-1", "i-2", "i-3", "i-4", "i-5"}
for _, id := range instanceIDs {
fakeEC2API.Instances.Store(id, &ec2.Instance{InstanceId: aws.String(id)})
}
var wg sync.WaitGroup
var receivedInstance int64
for _, instanceID := range instanceIDs {
wg.Add(1)
go func(instanceID string) {
defer GinkgoRecover()
defer wg.Done()
rsp, err := cfb.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
})
Expect(err).To(BeNil())
atomic.AddInt64(&receivedInstance, 1)
Expect(rsp.Reservations).To(HaveLen(1))
Expect(rsp.Reservations[0].Instances).To(HaveLen(1))
}(instanceID)
}
wg.Wait()
Expect(receivedInstance).To(BeNumerically("==", len(instanceIDs)))
Expect(fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Len()).To(BeNumerically("==", 1))
call := fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Pop()
Expect(len(call.InstanceIds)).To(BeNumerically("==", len(instanceIDs)))
})
It("should batch input correctly when receiving multiple calls with the same instance id", func() {
instanceIDs := []string{"i-1", "i-1", "i-1", "i-2", "i-2"}
for _, id := range instanceIDs {
fakeEC2API.Instances.Store(id, &ec2.Instance{InstanceId: aws.String(id)})
}
var wg sync.WaitGroup
var receivedInstance int64
for _, instanceID := range instanceIDs {
wg.Add(1)
go func(instanceID string) {
defer GinkgoRecover()
defer wg.Done()
rsp, err := cfb.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
})
Expect(err).To(BeNil())
atomic.AddInt64(&receivedInstance, 1)
Expect(rsp.Reservations).To(HaveLen(1))
Expect(rsp.Reservations[0].Instances).To(HaveLen(1))
}(instanceID)
}
wg.Wait()
Expect(receivedInstance).To(BeNumerically("==", len(instanceIDs)))
Expect(fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Len()).To(BeNumerically("==", 1))
call := fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Pop()
Expect(len(call.InstanceIds)).To(BeNumerically("==", len(instanceIDs)))
})
It("should handle partial terminations on batched call and recover with individual requests", func() {
instanceIDs := []string{"i-1", "i-2", "i-3"}
// Output with only the first Instance
fakeEC2API.DescribeInstancesBehavior.Output.Set(&ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
InstanceId: aws.String("i-1"),
},
},
},
},
})
runningFilter := &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String(ec2.InstanceStateNameRunning)},
}
var wg sync.WaitGroup
var receivedInstance int64
var numUnfulfilled int64
for _, instanceID := range instanceIDs {
wg.Add(1)
go func(instanceID string) {
defer GinkgoRecover()
defer wg.Done()
rsp, err := cfb.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
Filters: []*ec2.Filter{runningFilter},
})
Expect(err).To(BeNil())
if len(rsp.Reservations) > 0 {
Expect(len(rsp.Reservations[0].Instances)).To(BeNumerically("<=", 1))
if len(rsp.Reservations[0].Instances) == 1 {
atomic.AddInt64(&receivedInstance, 1)
} else {
atomic.AddInt64(&numUnfulfilled, 1)
}
}
}(instanceID)
}
wg.Wait()
// should execute the batched call and then one for each that failed in the batched request
Expect(fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Len()).To(BeNumerically("==", 3))
lastCall := fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Pop()
Expect(len(lastCall.InstanceIds)).To(BeNumerically("==", 1))
Expect(len(lastCall.Filters)).To(BeNumerically("==", 1))
Expect(*lastCall.Filters[0].Name).To(Equal("instance-state-name"))
nextToLastCall := fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Pop()
Expect(len(nextToLastCall.InstanceIds)).To(BeNumerically("==", 1))
Expect(len(nextToLastCall.Filters)).To(BeNumerically("==", 1))
Expect(*lastCall.Filters[0].Name).To(Equal("instance-state-name"))
firstCall := fakeEC2API.DescribeInstancesBehavior.CalledWithInput.Pop()
Expect(len(firstCall.InstanceIds)).To(BeNumerically("==", 3))
Expect(len(firstCall.Filters)).To(BeNumerically("==", 1))
Expect(*lastCall.Filters[0].Name).To(Equal("instance-state-name"))
Expect(receivedInstance).To(BeNumerically("==", 3))
Expect(numUnfulfilled).To(BeNumerically("==", 0))
})
It("should return errors to all callers when erroring on the batched call", func() {
instanceIDs := []string{"i-1", "i-2", "i-3", "i-4", "i-5"}
fakeEC2API.DescribeInstancesBehavior.Error.Set(fmt.Errorf("error"), fake.MaxCalls(6))
var wg sync.WaitGroup
for _, instanceID := range instanceIDs {
wg.Add(1)
go func(instanceID string) {
defer GinkgoRecover()
defer wg.Done()
_, err := cfb.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
})
Expect(err).ToNot(BeNil())
}(instanceID)
}
wg.Wait()
// We expect 6 calls since we do one full batched call and 5 individual since the batched call returns an error
Expect(fakeEC2API.DescribeInstancesBehavior.Calls()).To(BeNumerically("==", 6))
})
})