Tiancong Wang | af05017 | 2019-07-10 11:52:03 -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 | """Unittests for Toolchain-related operations.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import mock |
| 11 | import os |
| 12 | |
| 13 | from chromite.api.controller import toolchain |
| 14 | from chromite.api.gen.chromite.api import toolchain_pb2 |
| 15 | |
| 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.lib import gs |
| 18 | from chromite.lib import toolchain_util |
| 19 | |
| 20 | |
| 21 | class UpdateChromeEbuildWithOrderfileTest(cros_test_lib.MockTestCase): |
| 22 | """Unittests for UpdateChromeEbuildWithOrderfile.""" |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.command = self.PatchObject(toolchain_util, |
| 26 | 'OrderfileUpdateChromeEbuild') |
| 27 | self.board = 'board' |
| 28 | self.input_proto = toolchain_pb2.UpdateChromeEbuildRequest( |
| 29 | build_target={'name': self.board}) |
| 30 | |
| 31 | def testSuccess(self): |
| 32 | """Test the command is called correctly.""" |
| 33 | output_proto = toolchain_pb2.UpdateChromeEbuildResponse() |
| 34 | toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto, |
| 35 | output_proto) |
| 36 | self.command.assert_called_once_with(self.board) |
| 37 | |
| 38 | |
| 39 | class UploadVettedOrderfileTest(cros_test_lib.MockTestCase): |
| 40 | """Unittests for UploadVettedOrderfile.""" |
| 41 | |
| 42 | def setUp(self): |
| 43 | self.find_command = self.PatchObject( |
| 44 | toolchain_util, |
| 45 | 'FindLatestChromeOrderfile', |
| 46 | return_value='orderfile-3.0.tar.xz') |
| 47 | self.copy_command = self.PatchObject(gs.GSContext, 'Copy', autospec=True) |
| 48 | self.from_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_UNVETTED, |
| 49 | 'orderfile-3.0.tar.xz') |
| 50 | self.to_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_VETTED, |
| 51 | 'orderfile-3.0.tar.xz') |
| 52 | self.input_proto = toolchain_pb2.UploadVettedOrderfileRequest() |
| 53 | |
| 54 | def testRunFail(self): |
| 55 | """Test that it fails to upload an orderfile because it's already vetted.""" |
| 56 | exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=True) |
| 57 | output_proto = toolchain_pb2.UploadVettedOrderfileResponse() |
| 58 | toolchain.UploadVettedOrderfile(self.input_proto, output_proto) |
| 59 | self.assertFalse(output_proto.status) |
| 60 | self.find_command.assert_called_once_with( |
| 61 | toolchain_util.ORDERFILE_GS_URL_UNVETTED) |
| 62 | exist_command.assert_called_once_with(self.to_url) |
| 63 | |
| 64 | def testRunPass(self): |
| 65 | """Test run successfully.""" |
| 66 | exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=False) |
| 67 | output_proto = toolchain_pb2.UploadVettedOrderfileResponse() |
| 68 | toolchain.UploadVettedOrderfile(self.input_proto, output_proto) |
| 69 | self.assertTrue(output_proto.status) |
| 70 | self.find_command.assert_called_once_with( |
| 71 | toolchain_util.ORDERFILE_GS_URL_UNVETTED) |
| 72 | exist_command.assert_called_once_with(self.to_url) |
| 73 | self.copy_command.assert_called_once_with( |
| 74 | mock.ANY, # Placeholder for 'self' |
| 75 | self.from_url, |
| 76 | self.to_url, |
| 77 | acl='public-read') |