Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Image API Service. |
| 7 | |
| 8 | The image related API endpoints should generally be found here. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | |
| 13 | import os |
| 14 | |
| 15 | from chromite.lib.api import image |
| 16 | |
| 17 | class Error(Exception): |
| 18 | """The module's base error class.""" |
| 19 | |
| 20 | |
| 21 | class InvalidTestArgumentError(Error): |
| 22 | """Invalid argument to the Test function.""" |
| 23 | |
| 24 | |
| 25 | def Test(input_proto, output_proto): |
| 26 | """Run image tests. |
| 27 | |
| 28 | Args: |
| 29 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 30 | output_proto (image_pb2.ImageTestResult): The output message. |
| 31 | """ |
| 32 | image_path = input_proto.image.path |
| 33 | board = input_proto.build_target.name |
| 34 | result_directory = input_proto.result.directory |
| 35 | |
| 36 | if not board: |
| 37 | raise InvalidTestArgumentError('The build_target.name is required.') |
| 38 | if not result_directory: |
| 39 | raise InvalidTestArgumentError('The result.directory is required.') |
| 40 | if not image_path: |
| 41 | raise InvalidTestArgumentError('The image.path is required.') |
| 42 | |
| 43 | if not os.path.isfile(image_path) or not image_path.endswith('.bin'): |
| 44 | raise InvalidTestArgumentError( |
| 45 | 'The image.path must be an existing image file with a .bin extension.') |
| 46 | |
| 47 | output_proto.success = image.Test(board, result_directory, |
| 48 | image_dir=image_path) |