Skip to content

Commit 869e56e

Browse files
author
Takashi Matsuo
authored
testing: various cleanups (#3877)
* testing: various cleanups * [iap]: only run iap tests on Kokoro * [vision/automl]: use temporary directory for temporary files * [appengine/flexible/scipy]: use temporary directory * [bigtable/snippets/reads]: update pytest snapshot * [texttospeech/cloud-client]: added output.mp3 to .gitignore * [iot/api-client/gcs_file_to_device]: use temporary directory * [iot/api-client/mqtt_example]: use temporary directory * [logging/cloud-client]: use uuid and add backoff * use project directory with Trampoline V1
1 parent 572ea21 commit 869e56e

File tree

11 files changed

+56
-43
lines changed

11 files changed

+56
-43
lines changed
-8.44 KB
Binary file not shown.
-8.44 KB
Binary file not shown.

appengine/flexible/scipy/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717

1818
from flask import Flask
19+
from flask import request
1920
import imageio
2021
from PIL import Image
2122

@@ -30,8 +31,7 @@ def resize():
3031
img = Image.fromarray(imageio.imread(image_path))
3132
img_tinted = img.resize((300, 300))
3233

33-
output_image_path = os.path.join(
34-
app_path, 'assets/resized_google_logo.jpg')
34+
output_image_path = request.args.get('output_image_path')
3535
# Write the tinted image back to disk
3636
imageio.imwrite(output_image_path, img_tinted)
3737
return "Image resized."

appengine/flexible/scipy/main_test.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,18 @@
1313
# limitations under the License.
1414

1515
import os
16+
import tempfile
1617

1718
import main
1819

1920

2021
def test_index():
2122
main.app.testing = True
2223
client = main.app.test_client()
23-
test_path = os.path.dirname(os.path.realpath(__file__))
24-
asset_path = os.path.join(
25-
test_path, 'assets/resized_google_logo.jpg')
26-
fixtured_path = os.path.join(
27-
test_path, 'fixtures/assets/resized_google_logo.jpg')
28-
try:
29-
os.remove(asset_path)
30-
except OSError:
31-
pass # if doesn't exist
32-
r = client.get('/')
24+
with tempfile.TemporaryDirectory() as test_dir:
25+
output_image_path = os.path.join(test_dir, 'resized_google_logo.jpg')
26+
r = client.get(
27+
'/', query_string={'output_image_path': output_image_path})
3328

34-
assert os.path.isfile(fixtured_path)
35-
assert r.status_code == 200
29+
assert os.path.isfile(output_image_path)
30+
assert r.status_code == 200

bigtable/snippets/reads/snapshots/snap_reads_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77

88
snapshots = Snapshot()
99

10-
snapshots['test_read_simple 1'] = '''Reading data for phone#4c410523#20190501:
11-
Column Family stats_summary
12-
\tconnected_cell: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
13-
\tconnected_wifi: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
14-
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
15-
16-
'''
17-
1810
snapshots['test_read_row_partial 1'] = '''Reading data for phone#4c410523#20190501:
1911
Column Family stats_summary
2012
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
@@ -140,3 +132,11 @@
140132
\tos_build: PQ2A.190406.000 @2019-05-01 00:00:00+00:00
141133
142134
'''
135+
136+
snapshots['test_read_row 1'] = '''Reading data for phone#4c410523#20190501:
137+
Column Family stats_summary
138+
\tconnected_cell: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
139+
\tconnected_wifi: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
140+
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
141+
142+
'''

iap/iap_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Test script for Identity-Aware Proxy code samples."""
1616

17+
import os
18+
1719
import pytest
1820

1921
import make_iap_request
@@ -36,6 +38,10 @@
3638

3739
@pytest.mark.flaky
3840
def test_main(capsys):
41+
# It only passes on Kokoro now. Skipping in other places.
42+
# The envvar `TRAMPOLINE_CI` will be set once #3860 is merged.
43+
if os.environ.get('TRAMPOLINE_CI', 'kokoro') != 'kokoro':
44+
pytest.skip('Only passing on Kokoro.')
3945
# JWTs are obtained by IAP-protected applications whenever an
4046
# end-user makes a request. We've set up an app that echoes back
4147
# the JWT in order to expose it to this test. Thus, this test

iot/api-client/gcs_file_to_device/gcs_example_mqtt_device_test.py

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

1515
import os
16+
import tempfile
1617

1718
from google.cloud import storage
1819

@@ -22,7 +23,6 @@
2223

2324
gcs_bucket = os.environ['CLOUD_STORAGE_BUCKET']
2425
cloud_region = 'us-central1'
25-
destination_file_name = 'destination-file.bin'
2626
project_id = os.environ['GCLOUD_PROJECT']
2727

2828

@@ -41,8 +41,10 @@ def test_blob():
4141

4242

4343
def test_download_blob(test_blob, capsys):
44-
device.download_blob(gcs_bucket, test_blob.name, destination_file_name)
44+
with tempfile.TemporaryDirectory() as tmp_dir:
45+
destination_file_name = os.path.join(tmp_dir, 'destination-file.bin')
46+
device.download_blob(gcs_bucket, test_blob.name, destination_file_name)
4547

46-
out, _ = capsys.readouterr()
47-
assert 'Config {} downloaded to {}.'.format(
48-
test_blob.name, destination_file_name) in out
48+
out, _ = capsys.readouterr()
49+
assert 'Config {} downloaded to {}.'.format(
50+
test_blob.name, destination_file_name) in out

iot/api-client/mqtt_example/cloudiot_mqtt_image_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
import os
1616
import sys
17+
import tempfile
1718

1819
# Add manager as library
1920
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
@@ -63,8 +64,9 @@ def test_image_recv(
6364
cloud_region, test_registry_id, test_device_id, rsa_private_path,
6465
ca_cert_path, image_path, project_id, service_account_json)
6566

66-
cloudiot_mqtt_image.receive_image(
67-
project_id, test_subscription.name, 'test', 'png', 120)
67+
with tempfile.TemporaryDirectory() as tmp_dir:
68+
cloudiot_mqtt_image.receive_image(
69+
project_id, test_subscription.name, tmp_dir + '/test', 'png', 120)
6870

6971
out, _ = capsys.readouterr()
7072
assert 'Received image' in out

logging/cloud-client/snippets_test.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import uuid
16+
1517
import backoff
18+
from google.api_core.exceptions import NotFound
1619
from google.cloud import logging
1720
import pytest
1821

1922
import snippets
2023

2124

22-
TEST_LOGGER_NAME = 'example_log'
25+
TEST_LOGGER_NAME = 'example_log_{}'.format(uuid.uuid4().hex)
2326

2427

2528
@pytest.fixture
@@ -46,6 +49,8 @@ def test_write():
4649

4750

4851
def test_delete(example_log, capsys):
49-
snippets.delete_logger(TEST_LOGGER_NAME)
50-
out, _ = capsys.readouterr()
51-
assert TEST_LOGGER_NAME in out
52+
@backoff.on_exception(backoff.expo, NotFound, max_time=120)
53+
def eventually_consistent_test():
54+
snippets.delete_logger(TEST_LOGGER_NAME)
55+
out, _ = capsys.readouterr()
56+
assert TEST_LOGGER_NAME in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output.mp3

0 commit comments

Comments
 (0)