blob: 017e7fc49bbeddbda712fe7bf10015a891caec64 [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -06001# -*- 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 Binhost operations."""
7
8from __future__ import print_function
9
Alex Klein82c85d42019-08-14 15:47:51 -060010import mock
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Evan Hernandezd437b4e2019-03-25 13:48:30 -060013from chromite.api.controller import binhost
14from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060015from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060016from chromite.lib import cros_test_lib
17from chromite.service import binhost as binhost_service
18
Alex Klein231d2da2019-07-22 16:44:45 -060019
20class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase,
21 api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060022 """Unittests for PrepareBinhostUploads."""
23
24 def setUp(self):
25 self.PatchObject(binhost_service, 'GetPrebuiltsRoot',
26 return_value='/build/target/packages')
27 self.PatchObject(binhost_service, 'GetPrebuiltsFiles',
28 return_value=['foo.tbz2', 'bar.tbz2'])
29 self.PatchObject(binhost_service, 'UpdatePackageIndex',
30 return_value='/build/target/packages/Packages')
31
Alex Klein231d2da2019-07-22 16:44:45 -060032 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
33
34 def testValidateOnly(self):
35 """Sanity check that a validate only call does not execute any logic."""
36 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
37
38 request = binhost_pb2.PrepareBinhostUploadsRequest()
39 request.build_target.name = 'target'
40 request.uri = 'gs://chromeos-prebuilt/target'
41 rc = binhost.PrepareBinhostUploads(request, self.response,
42 self.validate_only_config)
43 patch.assert_not_called()
44 self.assertEqual(rc, 0)
45
Evan Hernandezd437b4e2019-03-25 13:48:30 -060046 def testPrepareBinhostUploads(self):
47 """PrepareBinhostUploads returns Packages and tar files."""
48 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
49 input_proto.build_target.name = 'target'
50 input_proto.uri = 'gs://chromeos-prebuilt/target'
Alex Klein231d2da2019-07-22 16:44:45 -060051 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
52 self.assertEqual(self.response.uploads_dir, '/build/target/packages')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060053 self.assertItemsEqual(
Alex Klein231d2da2019-07-22 16:44:45 -060054 [ut.path for ut in self.response.upload_targets],
Evan Hernandezd437b4e2019-03-25 13:48:30 -060055 ['Packages', 'foo.tbz2', 'bar.tbz2'])
56
57 def testPrepareBinhostUploadsNonGsUri(self):
58 """PrepareBinhostUploads dies when URI does not point to GS."""
59 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
60 input_proto.build_target.name = 'target'
61 input_proto.uri = 'https://foo.bar'
Evan Hernandezd437b4e2019-03-25 13:48:30 -060062 with self.assertRaises(ValueError):
Alex Klein231d2da2019-07-22 16:44:45 -060063 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060064
65
Alex Klein231d2da2019-07-22 16:44:45 -060066class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060067 """Unittests for SetBinhost."""
68
Alex Klein231d2da2019-07-22 16:44:45 -060069 def setUp(self):
70 self.response = binhost_pb2.SetBinhostResponse()
71
72 def testValidateOnly(self):
73 """Sanity check that a validate only call does not execute any logic."""
74 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
75
76 request = binhost_pb2.PrepareBinhostUploadsRequest()
77 request.build_target.name = 'target'
78 request.uri = 'gs://chromeos-prebuilt/target'
79 binhost.PrepareBinhostUploads(request, self.response,
80 self.validate_only_config)
81 patch.assert_not_called()
82
Evan Hernandezd437b4e2019-03-25 13:48:30 -060083 def testSetBinhost(self):
84 """SetBinhost calls service with correct args."""
85 set_binhost = self.PatchObject(binhost_service, 'SetBinhost',
86 return_value='/path/to/BINHOST.conf')
87
88 input_proto = binhost_pb2.SetBinhostRequest()
89 input_proto.build_target.name = 'target'
90 input_proto.private = True
91 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
92 input_proto.uri = 'gs://chromeos-prebuilt/target'
93
Alex Klein231d2da2019-07-22 16:44:45 -060094 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060095
Alex Klein231d2da2019-07-22 16:44:45 -060096 self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf')
LaMont Jonesc64ae212019-04-15 15:41:28 -060097 set_binhost.assert_called_once_with(
Alex Klein6fb0eb82019-05-20 16:16:14 -060098 'target', 'PARALLEL_POSTSUBMIT_BINHOST',
99 'gs://chromeos-prebuilt/target', private=True)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600100
Alex Klein231d2da2019-07-22 16:44:45 -0600101
102class RegenBuildCacheTest(cros_test_lib.MockTestCase,
103 api_config.ApiConfigMixin):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600104 """Unittests for RegenBuildCache."""
105
Alex Klein231d2da2019-07-22 16:44:45 -0600106 def setUp(self):
107 self.response = binhost_pb2.RegenBuildCacheResponse()
108
109 def testValidateOnly(self):
110 """Sanity check that a validate only call does not execute any logic."""
111 patch = self.PatchObject(binhost_service, 'RegenBuildCache')
112
113 request = binhost_pb2.RegenBuildCacheRequest()
114 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
115 binhost.RegenBuildCache(request, self.response, self.validate_only_config)
116 patch.assert_not_called()
117
LaMont Jonesc64ae212019-04-15 15:41:28 -0600118 def testRegenBuildCache(self):
119 """RegenBuildCache calls service with the correct args."""
120 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
121
122 input_proto = binhost_pb2.RegenBuildCacheRequest()
123 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
LaMont Jonesc64ae212019-04-15 15:41:28 -0600124
Alex Klein231d2da2019-07-22 16:44:45 -0600125 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
Alex Klein82c85d42019-08-14 15:47:51 -0600126 regen_cache.assert_called_once_with(mock.ANY, 'both')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600127
128 def testRequiresOverlayType(self):
129 """RegenBuildCache dies if overlay_type not specified."""
130 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600131
132 input_proto = binhost_pb2.RegenBuildCacheRequest()
133 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
LaMont Jonesc64ae212019-04-15 15:41:28 -0600134
Alex Klein231d2da2019-07-22 16:44:45 -0600135 with self.assertRaises(cros_build_lib.DieSystemExit):
136 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600137 regen_cache.assert_not_called()