Use the last two parts of the archive path to determine version string.

On builders, cros_generate_test_payloads may run sequentially for two
different boards with the same version. Archive paths are given with
.../board_config/version. Use the last two parts and not just the version
when caching.

BUG=chromium:328615
TEST=Unittests

Change-Id: I57be88b09f0d26211f0e8799c81e07e40fb2cf2b
Reviewed-on: https://chromium-review.googlesource.com/180683
Tested-by: Chris Sosa <sosa@chromium.org>
Reviewed-by: Yu-Ju Hong <yjhong@chromium.org>
Commit-Queue: Chris Sosa <sosa@chromium.org>
diff --git a/lib/image_extractor.py b/lib/image_extractor.py
index a3596e2..6ae33f1 100644
--- a/lib/image_extractor.py
+++ b/lib/image_extractor.py
@@ -63,13 +63,20 @@
     string. In order to save time, if it is attempting
     to re-unzip the same image with the same version string, it uses the
     cached image in SRC_ARCHIVE_DIR. It determines the version string based
-    on the basename of the image_dir.
+    on the last path parts of the image_dir.
 
-    Returns: the path to the image.bin file after it has been unzipped.
-    Raises: MissingImageZipException if there is nothing to unzip within
+    Args:
+      image_dir: Directory with image to unzip.
+
+    Returns:
+      The path to the image.bin file after it has been unzipped.
+
+    Raises:
+      MissingImageZipException if there is nothing to unzip within
       the image_dir.
     """
-    version_string = os.path.basename(image_dir)
+    # Use the last 2 paths as the version_string path (may include board id).
+    version_string = os.path.join(*image_dir.split(os.path.sep)[-2:])
     cached_dir = os.path.join(ImageExtractor.SRC_ARCHIVE_DIR, version_string)
     cached_image = os.path.abspath(os.path.join(
         cached_dir, self.image_to_extract))
diff --git a/lib/image_extractor_unittest.py b/lib/image_extractor_unittest.py
index 0004371..a4218ee 100755
--- a/lib/image_extractor_unittest.py
+++ b/lib/image_extractor_unittest.py
@@ -27,9 +27,9 @@
     super(ImageExtractorTest, self).setUp()
 
     self.work_dir = tempfile.mkdtemp('ImageExtractorTest')
+    self.board = 'x86-generic-full'
     # Set constants to be easily testable.
-    self.archive_dir = os.path.join(self.work_dir, 'archive',
-                                    'x86-generic-full')
+    self.archive_dir = os.path.join(self.work_dir, 'archive', self.board)
     image_extractor.ImageExtractor.SRC_ARCHIVE_DIR = os.path.join(self.work_dir,
                                                                   'src')
     # Our test object.
@@ -115,9 +115,9 @@
 
   def testUnzipImageArchiveAlready(self):
     """Ensure we create a new archive and delete the old one."""
-    old_entry = os.path.join(self.src_archive, 'R16-158.0.0-a1')
+    old_entry = os.path.join(self.src_archive, self.board, 'R16-158.0.0-a1')
     os.makedirs(old_entry)
-    new_entry = os.path.join(self.src_archive, 'R16-158.0.1-a1')
+    new_entry = os.path.join(self.src_archive, self.board, 'R16-158.0.1-a1')
     archived_image_dir = os.path.join(self.archive_dir, 'R16-158.0.1-a1')
     ImageExtractorTest._TouchImageZip(archived_image_dir)
 
@@ -132,7 +132,7 @@
 
   def testUnzipImageNoArchive(self):
     """Ensure we create a new archive with none before."""
-    new_entry = os.path.join(self.src_archive, 'R16-158.0.1-a1')
+    new_entry = os.path.join(self.src_archive, self.board, 'R16-158.0.1-a1')
     archived_image_dir = os.path.join(self.archive_dir, 'R16-158.0.1-a1')
     ImageExtractorTest._TouchImageZip(archived_image_dir)