blob: f46afd1e29b694ed79e64c921373994671f1d3bd [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
Tiancong Wangaf050172019-07-10 11:52:03 -070010import os
11
Mike Frysinger6db648e2018-07-24 19:57:58 -040012import mock
13
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import api_config
Tiancong Wangaf050172019-07-10 11:52:03 -070015from chromite.api.controller import toolchain
16from chromite.api.gen.chromite.api import toolchain_pb2
17
18from chromite.lib import cros_test_lib
19from chromite.lib import gs
20from chromite.lib import toolchain_util
21
22
Alex Klein231d2da2019-07-22 16:44:45 -060023class UpdateChromeEbuildWithOrderfileTest(cros_test_lib.MockTestCase,
24 api_config.ApiConfigMixin):
Tiancong Wangaf050172019-07-10 11:52:03 -070025 """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 Klein231d2da2019-07-22 16:44:45 -060034 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 Wangaf050172019-07-10 11:52:03 -070043 def testSuccess(self):
44 """Test the command is called correctly."""
45 output_proto = toolchain_pb2.UpdateChromeEbuildResponse()
46 toolchain.UpdateChromeEbuildWithOrderfile(self.input_proto,
Alex Klein231d2da2019-07-22 16:44:45 -060047 output_proto, self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070048 self.command.assert_called_once_with(self.board)
49
50
Alex Klein231d2da2019-07-22 16:44:45 -060051class UploadVettedOrderfileTest(cros_test_lib.MockTestCase,
52 api_config.ApiConfigMixin):
Tiancong Wangaf050172019-07-10 11:52:03 -070053 """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 Klein231d2da2019-07-22 16:44:45 -060067 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 Wangaf050172019-07-10 11:52:03 -070076 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 Klein231d2da2019-07-22 16:44:45 -060080 toolchain.UploadVettedOrderfile(self.input_proto, output_proto,
81 self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070082 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 Klein231d2da2019-07-22 16:44:45 -060091 toolchain.UploadVettedOrderfile(self.input_proto, output_proto,
92 self.api_config)
Tiancong Wangaf050172019-07-10 11:52:03 -070093 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')