Home | History | Annotate | Download | only in lisa
      1 # SPDX-License-Identifier: Apache-2.0
      2 #
      3 # Copyright (C) 2017, ARM Limited and contributors.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
      6 # 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, WITHOUT
     13 # 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 """Base classes and utilities for self-testing LISA's wlgen packages"""
     19 
     20 import os
     21 import shutil
     22 from unittest import TestCase
     23 
     24 from devlib import LocalLinuxTarget, Platform
     25 
     26 dummy_calibration = {}
     27 
     28 class TestTarget(LocalLinuxTarget):
     29     """
     30     Devlib target for self-testing LISA
     31 
     32     Uses LocalLinuxTarget configured to disallow using root.
     33     Adds facility to record the commands that were executed for asserting LISA
     34     behaviour.
     35     """
     36     def __init__(self):
     37         self.execute_calls = []
     38         super(TestTarget, self).__init__(platform=Platform(),
     39                                          load_default_modules=False,
     40                                          connection_settings={'unrooted': True})
     41 
     42     def execute(self, *args, **kwargs):
     43         self.execute_calls.append((args, kwargs))
     44         return super(TestTarget, self).execute(*args, **kwargs)
     45 
     46     @property
     47     def executed_commands(self):
     48         return [args[0] if args else kwargs['command']
     49                 for args, kwargs in self.execute_calls]
     50 
     51     def clear_execute_calls(self):
     52         self.execute_calls = []
     53 
     54 class WlgenSelfBase(TestCase):
     55     """
     56     Base class for wlgen self-tests
     57 
     58     Creates and sets up a TestTarget.
     59 
     60     Provides directory paths to use for output files. Deletes those paths if
     61     they already exist, to try and provide a clean test environment. This
     62     doesn't create those paths, tests should create them if necessary.
     63     """
     64 
     65     tools = []
     66     """Tools to install on the 'target' before each test"""
     67 
     68     @property
     69     def target_run_dir(self):
     70         """Unique directory to use for creating files on the 'target'"""
     71         return os.path.join(self.target.working_directory,
     72                             'lisa_target_{}'.format(self.__class__.__name__))
     73 
     74     @property
     75     def host_out_dir(self):
     76         """Unique directory to use for creating files on the host"""
     77         return os.path.join(
     78             os.getenv('LISA_HOME'), 'results',
     79             'lisa_selftest_out_{}'.format(self.__class__.__name__))
     80 
     81     def setUp(self):
     82         self.target = TestTarget()
     83 
     84         tools_path = os.path.join(os.getenv('LISA_HOME'),
     85                                   'tools', self.target.abi)
     86         self.target.setup([os.path.join(tools_path, tool)
     87                            for tool in self.tools])
     88 
     89         if self.target.directory_exists(self.target_run_dir):
     90             self.target.remove(self.target_run_dir)
     91 
     92         if os.path.isdir(self.host_out_dir):
     93             shutil.rmtree(self.host_out_dir)
     94 
     95         self.target.clear_execute_calls()
     96