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
/
s3_test.go
183 lines (155 loc) · 4.63 KB
/
s3_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
/******************************************************************************
Cloud Resource Counter
File: s3_test.go
Summary: The Unit Test for s3.
******************************************************************************/
package main
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/expel-io/cloud-resource-counter/mock"
)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake S3 Buckets
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This simulates the minimal response from an AWS call
var fakeS3BucketsSlice = &s3.ListBucketsOutput{
Buckets: []*s3.Bucket{
{
Name: aws.String("bucket1"),
},
{
Name: aws.String("bucket2"),
},
{
Name: aws.String("bucket3"),
},
{
Name: aws.String("bucket4"),
},
{
Name: aws.String("bucket5"),
},
{
Name: aws.String("bucket6"),
},
{
Name: aws.String("bucket7"),
},
{
Name: aws.String("bucket8"),
},
},
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake S3 Service
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// To use this struct, the caller must supply a ListBucketsOutput struct. If
// it is missing, it will trigger the mock function to simulate an error from
// the corresponding function.
type fakeS3Service struct {
s3iface.S3API
LBResponse *s3.ListBucketsOutput
}
func (fs3 *fakeS3Service) ListBuckets(input *s3.ListBucketsInput) (*s3.ListBucketsOutput, error) {
// If there was no supplied response, then simulate a possible error
if fs3.LBResponse == nil {
return nil, errors.New("ListBuckets returns an unexpected error: 2345")
}
return fs3.LBResponse, nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Service Factory
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This structure simulates the AWS Service Factory by storing some pregenerated
// responses (that would come from AWS).
type fakeS3ServiceFactory struct {
LBResponse *s3.ListBucketsOutput
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) Init() {}
// Don't implement
func (fsf fakeS3ServiceFactory) GetCurrentRegion() string {
return ""
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetAccountIDService() *AccountIDService {
return nil
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetEC2InstanceService(string) *EC2InstanceService {
return nil
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetRDSInstanceService(regionName string) *RDSInstanceService {
return nil
}
// Simply return our fake S3 Service
func (fsf fakeS3ServiceFactory) GetS3Service() *S3Service {
return &S3Service{
Client: &fakeS3Service{
LBResponse: fsf.LBResponse,
},
}
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetLambdaService(string) *LambdaService {
return nil
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetContainerService(string) *ContainerService {
return nil
}
// Don't need to implement
func (fsf fakeS3ServiceFactory) GetLightsailService(string) *LightsailService {
return nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Unit Test for S3Buckets
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func TestS3Buckets(t *testing.T) {
// Describe all of our test cases: 1 failure and 1 success
cases := []struct {
ExpectedCount int
ExpectError bool
}{
{
ExpectedCount: 8,
}, {
ExpectError: true,
},
}
// Loop through each test case
for _, c := range cases {
// Construct a ListBucketsOutput object based on whether
// we expect an error or not
lbResponse := fakeS3BucketsSlice
if c.ExpectError {
lbResponse = nil
}
// Create our fake service factory
sf := fakeS3ServiceFactory{
LBResponse: lbResponse,
}
// Create a mock activity monitor
mon := &mock.ActivityMonitorImpl{}
// Invoke our S3 Buckets function
actualCount := S3Buckets(sf, mon, false)
// 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: S3Buckets 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)
}
}
}