Home | History | Annotate | Download | only in version
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2018 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 
     18 import logging
     19 import re
     20 
     21 from vts.runners.host import asserts
     22 from vts.runners.host import base_test
     23 from vts.runners.host import const
     24 from vts.runners.host import keys
     25 from vts.runners.host import test_runner
     26 from vts.utils.python.controllers import android_device
     27 from vts.utils.python.file import target_file_utils
     28 
     29 from vts.testcases.kernel.lib import version
     30 
     31 
     32 class VtsKernelVersionTest(base_test.BaseTestClass):
     33     """Test case which verifies the version of the kernel on the DUT is
     34     supported.
     35     """
     36 
     37     def setUpClass(self):
     38         required_params = [keys.ConfigKeys.IKEY_DATA_FILE_PATH]
     39         self.getUserParams(required_params)
     40         self.dut = self.android_devices[0]
     41         self.shell = self.dut.shell
     42         self.supported_kernel_versions = version.getSupportedKernels(self.dut)
     43 
     44     def testKernelVersion(self):
     45         """Validate the kernel version of DUT is a valid kernel version.
     46 
     47         Returns:
     48             string, kernel version of device
     49         """
     50         cmd = "uname -a"
     51         results = self.shell.Execute(cmd)
     52         logging.info("Shell command '%s' results: %s", cmd, results)
     53 
     54         match = re.search(r"(\d+)\.(\d+)\.(\d+)", results[const.STDOUT][0])
     55         if match is None:
     56             asserts.fail("Failed to detect kernel version of device.")
     57         else:
     58             kernel_version = int(match.group(1))
     59             kernel_patchlevel = int(match.group(2))
     60             kernel_sublevel = int(match.group(3))
     61         logging.info("Detected kernel version: %s.%s.%s" % (kernel_version,
     62             kernel_patchlevel, kernel_sublevel))
     63 
     64         for v in self.supported_kernel_versions:
     65             if (kernel_version == v[0] and kernel_patchlevel == v[1] and
     66                 kernel_sublevel >= v[2]):
     67                 logging.info("Compliant kernel version %s.%s.%s found." %
     68                         (kernel_version, kernel_patchlevel, kernel_sublevel))
     69                 return
     70 
     71         asserts.fail("Device is running an unsupported kernel version (%s.%s.%s)" %
     72                 (kernel_version, kernel_patchlevel, kernel_sublevel))
     73 
     74 
     75 if __name__ == "__main__":
     76     test_runner.main()
     77