Skip to content

Commit

Permalink
testing: various cleanups (GoogleCloudPlatform#3877)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Takashi Matsuo authored May 26, 2020
1 parent 572ea21 commit 869e56e
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 43 deletions.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions appengine/flexible/scipy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

from flask import Flask
from flask import request
import imageio
from PIL import Image

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

output_image_path = os.path.join(
app_path, 'assets/resized_google_logo.jpg')
output_image_path = request.args.get('output_image_path')
# Write the tinted image back to disk
imageio.imwrite(output_image_path, img_tinted)
return "Image resized."
Expand Down
19 changes: 7 additions & 12 deletions appengine/flexible/scipy/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@
# limitations under the License.

import os
import tempfile

import main


def test_index():
main.app.testing = True
client = main.app.test_client()
test_path = os.path.dirname(os.path.realpath(__file__))
asset_path = os.path.join(
test_path, 'assets/resized_google_logo.jpg')
fixtured_path = os.path.join(
test_path, 'fixtures/assets/resized_google_logo.jpg')
try:
os.remove(asset_path)
except OSError:
pass # if doesn't exist
r = client.get('/')
with tempfile.TemporaryDirectory() as test_dir:
output_image_path = os.path.join(test_dir, 'resized_google_logo.jpg')
r = client.get(
'/', query_string={'output_image_path': output_image_path})

assert os.path.isfile(fixtured_path)
assert r.status_code == 200
assert os.path.isfile(output_image_path)
assert r.status_code == 200
16 changes: 8 additions & 8 deletions bigtable/snippets/reads/snapshots/snap_reads_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

snapshots = Snapshot()

snapshots['test_read_simple 1'] = '''Reading data for phone#4c410523#20190501:
Column Family stats_summary
\tconnected_cell: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
\tconnected_wifi: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
'''

snapshots['test_read_row_partial 1'] = '''Reading data for phone#4c410523#20190501:
Column Family stats_summary
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
Expand Down Expand Up @@ -140,3 +132,11 @@
\tos_build: PQ2A.190406.000 @2019-05-01 00:00:00+00:00
'''

snapshots['test_read_row 1'] = '''Reading data for phone#4c410523#20190501:
Column Family stats_summary
\tconnected_cell: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
\tconnected_wifi: \x00\x00\x00\x00\x00\x00\x00\x01 @2019-05-01 00:00:00+00:00
\tos_build: PQ2A.190405.003 @2019-05-01 00:00:00+00:00
'''
6 changes: 6 additions & 0 deletions iap/iap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

import os

import pytest

import make_iap_request
Expand All @@ -36,6 +38,10 @@

@pytest.mark.flaky
def test_main(capsys):
# It only passes on Kokoro now. Skipping in other places.
# The envvar `TRAMPOLINE_CI` will be set once #3860 is merged.
if os.environ.get('TRAMPOLINE_CI', 'kokoro') != 'kokoro':
pytest.skip('Only passing on Kokoro.')
# JWTs are obtained by IAP-protected applications whenever an
# end-user makes a request. We've set up an app that echoes back
# the JWT in order to expose it to this test. Thus, this test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import tempfile

from google.cloud import storage

Expand All @@ -22,7 +23,6 @@

gcs_bucket = os.environ['CLOUD_STORAGE_BUCKET']
cloud_region = 'us-central1'
destination_file_name = 'destination-file.bin'
project_id = os.environ['GCLOUD_PROJECT']


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


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

out, _ = capsys.readouterr()
assert 'Config {} downloaded to {}.'.format(
test_blob.name, destination_file_name) in out
out, _ = capsys.readouterr()
assert 'Config {} downloaded to {}.'.format(
test_blob.name, destination_file_name) in out
6 changes: 4 additions & 2 deletions iot/api-client/mqtt_example/cloudiot_mqtt_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
import os
import sys
import tempfile

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

cloudiot_mqtt_image.receive_image(
project_id, test_subscription.name, 'test', 'png', 120)
with tempfile.TemporaryDirectory() as tmp_dir:
cloudiot_mqtt_image.receive_image(
project_id, test_subscription.name, tmp_dir + '/test', 'png', 120)

out, _ = capsys.readouterr()
assert 'Received image' in out
13 changes: 9 additions & 4 deletions logging/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid

import backoff
from google.api_core.exceptions import NotFound
from google.cloud import logging
import pytest

import snippets


TEST_LOGGER_NAME = 'example_log'
TEST_LOGGER_NAME = 'example_log_{}'.format(uuid.uuid4().hex)


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


def test_delete(example_log, capsys):
snippets.delete_logger(TEST_LOGGER_NAME)
out, _ = capsys.readouterr()
assert TEST_LOGGER_NAME in out
@backoff.on_exception(backoff.expo, NotFound, max_time=120)
def eventually_consistent_test():
snippets.delete_logger(TEST_LOGGER_NAME)
out, _ = capsys.readouterr()
assert TEST_LOGGER_NAME in out
1 change: 1 addition & 0 deletions texttospeech/cloud-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output.mp3
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@

import os
import subprocess
import tempfile
import time

import pytest

import automl_vision_edge_container_predict as predict # noqa


# The absolute path of the current file. This will locate the model_path when
# run docker containers.
ROOT_DIR = os.environ.get(
'KOKORO_ROOT', os.path.abspath(os.path.dirname(__file__)))
MODEL_PATH = os.path.join(ROOT_DIR, 'model_path')

IMAGE_FILE_PATH = os.path.join(os.path.dirname(__file__), 'test.jpg')
# The cpu docker gcs path is from 'Edge container tutorial'.
CPU_DOCKER_GCS_PATH = '{}'.format(
Expand All @@ -64,15 +59,20 @@ def edge_container_predict_server_port():
['docker', 'pull', CPU_DOCKER_GCS_PATH],
env={'DOCKER_API_VERSION': '1.38'})

if os.environ.get('TRAMPOLINE_V2') == 'true':
# Use /tmp
model_path = tempfile.TemporaryDirectory()
else:
# Use project directory with Trampoline V1.
model_path = tempfile.TemporaryDirectory(dir=os.path.dirname(__file__))
print("Using model_path: {}".format(model_path))
# Get the sample saved model.
if not os.path.exists(MODEL_PATH):
os.mkdir(MODEL_PATH)
subprocess.check_output(
['gsutil', '-m', 'cp', SAMPLE_SAVED_MODEL, MODEL_PATH])
['gsutil', '-m', 'cp', SAMPLE_SAVED_MODEL, model_path.name])

# Start the CPU docker.
subprocess.Popen(['docker', 'run', '--rm', '--name', NAME, '-v',
MODEL_PATH + ':/tmp/mounted_model/0001', '-p',
model_path.name + ':/tmp/mounted_model/0001', '-p',
str(PORT_NUMBER) + ':8501', '-t',
CPU_DOCKER_GCS_PATH],
env={'DOCKER_API_VERSION': '1.38'})
Expand All @@ -89,6 +89,8 @@ def edge_container_predict_server_port():
subprocess.check_output(
['docker', 'rmi', CPU_DOCKER_GCS_PATH],
env={'DOCKER_API_VERSION': '1.38'})
# Remove the temporery directory.
model_path.cleanup()


def test_edge_container_predict(capsys, edge_container_predict_server_port):
Expand Down

0 comments on commit 869e56e

Please sign in to comment.