cheets_CTS: verify media files after adb push.

This should help us troubleshoot network problems.

BUG=b:32978387
TEST=test_that $DUT cheets_CTS.6.0_r15.x86.android.mediastress

Change-Id: Ie7d581393a033939db1531f34970c7a3b5c52079
Reviewed-on: https://chromium-review.googlesource.com/441936
Reviewed-by: Ilja H. Friedel <ihf@chromium.org>
Tested-by: Ilja H. Friedel <ihf@chromium.org>
diff --git a/server/site_tests/cheets_CTS/cheets_CTS.py b/server/site_tests/cheets_CTS/cheets_CTS.py
index 106e760..5f953b9 100644
--- a/server/site_tests/cheets_CTS/cheets_CTS.py
+++ b/server/site_tests/cheets_CTS/cheets_CTS.py
@@ -16,6 +16,7 @@
 import logging
 import os
 import shutil
+import subprocess
 
 from autotest_lib.client.common_lib import error
 from autotest_lib.server import utils
@@ -104,9 +105,8 @@
         src_plan_file = os.path.join(self.bindir, 'plans', '%s.xml' % plan)
         shutil.copy(src_plan_file, plans_dir)
 
-    def _push_media(self):
-        """Downloads, caches and pushed media files to DUT."""
-        media = self._install_bundle(_CTS_URI['media'])
+    def _copy_media(self, media):
+        """Calls copy_media to push media files to DUT via adb."""
         base = os.path.splitext(os.path.basename(_CTS_URI['media']))[0]
         cts_media = os.path.join(media, base)
         copy_media = os.path.join(cts_media, 'copy_media.sh')
@@ -131,6 +131,36 @@
                 stdout_tee=utils.TEE_TO_LOGS,
                 stderr_tee=utils.TEE_TO_LOGS)
 
+    def _verify_media(self, media):
+        """Verify that the local media directory matches the DUT.
+        Used for debugging b/32978387 where we may see file corruption."""
+        # TODO(ihf): Remove function once b/32978387 is resolved.
+        # Find all files in the bbb_short and bbb_full directories, md5sum these
+        # files and sort by filename. The result for local and DUT hierarchies
+        # is piped through the diff command.
+        cmd = ('diff '
+               '<(adb shell "cd /sdcard/test; '
+                   'find ./bbb_short ./bbb_full -type f -print0 | '
+                   'xargs -0 md5sum | grep -v "\.DS_Store" | sort -k 2")'
+               '<(cd %s; '
+                   'find ./bbb_short ./bbb_full -type f -print0 | '
+                   'xargs -0 md5sum | grep -v "\.DS_Store" | sort -k 2)'
+                   % media)
+        output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
+        if output:
+            logging.error('Some media files differ on DUT /sdcard/test vs. local.')
+            logging.error(output)
+            return False
+        logging.info('Media files identical on DUT /sdcard/test vs. local.')
+        return True
+
+    def _push_media(self):
+        """Downloads, caches and pushed media files to DUT."""
+        media = self._install_bundle(_CTS_URI['media'])
+        self._copy_media(media)
+        if not self._verify_media(media):
+            raise error.TestFail('Error: saw corruption pushing media files.')
+
     def _tradefed_run_command(self,
                               package=None,
                               plan=None,