forked from GoogleCloudPlatform/functions-framework-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_cli.rb
285 lines (262 loc) · 7.6 KB
/
test_cli.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "helper"
require "net/http"
require "uri"
require "functions_framework/cli"
describe FunctionsFramework::CLI do
let(:http_source) { File.join __dir__, "function_definitions", "simple_http.rb" }
let(:event_source) { File.join __dir__, "function_definitions", "simple_event.rb" }
let(:http_return_source) { File.join __dir__, "function_definitions", "return_http.rb" }
let(:startup_source) { File.join __dir__, "function_definitions", "startup_block.rb" }
let(:retry_count) { 10 }
let(:retry_interval) { 0.5 }
let(:port) { "8066" }
let(:pidfile) { "server.pid" }
let(:timeout) { 10 }
def run_with_retry cli
server = cli.start_server
begin
last_error = nil
retry_count.times do
return yield
rescue ::SystemCallError => e
last_error = e
sleep retry_interval
end
raise last_error
ensure
server.stop.wait_until_stopped timeout: timeout
end
end
def run_in_timeout cli
::Timeout.timeout(timeout) { cli.run }
end
before do
@saved_registry = FunctionsFramework.global_registry
FunctionsFramework.global_registry = FunctionsFramework::Registry.new
@saved_level = FunctionsFramework.logger.level
end
after do
FunctionsFramework.global_registry = @saved_registry
FunctionsFramework.logger.level = @saved_level
ENV["FUNCTION_TARGET"] = nil
ENV["FUNCTION_SOURCE"] = nil
ENV["FUNCTION_SIGNATURE_TYPE"] = nil
end
it "prints the version when given the --version flag" do
args = [
"--version"
]
cli = FunctionsFramework::CLI.new.parse_args args
assert_output "#{FunctionsFramework::VERSION}\n" do
run_in_timeout cli
end
assert_nil cli.error_message
assert_equal 0, cli.exit_code
refute cli.error?
end
it "prints online help when given the --help flag" do
args = [
"--help"
]
cli = FunctionsFramework::CLI.new.parse_args args
assert_output(/^Usage:/) do
run_in_timeout cli
end
assert_nil cli.error_message
assert_equal 0, cli.exit_code
refute cli.error?
end
it "runs an http server" do
args = [
"--source", http_source,
"--target", "simple_http",
"--port", port,
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
response = run_with_retry cli do
Net::HTTP.get_response URI("http://127.0.0.1:#{port}/")
end
assert_equal "200", response.code
assert_equal "I received a request: GET http://127.0.0.1:#{port}/", response.body
end
it "runs an http server with a pidfile" do
args = [
"--source", http_source,
"--target", "simple_http",
"--port", port,
"--pidfile", pidfile,
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
_response = run_with_retry cli do
Net::HTTP.get_response URI("http://127.0.0.1:#{port}/")
end
assert_equal pidfile, cli.pidfile
end
it "runs an http server with a function that includes a return" do
args = [
"--source", http_return_source,
"--target", "return_http",
"--port", port,
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
response = run_with_retry cli do
Net::HTTP.get_response URI("http://127.0.0.1:#{port}/")
end
assert_equal "200", response.code
assert_equal "I received a GET request: http://127.0.0.1:#{port}/", response.body
end
it "succeeds the signature type check for an http server" do
args = [
"--source", http_source,
"--target", "simple_http",
"--port", port,
"--signature-type", "http",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
run_with_retry cli do
# Do nothing
end
end
it "fails the signature type check for an http server" do
args = [
"--source", http_source,
"--target", "simple_http",
"--port", port,
"--signature-type", "cloudevent",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
error = assert_raises RuntimeError do
run_with_retry cli do
# Do nothing
end
end
assert_match(/Function "simple_http" does not match type cloudevent/, error.message)
end
it "succeeds the signature type check for an event server" do
args = [
"--source", event_source,
"--target", "simple_event",
"--port", port,
"--signature-type", "cloudevent",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
run_with_retry cli do
# Do nothing
end
end
it "succeeds the signature type check for a legacy event server" do
args = [
"--source", event_source,
"--target", "simple_event",
"--port", port,
"--signature-type", "event",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
run_with_retry cli do
# Do nothing
end
end
it "fails the signature type check for an event server" do
args = [
"--source", event_source,
"--target", "simple_event",
"--port", port,
"--signature-type", "http",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
error = assert_raises RuntimeError do
run_with_retry cli do
# Do nothing
end
end
assert_match(/Function "simple_event" does not match type http/, error.message)
end
it "passes verification" do
args = [
"--verify",
"--source", http_source,
"--target", "simple_http",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
assert_output "OK\n" do
run_in_timeout cli
end
assert_nil cli.error_message
assert_equal 0, cli.exit_code
refute cli.error?
end
it "fails verification if the target specifies a nonexistent function" do
args = [
"--verify",
"--source", http_source,
"--target", "wrong_function_name",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
run_in_timeout cli
assert_equal 'Undefined function: "wrong_function_name"', cli.error_message
assert_equal 1, cli.exit_code
assert cli.error?
end
it "fails on an unrecognized flag" do
args = [
"--blahblahblah"
]
cli = FunctionsFramework::CLI.new.parse_args args
run_in_timeout cli
assert_match(/^invalid option: --blahblahblah/, cli.error_message)
assert_equal 2, cli.exit_code
assert cli.error?
end
it "runs a startup block" do
args = [
"--source", startup_source,
"--target", "simple_http",
"--port", port
]
cli = FunctionsFramework::CLI.new.parse_args args
response = nil
_out, err = capture_subprocess_io do
response = run_with_retry cli do
Net::HTTP.get_response URI("http://127.0.0.1:#{port}/")
end
end
assert_match(/in startup block/, err)
assert_equal "200", response.code
assert_equal "OK", response.body
end
it "does not run startup block in verify mode" do
args = [
"--verify",
"--source", startup_source,
"--target", "simple_http",
"-q"
]
cli = FunctionsFramework::CLI.new.parse_args args
assert_output "OK\n" do
run_in_timeout cli
end
end
end