Home | History | Annotate | Download | only in early_mount
      1 #!/usr/bin/python
      2 #
      3 # Copyright 2017 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 # http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 """Test cases for device tree overlays for early mounting partitions."""
     18 
     19 import os
     20 import unittest
     21 
     22 
     23 # Early mount fstab entry must have following properties defined.
     24 REQUIRED_FSTAB_PROPERTIES = [
     25     'dev',
     26     'type',
     27     'mnt_flags',
     28     'fsmgr_flags',
     29 ]
     30 
     31 
     32 def ReadFile(file_path):
     33   with open(file_path, 'r') as f:
     34     # Strip all trailing spaces, newline and null characters.
     35     return f.read().rstrip(' \n\x00')
     36 
     37 
     38 def GetAndroidDtDir():
     39   """Returns location of android device tree directory."""
     40   with open('/proc/cmdline', 'r') as f:
     41     cmdline_list = f.read().split()
     42 
     43   # Find android device tree directory path passed through kernel cmdline.
     44   for option in cmdline_list:
     45     if option.startswith('androidboot.android_dt_dir'):
     46       return option.split('=')[1]
     47 
     48   # If no custom path found, return the default location.
     49   return '/proc/device-tree/firmware/android'
     50 
     51 
     52 class DtEarlyMountTest(unittest.TestCase):
     53   """Test device tree overlays for early mounting."""
     54 
     55   def setUp(self):
     56     self._android_dt_dir = GetAndroidDtDir()
     57     self._fstab_dt_dir = os.path.join(self._android_dt_dir, 'fstab')
     58 
     59   def GetEarlyMountedPartitions(self):
     60     """Returns a list of partitions specified in fstab for early mount."""
     61     # Device tree nodes are represented as directories in the filesystem.
     62     return [x for x in os.listdir(self._fstab_dt_dir) if os.path.isdir(x)]
     63 
     64   def VerifyFstabEntry(self, partition):
     65     partition_dt_dir = os.path.join(self._fstab_dt_dir, partition)
     66     properties = [x for x in os.listdir(partition_dt_dir)]
     67 
     68     self.assertTrue(
     69         set(REQUIRED_FSTAB_PROPERTIES).issubset(properties),
     70         'fstab entry for /%s is missing required properties' % partition)
     71 
     72   def testFstabCompatible(self):
     73     """Verify fstab compatible string."""
     74     compatible = ReadFile(os.path.join(self._fstab_dt_dir, 'compatible'))
     75     self.assertEqual('android,fstab', compatible)
     76 
     77   def testFstabEntries(self):
     78     """Verify properties of early mount fstab entries."""
     79     for partition in self.GetEarlyMountedPartitions():
     80       self.VerifyFstabEntry(partition)
     81 
     82 if __name__ == '__main__':
     83   unittest.main()
     84 
     85