sysroot_controller: Ignore package versions in InstallPackages.

The version is included from the depgraph, but we don't use it for
install packages. Ignore the version when it exists.

BUG=chromium:1124093
TEST=./run_tests

Change-Id: Ib1cca7f406968eb5d11c35d0586512556fe70397
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2392045
Commit-Queue: Alex Klein <saklein@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: Navil Perez <navil@google.com>
diff --git a/api/controller/sysroot.py b/api/controller/sysroot.py
index d632f06..c55e757 100644
--- a/api/controller/sysroot.py
+++ b/api/controller/sysroot.py
@@ -162,8 +162,15 @@
   target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
   build_target = controller_util.ParseBuildTarget(
       input_proto.sysroot.build_target)
-  packages = [controller_util.PackageInfoToString(x)
+
+  # Get the package atom for each specified package. The field is optional, so
+  # error only when we cannot parse an atom for each of the given packages.
+  packages = [controller_util.PackageInfoToCPV(x).cp
               for x in input_proto.packages]
+  if input_proto.packages and not all(packages):
+    cros_build_lib.Die(
+        'Invalid package(s) specified. Unable to parse atom from all packages.')
+
   package_indexes = [
       binpkg.PackageIndexInfo.from_protobuf(x)
       for x in input_proto.package_indexes
diff --git a/api/controller/sysroot_unittest.py b/api/controller/sysroot_unittest.py
index 454ce51..6f2b3fb 100644
--- a/api/controller/sysroot_unittest.py
+++ b/api/controller/sysroot_unittest.py
@@ -13,6 +13,7 @@
 
 from chromite.api import api_config
 from chromite.api import controller
+from chromite.api.controller import controller_util
 from chromite.api.controller import sysroot as sysroot_controller
 from chromite.api.gen.chromite.api import sysroot_pb2
 from chromite.api.gen.chromiumos import common_pb2
@@ -446,7 +447,7 @@
   def _InputProto(self, build_target=None, sysroot_path=None,
                   build_source=False, goma_dir=None, goma_log_dir=None,
                   goma_stats_file=None, goma_counterz_file=None,
-                  package_indexes=None):
+                  package_indexes=None, packages=None):
     """Helper to build an input proto instance."""
     instance = sysroot_pb2.InstallPackagesRequest()
 
@@ -466,7 +467,11 @@
       instance.goma_config.counterz_file = goma_counterz_file
     if package_indexes:
       instance.package_indexes.extend(package_indexes)
-
+    if packages:
+      for pkg in packages:
+        pkg_info = instance.packages.add()
+        cpv = portage_util.SplitCPV(pkg, strict=False)
+        controller_util.CPVToPackageInfo(cpv, pkg_info)
     return instance
 
   def _OutputProto(self):
@@ -554,6 +559,14 @@
     with self.assertRaises(cros_build_lib.DieSystemExit):
       sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config)
 
+  def testArgumentValidationInvalidPackage(self):
+    out_proto = self._OutputProto()
+    in_proto = self._InputProto(build_target=self.build_target,
+                                sysroot_path=self.sysroot,
+                                packages=['package-1.0.0-r2'])
+    with self.assertRaises(cros_build_lib.DieSystemExit):
+      sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config)
+
   def testSuccessOutputHandling(self):
     """Test successful call output handling."""
     # Prevent argument validation error.