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