This repository was archived by the owner on Mar 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector_test.go
216 lines (199 loc) · 5.39 KB
/
connector_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
package raws
import (
"context"
"errors"
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
)
type mockSTS struct {
stsiface.STSAPI
// Mocking of GetCallerIdentity
gcio *sts.GetCallerIdentityOutput
gcierr error
}
func (m mockSTS) GetCallerIdentityWithContext(_ aws.Context, _ *sts.GetCallerIdentityInput, _ ...request.Option) (*sts.GetCallerIdentityOutput, error) {
return m.gcio, m.gcierr
}
type mockEC2 struct {
ec2iface.EC2API
// Mocking of DescribeRegions
dro *ec2.DescribeRegionsOutput
drerr error
}
func (m mockEC2) DescribeRegionsWithContext(
_ aws.Context, _ *ec2.DescribeRegionsInput, _ ...request.Option,
) (*ec2.DescribeRegionsOutput, error) {
return m.dro, m.drerr
}
func TestConnector_setRegion(t *testing.T) {
var ec2Regions = &ec2.DescribeRegionsOutput{
Regions: []*ec2.Region{
{RegionName: aws.String("eu-west-1")},
{RegionName: aws.String("eu-west-2")},
{RegionName: aws.String("eu-central-1")},
{RegionName: aws.String("us-west-1")},
},
}
tests := []struct {
name string
mocked mockEC2
regionsInput []string
expectedRegions []string
expectedError error
}{
{name: "no regions given",
mocked: mockEC2{
dro: ec2Regions,
drerr: nil,
},
regionsInput: []string{},
expectedRegions: nil,
expectedError: errors.New("at least one region name is required"),
},
{name: "match all eu regions",
mocked: mockEC2{
dro: ec2Regions,
drerr: nil,
},
regionsInput: []string{"eu-*"},
expectedRegions: []string{"eu-west-1", "eu-west-2", "eu-central-1"},
expectedError: nil,
},
{name: "match all the regions",
mocked: mockEC2{
dro: ec2Regions,
drerr: nil,
},
regionsInput: []string{"*"},
expectedRegions: []string{"eu-west-1", "eu-west-2", "eu-central-1", "us-west-1"},
expectedError: nil,
},
{name: "match nothing",
mocked: mockEC2{
dro: ec2Regions,
drerr: nil,
},
regionsInput: []string{"matchnothing-*"},
expectedRegions: nil,
expectedError: errors.New("found 0 regions matching: [matchnothing-*]"),
},
{name: "error in describe regions",
mocked: mockEC2{
dro: nil,
drerr: errors.New("fail"),
},
regionsInput: []string{"matchnothing-*"},
expectedRegions: nil,
expectedError: errors.New("fail"),
}}
var ctx = context.Background()
for i, tt := range tests {
c := &connector{}
err := c.setRegions(ctx, tt.mocked, tt.regionsInput)
checkErrors(t, tt.name, i, err, tt.expectedError)
if !reflect.DeepEqual(c.regions, tt.expectedRegions) {
t.Errorf("%s [%d] - regions: received=%+v | expected=%+v",
tt.name, i, c.regions, tt.expectedRegions)
}
}
}
func TestConnector_setAccountID(t *testing.T) {
tests := []struct {
name string
mocked mockSTS
expectedID *string
expectedError error
}{
{name: "no error while getting identity",
mocked: mockSTS{
gcio: &sts.GetCallerIdentityOutput{
Account: aws.String("1"),
},
gcierr: nil,
},
expectedID: aws.String("1"),
expectedError: nil,
},
{name: "error while getting identity",
mocked: mockSTS{
gcio: nil,
gcierr: errors.New("fail"),
},
expectedID: nil,
expectedError: errors.New("fail"),
}}
var ctx = context.Background()
for i, tt := range tests {
c := &connector{}
err := c.setAccountID(ctx, tt.mocked)
checkErrors(t, tt.name, i, err, tt.expectedError)
if !reflect.DeepEqual(c.accountID, tt.expectedID) {
t.Errorf("%s [%d] - accountID: received=%+v | expected=%+v",
tt.name, i, c.accountID, tt.expectedID)
}
}
}
func TestConnector_setServices(t *testing.T) {
tests := []struct {
name string
configInput *aws.Config
credsInput *credentials.Credentials
regionsInput []string
expectedSvcs []*serviceConnector
}{
{name: "one region service - no config",
configInput: nil,
credsInput: nil,
regionsInput: []string{"eu-west-1"},
expectedSvcs: []*serviceConnector{
{region: "eu-west-1"},
},
},
{name: "one region service - with config",
configInput: &aws.Config{},
credsInput: &credentials.Credentials{},
regionsInput: []string{"eu-west-1"},
expectedSvcs: []*serviceConnector{
{region: "eu-west-1"},
},
},
{name: "multiple region service - no config",
configInput: nil,
regionsInput: []string{"eu-west-1", "eu-west-2", "eu-central-1"},
expectedSvcs: []*serviceConnector{
{region: "eu-west-1"},
{region: "eu-west-2"},
{region: "eu-central-1"},
},
},
{name: "multiple region service - with config",
configInput: &aws.Config{},
credsInput: &credentials.Credentials{},
regionsInput: []string{"eu-west-1", "eu-west-2", "eu-central-1"},
expectedSvcs: []*serviceConnector{
{region: "eu-west-1"},
{region: "eu-west-2"},
{region: "eu-central-1"},
},
}}
for i, tt := range tests {
c := &connector{
regions: tt.regionsInput,
creds: tt.credsInput,
}
c.setServices(tt.configInput)
for index, svc := range c.svcs {
if svc.region != tt.expectedSvcs[index].region {
t.Errorf("%s [%d] - services: received=%+v | expected=%+v",
tt.name, i, c.svcs, tt.expectedSvcs)
}
}
}
}