afdo: Fix orderfile script path inside chroot

Last patch that fixed the source path definition messed up with
a path that's supposed to be within chroot. Fix the definition
there in this patch. Also add more error printing for easier
debugging in the future.

BUG=chromium:1165817
TEST=unittest and staging builder

Change-Id: I30573cec6070c091abe1742e87ef34479c5e8aa5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2625030
Tested-by: Tiancong Wang <tcwang@google.com>
Auto-Submit: Tiancong Wang <tcwang@google.com>
Commit-Queue: Tiancong Wang <tcwang@google.com>
Commit-Queue: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
diff --git a/lib/toolchain_util.py b/lib/toolchain_util.py
index e88f131..6fd468b 100644
--- a/lib/toolchain_util.py
+++ b/lib/toolchain_util.py
@@ -79,8 +79,12 @@
 BZ2_COMPRESSION_SUFFIX = '.bz2'
 XZ_COMPRESSION_SUFFIX = '.xz'
 KERNEL_AFDO_COMPRESSION_SUFFIX = '.gcov.xz'
-TOOLCHAIN_UTILS_PATH = os.path.join(
-    constants.SOURCE_ROOT, 'src/third_party/toolchain-utils')
+# FIXME: we should only use constants.SOURCE_ROOT and use
+# path_util.ToChrootPath to convert to inchroot path when needed. So we
+# need fix all the use cases for this variable (we can remove all but one
+# when legacy is retired).
+TOOLCHAIN_UTILS_PATH = os.path.join(constants.CHROOT_SOURCE_ROOT,
+                                    'src/third_party/toolchain-utils')
 AFDO_PROFILE_PATH_IN_CHROMIUM = 'src/chromeos/profiles/%s.afdo.newest.txt'
 MERGED_AFDO_NAME = 'chromeos-chrome-amd64-%s'
 
@@ -756,10 +760,16 @@
     ]
 
     try:
-      cros_build_lib.run(cmd, enter_chroot=True, chroot_args=self.chroot_args)
-    except cros_build_lib.RunCommandError:
+      cros_build_lib.run(
+          cmd,
+          enter_chroot=True,
+          chroot_args=self.chroot_args,
+          check=True,
+          capture_output=True)
+    except cros_build_lib.RunCommandError as e:
       raise GenerateChromeOrderfileError(
-          'Unable to run %s to process orderfile.' % (cmd))
+          f'Unable to run %s to process orderfile {cmd} '
+          f'with error: {e.result.stdout} {e.result.stderr}.')
 
     # Return path inside chroot
     return result
@@ -2459,10 +2469,7 @@
     def FilterFile(file_path):
       return extension is None or file_path.endswith(extension)
 
-    files = self._CollectFiles(
-        src_dir,
-        destination,
-        include_file=FilterFile)
+    files = self._CollectFiles(src_dir, destination, include_file=FilterFile)
     if not files:
       logging.info('No data found for %s, skip bundle artifact', tarball)
       raise NoArtifactsToBundleError(f'No {extension} files in {src_dir}')
@@ -2479,11 +2486,8 @@
     with self.chroot.tempdir() as tempdir:
       try:
         return [
-          self._CreateBundle(
-            '/tmp/fatal_clang_warnings',
-            'fatal_clang_warnings',
-            tempdir,
-            '.json')
+            self._CreateBundle('/tmp/fatal_clang_warnings',
+                               'fatal_clang_warnings', tempdir, '.json')
         ]
       except NoArtifactsToBundleError:
         return []
@@ -2496,10 +2500,8 @@
     with osutils.TempDir(prefix='clang_crash_diagnoses_tarball') as tempdir:
       try:
         return [
-          self._CreateBundle(
-            '/tmp/clang_crash_diagnostics',
-            'clang_crash_diagnoses',
-            tempdir)
+            self._CreateBundle('/tmp/clang_crash_diagnostics',
+                               'clang_crash_diagnoses', tempdir)
         ]
       except NoArtifactsToBundleError:
         return []
@@ -2514,15 +2516,13 @@
     with self.chroot.tempdir() as tempdir:
       try:
         return [
-          self._CreateBundle(
-            '/tmp/compiler_rusage',
-            'compiler_rusage_logs',
-            tempdir,
-            '.json')
+            self._CreateBundle('/tmp/compiler_rusage', 'compiler_rusage_logs',
+                               tempdir, '.json')
         ]
       except NoArtifactsToBundleError:
         return []
 
+
 def PrepareForBuild(artifact_name, chroot, sysroot_path, build_target,
                     input_artifacts, profile_info):
   """Prepare for building artifacts.
diff --git a/lib/toolchain_util_unittest.py b/lib/toolchain_util_unittest.py
index 944734e..afdcc91 100644
--- a/lib/toolchain_util_unittest.py
+++ b/lib/toolchain_util_unittest.py
@@ -826,20 +826,24 @@
 
     # Test behaviour when artifacts are found.
     self.runToolchainBundleTest(
-      artifact_path,
-      tarball_name,
-      input_files=(
-        'good1.json', 'good2.json', 'good3.json',
-        'bad1.notjson', 'bad2', 'json',
-      ),
-      expected_output_files=(
-          'good1.json',
-          'good2.json',
-          'good3.json',
-          'good10.json',
-          'good20.json',
-          'good30.json',
-      ),
+        artifact_path,
+        tarball_name,
+        input_files=(
+            'good1.json',
+            'good2.json',
+            'good3.json',
+            'bad1.notjson',
+            'bad2',
+            'json',
+        ),
+        expected_output_files=(
+            'good1.json',
+            'good2.json',
+            'good3.json',
+            'good10.json',
+            'good20.json',
+            'good30.json',
+        ),
     )
 
 
@@ -1686,7 +1690,11 @@
         input_orderfile, '--output', output
     ]
     cros_build_lib.run.assert_called_with(
-        cmd, enter_chroot=True, chroot_args=self.chroot_args)
+        cmd,
+        enter_chroot=True,
+        chroot_args=self.chroot_args,
+        check=True,
+        capture_output=True)
 
   def testSuccessRun(self):
     """Test the main function is running successfully."""
@@ -2350,8 +2358,8 @@
 
     with open(self.json_file, 'w') as f:
       json.dump(self.afdo_versions, f)
-    self.PatchObject(git, 'GetTrackingBranch',
-                     return_value=git.RemoteRef('origin', 'main'))
+    self.PatchObject(
+        git, 'GetTrackingBranch', return_value=git.RemoteRef('origin', 'main'))
     GitStatus = collections.namedtuple('GitStatus', ['output'])
     self.mock_git = self.PatchObject(
         git, 'RunGit', return_value=GitStatus(output='non-empty'))
@@ -2385,8 +2393,7 @@
                                               self.afdo_sorted_by_freshness[1],
                                               self.afdo_sorted_by_freshness[2])
     calls = [
-        mock.call(
-            self.tempdir, ['pull', 'origin'], print_cmd=True),
+        mock.call(self.tempdir, ['pull', 'origin'], print_cmd=True),
         mock.call(
             self.tempdir, ['status', '--porcelain', '-uno'],
             capture_output=True,
@@ -2395,10 +2402,7 @@
         mock.call(
             self.tempdir, ['commit', '-a', '-m', message], print_cmd=True),
         mock.call(
-            self.tempdir, [
-                'push', 'origin',
-                'HEAD:main%submit'
-            ],
+            self.tempdir, ['push', 'origin', 'HEAD:main%submit'],
             capture_output=True,
             print_cmd=True)
     ]