blob: 12fa0c2f9df9b849d997e428db4ec828f8767a0c [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 Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Evan Hernandezd437b4e2019-03-25 13:48:30 -060011from chromite.api.controller import binhost
12from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060013from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060014from chromite.lib import cros_test_lib
15from chromite.service import binhost as binhost_service
16
Alex Klein231d2da2019-07-22 16:44:45 -060017
18class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase,
19 api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060020 """Unittests for PrepareBinhostUploads."""
21
22 def setUp(self):
23 self.PatchObject(binhost_service, 'GetPrebuiltsRoot',
24 return_value='/build/target/packages')
25 self.PatchObject(binhost_service, 'GetPrebuiltsFiles',
26 return_value=['foo.tbz2', 'bar.tbz2'])
27 self.PatchObject(binhost_service, 'UpdatePackageIndex',
28 return_value='/build/target/packages/Packages')
29
Alex Klein231d2da2019-07-22 16:44:45 -060030 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
31
32 def testValidateOnly(self):
33 """Sanity check that a validate only call does not execute any logic."""
34 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
35
36 request = binhost_pb2.PrepareBinhostUploadsRequest()
37 request.build_target.name = 'target'
38 request.uri = 'gs://chromeos-prebuilt/target'
39 rc = binhost.PrepareBinhostUploads(request, self.response,
40 self.validate_only_config)
41 patch.assert_not_called()
42 self.assertEqual(rc, 0)
43
Evan Hernandezd437b4e2019-03-25 13:48:30 -060044 def testPrepareBinhostUploads(self):
45 """PrepareBinhostUploads returns Packages and tar files."""
46 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
47 input_proto.build_target.name = 'target'
48 input_proto.uri = 'gs://chromeos-prebuilt/target'
Alex Klein231d2da2019-07-22 16:44:45 -060049 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
50 self.assertEqual(self.response.uploads_dir, '/build/target/packages')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060051 self.assertItemsEqual(
Alex Klein231d2da2019-07-22 16:44:45 -060052 [ut.path for ut in self.response.upload_targets],
Evan Hernandezd437b4e2019-03-25 13:48:30 -060053 ['Packages', 'foo.tbz2', 'bar.tbz2'])
54
55 def testPrepareBinhostUploadsNonGsUri(self):
56 """PrepareBinhostUploads dies when URI does not point to GS."""
57 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
58 input_proto.build_target.name = 'target'
59 input_proto.uri = 'https://foo.bar'
Evan Hernandezd437b4e2019-03-25 13:48:30 -060060 with self.assertRaises(ValueError):
Alex Klein231d2da2019-07-22 16:44:45 -060061 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060062
63
Alex Klein231d2da2019-07-22 16:44:45 -060064class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060065 """Unittests for SetBinhost."""
66
Alex Klein231d2da2019-07-22 16:44:45 -060067 def setUp(self):
68 self.response = binhost_pb2.SetBinhostResponse()
69
70 def testValidateOnly(self):
71 """Sanity check that a validate only call does not execute any logic."""
72 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
73
74 request = binhost_pb2.PrepareBinhostUploadsRequest()
75 request.build_target.name = 'target'
76 request.uri = 'gs://chromeos-prebuilt/target'
77 binhost.PrepareBinhostUploads(request, self.response,
78 self.validate_only_config)
79 patch.assert_not_called()
80
Evan Hernandezd437b4e2019-03-25 13:48:30 -060081 def testSetBinhost(self):
82 """SetBinhost calls service with correct args."""
83 set_binhost = self.PatchObject(binhost_service, 'SetBinhost',
84 return_value='/path/to/BINHOST.conf')
85
86 input_proto = binhost_pb2.SetBinhostRequest()
87 input_proto.build_target.name = 'target'
88 input_proto.private = True
89 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
90 input_proto.uri = 'gs://chromeos-prebuilt/target'
91
Alex Klein231d2da2019-07-22 16:44:45 -060092 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060093
Alex Klein231d2da2019-07-22 16:44:45 -060094 self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf')
LaMont Jonesc64ae212019-04-15 15:41:28 -060095 set_binhost.assert_called_once_with(
Alex Klein6fb0eb82019-05-20 16:16:14 -060096 'target', 'PARALLEL_POSTSUBMIT_BINHOST',
97 'gs://chromeos-prebuilt/target', private=True)
LaMont Jonesc64ae212019-04-15 15:41:28 -060098
Alex Klein231d2da2019-07-22 16:44:45 -060099
100class RegenBuildCacheTest(cros_test_lib.MockTestCase,
101 api_config.ApiConfigMixin):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600102 """Unittests for RegenBuildCache."""
103
Alex Klein231d2da2019-07-22 16:44:45 -0600104 def setUp(self):
105 self.response = binhost_pb2.RegenBuildCacheResponse()
106
107 def testValidateOnly(self):
108 """Sanity check that a validate only call does not execute any logic."""
109 patch = self.PatchObject(binhost_service, 'RegenBuildCache')
110
111 request = binhost_pb2.RegenBuildCacheRequest()
112 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
113 binhost.RegenBuildCache(request, self.response, self.validate_only_config)
114 patch.assert_not_called()
115
LaMont Jonesc64ae212019-04-15 15:41:28 -0600116 def testRegenBuildCache(self):
117 """RegenBuildCache calls service with the correct args."""
118 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
119
120 input_proto = binhost_pb2.RegenBuildCacheRequest()
121 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
LaMont Jonesc64ae212019-04-15 15:41:28 -0600122
Alex Klein231d2da2019-07-22 16:44:45 -0600123 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
Alex Kleinaf0e0452019-06-03 18:10:01 -0600124 regen_cache.assert_called_once_with('both')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600125
126 def testRequiresOverlayType(self):
127 """RegenBuildCache dies if overlay_type not specified."""
128 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600129
130 input_proto = binhost_pb2.RegenBuildCacheRequest()
131 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
LaMont Jonesc64ae212019-04-15 15:41:28 -0600132
Alex Klein231d2da2019-07-22 16:44:45 -0600133 with self.assertRaises(cros_build_lib.DieSystemExit):
134 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600135 regen_cache.assert_not_called()