|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Tests for automl_vision_edge_container_predict. |
| 18 | +
|
| 19 | +The test will automatically start a container with a sample saved_model.pb, |
| 20 | +send a request with one image, verify the response and delete the started |
| 21 | +container. |
| 22 | +
|
| 23 | +If you want to try the test, please install |
| 24 | +[gsutil tools](https://cloud.google.com/storage/docs/gsutil_install) and |
| 25 | +[Docker CE](https://docs.docker.com/install/) first. |
| 26 | +
|
| 27 | +Examples: |
| 28 | +sudo python -m pytest automl_vision_edge_container_predict_test.py |
| 29 | +""" |
| 30 | + |
| 31 | +import os |
| 32 | +import subprocess |
| 33 | +import time |
| 34 | +import automl_vision_edge_container_predict as predict |
| 35 | +import pytest |
| 36 | + |
| 37 | + |
| 38 | +# The absolute path of the current file. This will locate the model_path when |
| 39 | +# run docker containers. |
| 40 | +ROOT_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 41 | +MODEL_PATH = os.path.join(ROOT_DIR, 'model_path') |
| 42 | +# The cpu docker gcs path is from 'Edge container tutorial'. |
| 43 | +DOCKER_GCS_DIR = 'gcr.io/automl-vision-ondevice/' |
| 44 | +CPU_DOCKER_GCS_PATH = DOCKER_GCS_DIR + 'gcloud-container-1.12.0:latest' |
| 45 | +# The path of a sample saved model. |
| 46 | +MODEL_GCS_DIR = 'gs://cloud-samples-data/vision/edge_container_predict/' |
| 47 | +SAMPLE_SAVED_MODEL = MODEL_GCS_DIR + 'saved_model.pb' |
| 48 | +# Container Name. |
| 49 | +NAME = 'AutomlVisionEdgeContainerPredictTest' |
| 50 | +# Port Number. |
| 51 | +PORT_NUMBER = 8505 |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def edge_container_predict_server_port(): |
| 56 | + # set up |
| 57 | + # Pull the CPU docker. |
| 58 | + subprocess.check_output(['docker', 'pull', CPU_DOCKER_GCS_PATH]) |
| 59 | + # Get the sample saved model. |
| 60 | + |
| 61 | + if not os.path.exists(MODEL_PATH): |
| 62 | + os.mkdir(MODEL_PATH) |
| 63 | + subprocess.check_output( |
| 64 | + ['gsutil', '-m', 'cp', SAMPLE_SAVED_MODEL, MODEL_PATH]) |
| 65 | + |
| 66 | + # Start the CPU docker. |
| 67 | + subprocess.Popen(['docker', 'run', '--rm', '--name', NAME, '-v', |
| 68 | + MODEL_PATH + ':/tmp/mounted_model/0001', '-p', |
| 69 | + str(PORT_NUMBER) + ':8501', '-t', |
| 70 | + CPU_DOCKER_GCS_PATH]) |
| 71 | + # Sleep a few seconds to wait for the container running. |
| 72 | + time.sleep(10) |
| 73 | + |
| 74 | + yield PORT_NUMBER |
| 75 | + |
| 76 | + # tear down |
| 77 | + # Stop the container. |
| 78 | + subprocess.check_output(['docker', 'stop', NAME]) |
| 79 | + # Remove the docker image. |
| 80 | + subprocess.check_output(['docker', 'rmi', CPU_DOCKER_GCS_PATH]) |
| 81 | + |
| 82 | +# TODO(dizcology): Enable tests in future. |
| 83 | +@pytest.mark.skip(reason='skipping to avoid running docker in docker') |
| 84 | +def test_edge_container_predict(capsys, edge_container_predict_server_port): |
| 85 | + image_file_path = 'test.jpg' |
| 86 | + # If you send requests with one image each time, the key value does not |
| 87 | + # matter. If you send requests with multiple images, please used different |
| 88 | + # keys to indicated different images, which can make sure that the |
| 89 | + # responses corresponding to the given image. |
| 90 | + image_key = '1' |
| 91 | + # Send a request. |
| 92 | + response = predict.container_predict( |
| 93 | + image_file_path, image_key, PORT_NUMBER) |
| 94 | + # Verify the response. |
| 95 | + assert 'predictions' in response |
| 96 | + assert 'key' in response['predictions'][0] |
| 97 | + assert image_key == response['predictions'][0]['key'] |
0 commit comments