Home | History | Annotate | Download | only in platform_CrosDisksFormat
      1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 import json
      7 
      8 from autotest_lib.client.bin import test
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.client.cros.cros_disks import CrosDisksTester
     11 from autotest_lib.client.cros.cros_disks import VirtualFilesystemImage
     12 from autotest_lib.client.cros.cros_disks import DefaultFilesystemTestContent
     13 
     14 
     15 class CrosDisksFormatTester(CrosDisksTester):
     16     """A tester to verify format support in CrosDisks.
     17     """
     18     def __init__(self, test, test_configs):
     19         super(CrosDisksFormatTester, self).__init__(test)
     20         self._test_configs = test_configs
     21 
     22     def _run_test_config(self, config):
     23         logging.info('Testing "%s"', config['description'])
     24         filesystem_type = config['filesystem_type']
     25         format_options = config.get('format_options')
     26         # Create a zero-filled virtual filesystem image to help simulate
     27         # a removable drive.
     28         with VirtualFilesystemImage(
     29                 block_size=1024,
     30                 block_count=65536,
     31                 filesystem_type=filesystem_type) as image:
     32             # Attach the zero-filled virtual filesystem image to a loop device
     33             # without actually formatting it.
     34             device_file = image.attach_to_loop_device()
     35 
     36             # Format the virtual filesystem image via CrosDisks.
     37             self.cros_disks.format(device_file, filesystem_type, format_options)
     38             expected_format_completion = {
     39                 'path': device_file
     40             }
     41             if 'expected_format_status' in config:
     42                 expected_format_completion['status'] = \
     43                         config['expected_format_status']
     44             result = self.cros_disks.expect_format_completion(
     45                 expected_format_completion)
     46 
     47             if result['status'] == 0:
     48                 # Test creating and verifying content the formatted device.
     49                 logging.info("Test filesystem access on formatted device")
     50                 test_content = DefaultFilesystemTestContent()
     51                 mount_path = image.mount()
     52                 if not test_content.create(mount_path):
     53                     raise error.TestFail("Failed to create test content")
     54                 if not test_content.verify(mount_path):
     55                     raise error.TestFail("Failed to verify test content")
     56 
     57     def test_using_virtual_filesystem_image(self):
     58         for config in self._test_configs:
     59             self._run_test_config(config)
     60 
     61     def get_tests(self):
     62         return [self.test_using_virtual_filesystem_image]
     63 
     64 
     65 class platform_CrosDisksFormat(test.test):
     66     version = 1
     67 
     68     def run_once(self, *args, **kwargs):
     69         test_configs = []
     70         config_file = '%s/%s' % (self.bindir, kwargs['config_file'])
     71         with open(config_file, 'rb') as f:
     72             test_configs.extend(json.load(f))
     73 
     74         tester = CrosDisksFormatTester(self, test_configs)
     75         tester.run(*args, **kwargs)
     76