brillo_Gtests: Support running in the lab.

* Updated brillo_Gtests to download and install the nativetests
  package when running in the lab.
* Updated/refactored adb_host's download_file method to handle
  unzipping of files.
* Fixed a bug in get_build_from_afe to support Android/Brillo
  builds.

BUG=b:26967765
TEST=Ran brillo_Gtests on a MobLab.

Change-Id: I26c968a8d0a3fc7556e40ee1c11ad0bb4d5c8854
Reviewed-on: https://chromium-review.googlesource.com/336271
Commit-Ready: Simran Basi <sbasi@chromium.org>
Tested-by: Simran Basi <sbasi@chromium.org>
Reviewed-by: Simran Basi <sbasi@chromium.org>
diff --git a/server/hosts/adb_host.py b/server/hosts/adb_host.py
index 4d6c6af..52a6a50 100644
--- a/server/hosts/adb_host.py
+++ b/server/hosts/adb_host.py
@@ -1005,13 +1005,17 @@
 
 
     @retry.retry(error.AutoservRunError, timeout_min=10)
-    def download_file(self, build_url, file, dest_dir):
+    def download_file(self, build_url, file, dest_dir, unzip=False,
+                      unzip_dest=None):
         """Download the given file from the build url.
 
         @param build_url: The url to use for downloading Android artifacts.
                 pattern: http://$devserver:###/static/branch/target/build_id
         @param file: Name of the file to be downloaded, e.g., boot.img.
         @param dest_dir: Destination folder for the file to be downloaded to.
+        @param unzip: If True, unzip the downloaded file.
+        @param unzip_dest: Location to unzip the downloaded file to. If not
+                           provided, dest_dir is used.
         """
         # Append the file name to the url if build_url is linked to the folder
         # containing the file.
@@ -1022,6 +1026,10 @@
         dest_file = os.path.join(dest_dir, file)
         try:
             self.teststation.run('wget -q -O "%s" "%s"' % (dest_file, src_url))
+            if unzip:
+                unzip_dest = unzip_dest or dest_dir
+                self.teststation.run('unzip "%s/%s" -x -d "%s"' %
+                                     (dest_dir, file, unzip_dest))
         except:
             # Delete the destination file if download failed.
             self.teststation.run('rm -f "%s"' % dest_file)
@@ -1041,15 +1049,13 @@
 
         zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info
         image_dir = self.teststation.get_tmp_dir()
-        image_files = [zipped_image_file] + ANDROID_STANDALONE_IMAGES
 
         try:
-            for image_file in image_files:
+            self.download_file(build_url, zipped_image_file, image_dir,
+                               unzip=True)
+            for image_file in ANDROID_STANDALONE_IMAGES:
                 self.download_file(build_url, image_file, image_dir)
 
-            self.teststation.run('unzip "%s/%s" -x -d "%s"' %
-                                 (image_dir, zipped_image_file, image_dir))
-
             return image_dir
         except:
             self.teststation.run('rm -rf %s' % image_dir)
@@ -1070,17 +1076,13 @@
         zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info
         vendor_partitions_file = BRILLO_VENDOR_PARTITIONS_FILE_FMT % build_info
         image_dir = self.teststation.get_tmp_dir()
-        image_files = [zipped_image_file, vendor_partitions_file]
 
         try:
-            for image_file in image_files:
-                self.download_file(build_url, image_file, image_dir)
-
-            self.teststation.run('unzip "%s/%s" -x -d "%s"' %
-                                 (image_dir, zipped_image_file, image_dir))
-            self.teststation.run('unzip "%s/%s" -x -d "%s"' %
-                                 (image_dir, vendor_partitions_file,
-                                  os.path.join(image_dir, 'vendor')))
+            self.download_file(build_url, zipped_image_file, image_dir,
+                               unzip=True)
+            self.download_file(build_url, vendor_partitions_file, image_dir,
+                               unzip=True,
+                               unzip_dest=os.path.join(image_dir, 'vendor'))
             return image_dir
         except:
             self.teststation.run('rm -rf %s' % image_dir)
diff --git a/server/site_tests/brillo_Gtests/brillo_Gtests.py b/server/site_tests/brillo_Gtests/brillo_Gtests.py
index 08e7cb8..501d0d7 100644
--- a/server/site_tests/brillo_Gtests/brillo_Gtests.py
+++ b/server/site_tests/brillo_Gtests/brillo_Gtests.py
@@ -9,12 +9,15 @@
 
 import common
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros import dev_server
+from autotest_lib.server import afe_utils
 from autotest_lib.server import site_gtest_runner
 from autotest_lib.server import test
 
 
 NATIVE_TESTS_PATH = '/data/nativetest'
 WHITELIST_FILE = '/data/nativetest/tests.txt'
+NATIVE_TESTS_FILE_FMT = '%(build_target)s-brillo-tests-%(build_id)s.zip'
 LIST_TEST_BINARIES_TEMPLATE = (
         'find %(path)s -type f -mindepth 2 -maxdepth 2 '
         '\( -perm -100 -o -perm -010 -o -perm -001 \)')
@@ -26,6 +29,36 @@
     version = 1
 
 
+    def initialize(self, host=None, gtest_suites=None, use_whitelist=False,
+                   filter_tests=None, native_tests=None):
+        if not afe_utils.host_in_lab(host):
+            return
+        self._install_nativetests(host)
+
+
+    def _install_nativetests(self, host):
+        """Install the nativetests zip onto the DUT.
+
+        Device images built by the Android Build System do not have the
+        native gTests installed. This method requests the devserver to
+        download the nativetests package into the lab, the test station
+        will download/unzip the package, and finally install it onto the DUT.
+
+        @param host: host object to install the nativetests onto.
+        """
+        build = afe_utils.get_build(host)
+        ds = dev_server.AndroidBuildServer.resolve(build)
+        ds.stage_artifacts(image=build, artifacts=['nativetests'])
+        build_url = os.path.join(ds.url(), 'static', build)
+        nativetests_file = (NATIVE_TESTS_FILE_FMT %
+                            host.get_build_info_from_build_url(build_url))
+        tmp_dir = host.teststation.get_tmp_dir()
+        host.download_file(build_url, nativetests_file, tmp_dir, unzip=True)
+        host.adb_run('push %s %s' % (os.path.join(tmp_dir, 'DATA',
+                                                  'nativetest'),
+                                     NATIVE_TESTS_PATH))
+
+
     def _get_whitelisted_tests(self, whitelist_path):
         """Return the list of whitelisted tests.
 
diff --git a/server/site_utils.py b/server/site_utils.py
index 4865e09..330c6f9 100644
--- a/server/site_utils.py
+++ b/server/site_utils.py
@@ -18,6 +18,7 @@
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib import global_config
 from autotest_lib.client.common_lib import host_queue_entry_states
+from autotest_lib.server.cros import provision
 from autotest_lib.server.cros.dynamic_suite import constants
 from autotest_lib.server.cros.dynamic_suite import job_status
 
@@ -149,7 +150,12 @@
              were multiple build labels assigned to this host.
 
     """
-    return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe)
+    for prefix in [provision.CROS_VERSION_PREFIX,
+                   provision.ANDROID_BUILD_VERSION_PREFIX]:
+        build = get_label_from_afe(hostname, prefix + ':', afe)
+        if build:
+            return build
+    return None
 
 
 def get_sheriffs(lab_only=False):