crosperf: Reboot dut even if checksums match.

We have seen problems where if machine has the image preinstalled
but the machine has been ON for a long time, the reports can be
bizzare.
So lets reboot the machine before the experiments.

BUG=chromium:957149
TEST=verified that machine reboots with image preinstalled.

Change-Id: I32af330700f9b60801c365cb20ac54b304a82c3a
Reviewed-on: https://chromium-review.googlesource.com/1585616
Commit-Ready: Manoj Gupta <manojgupta@chromium.org>
Tested-by: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
diff --git a/image_chromeos.py b/image_chromeos.py
index aa8824b..bea6fe5 100755
--- a/image_chromeos.py
+++ b/image_chromeos.py
@@ -1,6 +1,10 @@
 #!/usr/bin/env python2
+# -*- coding: utf-8 -*-
 #
-# Copyright 2011 Google Inc. All Rights Reserved.
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
 """Script to image a ChromeOS device.
 
 This script images a remote ChromeOS device with a specific image."
@@ -238,11 +242,6 @@
       cmd_executer.CrosRunCommand(
           command, chromeos_root=options.chromeos_root, machine=options.remote)
 
-      real_src_dir = os.path.join(
-          os.path.realpath(options.chromeos_root), 'src')
-      real_chroot_dir = os.path.join(
-          os.path.realpath(options.chromeos_root), 'chroot')
-
       # Check to see if cros flash will work for the remote machine.
       CheckForCrosFlash(options.chromeos_root, options.remote, log_level)
 
@@ -289,7 +288,7 @@
       # If this is a non-local image, then the ret returned from
       # EnsureMachineUp is the one that will be returned by this function;
       # in that case, make sure the value in 'ret' is appropriate.
-      if not local_image and ret == True:
+      if not local_image and ret is True:
         ret = 0
       else:
         ret = 1
@@ -312,12 +311,18 @@
         TryRemountPartitionAsRW(options.chromeos_root, options.remote,
                                 log_level)
 
-      if found == False:
+      if found is False:
         temp_dir = os.path.dirname(located_image)
         l.LogOutput('Deleting temp image dir: %s' % temp_dir)
         shutil.rmtree(temp_dir)
     else:
-      l.LogOutput('Checksums match. Skipping reimage')
+      l.LogOutput('Checksums match. Skipping reimage, doing a reboot.')
+      command = 'reboot && exit'
+      _ = cmd_executer.CrosRunCommand(
+          command, chromeos_root=options.chromeos_root, machine=options.remote)
+      # Wait 30s after reboot.
+      time.sleep(30)
+
     return ret
   finally:
     if should_unlock:
@@ -338,8 +343,8 @@
     return [True, image]
 
   # First search within the existing build dirs for any matching files.
-  images_glob = ('%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath,
-                                                     board_glob))
+  images_glob = (
+      '%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath, board_glob))
   images_list = glob.glob(images_glob)
   for potential_image in images_list:
     if filecmp.cmp(potential_image, image):
@@ -359,7 +364,7 @@
   return [False, new_image]
 
 
-def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
+def GetImageMountCommand(image, rootfs_mp, stateful_mp):
   image_dir = os.path.dirname(image)
   image_file = os.path.basename(image)
   mount_command = ('cd ~/trunk/src/scripts &&'
@@ -379,7 +384,7 @@
                unmount=False,
                extra_commands=''):
   cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
-  command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
+  command = GetImageMountCommand(image, rootfs_mp, stateful_mp)
   if unmount:
     command = '%s --unmount' % command
   if extra_commands:
@@ -440,10 +445,7 @@
   [_, o, _] = cmd_executer.CrosRunCommandWOutput(
       command, chromeos_root=chromeos_root, machine=remote)
   device_chrome_checksum = o.split()[0]
-  if image_chrome_checksum.strip() == device_chrome_checksum.strip():
-    return True
-  else:
-    return False
+  return image_chrome_checksum.strip() == device_chrome_checksum.strip()
 
 
 # Remount partition as writable.