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 | |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 10 | import os |
| 11 | |
Mike Frysinger | 6db648e | 2018-07-24 19:57:58 -0400 | [diff] [blame] | 12 | import mock |
| 13 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.api import api_config |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 15 | from chromite.api.controller import toolchain |
| 16 | from chromite.api.gen.chromite.api import toolchain_pb2 |
| 17 | |
| 18 | from chromite.lib import cros_test_lib |
| 19 | from chromite.lib import gs |
| 20 | from chromite.lib import toolchain_util |
| 21 | |
| 22 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 23 | class UpdateChromeEbuildWithOrderfileTest(cros_test_lib.MockTestCase, |
| 24 | api_config.ApiConfigMixin): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 25 | """Unittests for UpdateChromeEbuildWithOrderfile.""" |
| 26 | |
| 27 | def setUp(self): |
| 28 | self.command = self.PatchObject(toolchain_util, |
| 29 | 'OrderfileUpdateChromeEbuild') |
| 30 | self.board = 'board' |
| 31 | self.input_proto = toolchain_pb2.UpdateChromeEbuildRequest( |
| 32 | build_target={'name': self.board}) |
| 33 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 34 | def testValidateOnly(self): |
| 35 | """Sanity check that a validate only call does not execute any logic.""" |
| 36 | patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild') |
| 37 | |
| 38 | response = toolchain_pb2.UpdateChromeEbuildResponse() |
| 39 | toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto, response, |
| 40 | self.validate_only_config) |
| 41 | patch.assert_not_called() |
| 42 | |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 43 | def testSuccess(self): |
| 44 | """Test the command is called correctly.""" |
| 45 | output_proto = toolchain_pb2.UpdateChromeEbuildResponse() |
| 46 | toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 47 | output_proto, self.api_config) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 48 | self.command.assert_called_once_with(self.board) |
| 49 | |
| 50 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 51 | class UploadVettedOrderfileTest(cros_test_lib.MockTestCase, |
| 52 | api_config.ApiConfigMixin): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 53 | """Unittests for UploadVettedOrderfile.""" |
| 54 | |
| 55 | def setUp(self): |
| 56 | self.find_command = self.PatchObject( |
| 57 | toolchain_util, |
| 58 | 'FindLatestChromeOrderfile', |
| 59 | return_value='orderfile-3.0.tar.xz') |
| 60 | self.copy_command = self.PatchObject(gs.GSContext, 'Copy', autospec=True) |
| 61 | self.from_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_UNVETTED, |
| 62 | 'orderfile-3.0.tar.xz') |
| 63 | self.to_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_VETTED, |
| 64 | 'orderfile-3.0.tar.xz') |
| 65 | self.input_proto = toolchain_pb2.UploadVettedOrderfileRequest() |
| 66 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 67 | def testValidateOnly(self): |
| 68 | """Sanity check that a validate only call does not execute any logic.""" |
| 69 | patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild') |
| 70 | |
| 71 | response = toolchain_pb2.UploadVettedOrderfileResponse() |
| 72 | toolchain.UploadVettedOrderfile(self.input_proto, response, |
| 73 | self.validate_only_config) |
| 74 | patch.assert_not_called() |
| 75 | |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 76 | def testRunFail(self): |
| 77 | """Test that it fails to upload an orderfile because it's already vetted.""" |
| 78 | exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=True) |
| 79 | output_proto = toolchain_pb2.UploadVettedOrderfileResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 80 | toolchain.UploadVettedOrderfile(self.input_proto, output_proto, |
| 81 | self.api_config) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 82 | self.assertFalse(output_proto.status) |
| 83 | self.find_command.assert_called_once_with( |
| 84 | toolchain_util.ORDERFILE_GS_URL_UNVETTED) |
| 85 | exist_command.assert_called_once_with(self.to_url) |
| 86 | |
| 87 | def testRunPass(self): |
| 88 | """Test run successfully.""" |
| 89 | exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=False) |
| 90 | output_proto = toolchain_pb2.UploadVettedOrderfileResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 91 | toolchain.UploadVettedOrderfile(self.input_proto, output_proto, |
| 92 | self.api_config) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 93 | self.assertTrue(output_proto.status) |
| 94 | self.find_command.assert_called_once_with( |
| 95 | toolchain_util.ORDERFILE_GS_URL_UNVETTED) |
| 96 | exist_command.assert_called_once_with(self.to_url) |
| 97 | self.copy_command.assert_called_once_with( |
| 98 | mock.ANY, # Placeholder for 'self' |
| 99 | self.from_url, |
| 100 | self.to_url, |
| 101 | acl='public-read') |