1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com> 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun""" 7*4882a593SmuzhiyunKconfig unit testing framework. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunThis provides fixture functions commonly used from test files. 10*4882a593Smuzhiyun""" 11*4882a593Smuzhiyun 12*4882a593Smuzhiyunimport os 13*4882a593Smuzhiyunimport pytest 14*4882a593Smuzhiyunimport shutil 15*4882a593Smuzhiyunimport subprocess 16*4882a593Smuzhiyunimport tempfile 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunCONF_PATH = os.path.abspath(os.path.join('scripts', 'kconfig', 'conf')) 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun 21*4882a593Smuzhiyunclass Conf: 22*4882a593Smuzhiyun """Kconfig runner and result checker. 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun This class provides methods to run text-based interface of Kconfig 25*4882a593Smuzhiyun (scripts/kconfig/conf) and retrieve the resulted configuration, 26*4882a593Smuzhiyun stdout, and stderr. It also provides methods to compare those 27*4882a593Smuzhiyun results with expectations. 28*4882a593Smuzhiyun """ 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun def __init__(self, request): 31*4882a593Smuzhiyun """Create a new Conf instance. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun request: object to introspect the requesting test module 34*4882a593Smuzhiyun """ 35*4882a593Smuzhiyun # the directory of the test being run 36*4882a593Smuzhiyun self._test_dir = os.path.dirname(str(request.fspath)) 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun # runners 39*4882a593Smuzhiyun def _run_conf(self, mode, dot_config=None, out_file='.config', 40*4882a593Smuzhiyun interactive=False, in_keys=None, extra_env={}): 41*4882a593Smuzhiyun """Run text-based Kconfig executable and save the result. 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun mode: input mode option (--oldaskconfig, --defconfig=<file> etc.) 44*4882a593Smuzhiyun dot_config: .config file to use for configuration base 45*4882a593Smuzhiyun out_file: file name to contain the output config data 46*4882a593Smuzhiyun interactive: flag to specify the interactive mode 47*4882a593Smuzhiyun in_keys: key inputs for interactive modes 48*4882a593Smuzhiyun extra_env: additional environments 49*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 50*4882a593Smuzhiyun """ 51*4882a593Smuzhiyun command = [CONF_PATH, mode, 'Kconfig'] 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun # Override 'srctree' environment to make the test as the top directory 54*4882a593Smuzhiyun extra_env['srctree'] = self._test_dir 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun # Run Kconfig in a temporary directory. 57*4882a593Smuzhiyun # This directory is automatically removed when done. 58*4882a593Smuzhiyun with tempfile.TemporaryDirectory() as temp_dir: 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun # if .config is given, copy it to the working directory 61*4882a593Smuzhiyun if dot_config: 62*4882a593Smuzhiyun shutil.copyfile(os.path.join(self._test_dir, dot_config), 63*4882a593Smuzhiyun os.path.join(temp_dir, '.config')) 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun ps = subprocess.Popen(command, 66*4882a593Smuzhiyun stdin=subprocess.PIPE, 67*4882a593Smuzhiyun stdout=subprocess.PIPE, 68*4882a593Smuzhiyun stderr=subprocess.PIPE, 69*4882a593Smuzhiyun cwd=temp_dir, 70*4882a593Smuzhiyun env=dict(os.environ, **extra_env)) 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun # If input key sequence is given, feed it to stdin. 73*4882a593Smuzhiyun if in_keys: 74*4882a593Smuzhiyun ps.stdin.write(in_keys.encode('utf-8')) 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun while ps.poll() is None: 77*4882a593Smuzhiyun # For interactive modes such as oldaskconfig, oldconfig, 78*4882a593Smuzhiyun # send 'Enter' key until the program finishes. 79*4882a593Smuzhiyun if interactive: 80*4882a593Smuzhiyun ps.stdin.write(b'\n') 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun self.retcode = ps.returncode 83*4882a593Smuzhiyun self.stdout = ps.stdout.read().decode() 84*4882a593Smuzhiyun self.stderr = ps.stderr.read().decode() 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun # Retrieve the resulted config data only when .config is supposed 87*4882a593Smuzhiyun # to exist. If the command fails, the .config does not exist. 88*4882a593Smuzhiyun # 'listnewconfig' does not produce .config in the first place. 89*4882a593Smuzhiyun if self.retcode == 0 and out_file: 90*4882a593Smuzhiyun with open(os.path.join(temp_dir, out_file)) as f: 91*4882a593Smuzhiyun self.config = f.read() 92*4882a593Smuzhiyun else: 93*4882a593Smuzhiyun self.config = None 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun # Logging: 96*4882a593Smuzhiyun # Pytest captures the following information by default. In failure 97*4882a593Smuzhiyun # of tests, the captured log will be displayed. This will be useful to 98*4882a593Smuzhiyun # figure out what has happened. 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun print("[command]\n{}\n".format(' '.join(command))) 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun print("[retcode]\n{}\n".format(self.retcode)) 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun print("[stdout]") 105*4882a593Smuzhiyun print(self.stdout) 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun print("[stderr]") 108*4882a593Smuzhiyun print(self.stderr) 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun if self.config is not None: 111*4882a593Smuzhiyun print("[output for '{}']".format(out_file)) 112*4882a593Smuzhiyun print(self.config) 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun return self.retcode 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun def oldaskconfig(self, dot_config=None, in_keys=None): 117*4882a593Smuzhiyun """Run oldaskconfig. 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun dot_config: .config file to use for configuration base (optional) 120*4882a593Smuzhiyun in_key: key inputs (optional) 121*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 122*4882a593Smuzhiyun """ 123*4882a593Smuzhiyun return self._run_conf('--oldaskconfig', dot_config=dot_config, 124*4882a593Smuzhiyun interactive=True, in_keys=in_keys) 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun def oldconfig(self, dot_config=None, in_keys=None): 127*4882a593Smuzhiyun """Run oldconfig. 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun dot_config: .config file to use for configuration base (optional) 130*4882a593Smuzhiyun in_key: key inputs (optional) 131*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 132*4882a593Smuzhiyun """ 133*4882a593Smuzhiyun return self._run_conf('--oldconfig', dot_config=dot_config, 134*4882a593Smuzhiyun interactive=True, in_keys=in_keys) 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun def olddefconfig(self, dot_config=None): 137*4882a593Smuzhiyun """Run olddefconfig. 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun dot_config: .config file to use for configuration base (optional) 140*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 141*4882a593Smuzhiyun """ 142*4882a593Smuzhiyun return self._run_conf('--olddefconfig', dot_config=dot_config) 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun def defconfig(self, defconfig): 145*4882a593Smuzhiyun """Run defconfig. 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun defconfig: defconfig file for input 148*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 149*4882a593Smuzhiyun """ 150*4882a593Smuzhiyun defconfig_path = os.path.join(self._test_dir, defconfig) 151*4882a593Smuzhiyun return self._run_conf('--defconfig={}'.format(defconfig_path)) 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun def _allconfig(self, mode, all_config): 154*4882a593Smuzhiyun if all_config: 155*4882a593Smuzhiyun all_config_path = os.path.join(self._test_dir, all_config) 156*4882a593Smuzhiyun extra_env = {'KCONFIG_ALLCONFIG': all_config_path} 157*4882a593Smuzhiyun else: 158*4882a593Smuzhiyun extra_env = {} 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun return self._run_conf('--{}config'.format(mode), extra_env=extra_env) 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun def allyesconfig(self, all_config=None): 163*4882a593Smuzhiyun """Run allyesconfig. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun all_config: fragment config file for KCONFIG_ALLCONFIG (optional) 166*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 167*4882a593Smuzhiyun """ 168*4882a593Smuzhiyun return self._allconfig('allyes', all_config) 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun def allmodconfig(self, all_config=None): 171*4882a593Smuzhiyun """Run allmodconfig. 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun all_config: fragment config file for KCONFIG_ALLCONFIG (optional) 174*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 175*4882a593Smuzhiyun """ 176*4882a593Smuzhiyun return self._allconfig('allmod', all_config) 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun def allnoconfig(self, all_config=None): 179*4882a593Smuzhiyun """Run allnoconfig. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun all_config: fragment config file for KCONFIG_ALLCONFIG (optional) 182*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 183*4882a593Smuzhiyun """ 184*4882a593Smuzhiyun return self._allconfig('allno', all_config) 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun def alldefconfig(self, all_config=None): 187*4882a593Smuzhiyun """Run alldefconfig. 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun all_config: fragment config file for KCONFIG_ALLCONFIG (optional) 190*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 191*4882a593Smuzhiyun """ 192*4882a593Smuzhiyun return self._allconfig('alldef', all_config) 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun def randconfig(self, all_config=None): 195*4882a593Smuzhiyun """Run randconfig. 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun all_config: fragment config file for KCONFIG_ALLCONFIG (optional) 198*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 199*4882a593Smuzhiyun """ 200*4882a593Smuzhiyun return self._allconfig('rand', all_config) 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun def savedefconfig(self, dot_config): 203*4882a593Smuzhiyun """Run savedefconfig. 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun dot_config: .config file for input 206*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 207*4882a593Smuzhiyun """ 208*4882a593Smuzhiyun return self._run_conf('--savedefconfig', out_file='defconfig') 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun def listnewconfig(self, dot_config=None): 211*4882a593Smuzhiyun """Run listnewconfig. 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun dot_config: .config file to use for configuration base (optional) 214*4882a593Smuzhiyun returncode: exit status of the Kconfig executable 215*4882a593Smuzhiyun """ 216*4882a593Smuzhiyun return self._run_conf('--listnewconfig', dot_config=dot_config, 217*4882a593Smuzhiyun out_file=None) 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun # checkers 220*4882a593Smuzhiyun def _read_and_compare(self, compare, expected): 221*4882a593Smuzhiyun """Compare the result with expectation. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun compare: function to compare the result with expectation 224*4882a593Smuzhiyun expected: file that contains the expected data 225*4882a593Smuzhiyun """ 226*4882a593Smuzhiyun with open(os.path.join(self._test_dir, expected)) as f: 227*4882a593Smuzhiyun expected_data = f.read() 228*4882a593Smuzhiyun return compare(self, expected_data) 229*4882a593Smuzhiyun 230*4882a593Smuzhiyun def _contains(self, attr, expected): 231*4882a593Smuzhiyun return self._read_and_compare( 232*4882a593Smuzhiyun lambda s, e: getattr(s, attr).find(e) >= 0, 233*4882a593Smuzhiyun expected) 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun def _matches(self, attr, expected): 236*4882a593Smuzhiyun return self._read_and_compare(lambda s, e: getattr(s, attr) == e, 237*4882a593Smuzhiyun expected) 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun def config_contains(self, expected): 240*4882a593Smuzhiyun """Check if resulted configuration contains expected data. 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun expected: file that contains the expected data 243*4882a593Smuzhiyun returncode: True if result contains the expected data, False otherwise 244*4882a593Smuzhiyun """ 245*4882a593Smuzhiyun return self._contains('config', expected) 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun def config_matches(self, expected): 248*4882a593Smuzhiyun """Check if resulted configuration exactly matches expected data. 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun expected: file that contains the expected data 251*4882a593Smuzhiyun returncode: True if result matches the expected data, False otherwise 252*4882a593Smuzhiyun """ 253*4882a593Smuzhiyun return self._matches('config', expected) 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun def stdout_contains(self, expected): 256*4882a593Smuzhiyun """Check if resulted stdout contains expected data. 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun expected: file that contains the expected data 259*4882a593Smuzhiyun returncode: True if result contains the expected data, False otherwise 260*4882a593Smuzhiyun """ 261*4882a593Smuzhiyun return self._contains('stdout', expected) 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun def stdout_matches(self, expected): 264*4882a593Smuzhiyun """Check if resulted stdout exactly matches expected data. 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun expected: file that contains the expected data 267*4882a593Smuzhiyun returncode: True if result matches the expected data, False otherwise 268*4882a593Smuzhiyun """ 269*4882a593Smuzhiyun return self._matches('stdout', expected) 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun def stderr_contains(self, expected): 272*4882a593Smuzhiyun """Check if resulted stderr contains expected data. 273*4882a593Smuzhiyun 274*4882a593Smuzhiyun expected: file that contains the expected data 275*4882a593Smuzhiyun returncode: True if result contains the expected data, False otherwise 276*4882a593Smuzhiyun """ 277*4882a593Smuzhiyun return self._contains('stderr', expected) 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun def stderr_matches(self, expected): 280*4882a593Smuzhiyun """Check if resulted stderr exactly matches expected data. 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun expected: file that contains the expected data 283*4882a593Smuzhiyun returncode: True if result matches the expected data, False otherwise 284*4882a593Smuzhiyun """ 285*4882a593Smuzhiyun return self._matches('stderr', expected) 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun@pytest.fixture(scope="module") 289*4882a593Smuzhiyundef conf(request): 290*4882a593Smuzhiyun """Create a Conf instance and provide it to test functions.""" 291*4882a593Smuzhiyun return Conf(request) 292