Merge "Leave the evidence lying around if an adb test fails."

This commit is contained in:
Elliott Hughes 2015-08-03 21:03:35 +00:00 committed by Gerrit Code Review
commit 0fac7fd152

View file

@ -333,107 +333,96 @@ class FileOperationsTest(DeviceTest):
def _test_push(self, local_file, checksum): def _test_push(self, local_file, checksum):
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE]) self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
try: self.device.push(local=local_file, remote=self.DEVICE_TEMP_FILE)
self.device.push( dev_md5, _ = self.device.shell([get_md5_prog(self.device),
local=local_file, remote=self.DEVICE_TEMP_FILE) self.DEVICE_TEMP_FILE]).split()
dev_md5, _ = self.device.shell( self.assertEqual(checksum, dev_md5)
[get_md5_prog(self.device), self.DEVICE_TEMP_FILE]).split() self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
self.assertEqual(checksum, dev_md5)
finally:
self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
def test_push(self): def test_push(self):
"""Push a randomly generated file to specified device.""" """Push a randomly generated file to specified device."""
kbytes = 512 kbytes = 512
tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False) tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False)
try: rand_str = os.urandom(1024 * kbytes)
rand_str = os.urandom(1024 * kbytes) tmp.write(rand_str)
tmp.write(rand_str) tmp.close()
tmp.close() self._test_push(tmp.name, compute_md5(rand_str))
self._test_push(tmp.name, compute_md5(rand_str)) os.remove(tmp.name)
finally:
os.remove(tmp.name)
# TODO: write push directory test. # TODO: write push directory test.
def _test_pull(self, remote_file, checksum): def _test_pull(self, remote_file, checksum):
tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False) tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
try: tmp_write.close()
tmp_write.close() self.device.pull(remote=remote_file, local=tmp_write.name)
self.device.pull(remote=remote_file, local=tmp_write.name) with open(tmp_write.name, 'rb') as tmp_read:
with open(tmp_write.name, 'rb') as tmp_read: host_contents = tmp_read.read()
host_contents = tmp_read.read() host_md5 = compute_md5(host_contents)
host_md5 = compute_md5(host_contents) self.assertEqual(checksum, host_md5)
self.assertEqual(checksum, host_md5) os.remove(tmp_write.name)
finally:
os.remove(tmp_write.name)
def test_pull(self): def test_pull(self):
"""Pull a randomly generated file from specified device.""" """Pull a randomly generated file from specified device."""
kbytes = 512 kbytes = 512
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE]) self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
try: cmd = ['dd', 'if=/dev/urandom',
cmd = ['dd', 'if=/dev/urandom', 'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024',
'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024', 'count={}'.format(kbytes)]
'count={}'.format(kbytes)] self.device.shell(cmd)
self.device.shell(cmd) dev_md5, _ = self.device.shell(
dev_md5, _ = self.device.shell( [get_md5_prog(self.device), self.DEVICE_TEMP_FILE]).split()
[get_md5_prog(self.device), self.DEVICE_TEMP_FILE]).split() self._test_pull(self.DEVICE_TEMP_FILE, dev_md5)
self._test_pull(self.DEVICE_TEMP_FILE, dev_md5) self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE])
finally:
self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE])
def test_pull_dir(self): def test_pull_dir(self):
"""Pull a randomly generated directory of files from the device.""" """Pull a randomly generated directory of files from the device."""
host_dir = tempfile.mkdtemp() host_dir = tempfile.mkdtemp()
try: self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
# Populate device directory with random files. # Populate device directory with random files.
temp_files = make_random_device_files( temp_files = make_random_device_files(
self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32) self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir) self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir)
for temp_file in temp_files: for temp_file in temp_files:
host_path = os.path.join(host_dir, temp_file.base_name) host_path = os.path.join(host_dir, temp_file.base_name)
with open(host_path, 'rb') as host_file: with open(host_path, 'rb') as host_file:
host_md5 = compute_md5(host_file.read()) host_md5 = compute_md5(host_file.read())
self.assertEqual(host_md5, temp_file.checksum) self.assertEqual(host_md5, temp_file.checksum)
finally:
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
if host_dir is not None: if host_dir is not None:
shutil.rmtree(host_dir) shutil.rmtree(host_dir)
def test_sync(self): def test_sync(self):
"""Sync a randomly generated directory of files to specified device.""" """Sync a randomly generated directory of files to specified device."""
base_dir = tempfile.mkdtemp() base_dir = tempfile.mkdtemp()
try:
# Create mirror device directory hierarchy within base_dir.
full_dir_path = base_dir + self.DEVICE_TEMP_DIR
os.makedirs(full_dir_path)
# Create 32 random files within the host mirror. # Create mirror device directory hierarchy within base_dir.
temp_files = make_random_host_files(in_dir=full_dir_path, full_dir_path = base_dir + self.DEVICE_TEMP_DIR
num_files=32) os.makedirs(full_dir_path)
# Clean up any trash on the device. # Create 32 random files within the host mirror.
device = adb.get_device(product=base_dir) temp_files = make_random_host_files(in_dir=full_dir_path, num_files=32)
device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
device.sync('data') # Clean up any trash on the device.
device = adb.get_device(product=base_dir)
device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
# Confirm that every file on the device mirrors that on the host. device.sync('data')
for temp_file in temp_files:
device_full_path = posixpath.join( # Confirm that every file on the device mirrors that on the host.
self.DEVICE_TEMP_DIR, temp_file.base_name) for temp_file in temp_files:
dev_md5, _ = device.shell( device_full_path = posixpath.join(self.DEVICE_TEMP_DIR,
[get_md5_prog(self.device), device_full_path]).split() temp_file.base_name)
self.assertEqual(temp_file.checksum, dev_md5) dev_md5, _ = device.shell(
finally: [get_md5_prog(self.device), device_full_path]).split()
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) self.assertEqual(temp_file.checksum, dev_md5)
shutil.rmtree(base_dir + self.DEVICE_TEMP_DIR)
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
shutil.rmtree(base_dir + self.DEVICE_TEMP_DIR)
def test_unicode_paths(self): def test_unicode_paths(self):
"""Ensure that we can support non-ASCII paths, even on Windows.""" """Ensure that we can support non-ASCII paths, even on Windows."""