Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 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 service tests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.api.gen import image_pb2 |
| 13 | from chromite.api.service import image as image_service |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import osutils |
| 16 | from chromite.lib.api import image as image_lib |
| 17 | |
| 18 | |
| 19 | class ImageTest(cros_test_lib.MockTempDirTestCase): |
| 20 | """Image service tests.""" |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
| 24 | self.board = 'board' |
| 25 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 26 | |
| 27 | osutils.SafeMakedirs(self.result_directory) |
| 28 | osutils.Touch(self.image_path) |
| 29 | |
| 30 | def testTestArgumentValidation(self): |
| 31 | """Test function argument validation tests.""" |
| 32 | self.PatchObject(image_lib, 'Test', return_value=True) |
| 33 | input_proto = image_pb2.TestImageRequest() |
| 34 | output_proto = image_pb2.TestImageResult() |
| 35 | |
| 36 | # Nothing provided. |
| 37 | with self.assertRaises(image_service.InvalidTestArgumentError): |
| 38 | image_service.Test(input_proto, output_proto) |
| 39 | |
| 40 | # Just one argument. |
| 41 | input_proto.build_target.name = self.board |
| 42 | with self.assertRaises(image_service.InvalidTestArgumentError): |
| 43 | image_service.Test(input_proto, output_proto) |
| 44 | |
| 45 | # Two arguments provided. |
| 46 | input_proto.result.directory = self.result_directory |
| 47 | with self.assertRaises(image_service.InvalidTestArgumentError): |
| 48 | image_service.Test(input_proto, output_proto) |
| 49 | |
| 50 | # Invalid image path. |
| 51 | input_proto.image.path = '/invalid/image/path' |
| 52 | with self.assertRaises(image_service.InvalidTestArgumentError): |
| 53 | image_service.Test(input_proto, output_proto) |
| 54 | |
| 55 | # All valid arguments. |
| 56 | input_proto.image.path = self.image_path |
| 57 | image_service.Test(input_proto, output_proto) |
| 58 | |
| 59 | def testTestOutputHandling(self): |
| 60 | """Test function output tests.""" |
| 61 | input_proto = image_pb2.TestImageRequest() |
| 62 | input_proto.image.path = self.image_path |
| 63 | input_proto.build_target.name = self.board |
| 64 | input_proto.result.directory = self.result_directory |
| 65 | output_proto = image_pb2.TestImageResult() |
| 66 | |
| 67 | self.PatchObject(image_lib, 'Test', return_value=True) |
| 68 | image_service.Test(input_proto, output_proto) |
| 69 | self.assertTrue(output_proto.success) |
| 70 | |
| 71 | self.PatchObject(image_lib, 'Test', return_value=False) |
| 72 | image_service.Test(input_proto, output_proto) |
| 73 | self.assertFalse(output_proto.success) |