|
| 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