This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rds_test.go
266 lines (233 loc) · 7.63 KB
/
rds_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
/******************************************************************************
Cloud Resource Counter
File: rds_test.go
Summary: The Unit Test for rds.
******************************************************************************/
package main
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
"github.com/expel-io/cloud-resource-counter/mock"
)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake RDS Instance Data
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is our map of regions and the instances in each
var rdsInstancesPerRegion = map[string][]*rds.DescribeDBInstancesOutput{
// US-EAST-1 illustrates a case where DescribeDBInstancesPages returns 1
// page of NO results
"us-east-1": []*rds.DescribeDBInstancesOutput{
&rds.DescribeDBInstancesOutput{},
},
// US-EAST-2 illustrates a case where DescribeDBInstancesPages returns two pages of results.
// First page: 5 instances, 3 are available
// Second page: 4 instances, 2 are available
"us-east-2": []*rds.DescribeDBInstancesOutput{
&rds.DescribeDBInstancesOutput{
DBInstances: []*rds.DBInstance{
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("backing-up"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("creating"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
},
},
&rds.DescribeDBInstancesOutput{
DBInstances: []*rds.DBInstance{
&rds.DBInstance{
DBInstanceStatus: aws.String("stopped"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("stopping"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
},
},
},
// AF-SOUTH-1 is an "opted in" region (Cape Town, Africa). We are going to
// simply indicate that 3 instance exists here (only 1 running).
"af-south-1": []*rds.DescribeDBInstancesOutput{
&rds.DescribeDBInstancesOutput{
DBInstances: []*rds.DBInstance{
&rds.DBInstance{
DBInstanceStatus: aws.String("upgrading"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("available"),
},
&rds.DBInstance{
DBInstanceStatus: aws.String("backtracking"),
},
},
},
},
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake RDS Service
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// To use this struct, the caller must supply a DescribeDBInstances slice. If
// it is missing, it will trigger the mock function to simulate an error from
// the corresponding function.
type fakeRDSService struct {
rdsiface.RDSAPI
DDBIResponse []*rds.DescribeDBInstancesOutput
}
// Simulate the DescribeDBInstancesPages function
func (fake *fakeRDSService) DescribeDBInstancesPages(input *rds.DescribeDBInstancesInput, fn func(*rds.DescribeDBInstancesOutput, bool) bool) error {
// If the supplied response is nil, then simulate an error
if fake.DDBIResponse == nil {
return errors.New("DescribeDBInstancesPages encountered an unexpected error: 1234")
}
// Apply filtering to the supplied response
// NOTE: I have not implemented this feature as our code does not require it.
// To prevent unexpected cases, if the caller supplies an input other then
// the "zero" input, the unit test fails.
if input.DBInstanceIdentifier != nil || input.Filters != nil {
return errors.New("The unit test does not support a DescribeDBInstancesInput other than 'zero' (no parameters)")
}
// Loop through the slice of responses, invoking the supplied function
for index, output := range fake.DDBIResponse {
// Are we looking at the last "page" of our output?
lastPage := index == len(fake.DDBIResponse)-1
// Invoke our fn
cont := fn(output, lastPage)
// Shall we exit our loop?
if !cont {
break
}
}
return nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Service Factory
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This structure simulates the AWS Service Factory by storing some pregenerated
// responses (that would come from AWS).
type fakeRDSServiceFactory struct {
RegionName string
DRResponse *ec2.DescribeRegionsOutput
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) Init() {}
// Return our current region
func (fsf fakeRDSServiceFactory) GetCurrentRegion() string {
return fsf.RegionName
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) GetAccountIDService() *AccountIDService {
return nil
}
// This implementation of GetEC2InstanceService is limited to supporting DescribeRegions API
// only.
func (fsf fakeRDSServiceFactory) GetEC2InstanceService(string) *EC2InstanceService {
return &EC2InstanceService{
Client: &fakeEC2Service{
DRResponse: fsf.DRResponse,
},
}
}
// Implement a way to return a RDSInstanceService which is associated with the supplied
// region.
func (fsf fakeRDSServiceFactory) GetRDSInstanceService(regionName string) *RDSInstanceService {
// If the caller failed to specify a region, then use what is associated with our factory
var resolvedRegionName string
if regionName == "" {
resolvedRegionName = fsf.RegionName
} else {
resolvedRegionName = regionName
}
return &RDSInstanceService{
Client: &fakeRDSService{
DDBIResponse: rdsInstancesPerRegion[resolvedRegionName],
},
}
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) GetS3Service() *S3Service {
return nil
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) GetLambdaService(string) *LambdaService {
return nil
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) GetContainerService(string) *ContainerService {
return nil
}
// Don't need to implement
func (fsf fakeRDSServiceFactory) GetLightsailService(string) *LightsailService {
return nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Unit Test for RDSInstances
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func TestRDSInstances(t *testing.T) {
// Describe all of our test cases: 1 failure and 4 success cases
cases := []struct {
RegionName string
AllRegions bool
ExpectedCount int
ExpectError bool
}{
{
RegionName: "us-east-1",
ExpectedCount: 0,
}, {
RegionName: "us-east-2",
ExpectedCount: 5,
}, {
RegionName: "af-south-1",
ExpectedCount: 1,
}, {
RegionName: "undefined-region",
ExpectError: true,
}, {
AllRegions: true,
ExpectedCount: 6,
},
}
// Loop through each test case
for _, c := range cases {
// Create our fake service factory
sf := fakeRDSServiceFactory{
RegionName: c.RegionName,
DRResponse: ec2Regions,
}
// Create a mock activity monitor
mon := &mock.ActivityMonitorImpl{}
// Invoke our RDS Counter function
actualCount := RDSInstances(sf, mon, c.AllRegions)
// Did we expect an error?
if c.ExpectError {
// Did it fail to arrive?
if !mon.ErrorOccured {
t.Error("Expected an error to occur, but it did not... :^(")
}
} else if mon.ErrorOccured {
t.Errorf("Unexpected error occurred: %s", mon.ErrorMessage)
} else if actualCount != c.ExpectedCount {
t.Errorf("Error: RDSInstances returned %d; expected %d", actualCount, c.ExpectedCount)
} else if mon.ProgramExited {
t.Errorf("Unexpected Exit: The program unexpected exited with status code=%d", mon.ExitCode)
}
}
}