Add handling for build_target in BundleChromeOSConfig.

- Needed so this endpoint can handle current Recipes
calls that set build_target.

- Add test to api/controller/artifacts_unittest.py.

- Also add test in service/artifacts_unittest.py for
case when no config payload is found.

- Add call template for BundleChromeOSConfig.

TEST=./run_tests
BUG=chromium:987401

Change-Id: I9b7da6e5f2de71a3ada8ca36f66e594c54dc2175
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1756263
Tested-by: Andrew Lamb <andrewlamb@chromium.org>
Reviewed-by: David Burger <dburger@chromium.org>
Commit-Queue: Andrew Lamb <andrewlamb@chromium.org>
diff --git a/api/controller/artifacts.py b/api/controller/artifacts.py
index 09799d5..07f0ed4 100644
--- a/api/controller/artifacts.py
+++ b/api/controller/artifacts.py
@@ -322,9 +322,18 @@
     _config (api_config.ApiConfig): The API call config.
   """
   output_dir = input_proto.output_dir
-  sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
+  sysroot_path = input_proto.sysroot.path
   chroot = controller_util.ParseChroot(input_proto.chroot)
 
+  # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
+  target = input_proto.build_target.name
+  if target:
+    # Legacy handling.
+    build_root = constants.SOURCE_ROOT
+    chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
+    sysroot_path = os.path.join('/build', target)
+
+  sysroot = sysroot_lib.Sysroot(sysroot_path)
   chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
   if chromeos_config is None:
     cros_build_lib.Die(
diff --git a/api/controller/artifacts_unittest.py b/api/controller/artifacts_unittest.py
index 10995e2..43b28c2 100644
--- a/api/controller/artifacts_unittest.py
+++ b/api/controller/artifacts_unittest.py
@@ -407,6 +407,54 @@
       artifacts.BundleEbuildLogs(self.request, self.response, self.api_config)
 
 
+class BundleChromeOSConfigTest(BundleTestCase):
+  """Unittests for BundleChromeOSConfig"""
+
+  def testValidateOnly(self):
+    """Sanity check that a validate only call does not execute any logic."""
+    patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
+    artifacts.BundleChromeOSConfig(self.input_proto, self.output_proto,
+                                   self.validate_only_config)
+    patch.assert_not_called()
+
+  def testBundleChromeOSConfigCallWithSysroot(self):
+    """Call with a request that sets sysroot."""
+    bundle_chromeos_config = self.PatchObject(
+        artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
+    artifacts.BundleChromeOSConfig(self.request, self.output_proto,
+                                   self.api_config)
+    self.assertEqual(
+        [artifact.path for artifact in self.output_proto.artifacts],
+        [os.path.join(self.output_dir, 'config.yaml')])
+
+    sysroot = sysroot_lib.Sysroot(self.sysroot_path)
+    self.assertEqual(bundle_chromeos_config.call_args_list,
+                     [mock.call(mock.ANY, sysroot, self.output_dir)])
+
+  def testBundleChromeOSConfigCallWithBuildTarget(self):
+    """Call with a request that sets build_target."""
+    bundle_chromeos_config = self.PatchObject(
+        artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
+    artifacts.BundleChromeOSConfig(self.input_proto, self.output_proto,
+                                   self.api_config)
+
+    self.assertEqual(
+        [artifact.path for artifact in self.output_proto.artifacts],
+        [os.path.join(self.output_dir, 'config.yaml')])
+
+    sysroot = sysroot_lib.Sysroot(self.sysroot_path)
+    self.assertEqual(bundle_chromeos_config.call_args_list,
+                     [mock.call(mock.ANY, sysroot, self.output_dir)])
+
+  def testBundleChromeOSConfigNoConfigFound(self):
+    """An error is raised if the config payload isn't found."""
+    self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None)
+
+    with self.assertRaises(cros_build_lib.DieSystemExit):
+      artifacts.BundleChromeOSConfig(self.request, self.output_proto,
+                                     self.api_config)
+
+
 class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase,
                                    api_config.ApiConfigMixin):
   """Unittests for BundleTestUpdatePayloads."""