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 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 10 | from chromite.api.controller import binhost |
| 11 | from chromite.api.gen.chromite.api import binhost_pb2 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 12 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.service import binhost as binhost_service |
| 15 | |
| 16 | class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase): |
| 17 | """Unittests for PrepareBinhostUploads.""" |
| 18 | |
| 19 | def setUp(self): |
| 20 | self.PatchObject(binhost_service, 'GetPrebuiltsRoot', |
| 21 | return_value='/build/target/packages') |
| 22 | self.PatchObject(binhost_service, 'GetPrebuiltsFiles', |
| 23 | return_value=['foo.tbz2', 'bar.tbz2']) |
| 24 | self.PatchObject(binhost_service, 'UpdatePackageIndex', |
| 25 | return_value='/build/target/packages/Packages') |
| 26 | |
| 27 | def testPrepareBinhostUploads(self): |
| 28 | """PrepareBinhostUploads returns Packages and tar files.""" |
| 29 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 30 | input_proto.build_target.name = 'target' |
| 31 | input_proto.uri = 'gs://chromeos-prebuilt/target' |
| 32 | output_proto = binhost_pb2.PrepareBinhostUploadsResponse() |
| 33 | binhost.PrepareBinhostUploads(input_proto, output_proto) |
| 34 | self.assertEqual(output_proto.uploads_dir, '/build/target/packages') |
| 35 | self.assertItemsEqual( |
| 36 | [ut.path for ut in output_proto.upload_targets], |
| 37 | ['Packages', 'foo.tbz2', 'bar.tbz2']) |
| 38 | |
| 39 | def testPrepareBinhostUploadsNonGsUri(self): |
| 40 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 41 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 42 | input_proto.build_target.name = 'target' |
| 43 | input_proto.uri = 'https://foo.bar' |
| 44 | output_proto = binhost_pb2.PrepareBinhostUploadsResponse() |
| 45 | with self.assertRaises(ValueError): |
| 46 | binhost.PrepareBinhostUploads(input_proto, output_proto) |
| 47 | |
| 48 | |
| 49 | class SetBinhostTest(cros_test_lib.MockTestCase): |
| 50 | """Unittests for SetBinhost.""" |
| 51 | |
| 52 | def testSetBinhost(self): |
| 53 | """SetBinhost calls service with correct args.""" |
| 54 | set_binhost = self.PatchObject(binhost_service, 'SetBinhost', |
| 55 | return_value='/path/to/BINHOST.conf') |
| 56 | |
| 57 | input_proto = binhost_pb2.SetBinhostRequest() |
| 58 | input_proto.build_target.name = 'target' |
| 59 | input_proto.private = True |
| 60 | input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 61 | input_proto.uri = 'gs://chromeos-prebuilt/target' |
| 62 | |
| 63 | output_proto = binhost_pb2.SetBinhostResponse() |
| 64 | |
| 65 | binhost.SetBinhost(input_proto, output_proto) |
| 66 | |
| 67 | self.assertEqual(output_proto.output_file, '/path/to/BINHOST.conf') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 68 | set_binhost.assert_called_once_with( |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 69 | 'target', 'PARALLEL_POSTSUBMIT_BINHOST', |
| 70 | 'gs://chromeos-prebuilt/target', private=True) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 71 | |
| 72 | class RegenBuildCacheTest(cros_test_lib.MockTestCase): |
| 73 | """Unittests for RegenBuildCache.""" |
| 74 | |
| 75 | def testRegenBuildCache(self): |
| 76 | """RegenBuildCache calls service with the correct args.""" |
| 77 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
| 78 | |
| 79 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 80 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 81 | input_proto.sysroot.path = '/path/to/chroot' |
| 82 | input_proto.sysroot.build_target.name = 'target' |
| 83 | |
| 84 | binhost.RegenBuildCache(input_proto) |
| 85 | regen_cache.assert_called_once_with('both', '/path/to/chroot') |
| 86 | |
| 87 | def testRequiresOverlayType(self): |
| 88 | """RegenBuildCache dies if overlay_type not specified.""" |
| 89 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
| 90 | die = self.PatchObject(cros_build_lib, 'Die') |
| 91 | |
| 92 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 93 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
| 94 | input_proto.sysroot.path = '/path/to/chroot' |
| 95 | input_proto.sysroot.build_target.name = 'target' |
| 96 | |
| 97 | binhost.RegenBuildCache(input_proto) |
| 98 | die.assert_called_once() |
| 99 | regen_cache.assert_not_called() |