Skip to content

Commit 7d9e380

Browse files
authored
Add system tests for pubsub and storage GCF (#1584)
1 parent 8179bda commit 7d9e380

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2018 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_system_test]
16+
from datetime import datetime
17+
from os import getenv
18+
import subprocess
19+
import time
20+
import uuid
21+
22+
from google.cloud import pubsub_v1
23+
import pytest
24+
25+
PROJECT = getenv('GCLOUD_PROJECT')
26+
TOPIC = getenv('TOPIC')
27+
28+
assert PROJECT is not None
29+
assert TOPIC is not None
30+
31+
32+
@pytest.fixture(scope='module')
33+
def publisher_client():
34+
yield pubsub_v1.PublisherClient()
35+
36+
37+
def test_print_name(publisher_client):
38+
start_time = datetime.utcnow().isoformat()
39+
topic_path = publisher_client.topic_path(PROJECT, TOPIC)
40+
41+
# Publish the message
42+
name = uuid.uuid4()
43+
data = str(name).encode('utf-8')
44+
publisher_client.publish(topic_path, data=data).result()
45+
46+
# Wait for logs to become consistent
47+
time.sleep(15)
48+
49+
# Check logs after a delay
50+
log_process = subprocess.Popen([
51+
'gcloud',
52+
'alpha',
53+
'functions',
54+
'logs',
55+
'read',
56+
'hello_pubsub',
57+
'--start-time',
58+
start_time
59+
], stdout=subprocess.PIPE)
60+
logs = str(log_process.communicate()[0])
61+
print logs
62+
print start_time
63+
assert 'Hello, {}!'.format(name) in logs
64+
# [END functions_pubsub_system_test]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2018 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_system_test]
16+
from datetime import datetime
17+
from os import getenv, path
18+
import subprocess
19+
import time
20+
import uuid
21+
22+
from google.cloud import storage
23+
import pytest
24+
25+
PROJECT = getenv('GCLOUD_PROJECT')
26+
BUCKET = getenv('BUCKET')
27+
28+
assert PROJECT is not None
29+
assert BUCKET is not None
30+
31+
32+
@pytest.fixture(scope='module')
33+
def storage_client():
34+
yield storage.Client()
35+
36+
37+
@pytest.fixture(scope='module')
38+
def bucket_object(storage_client):
39+
bucket_object = storage_client.get_bucket(BUCKET)
40+
yield bucket_object
41+
42+
43+
@pytest.fixture(scope='module')
44+
def uploaded_file(bucket_object):
45+
name = 'test-{}.txt'.format(str(uuid.uuid4()))
46+
blob = bucket_object.blob(name)
47+
48+
test_dir = path.dirname(path.abspath(__file__))
49+
blob.upload_from_filename(path.join(test_dir, 'test.txt'))
50+
yield name
51+
blob.delete()
52+
53+
54+
def test_hello_gcs(uploaded_file):
55+
start_time = datetime.utcnow().isoformat()
56+
time.sleep(10) # Wait for logs to become consistent
57+
58+
log_process = subprocess.Popen([
59+
'gcloud',
60+
'alpha',
61+
'functions',
62+
'logs',
63+
'read',
64+
'hello_gcs',
65+
'--start-time',
66+
start_time
67+
], stdout=subprocess.PIPE)
68+
logs = str(log_process.communicate()[0])
69+
assert uploaded_file in logs
70+
# [END functions_storage_system_test]

0 commit comments

Comments
 (0)