-
Notifications
You must be signed in to change notification settings - Fork 417
/
ec2provider_test.go
152 lines (137 loc) · 4.04 KB
/
ec2provider_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
package ec2provider
import (
"strconv"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
)
const (
DescribeDelay = 100
)
type mockEc2Client struct {
ec2iface.EC2API
Reservations []*ec2.Reservation
}
func (c *mockEc2Client) DescribeInstances(in *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
// simulate the time it takes for aws to return
time.Sleep(DescribeDelay * time.Millisecond)
var reservations []*ec2.Reservation
for _, res := range c.Reservations {
var reservation ec2.Reservation
for _, inst := range res.Instances {
for _, id := range in.InstanceIds {
if aws.StringValue(id) == aws.StringValue(inst.InstanceId) {
reservation.Instances = append(reservation.Instances, inst)
}
}
}
if len(reservation.Instances) > 0 {
reservations = append(reservations, &reservation)
}
}
return &ec2.DescribeInstancesOutput{
Reservations: reservations,
}, nil
}
func newMockedEC2ProviderImpl() *ec2ProviderImpl {
dnsCache := ec2PrivateDNSCache{
cache: make(map[string]string),
lock: sync.RWMutex{},
}
ec2Requests := ec2Requests{
set: make(map[string]bool),
lock: sync.RWMutex{},
}
return &ec2ProviderImpl{
ec2: &mockEc2Client{},
privateDNSCache: dnsCache,
ec2Requests: ec2Requests,
instanceIdsChannel: make(chan string, maxChannelSize),
}
}
func TestGetPrivateDNSName(t *testing.T) {
metrics.InitMetrics(prometheus.NewRegistry())
ec2Provider := newMockedEC2ProviderImpl()
ec2Provider.ec2 = &mockEc2Client{Reservations: prepareSingleInstanceOutput()}
go ec2Provider.StartEc2DescribeBatchProcessing()
dns_name, err := ec2Provider.GetPrivateDNSName("ec2-1")
if err != nil {
t.Error("There is an error which is not expected when calling ec2 API with setting up mocks")
}
if dns_name != "ec2-dns-1" {
t.Errorf("want: %v, got: %v", "ec2-dns-1", dns_name)
}
}
func prepareSingleInstanceOutput() []*ec2.Reservation {
reservations := []*ec2.Reservation{
{
Groups: nil,
Instances: []*ec2.Instance{
&ec2.Instance{
InstanceId: aws.String("ec2-1"),
PrivateDnsName: aws.String("ec2-dns-1"),
},
},
OwnerId: nil,
RequesterId: nil,
ReservationId: nil,
},
}
return reservations
}
func TestGetPrivateDNSNameWithBatching(t *testing.T) {
metrics.InitMetrics(prometheus.NewRegistry())
ec2Provider := newMockedEC2ProviderImpl()
reservations := prepare100InstanceOutput()
ec2Provider.ec2 = &mockEc2Client{Reservations: reservations}
go ec2Provider.StartEc2DescribeBatchProcessing()
var wg sync.WaitGroup
for i := 1; i < 101; i++ {
instanceString := "ec2-" + strconv.Itoa(i)
dnsString := "ec2-dns-" + strconv.Itoa(i)
wg.Add(1)
// This code helps test the batch functionality twice
if i == 50 {
time.Sleep(200 * time.Millisecond)
}
go getPrivateDNSName(ec2Provider, instanceString, dnsString, t, &wg)
}
wg.Wait()
}
func getPrivateDNSName(ec2provider *ec2ProviderImpl, instanceString string, dnsString string, t *testing.T, wg *sync.WaitGroup) {
defer wg.Done()
dnsName, err := ec2provider.GetPrivateDNSName(instanceString)
if err != nil {
t.Error("There is an error which is not expected when calling ec2 API with setting up mocks")
}
if dnsName != dnsString {
t.Errorf("want: %v, got: %v", dnsString, dnsName)
}
}
func prepare100InstanceOutput() []*ec2.Reservation {
var reservations []*ec2.Reservation
for i := 1; i < 101; i++ {
instanceString := "ec2-" + strconv.Itoa(i)
dnsString := "ec2-dns-" + strconv.Itoa(i)
instance := &ec2.Instance{
InstanceId: aws.String(instanceString),
PrivateDnsName: aws.String(dnsString),
}
var instances []*ec2.Instance
instances = append(instances, instance)
res1 := &ec2.Reservation{
Groups: nil,
Instances: instances,
OwnerId: nil,
RequesterId: nil,
ReservationId: nil,
}
reservations = append(reservations, res1)
}
return reservations
}