BuildAPI: Add remaining unit tests and mocks for Binhost Service.

Also did minor cleanup of existing unit tests.

BUG=chromium:1000849
TEST=run_tests

Change-Id: I89180bc83f6e385cbbc11e78b0665b74433d6e44
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1917291
Reviewed-by: Alex Klein <saklein@chromium.org>
Tested-by: Michael Mortensen <mmortensen@google.com>
Commit-Queue: Michael Mortensen <mmortensen@google.com>
diff --git a/api/controller/binhost_unittest.py b/api/controller/binhost_unittest.py
index 3ccae46..e494c64 100644
--- a/api/controller/binhost_unittest.py
+++ b/api/controller/binhost_unittest.py
@@ -29,7 +29,7 @@
     """Sanity check that a validate only call does not execute any logic."""
     patch = self.PatchObject(binhost_service, 'GetBinhosts')
 
-    request = binhost_pb2.PrepareBinhostUploadsRequest()
+    request = binhost_pb2.BinhostGetRequest()
     request.build_target.name = 'target'
     binhost.GetBinhosts(request, self.response, self.validate_only_config)
     patch.assert_not_called()
@@ -65,6 +65,56 @@
     get_binhost.assert_called_once_with(mock.ANY)
 
 
+class GetPrivatePrebuiltAclArgsTest(cros_test_lib.MockTestCase,
+                                    api_config.ApiConfigMixin):
+  """Unittests for GetPrivatePrebuiltAclArgs."""
+
+  def setUp(self):
+    self.response = binhost_pb2.AclArgsResponse()
+
+  def testValidateOnly(self):
+    """Sanity check that a validate only call does not execute any logic."""
+    patch = self.PatchObject(binhost_service, 'GetPrebuiltAclArgs')
+
+    request = binhost_pb2.AclArgsRequest()
+    request.build_target.name = 'target'
+    binhost.GetPrivatePrebuiltAclArgs(request, self.response,
+                                      self.validate_only_config)
+    patch.assert_not_called()
+
+  def testMockCall(self):
+    """Test that a mock call does not execute logic, returns mocked value."""
+    patch = self.PatchObject(binhost_service, 'GetPrebuiltAclArgs')
+
+    input_proto = binhost_pb2.AclArgsRequest()
+    input_proto.build_target.name = 'target'
+
+    binhost.GetPrivatePrebuiltAclArgs(input_proto, self.response,
+                                      self.mock_call_config)
+
+    self.assertEqual(len(self.response.args), 1)
+    self.assertEqual(self.response.args[0].arg, '-g')
+    self.assertEqual(self.response.args[0].value, 'group1:READ')
+    patch.assert_not_called()
+
+  def testGetPrivatePrebuiltAclArgs(self):
+    """GetPrivatePrebuildAclsArgs calls service with correct args."""
+    argvalue_list = [['-g', 'group1:READ']]
+    get_binhost = self.PatchObject(binhost_service, 'GetPrebuiltAclArgs',
+                                   return_value=argvalue_list)
+
+    input_proto = binhost_pb2.AclArgsRequest()
+    input_proto.build_target.name = 'target'
+
+    binhost.GetPrivatePrebuiltAclArgs(input_proto, self.response,
+                                      self.api_config)
+
+    self.assertEqual(len(self.response.args), 1)
+    self.assertEqual(self.response.args[0].arg, '-g')
+    self.assertEqual(self.response.args[0].value, 'group1:READ')
+    get_binhost.assert_called_once_with(mock.ANY)
+
+
 class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase,
                                 api_config.ApiConfigMixin):
   """Unittests for PrepareBinhostUploads."""
@@ -91,6 +141,20 @@
     patch.assert_not_called()
     self.assertEqual(rc, 0)
 
+  def testMockCall(self):
+    """Test that a mock call does not execute logic, returns mocked value."""
+    patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
+
+    request = binhost_pb2.PrepareBinhostUploadsRequest()
+    request.build_target.name = 'target'
+    request.uri = 'gs://chromeos-prebuilt/target'
+    rc = binhost.PrepareBinhostUploads(request, self.response,
+                                       self.mock_call_config)
+    self.assertEqual(self.response.uploads_dir, '/upload/directory')
+    self.assertEqual(self.response.upload_targets[0].path, 'upload_target')
+    patch.assert_not_called()
+    self.assertEqual(rc, 0)
+
   def testPrepareBinhostUploads(self):
     """PrepareBinhostUploads returns Packages and tar files."""
     input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
