ctest: Move the remaining ctest capabilities to cros_au_test_harness

Ctest don't have much and it is calling cros_au_test_harness
anyway. After this we can call cros_au_test_harness directly.

Also deprecate finding target_image automatically. This logic is not
needed here and is fragile.

BUG=chromium:872441
TEST=relevant unittests
TEST=betty-release-tryjob

Change-Id: I32e816c1f8ad4a0f33231eef584499fa03713353
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crostestutils/+/1828293
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
diff --git a/au_test_harness/cros_au_test_harness.py b/au_test_harness/cros_au_test_harness.py
index 422bfc6..9fd964b 100755
--- a/au_test_harness/cros_au_test_harness.py
+++ b/au_test_harness/cros_au_test_harness.py
@@ -34,7 +34,6 @@
 from chromite.lib import timeout_util
 
 from crostestutils.au_test_harness import au_test
-
 from crostestutils.lib import test_helper
 
 
@@ -145,22 +144,32 @@
     opts.test_results_root = tempfile.mkdtemp(
         prefix='au_test_harness', dir=chroot_tmp)
 
+  # Force absolute path for target_image, since a chdir occurs deeper in the
+  # codebase.
+  if opts.type != 'gce':
+    # In this case target_image is a not a Google Storage path.
+    opts.target_image = os.path.abspath(opts.target_image)
+
+  if not opts.base_image:
+    logging.info('Did not pass the base image to use. Using target instead.')
+    opts.base_image = opts.target_image
+
 
 def main():
   test_helper.SetupCommonLoggingFormat()
   parser = argparse.ArgumentParser()
   parser.add_argument('--base_image',
                       help='path to the base image.')
