Add --target_image flag to ctest.

If multiple images are being built in parallel, ctest needs to
know what image to run so it is guaranteed to pick the right
image. This avoids flakiness and race conditions.

BUG=chromium-os:18856
TEST=Run ctest with and without --target_image option.

Change-Id: I311434b3da1c77f6ef9aa604d4e7f480b336ff81
Reviewed-on: http://gerrit.chromium.org/gerrit/5490
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/ctest/ctest.py b/ctest/ctest.py
index ed11019..e493e7c 100755
--- a/ctest/ctest.py
+++ b/ctest/ctest.py
@@ -240,7 +240,8 @@
 
 
 def RunAUTestHarness(board, channel, zip_server_base,
-                     no_graphics, type, remote, clean, test_results_root):
+                     no_graphics, type, remote, clean, target_image,
+                     test_results_root):
   """Runs the auto update test harness.
 
   The auto update test harness encapsulates testing the auto-update mechanism
@@ -256,17 +257,19 @@
     type: which test harness to run.  Possible values: real, vm.
     remote: ip address for real test harness run.
     clean: Clean the state of test harness before running.
+    target_image: Target image to test.
     test_results_root: Root directory to store au_test_harness results.
   """
   crosutils_root = os.path.join(constants.SOURCE_ROOT, 'src', 'scripts')
 
-  # Grab the latest image we've built.
-  return_object = cros_lib.RunCommand(
-    ['./get_latest_image.sh', '--board=%s' % board], cwd=crosutils_root,
-    redirect_stdout=True, print_cmd=True)
+  if target_image is None:
+    # Grab the latest image we've built.
+    return_object = cros_lib.RunCommand(
+      ['./get_latest_image.sh', '--board=%s' % board], cwd=crosutils_root,
+      redirect_stdout=True, print_cmd=True)
 
-  latest_image_dir = return_object.output.strip()
-  target_image = os.path.join(latest_image_dir, _IMAGE_TO_EXTRACT)
+    latest_image_dir = return_object.output.strip()
+    target_image = os.path.join(latest_image_dir, _IMAGE_TO_EXTRACT)
 
   # Grab the latest official build for this board to use as the base image.
   # If it doesn't exist, run the update test against itself.
@@ -317,6 +320,8 @@
                     help='Base url for hosted images.')
   parser.add_option('--no_graphics', action='store_true', default=False,
                     help='Disable graphics for the vm test.')
+  parser.add_option('--target_image', default=None,
+                    help='Target image to test.')
   parser.add_option('--test_results_root', default=None,
                     help='Root directory to store test results.  Should '
                          'be defined relative to chroot root.')
@@ -336,7 +341,8 @@
 
   RunAUTestHarness(options.board, options.channel, options.zipbase,
                    options.no_graphics, options.type, options.remote,
-                   not options.cache, options.test_results_root)
+                   not options.cache, options.target_image,
+                   options.test_results_root)
 
 
 if __name__ == '__main__':