cheets_GTS: Support specifying an authentication key file.

GTS server side flag has switched and started mandating this auth
key to be set. This CL make it injectable from the control files.

BUG=b:112515274
TEST=cheets_GTS.6.0_r1.GtsPlacementTestCases

Change-Id: Ifc3580f7e2fa45cf1a790e752af9c8bb973d325e
Reviewed-on: https://chromium-review.googlesource.com/1173200
Commit-Ready: Kazuhiro Inaba <kinaba@chromium.org>
Tested-by: Kazuhiro Inaba <kinaba@chromium.org>
Reviewed-by: Pohsien Wang <pwang@chromium.org>
(cherry picked from commit 1300e388cb2c8f847422b25530a34b95888dc90f)
Reviewed-on: https://chromium-review.googlesource.com/1176901
Commit-Queue: Kazuhiro Inaba <kinaba@chromium.org>
Reviewed-by: Bernie Thompson <bhthompson@chromium.org>
diff --git a/server/cros/tradefed_test.py b/server/cros/tradefed_test.py
index 5b438d4..2f8d088 100644
--- a/server/cros/tradefed_test.py
+++ b/server/cros/tradefed_test.py
@@ -492,15 +492,11 @@
         @param uri: The Google Storage or dl.google.com uri.
         @return Path to the downloaded object, name.
         """
-        # Split uri into 3 pieces for use by gsutil and also by wget.
-        parsed = urlparse.urlparse(uri)
-        filename = os.path.basename(parsed.path)
         # We are hashing the uri instead of the binary. This is acceptable, as
         # the uris are supposed to contain version information and an object is
         # not supposed to be changed once created.
         output_dir = os.path.join(self._tradefed_cache,
                                   hashlib.md5(uri).hexdigest())
-        output = os.path.join(output_dir, filename)
         # Check for existence of cache entry. We check for directory existence
         # instead of file existence, so that _install_bundle can delete original
         # zip files to save disk space.
@@ -512,8 +508,22 @@
             if os.listdir(output_dir):
                 logging.info('Skipping download of %s, reusing content of %s.',
                              uri, output_dir)
-                return output
+                return os.path.join(output_dir,
+                    os.path.basename(urlparse.urlparse(uri).path))
             logging.error('Empty cache entry detected %s', output_dir)
+        return self._download_to_dir(uri, output_dir)
+
+    def _download_to_dir(self, uri, output_dir):
+        """Downloads the gs|http|https uri from the storage server.
+
+        @param uri: The Google Storage or dl.google.com uri.
+        @output_dir: The directory where the downloaded file should be placed.
+        @return Path to the downloaded object, name.
+        """
+        # Split uri into 3 pieces for use by gsutil and also by wget.
+        parsed = urlparse.urlparse(uri)
+        filename = os.path.basename(parsed.path)
+        output = os.path.join(output_dir, filename)
 
         self._safe_makedirs(output_dir)
         if parsed.scheme not in ['gs', 'http', 'https']:
diff --git a/server/site_tests/cheets_GTS/cheets_GTS.py b/server/site_tests/cheets_GTS/cheets_GTS.py
index c501a19..f3ca7e8 100644
--- a/server/site_tests/cheets_GTS/cheets_GTS.py
+++ b/server/site_tests/cheets_GTS/cheets_GTS.py
@@ -14,6 +14,7 @@
 
 import logging
 import os
+import tempfile
 
 from autotest_lib.server import utils
 from autotest_lib.server.cros import tradefed_test
@@ -73,6 +74,9 @@
         @return: The result object from utils.run.
         """
         gts_tradefed = os.path.join(self._repository, 'tools', 'gts-tradefed')
+        env = None
+        if self._authkey:
+            env = dict(os.environ, APE_API_KEY=self._authkey)
         with tradefed_test.adb_keepalive(self._get_adb_targets(),
                                          self._install_paths):
             for command in commands:
@@ -82,6 +86,7 @@
                 output = self._run(
                     gts_tradefed,
                     args=tuple(command),
+                    env=env,
                     timeout=timeout,
                     verbose=True,
                     ignore_status=False,
@@ -105,6 +110,7 @@
                  needs_push_media=False,
                  precondition_commands=[],
                  login_precondition_commands=[],
+                 authkey=None,
                  timeout=_GTS_TIMEOUT_SECONDS):
         """Runs the specified GTS once, but with several retries.
 
@@ -130,6 +136,12 @@
         dut before the log-in for the test is performed.
         """
 
+        # Download the GTS auth key to the local temp directory.
+        self._authkey = None
+        if authkey:
+            tmpdir = tempfile.mkdtemp()
+            self._authkey = self._download_to_dir(authkey, tmpdir)
+
         # On dev and beta channels timeouts are sharp, lenient on stable.
         self._timeout = timeout
         if self._get_release_channel() == 'stable':