Skip to content

Commit 688d20b

Browse files
author
Ace Nassri
authored
functions: consolidate redundant GCS samples (#4305)
* functions: remove redundant GCS sample * Fix typo: data -> event * Add missing integration test * Data -> event fix, take 2 * Lint-fix attempt * Fix lint * Address comments: data -> event * Test-fix: restore missing requirements-test.txt
1 parent bb89768 commit 688d20b

10 files changed

+110
-95
lines changed

functions/gcs/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

functions/gcs/main.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

functions/gcs/main_test.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

functions/gcs/requirements-test.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

functions/gcs/requirements.txt

Whitespace-only changes.

functions/helloworld/main.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,25 @@ def hello_pubsub(event, context):
8888
# [START functions_helloworld_storage]
8989
def hello_gcs(event, context):
9090
"""Background Cloud Function to be triggered by Cloud Storage.
91+
This generic function logs relevant data when a file is changed.
92+
9193
Args:
92-
event (dict): The dictionary with data specific to this type of event.
93-
context (google.cloud.functions.Context): The Cloud Functions
94-
event metadata.
94+
event (dict): The dictionary with data specific to this type of event.
95+
The `data` field contains a description of the event in
96+
the Cloud Storage `object` format described here:
97+
https://cloud.google.com/storage/docs/json_api/v1/objects#resource
98+
context (google.cloud.functions.Context): Metadata of triggering event.
99+
Returns:
100+
None; the output is written to Stackdriver Logging
95101
"""
96-
print("File: {}.".format(event['objectId']))
102+
103+
print('Event ID: {}'.format(context.event_id))
104+
print('Event type: {}'.format(context.event_type))
105+
print('Bucket: {}'.format(event['bucket']))
106+
print('File: {}'.format(event['name']))
107+
print('Metageneration: {}'.format(event['metageneration']))
108+
print('Created: {}'.format(event['timeCreated']))
109+
print('Updated: {}'.format(event['updated']))
97110
# [END functions_helloworld_storage]
98111

99112

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
functions-framework==2.0.0
2-
mock==4.0.2
3-
pytest==6.0.1
1+
functions-framework==2.0.0
2+
mock==4.0.2
3+
pytest==6.0.1
44
uuid==1.30

functions/helloworld/sample_storage_test.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@
1313
# limitations under the License.
1414

1515
# [START functions_storage_unit_test]
16+
import mock
17+
1618
import main
1719

1820

1921
def test_print(capsys):
2022
name = 'test'
21-
data = {'objectId': name}
23+
event = {
24+
'bucket': 'some-bucket',
25+
'name': name,
26+
'metageneration': 'some-metageneration',
27+
'timeCreated': '0',
28+
'updated': '0'
29+
}
30+
31+
context = mock.MagicMock()
32+
context.event_id = 'some-id'
33+
context.event_type = 'gcs-event'
2234

2335
# Call tested function
24-
main.hello_gcs(data, None)
36+
main.hello_gcs(event, context)
2537
out, err = capsys.readouterr()
26-
assert out == 'File: {}.\n'.format(name)
38+
assert 'File: {}\n'.format(name) in out
2739
# [END functions_storage_unit_test]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_storage_integration_test]
16+
import datetime
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+
filename = str(uuid.uuid4())
27+
port = 8089 # Each running framework instance needs a unique port
28+
29+
example_timestamp = datetime.datetime.now().isoformat()
30+
storage_message = {
31+
'data': {
32+
'name': filename,
33+
'bucket': 'my_bucket',
34+
'metageneration': '1',
35+
'timeCreated': example_timestamp,
36+
'updated': example_timestamp
37+
}
38+
}
39+
40+
process = subprocess.Popen(
41+
[
42+
'functions-framework',
43+
'--target', 'hello_gcs',
44+
'--signature-type', 'event',
45+
'--port', str(port)
46+
],
47+
cwd=os.path.dirname(__file__),
48+
stdout=subprocess.PIPE
49+
)
50+
51+
# Send HTTP request simulating Pub/Sub message
52+
# (GCF translates Pub/Sub messages to HTTP requests internally)
53+
url = f'http://localhost:{port}/'
54+
55+
retry_policy = Retry(total=6, backoff_factor=1)
56+
retry_adapter = requests.adapters.HTTPAdapter(
57+
max_retries=retry_policy)
58+
59+
session = requests.Session()
60+
session.mount(url, retry_adapter)
61+
62+
response = session.post(url, json=storage_message)
63+
64+
assert response.status_code == 200
65+
66+
# Stop the functions framework process
67+
process.kill()
68+
process.wait()
69+
out, err = process.communicate()
70+
71+
print(out, err, response.content)
72+
73+
assert f'File: {filename}' in str(out)
74+
# [END functions_storage_integration_test]

functions/helloworld/sample_storage_test_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_hello_gcs(uploaded_file):
6161
'functions',
6262
'logs',
6363
'read',
64-
'hello_gcs',
64+
'hello_gcs_generic',
6565
'--start-time',
6666
start_time
6767
], stdout=subprocess.PIPE)

0 commit comments

Comments
 (0)