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