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
/
lightsail_test.go
251 lines (219 loc) · 7.24 KB
/
lightsail_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
/******************************************************************************
Cloud Resource Counter
File: lightsail_test.go
Summary: The Unit Test for lightsail.
******************************************************************************/
package main
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/aws/aws-sdk-go/service/lightsail/lightsailiface"
"github.com/expel-io/cloud-resource-counter/mock"
)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Lightsail Data
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is our list of accessible regions for the purpose of unit testing.
var lightsailRegions *lightsail.GetRegionsOutput = &lightsail.GetRegionsOutput{
Regions: []*lightsail.Region{
&lightsail.Region{
Name: aws.String("us-east-1"),
},
&lightsail.Region{
Name: aws.String("us-east-2"),
},
&lightsail.Region{
Name: aws.String("eu-west-1"),
},
},
}
// This is our list of lightsail instances per region
var lightsailInstancesPerRegion = map[string]*lightsail.GetInstancesOutput{
// US-EAST-1 simulates a region where there are three Lightsail instances:
// one is Wordpress, one is Magento (but it is stopped) and the other is Node.js.
"us-east-1": &lightsail.GetInstancesOutput{
Instances: []*lightsail.Instance{
&lightsail.Instance{
Name: aws.String("WordPress-1"),
State: &lightsail.InstanceState{
Name: aws.String("running"),
},
},
&lightsail.Instance{
Name: aws.String("Magento-1"),
State: &lightsail.InstanceState{
Name: aws.String("pending"),
},
},
&lightsail.Instance{
Name: aws.String("Node-js-1"),
State: &lightsail.InstanceState{
Name: aws.String("running"),
},
},
},
},
// US-EAST-2 has no instances...
"us-east-2": &lightsail.GetInstancesOutput{},
// EU-WEST-1 has 2 instances (only 1 running)
"eu-west-1": &lightsail.GetInstancesOutput{
Instances: []*lightsail.Instance{
&lightsail.Instance{
Name: aws.String("WordPress-1"),
State: &lightsail.InstanceState{
Name: aws.String("running"),
},
},
&lightsail.Instance{
Name: aws.String("Magento-1"),
State: &lightsail.InstanceState{
Name: aws.String("stopped"),
},
},
},
},
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Lightsail Service
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is our fake Lightsail Service that implements the AWS API for Lightsail
type fakeLightsailService struct {
lightsailiface.LightsailAPI
GRResponse *lightsail.GetRegionsOutput
GIOResponse *lightsail.GetInstancesOutput
}
// GetRegions fakes the standard Lightsail API of the same name.
func (fake *fakeLightsailService) GetRegions(input *lightsail.GetRegionsInput) (*lightsail.GetRegionsOutput, error) {
// If the pre-canned regions response is nil, then simulate the API returning an error
if fake.GRResponse == nil {
return nil, errors.New("GetRegions encountered an unexpected error: 7531")
}
return fake.GRResponse, nil
}
// GetInstances fakes the standard Lightsail API of the same name.
func (fake *fakeLightsailService) GetInstances(input *lightsail.GetInstancesInput) (*lightsail.GetInstancesOutput, error) {
// If the supplied response is nil, then simulate an error
if fake.GIOResponse == nil {
return nil, errors.New("GetInstance encountered an unexpected error: 02468")
}
return fake.GIOResponse, nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Lightsail Service Factory
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is our fake Service Factory that implements a way to get a LightsailService.
type fakeLightsailServiceFactory struct {
RegionName string
GRResponse *lightsail.GetRegionsOutput
}
// Return our current region
func (fsf fakeLightsailServiceFactory) GetCurrentRegion() string {
return fsf.RegionName
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) Init() {}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetAccountIDService() *AccountIDService {
return nil
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetEC2InstanceService(string) *EC2InstanceService {
return nil
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetRDSInstanceService(regionName string) *RDSInstanceService {
return nil
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetS3Service() *S3Service {
return nil
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetLambdaService(string) *LambdaService {
return nil
}
// Don't need to implement
func (fsf fakeLightsailServiceFactory) GetContainerService(string) *ContainerService {
return nil
}
// Implement a way to return Lightsail regions and instances found in each
func (fsf fakeLightsailServiceFactory) GetLightsailService(regionName string) *LightsailService {
// 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 &LightsailService{
Client: &fakeLightsailService{
GRResponse: fsf.GRResponse,
GIOResponse: lightsailInstancesPerRegion[resolvedRegionName],
},
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Unit Test for LightsailInstances
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func TestLightsailInstances(t *testing.T) {
// Describe all of our test cases: 2 failures and 4 success cases
cases := []struct {
RegionName string
AllRegions bool
GRResponse *lightsail.GetRegionsOutput
ExpectedCount int
ExpectError bool
}{
{
RegionName: "us-east-1",
GRResponse: lightsailRegions,
ExpectedCount: 2,
}, {
RegionName: "us-east-2",
GRResponse: lightsailRegions,
ExpectedCount: 0,
}, {
RegionName: "eu-west-1",
GRResponse: lightsailRegions,
ExpectedCount: 1,
}, {
RegionName: "undefined-region",
GRResponse: lightsailRegions,
ExpectedCount: 0,
}, {
AllRegions: true,
GRResponse: lightsailRegions,
ExpectedCount: 3,
}, {
AllRegions: true,
ExpectError: true,
},
}
// Loop through each test case
for _, c := range cases {
// Create our fake service factory
sf := fakeLightsailServiceFactory{
RegionName: c.RegionName,
GRResponse: c.GRResponse,
}
// Create a mock activity monitor
mon := &mock.ActivityMonitorImpl{}
// Invoke our LightsailInstances function
actualCount := LightsailInstances(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: LightsailInstances 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)
}
}
}