Skip to content

Commit ee130dc

Browse files
author
Ace Nassri
authored
functions: add missing integration test samples (GoogleCloudPlatform#4324)
* functions: add missing integration test samples * Fix lint * Fix lint, take 2
1 parent 3e55269 commit ee130dc

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
functions-framework==2.0.0
12
mock==4.0.2
23
pytest==5.4.3
4+
uuid==1.30
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 220 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_http_integration_test]
16+
import os
17+
import subprocess
18+
import uuid
19+
20+
import requests
21+
from requests.packages.urllib3.util.retry import Retry
22+
23+
24+
def test_args():
25+
name = str(uuid.uuid4())
26+
port = 8080 # Each functions framework instance needs a unique por
27+
28+
process = subprocess.Popen(
29+
[
30+
'functions-framework',
31+
'--target', 'hello_http',
32+
'--port', str(port)
33+
],
34+
cwd=os.path.dirname(__file__),
35+
stdout=subprocess.PIPE
36+
)
37+
38+
# Send HTTP request simulating Pub/Sub message
39+
# (GCF translates Pub/Sub messages to HTTP requests internally)
40+
BASE_URL = os.getenv('BASE_URL')
41+
42+
retry_policy = Retry(total=6, backoff_factor=1)
43+
retry_adapter = requests.adapters.HTTPAdapter(
44+
max_retries=retry_policy)
45+
46+
session = requests.Session()
47+
session.mount(BASE_URL, retry_adapter)
48+
49+
name = str(uuid.uuid4())
50+
res = requests.post(
51+
BASE_URL,
52+
json={'name': name}
53+
)
54+
assert res.text == 'Hello {}!'.format(name)
55+
56+
# Stop the functions framework process
57+
process.kill()
58+
process.wait()
59+
# [END functions_http_integration_test]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_pubsub_integration_test]
16+
import base64
17+
import os
18+
import subprocess
19+
import uuid
20+
21+
import requests
22+
from requests.packages.urllib3.util.retry import Retry
23+
24+
25+
def test_print_name():
26+
name = str(uuid.uuid4())
27+
port = 8088 # Each running framework instance needs a unique port
28+
29+
encoded_name = base64.b64encode(name.encode('utf-8')).decode('utf-8')
30+
pubsub_message = {
31+
'data': {'data': encoded_name}
32+
}
33+
34+
process = subprocess.Popen(
35+
[
36+
'functions-framework',
37+
'--target', 'hello_pubsub',
38+
'--signature-type', 'event',
39+
'--port', str(port)
40+
],
41+
cwd=os.path.dirname(__file__),
42+
stdout=subprocess.PIPE
43+
)
44+
45+
# Send HTTP request simulating Pub/Sub message
46+
# (GCF translates Pub/Sub messages to HTTP requests internally)
47+
url = f'http://localhost:{port}/'
48+
49+
retry_policy = Retry(total=6, backoff_factor=1)
50+
retry_adapter = requests.adapters.HTTPAdapter(
51+
max_retries=retry_policy)
52+
53+
session = requests.Session()
54+
session.mount(url, retry_adapter)
55+
56+
response = session.post(url, json=pubsub_message)
57+
58+
assert response.status_code == 200
59+
60+
# Stop the functions framework process
61+
process.kill()
62+
process.wait()
63+
out, err = process.communicate()
64+
65+
print(out, err, response.content)
66+
67+
assert f'Hello {name}!' in str(out)
68+
# [END functions_pubsub_integration_test]

0 commit comments

Comments
 (0)