Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [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 Binhost operations.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame^] | 10 | import mock |
| 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import api_config |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 13 | from chromite.api.controller import binhost |
| 14 | from chromite.api.gen.chromite.api import binhost_pb2 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.service import binhost as binhost_service |
| 18 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 19 | |
| 20 | class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase, |
| 21 | api_config.ApiConfigMixin): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 22 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 32 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 46 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 51 | binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config) |
| 52 | self.assertEqual(self.response.uploads_dir, '/build/target/packages') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 53 | self.assertItemsEqual( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 54 | [ut.path for ut in self.response.upload_targets], |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 55 | ['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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 62 | with self.assertRaises(ValueError): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 63 | binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 64 | |
| 65 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 67 | """Unittests for SetBinhost.""" |
| 68 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 69 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 83 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 94 | binhost.SetBinhost(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 95 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 97 | set_binhost.assert_called_once_with( |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 98 | 'target', 'PARALLEL_POSTSUBMIT_BINHOST', |
| 99 | 'gs://chromeos-prebuilt/target', private=True) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 100 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | |
| 102 | class RegenBuildCacheTest(cros_test_lib.MockTestCase, |
| 103 | api_config.ApiConfigMixin): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 104 | """Unittests for RegenBuildCache.""" |
| 105 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 106 | 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 Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 118 | 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 Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 124 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 125 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame^] | 126 | regen_cache.assert_called_once_with(mock.ANY, 'both') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 127 | |
| 128 | def testRequiresOverlayType(self): |
| 129 | """RegenBuildCache dies if overlay_type not specified.""" |
| 130 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 131 | |
| 132 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 133 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 134 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 135 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 136 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 137 | regen_cache.assert_not_called() |