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/442052
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 754b961..5263a1b 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
@@ -139,6 +140,36 @@
         media = self._install_bundle(_CTS_URI['media'])
         self._copy_media(media)
 
+    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,