-  parser.add_argument('--board',
+  parser.add_argument('--board', required=True,
                       help='board for the images.')
   parser.add_argument('--no_graphics', action='store_true',
                       help='Disable graphics for the vm test.')
   parser.add_argument('-j', '--jobs',
                       default=test_helper.CalculateDefaultJobs(), type=int,
                       help='Number of simultaneous jobs')
-  parser.add_argument('--target_image',
+  parser.add_argument('--target_image', required=True,
                       help='path to the target image.')
-  parser.add_argument('--test_results_root', default=None,
+  parser.add_argument('--test_results_root', type='path', default=None,
                       help='Root directory to store test results.  Should '
                       'be defined relative to chroot root.')
   parser.add_argument('--test_prefix', default='test',
@@ -180,7 +189,7 @@
                       action='store_true',
                       help='Run multiple test stages in parallel (applies only '
                       'to vm tests). Default: False')
-  parser.add_argument('--ssh_private_key', default=None,
+  parser.add_argument('--ssh_private_key', type='path', default=None,
                       help='Path to the private key to use to ssh into the '
                       'image as the root user.')
   parser.add_argument('--ssh_port', default=None, type=int,
diff --git a/au_test_harness/cros_au_test_harness_unittest.py b/au_test_harness/cros_au_test_harness_unittest.py
index b0ce25e..05951e6 100755
--- a/au_test_harness/cros_au_test_harness_unittest.py
+++ b/au_test_harness/cros_au_test_harness_unittest.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python2
+#!/usr/bin/env python2
 # -*- coding: utf-8 -*-
 # Copyright 2015 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
diff --git a/ctest/ctest.py b/ctest/ctest.py
index c8aee19..be01e9a 100755
--- a/ctest/ctest.py
+++ b/ctest/ctest.py
@@ -21,7 +21,6 @@
 from chromite.lib import cros_build_lib
 from chromite.lib import cros_logging as logging
 
-from crostestutils.lib import image_extractor
 from crostestutils.lib import test_helper
 
 
@@ -62,24 +61,6 @@
     self.ssh_private_key = opts.ssh_private_key
     self.ssh_port = opts.ssh_port
 
-  def FindTargetAndBaseImages(self):
-    """Initializes the target and base images for CTest."""
-    if not self.target:
-      # Grab the latest image we've built.
-      board_images = os.path.join(constants.SOURCE_ROOT, 'src/build/images',
-                                  self.board)
-      latest = os.path.join(board_images, 'latest')
-      version_dir = os.readlink(latest)
-      latest_image_dir = os.path.join(board_images, version_dir)
-
-      self.target = os.path.join(
-          latest_image_dir, image_extractor.ImageExtractor.IMAGE_TO_EXTRACT)
-
-    if not self.base:
-      logging.info('Could not find a latest image to use. '
-                   'Using target instead.')
-      self.base = self.target
-
   def RunTestHarness(self, only_verify, suite):
     """Runs the test harness (suite:smoke).
 
@@ -160,23 +141,10 @@
 
   opts = parser.parse_args()
 
-  if not opts.board:
-    parser.error('Need board for image to compare against.')
   if opts.ssh_port and not opts.only_verify:
     parser.error('ssh_port should be specified with either --only_verify')
 
-  # force absolute path for these opts, since a chdir occurs deeper in the
-  # codebase.
-  for x in ('target_image', 'test_results_root'):
-    if x == 'target_image' and opts.type == 'gce':
-      # In this case |target_image| is a Google Storage path.
-      continue
-    val = getattr(opts, x)
-    if val is not None:
-      setattr(opts, x, os.path.abspath(val))
-
   ctest = CTest(opts)
-  ctest.FindTargetAndBaseImages()
   try:
     ctest.RunTestHarness(opts.only_verify, opts.suite)
   except TestException as e:
diff --git a/ctest/ctest_unittest.py b/ctest/ctest_unittest.py
deleted file mode 100755
index e460bed..0000000
--- a/ctest/ctest_unittest.py
+++ /dev/null
@@ -1,126 +0,0 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
-#
-# Copyright (c) 2012 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.
-
-"""Test module containing unittests for CTest."""
-
-from __future__ import print_function
-
-import mox
-import os
-import sys
-import unittest
-
-import constants
-sys.path.append(constants.SOURCE_ROOT)
-sys.path.append(constants.CROS_PLATFORM_ROOT)
-
-from chromite.lib import cros_build_lib
-from crostestutils.lib import image_extractor
-import ctest
-
-
-class CTestTest(mox.MoxTestBase):
-  """Testing the test-worthy methods in CTest."""
-
-  FAKE_ROOT = '/fake/root'
-  ARCHIVE_DIR = os.path.join(FAKE_ROOT, 'x86-generic-full')
-
-  def testFindTargetAndBaseImagesNoBaseNoArchive(self):
-    """Tests whether we can set the right vars if no Base image found.
-
-    If no base is found and there is no latest image, we should test the target
-    against the base.
-    """
-    self.mox.StubOutWithMock(ctest.CTest, '__init__')
-    self.mox.StubOutWithMock(image_extractor.ImageExtractor, 'GetLatestImage')
-
-    # TODO(sosa): Can we create mock objects but call a real method in an easier
-    # way?
-    ctest.CTest.__init__(None)
-    # pylint: disable=no-value-for-parameter
-    # For some reason pylint is not happy with the way mox mocks out functions.
-    image_extractor.ImageExtractor.GetLatestImage(
-        'target_version').AndReturn(None)
-    # pylint: enable=no-value-for-parameter
-
-    self.mox.ReplayAll()
-    ctester = ctest.CTest(None) # Calls mocked out __init__.
-    ctester.target = 'some_image/target_version/file.bin'
-    ctester.base = None
-    ctester.FindTargetAndBaseImages()
-    self.mox.VerifyAll()
-    self.assertEqual(ctester.base, ctester.target)
-
-  def testFindTargetAndBaseImagesBaseWithLatest(self):
-    """Tests whether we can set the right vars if base image found in archive.
-
-    Tests whether if we find a latest image, that we unzip it and set the
-    base accordingly.
-    """
-    self.mox.StubOutWithMock(ctest.CTest, '__init__')
-    self.mox.StubOutWithMock(image_extractor.ImageExtractor, 'GetLatestImage')
-    self.mox.StubOutWithMock(image_extractor.ImageExtractor, 'UnzipImage')
-
-    latest_base_dir = '/some/fake/path'
-    latest_base_path = os.path.join(
-        '/some/fake/path', image_extractor.ImageExtractor.IMAGE_TO_EXTRACT)
-
-    ctest.CTest.__init__(None)
-    # pylint: disable=no-value-for-parameter
-    image_extractor.ImageExtractor.GetLatestImage('target_version').AndReturn(
-        latest_base_dir)
-    image_extractor.ImageExtractor.UnzipImage(latest_base_dir).AndReturn(
-        latest_base_path)
-    # pylint: enable=no-value-for-parameter
-
-    self.mox.ReplayAll()
-    ctester = ctest.CTest(None) # Calls mocked out __init__.
-    ctester.target = 'some_image/target_version/file.bin'
-    ctester.base = None
-    ctester.FindTargetAndBaseImages()
-    self.mox.VerifyAll()
-    self.assertEqual(ctester.base, latest_base_path)
-
-  def testFindTargetAndBaseImagesBaseNothingSetSimple(self):
-    """Tests whether we can set the right vars if no target or base set.
-
-    Tests whether if there is no local archive and no target set, vars are set
-    correctly.  This means target should be inferred and base should be set to
-    target.
-    """
-    self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
-    self.mox.StubOutWithMock(ctest.CTest, '__init__')
-    self.mox.StubOutWithMock(image_extractor.ImageExtractor, 'GetLatestImage')
-    self.mox.StubOutWithMock(os, 'readlink')
-
-    fake_crosutils = os.path.join(self.FAKE_ROOT, 'src', 'scripts')
-    img_dir = os.path.join(constants.SOURCE_ROOT, 'src', 'build', 'images',
-                           'board', 'latest')
-    expected_base = os.path.join(constants.SOURCE_ROOT, 'src', 'build',
-                                 'images', 'board', 'linkval')
-
-    ctest.CTest.__init__(None)
-    # pylint: disable=no-value-for-parameter
-    os.readlink(img_dir).AndReturn('linkval')
-    image_extractor.ImageExtractor.GetLatestImage('linkval').AndReturn(None)
-    # pylint: enable=no-value-for-parameter
-
-    self.mox.ReplayAll()
-    ctester = ctest.CTest(None) # Calls mocked out __init__.
-    ctester.target = None
-    ctester.base = None
-    ctester.board = 'board'
-    ctester.crosutils_root = fake_crosutils
-    ctester.FindTargetAndBaseImages()
-    self.mox.VerifyAll()
-    self.assertEqual(ctester.base, ctester.target)
-    self.assertEqual(ctester.base, os.path.join(
-        expected_base, image_extractor.ImageExtractor.IMAGE_TO_EXTRACT))
-
-
-if __name__ == '__main__':
-  unittest.main()