forked from travis-ci/dpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.rb
192 lines (155 loc) · 5.41 KB
/
aws.rb
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
module Support
module Matchers
module Aws
class Base < Struct.new(:opts)
include Support::Helpers, RSpec::Matchers::BuiltIn,
RSpec::Mocks::Matchers, RSpec::Mocks::ArgumentMatchers
end
class HaveRequested < Base
def matches?(*)
!!request
end
def description
"have requested #{operation}"
end
def failure_message
msg = "Expected the operation #{operation.inspect} to be requested#{" with #{format_opts}" if opts.any?}, but it wasn't."
msg << "\n\nInstead the following requests were made:\n\n #{format_requests}" if requests.any?
msg
end
def failure_message_when_negated
"Expected the operation #{operation.inspect} to not be requested#{" with #{format_opts}" if opts.any?}, but it was."
end
def request
host, path, body, file, headers = opts.values_at(:host, :path, :body, :file, :headers)
headers ||= except(opts, :host, :path, :body, :file, :client, :operation)
headers = stringify(headers)
reqs = requests.select { |r| r[:operation] == operation }
reqs = reqs.select { |r| match?(r[:host], host) } if host
reqs = reqs.select { |r| r[:path] == path } if path
reqs = reqs.select { |r| match?(r[:body], body) } if body
reqs = reqs.select { |r| r[:file] == file } if file
reqs = reqs.select { |r| include?(r[:headers], headers) } if headers
reqs.any? and reqs[0][:request]
end
def requests
client.api_requests.map do |req|
compact(
operation: req[:operation_name],
host: req[:context].http_request.endpoint.host,
path: req[:context].http_request.endpoint.path,
body: body_from(req[:context].http_request.body),
file: req[:params][:body] && req[:params][:body].path,
headers: req[:context].http_request.headers,
request: req[:context].http_request
)
end
end
def body_from(obj)
case obj
when StringIO
obj.string
when ::Aws::Query::ParamList::IoWrapper
obj.read
end
end
def format_opts
except(opts, :client, :operation).map { |pair| pair.join('=') }.join(' ')
end
def format_requests
requests.map { |r| except(r, :request).map { |key, value| [key, value.inspect].join(': ') }.join("\n ") }.join("\n\n")
end
def match?(actual, expected)
case expected
when String then actual.include?(expected)
when Regexp then actual =~ expected
when Hash then include?(JSON.parse(actual), stringify(expected))
end
end
def include?(hash, other)
# Include.new(other).matches?(hash)
other.all? do |key, value|
case value
when RSpec::Mocks::ArgumentMatchers::InstanceOf
# just can't get this working in any other way ...
hash[key].is_a?(value.instance_variable_get(:@klass))
when Hash
include?(hash[key], value)
else
hash[key] == value
end
end
end
def client
opts[:client]
end
def operation
opts[:operation]
end
end
class CreateClient < Base
def matches?(*)
matcher.matches?(::Aws::S3::Client)
end
def description
matcher.description
end
def failure_message
matcher.failure_message
end
def failure_message_when_negated
matcher.failure_message_when_negated
end
def matcher
@matchers ||= HaveReceived.new(:new).with(hash_including(opts))
end
end
def create_client(opts)
CreateClient.new(opts)
end
def have_requested(operation, opts = {})
HaveRequested.new(opts.merge(client: client, operation: operation))
end
# elasticbeanstalk
def create_app_version(body = nil)
have_requested(:create_application_version, compact(body: body))
end
def update_environment
have_requested(:update_environment)
end
# lambda
def create_function(params)
have_requested(:create_function, body: params)
end
def update_function_config(params)
have_requested(:update_function_configuration, body: params)
end
def update_function_code(params)
have_requested(:update_function_code, body: params)
end
def tag_resource(params)
have_requested(:tag_resource, body: params)
end
# codedeploy and opsworks
def create_deployment(params)
have_requested(:create_deployment, body: params)
end
def get_deployment
have_requested(:get_deployment)
end
def describe_deployments(params)
have_requested(:describe_deployments, body: params)
end
def update_app(params)
have_requested(:update_app, body: params)
end
# s3
def put_object(file, opts = {})
have_requested(:put_object, opts.merge(file: file))
end
def put_bucket_website_suffix(suffix)
have_requested(:put_bucket_website, body: %r(<Suffix>#{suffix}</Suffix>))
end
end
end
end