Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions functions/helloworld/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
functions-framework==2.0.0
mock==4.0.2
pytest==5.4.3
uuid==1.30
58 changes: 58 additions & 0 deletions functions/helloworld/sample_http_test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 220 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
#
# http://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.

# [START functions_http_integration_test]
import os
import requests
from requests.packages.urllib3.util.retry import Retry
import subprocess
import uuid


def test_args():
name = str(uuid.uuid4())
port = 8080 # Each functions framework instance needs a unique por

process = subprocess.Popen(
[
'functions-framework',
'--target', 'hello_http',
'--port', str(port)
],
cwd=os.path.dirname(__file__),
stdout=subprocess.PIPE
)

# Send HTTP request simulating Pub/Sub message
# (GCF translates Pub/Sub messages to HTTP requests internally)
BASE_URL = os.getenv('BASE_URL')

retry_policy = Retry(total=6, backoff_factor=1)
retry_adapter = requests.adapters.HTTPAdapter(
max_retries=retry_policy)

session = requests.Session()
session.mount(BASE_URL, retry_adapter)

name = str(uuid.uuid4())
res = requests.post(
BASE_URL,
json={'name': name}
)
assert res.text == 'Hello {}!'.format(name)

# Stop the functions framework process
process.kill()
process.wait()
# [END functions_http_integration_test]
68 changes: 68 additions & 0 deletions functions/helloworld/sample_pubsub_test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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
#
# http://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.

# [START functions_pubsub_integration_test]
import base64
import os
import requests
import json
from requests.packages.urllib3.util.retry import Retry
import subprocess
import uuid


def test_print_name():
name = str(uuid.uuid4())
port = 8088 # Each running framework instance needs a unique port

encoded_name = base64.b64encode(name.encode('utf-8')).decode('utf-8')
pubsub_message = {
'data': {'data': encoded_name}
}

process = subprocess.Popen(
[
'functions-framework',
'--target', 'hello_pubsub',
'--signature-type', 'event',
'--port', str(port)
],
cwd=os.path.dirname(__file__),
stdout=subprocess.PIPE
)

# Send HTTP request simulating Pub/Sub message
# (GCF translates Pub/Sub messages to HTTP requests internally)
url = f'http://localhost:{port}/'

retry_policy = Retry(total=6, backoff_factor=1)
retry_adapter = requests.adapters.HTTPAdapter(
max_retries=retry_policy)

session = requests.Session()
session.mount(url, retry_adapter)

response = session.post(url, json=pubsub_message)

assert response.status_code == 200

# Stop the functions framework process
process.kill()
process.wait()
out, err = process.communicate()

print(out, err, response.content)

assert f'Hello {name}!' in str(out)
# [END functions_pubsub_integration_test]