-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mock_provider.go
243 lines (193 loc) · 7.25 KB
/
mock_provider.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
package mockprovider
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"github.com/aws/aws-sdk-go/service/cloudtrail/cloudtrailiface"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/aws/aws-sdk-go/service/elb/elbiface"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
//"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
//"github.com/aws/aws-sdk-go/awstesting/unit"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/eks/mocks"
)
// ProviderConfig holds current global config
var ProviderConfig = &api.ProviderConfig{
Region: api.DefaultRegion,
Profile: "default",
WaitTimeout: 1200000000000,
}
type MockAWSClient struct {
*client.Client
}
type MockInput struct{}
type MockOutput struct {
States []*MockState
}
type MockState struct {
State *string
}
// MockProvider stores the mocked APIs
type MockProvider struct {
Client *MockAWSClient
region string
cfnRoleARN string
asg *mocks.AutoScalingAPI
cfn *mocks.CloudFormationAPI
eks *mocks.EKSAPI
ec2 *mocks.EC2API
elb *mocks.ELBAPI
elbv2 *mocks.ELBV2API
sts *mocks.STSAPI
ssm *mocks.SSMAPI
iam *mocks.IAMAPI
cloudtrail *mocks.CloudTrailAPI
cloudwatchlogs *mocks.CloudWatchLogsAPI
configProvider *mocks.ConfigProvider
}
// NewMockProvider returns a new MockProvider
func NewMockProvider() *MockProvider {
return &MockProvider{
Client: NewMockAWSClient(),
asg: &mocks.AutoScalingAPI{},
cfn: &mocks.CloudFormationAPI{},
eks: &mocks.EKSAPI{},
ec2: &mocks.EC2API{},
elb: &mocks.ELBAPI{},
elbv2: &mocks.ELBV2API{},
sts: &mocks.STSAPI{},
ssm: &mocks.SSMAPI{},
iam: &mocks.IAMAPI{},
cloudtrail: &mocks.CloudTrailAPI{},
cloudwatchlogs: &mocks.CloudWatchLogsAPI{},
configProvider: &mocks.ConfigProvider{},
}
}
// CloudFormation returns a representation of the CloudFormation API
func (m MockProvider) CloudFormation() cloudformationiface.CloudFormationAPI { return m.cfn }
// CloudFormationRoleARN returns, if any, a service role used by CloudFormation to call AWS API on your behalf
func (m MockProvider) CloudFormationRoleARN() string { return m.cfnRoleARN }
// CloudFormationDisableRollback returns whether stacks should not rollback on failure
func (m MockProvider) CloudFormationDisableRollback() bool {
return false
}
// MockCloudFormation returns a mocked CloudFormation API
func (m MockProvider) MockCloudFormation() *mocks.CloudFormationAPI {
return m.CloudFormation().(*mocks.CloudFormationAPI)
}
// ASG returns a representation of the ASG API
func (m MockProvider) ASG() autoscalingiface.AutoScalingAPI { return m.asg }
// MockASG returns a mocked ASG API
func (m MockProvider) MockASG() *mocks.AutoScalingAPI { return m.ASG().(*mocks.AutoScalingAPI) }
// EKS returns a representation of the EKS API
func (m MockProvider) EKS() eksiface.EKSAPI { return m.eks }
// MockEKS returns a mocked EKS API
func (m MockProvider) MockEKS() *mocks.EKSAPI { return m.EKS().(*mocks.EKSAPI) }
// EC2 returns a representation of the EC2 API
func (m MockProvider) EC2() ec2iface.EC2API { return m.ec2 }
// ELB returns a representation of the ELB API
func (m MockProvider) ELB() elbiface.ELBAPI { return m.elb }
// ELBV2 returns a representation of the ELBV2 API
func (m MockProvider) ELBV2() elbv2iface.ELBV2API { return m.elbv2 }
// MockEC2 returns a mocked EC2 API
func (m MockProvider) MockEC2() *mocks.EC2API { return m.EC2().(*mocks.EC2API) }
// STS returns a representation of the STS API
func (m MockProvider) STS() stsiface.STSAPI { return m.sts }
// MockSTS returns a mocked STS API
func (m MockProvider) MockSTS() *mocks.STSAPI { return m.STS().(*mocks.STSAPI) }
// SSM returns a representation of the SSM API
func (m MockProvider) SSM() ssmiface.SSMAPI { return m.ssm }
// MockSSM returns a mocked SSM API
func (m MockProvider) MockSSM() *mocks.SSMAPI { return m.SSM().(*mocks.SSMAPI) }
// IAM returns a representation of the IAM API
func (m MockProvider) IAM() iamiface.IAMAPI { return m.iam }
// MockIAM returns a mocked IAM API
func (m MockProvider) MockIAM() *mocks.IAMAPI { return m.IAM().(*mocks.IAMAPI) }
// CloudTrail returns a representation of the CloudTrail API
func (m MockProvider) CloudTrail() cloudtrailiface.CloudTrailAPI { return m.cloudtrail }
// MockCloudTrail returns a mocked CloudTrail API
func (m MockProvider) MockCloudTrail() *mocks.CloudTrailAPI {
return m.CloudTrail().(*mocks.CloudTrailAPI)
}
// CloudWatchLogs returns a representation of the CloudWatchLogs API
func (m MockProvider) CloudWatchLogs() cloudwatchlogsiface.CloudWatchLogsAPI { return m.cloudwatchlogs }
// MockCloudWatchLogs returns a mocked CloudWatchLogs API
func (m MockProvider) MockCloudWatchLogs() *mocks.CloudWatchLogsAPI {
return m.CloudWatchLogs().(*mocks.CloudWatchLogsAPI)
}
// Profile returns current profile setting
func (m MockProvider) Profile() string { return ProviderConfig.Profile }
// Region returns current region setting
func (m MockProvider) Region() string {
if m.region != "" {
return m.region
}
return ProviderConfig.Region
}
// SetRegion can be used to set the region of the provider
func (m *MockProvider) SetRegion(r string) {
m.region = r
}
// WaitTimeout returns current timeout setting
func (m MockProvider) WaitTimeout() time.Duration { return ProviderConfig.WaitTimeout }
// ConfigProvider returns a representation of the ConfigProvider
func (m MockProvider) ConfigProvider() client.ConfigProvider {
return m.configProvider
}
// MockConfigProvider returns a mocked ConfigProvider
func (m MockProvider) MockConfigProvider() client.ConfigProvider {
return m.configProvider
}
func (m MockProvider) Session() *session.Session {
panic("not implemented")
}
func NewMockAWSClient() *MockAWSClient {
m := &MockAWSClient{
Client: awstesting.NewClient(&aws.Config{
Region: &ProviderConfig.Region,
}),
}
m.Handlers.Send.Clear()
m.Handlers.Unmarshal.Clear()
m.Handlers.UnmarshalMeta.Clear()
m.Handlers.ValidateResponse.Clear()
return m
}
func (m *MockAWSClient) MockRequestForMockOutput(input *MockInput) (*request.Request, *MockOutput) {
op := &request.Operation{
Name: "Mock",
HTTPMethod: "POST",
HTTPPath: "/",
}
if input == nil {
input = &MockInput{}
}
output := &MockOutput{}
req := m.NewRequest(op, input, output)
req.Data = output
return req, output
}
func (m *MockAWSClient) MockRequestForGivenOutput(input, output interface{}) *request.Request {
op := &request.Operation{
Name: "Mock",
HTTPMethod: "POST",
HTTPPath: "/",
}
if input == nil {
input = &MockInput{}
}
req := m.NewRequest(op, input, output)
req.Data = output
return req
}