blob: 1fa952572a366b38d7c2af81e029ce83d599da7b [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# -*- 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
8from __future__ import print_function
9
10import mock
11import os
12
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import api_config
Tiancong Wangaf050172019-07-10 11:52:03 -070014from chromite.api.controller import toolchain
15from chromite.api.gen.chromite.api import toolchain_pb2
16
17from chromite.lib import cros_test_lib
18from chromite.lib import gs
19from chromite.lib import toolchain_util
20
21
Alex Klein231d2da2019-07-22 16:44:45 -060022class UpdateChromeEbuildWithOrderfileTest(cros_test_lib.MockTestCase,
23 api_config.ApiConfigMixin):
Tiancong Wangaf050172019-07-10 11:52:03 -070024 """Unittests for UpdateChromeEbuildWithOrderfile."""
25
26 def setUp(self):
27 self.command = self.PatchObject(toolchain_util,
28 'OrderfileUpdateChromeEbuild')
29 self.board = 'board'
30 self.input_proto = toolchain_pb2.UpdateChromeEbuildRequest(
31 build_target={'name': self.board})
32
Alex Klein231d2da2019-07-22 16:44:45 -060033 def testValidateOnly(self):
34 """Sanity check that a validate only call does not execute any logic."""
35 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
36
37 response = toolchain_pb2.UpdateChromeEbuildResponse()
38 toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto, response,
39 self.validate_only_config)
40 patch.assert_not_called()
41
Tiancong Wangaf050172019-07-10 11:52:03 -070042 def testSuccess(self):
43 """Test the command is called correctly."""
44 output_proto = toolchain_pb2.UpdateChromeEbuildResponse()
45 toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto,
Alex Klein231d2da2019-07-22 16:44:45 -060046 output_proto, self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070047 self.command.assert_called_once_with(self.board)
48
49
Alex Klein231d2da2019-07-22 16:44:45 -060050class UploadVettedOrderfileTest(cros_test_lib.MockTestCase,
51 api_config.ApiConfigMixin):
Tiancong Wangaf050172019-07-10 11:52:03 -070052 """Unittests for UploadVettedOrderfile."""
53
54 def setUp(self):
55 self.find_command = self.PatchObject(
56 toolchain_util,
57 'FindLatestChromeOrderfile',
58 return_value='orderfile-3.0.tar.xz')
59 self.copy_command = self.PatchObject(gs.GSContext, 'Copy', autospec=True)
60 self.from_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_UNVETTED,
61 'orderfile-3.0.tar.xz')
62 self.to_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_VETTED,
63 'orderfile-3.0.tar.xz')
64 self.input_proto = toolchain_pb2.UploadVettedOrderfileRequest()
65
Alex Klein231d2da2019-07-22 16:44:45 -060066 def testValidateOnly(self):
67 """Sanity check that a validate only call does not execute any logic."""
68 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
69
70 response = toolchain_pb2.UploadVettedOrderfileResponse()
71 toolchain.UploadVettedOrderfile(self.input_proto, response,
72 self.validate_only_config)
73 patch.assert_not_called()
74
Tiancong Wangaf050172019-07-10 11:52:03 -070075 def testRunFail(self):
76 """Test that it fails to upload an orderfile because it's already vetted."""
77 exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=True)
78 output_proto = toolchain_pb2.UploadVettedOrderfileResponse()
Alex Klein231d2da2019-07-22 16:44:45 -060079 toolchain.UploadVettedOrderfile(self.input_proto, output_proto,
80 self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070081 self.assertFalse(output_proto.status)
82 self.find_command.assert_called_once_with(
83 toolchain_util.ORDERFILE_GS_URL_UNVETTED)
84 exist_command.assert_called_once_with(self.to_url)
85
86 def testRunPass(self):
87 """Test run successfully."""
88 exist_command = self.PatchObject(gs.GSContext, 'Exists', return_value=False)
89 output_proto = toolchain_pb2.UploadVettedOrderfileResponse()
Alex Klein231d2da2019-07-22 16:44:45 -060090 toolchain.UploadVettedOrderfile(self.input_proto, output_proto,
91 self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070092 self.assertTrue(output_proto.status)
93 self.find_command.assert_called_once_with(
94 toolchain_util.ORDERFILE_GS_URL_UNVETTED)
95 exist_command.assert_called_once_with(self.to_url)
96 self.copy_command.assert_called_once_with(
97 mock.ANY, # Placeholder for 'self'
98 self.from_url,
99 self.to_url,
100 acl='public-read')