@@ -119,15 +183,27 @@
 
   def testValidateOnly(self):
     """Sanity check that a validate only call does not execute any logic."""
-    patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
+    patch = self.PatchObject(binhost_service, 'SetBinhost')
 
-    request = binhost_pb2.PrepareBinhostUploadsRequest()
+    request = binhost_pb2.SetBinhostRequest()
     request.build_target.name = 'target'
+    request.key = binhost_pb2.POSTSUBMIT_BINHOST
     request.uri = 'gs://chromeos-prebuilt/target'
-    binhost.PrepareBinhostUploads(request, self.response,
-                                  self.validate_only_config)
+    binhost.SetBinhost(request, self.response, self.validate_only_config)
     patch.assert_not_called()
 
+  def testMockCall(self):
+    """Test that a mock call does not execute logic, returns mocked value."""
+    patch = self.PatchObject(binhost_service, 'SetBinhost')
+
+    request = binhost_pb2.SetBinhostRequest()
+    request.build_target.name = 'target'
+    request.key = binhost_pb2.POSTSUBMIT_BINHOST
+    request.uri = 'gs://chromeos-prebuilt/target'
+    binhost.SetBinhost(request, self.response, self.mock_call_config)
+    patch.assert_not_called()
+    self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf')
+
   def testSetBinhost(self):
     """SetBinhost calls service with correct args."""
     set_binhost = self.PatchObject(binhost_service, 'SetBinhost',
@@ -165,6 +241,19 @@
     binhost.RegenBuildCache(request, self.response, self.validate_only_config)
     patch.assert_not_called()
 
+  def testMockCall(self):
+    """Test that a mock call does not execute logic, returns mocked value."""
+    patch = self.PatchObject(binhost_service, 'RegenBuildCache')
+
+    request = binhost_pb2.RegenBuildCacheRequest()
+    request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
+    binhost.RegenBuildCache(request, self.response, self.mock_call_config)
+    patch.assert_not_called()
+    self.assertEqual(len(self.response.modified_overlays), 1)
+    self.assertEqual(self.response.modified_overlays[0].path,
+                     '/path/to/BuildCache')
+
+
   def testRegenBuildCache(self):
     """RegenBuildCache calls service with the correct args."""
     regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
@@ -249,6 +338,36 @@
     package_installable_file.close()
     self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
 
+  def testValidateOnly(self):
+    """Sanity check that a validate only call does not execute any logic."""
+    patch = self.PatchObject(binhost_service,
+                             'ReadDevInstallFilesToCreatePackageIndex')
+
+    input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
+    input_proto.uri = 'gs://chromeos-prebuilt/target'
+    input_proto.chroot.path = self.chroot_path
+    input_proto.sysroot.path = self.sysroot_path
+    input_proto.uploads_dir = self.uploads_dir
+    binhost.PrepareDevInstallBinhostUploads(input_proto, self.response,
+                                            self.validate_only_config)
+    patch.assert_not_called()
+
+  def testMockCall(self):
+    """Test that a mock call does not execute logic, returns mocked value."""
+    patch = self.PatchObject(binhost_service,
+                             'ReadDevInstallFilesToCreatePackageIndex')
+
+    input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
+    input_proto.uri = 'gs://chromeos-prebuilt/target'
+    input_proto.chroot.path = self.chroot_path
+    input_proto.sysroot.path = self.sysroot_path
+    input_proto.uploads_dir = self.uploads_dir
+    binhost.PrepareDevInstallBinhostUploads(input_proto, self.response,
+                                            self.mock_call_config)
+    self.assertEqual(len(self.response.upload_targets), 3)
+    self.assertEqual(self.response.upload_targets[2].path, 'Packages')
+    patch.assert_not_called()
+
   def testDevInstallerUpload(self):
     """Basic sanity test testing uploads of dev installer prebuilts."""
     # self.RunStage()