afdo: Fix breakage due to renaming of Chrome debug binary

Since we uploaded Chrome debug binary with a new name, the
following recipes also need to change to use the new name

Also fix a tiny bug that merge Android/Linux crash when one
profile to merge.

BUG=chromium:1092541
TEST=unittest; staging builder

Change-Id: I66b039ac56f698c9153c90920a1b32efa87447a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2341847
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: George Burgess <gbiv@chromium.org>
Commit-Queue: Tiancong Wang <tcwang@google.com>
Tested-by: Tiancong Wang <tcwang@google.com>
diff --git a/lib/toolchain_util.py b/lib/toolchain_util.py
index 2cb4a74..66b9221 100644
--- a/lib/toolchain_util.py
+++ b/lib/toolchain_util.py
@@ -1767,7 +1767,7 @@
       src_dir: A temp path holding the possible artifacts. It needs to be an
       absolute path.
     """
-    assert os.path.isabs(src_dir), '%s needs to be an absolute path '% src_dir
+    assert os.path.isabs(src_dir), '%s needs to be an absolute path ' % src_dir
     check_dirs = [
         self.chroot.full_path(x)
         for x in [src_dir,
@@ -1901,7 +1901,8 @@
       osutils.RmDir(workdir_full, ignore_missing=True, sudo=True)
       osutils.SafeMakedirs(workdir_full)
 
-      bin_name = os.path.basename(_CHROME_DEBUG_BIN) + BZ2_COMPRESSION_SUFFIX
+      bin_name = self._GetBenchmarkAFDOName(
+          CHROME_DEBUG_BINARY_NAME) + BZ2_COMPRESSION_SUFFIX
       bin_compressed = self._AfdoTmpPath(bin_name)
       bin_url = self._FindArtifact(
           bin_name, self.input_artifacts.get('ChromeDebugBinary', []))
@@ -2246,7 +2247,8 @@
     CHROME_UNSTRIPPED_NAME = 'chrome.unstripped'
     bin_path_in = self._AfdoTmpPath(CHROME_UNSTRIPPED_NAME)
     osutils.SafeSymlink(
-        os.path.basename(_CHROME_DEBUG_BIN), self.chroot.full_path(bin_path_in))
+        self._GetBenchmarkAFDOName(CHROME_DEBUG_BINARY_NAME),
+        self.chroot.full_path(bin_path_in))
     perf_path_inside = self._AfdoTmpPath(
         self._GetBenchmarkAFDOName(template=CHROME_PERF_AFDO_FILE))
     afdo_name = self._GetBenchmarkAFDOName()
@@ -2291,6 +2293,9 @@
     # Merge recent benchmark profiles for Android/Linux use
     merged_profile = self._CreateAndUploadMergedAFDOProfile(
         os.path.join(output_dir_full, afdo_name), output_dir_full)
+    if not merged_profile:
+      return []
+
     merged_profile_inside = self._AfdoTmpPath(os.path.basename(merged_profile))
     merged_profile_compressed = os.path.join(
         self.output_dir,
diff --git a/lib/toolchain_util_unittest.py b/lib/toolchain_util_unittest.py
index 3e9f88d..4a6369e 100644
--- a/lib/toolchain_util_unittest.py
+++ b/lib/toolchain_util_unittest.py
@@ -473,7 +473,8 @@
     expected = [
         mock.call('gs://path/to/unvetted/'
                   'chromeos-chrome-amd64-78.0.3893.0_rc-r1.afdo.bz2'),
-        mock.call('gs://image-archive/path/chrome.debug.bz2'),
+        mock.call('gs://image-archive/path/'
+                  'chromeos-chrome-amd64-78.0.3893.0_rc-r1.debug.bz2'),
         mock.call('gs://path/to/perfdata/'
                   'chromeos-chrome-amd64-78.0.3893.0.perf.data.bz2'),
     ]
@@ -508,6 +509,7 @@
       self.obj._CleanupArtifactDirectory('non/absolute/path')
     self.assertIn('needs to be an absolute path', str(context.exception))
 
+
 class BundleArtifactHandlerTest(PrepareBundleTest):
   """Test BundleArtifactHandler specific methods."""
 
@@ -525,7 +527,7 @@
         'benchmark-78.0.3893.0-r1.orderfile')
     self.afdo_name = 'chromeos-chrome-amd64-78.0.3893.0_rc-r1.afdo'
     self.perf_name = 'chromeos-chrome-amd64-78.0.3893.0.perf.data'
-    self.debug_binary_name = ('chromeos-chrome-amd64-78.0.3893.0_rc-r1.debug')
+    self.debug_binary_name = 'chromeos-chrome-amd64-78.0.3893.0_rc-r1.debug'
     self.merged_afdo_name = (
         'chromeos-chrome-amd64-78.0.3893.0_rc-r1-merged.afdo')
 
@@ -672,7 +674,7 @@
     self.assertEqual([afdo_path], ret)
     # Make sure the sym link to debug Chrome is created
     sym_link_command.assert_called_with(
-        os.path.basename(toolchain_util._CHROME_DEBUG_BIN),
+        self.debug_binary_name,
         self.chroot.full_path(
             os.path.join(self.afdo_tmp_path, 'chrome.unstripped')))
     afdo_path_inside = os.path.join(self.afdo_tmp_path, self.afdo_name)