blob: 9acb92ddc448c5241842e86eb4fb4b278c49f3c1 [file] [log] [blame]
Stephen Warrenef824f52016-01-22 12:30:12 -07001# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
2#
3# SPDX-License-Identifier: GPL-2.0
4
5# Utility code shared across multiple tests.
6
7import hashlib
Stephen Warrena3b8f992017-10-26 18:23:35 -06008import inspect
Stephen Warrenef824f52016-01-22 12:30:12 -07009import os
10import os.path
Heiko Schocher56549012016-05-09 10:08:24 +020011import pytest
Stephen Warrenef824f52016-01-22 12:30:12 -070012import sys
13import time
Heiko Schocher28404c62016-05-06 07:33:51 +020014import pytest
Stephen Warrenef824f52016-01-22 12:30:12 -070015
16def md5sum_data(data):
Stephen Warren75e731e2016-01-26 13:41:30 -070017 """Calculate the MD5 hash of some data.
Stephen Warrenef824f52016-01-22 12:30:12 -070018
19 Args:
20 data: The data to hash.
21
22 Returns:
23 The hash of the data, as a binary string.
Stephen Warren75e731e2016-01-26 13:41:30 -070024 """
Stephen Warrenef824f52016-01-22 12:30:12 -070025
26 h = hashlib.md5()
27 h.update(data)
28 return h.digest()
29
30def md5sum_file(fn, max_length=None):
Stephen Warren75e731e2016-01-26 13:41:30 -070031 """Calculate the MD5 hash of the contents of a file.
Stephen Warrenef824f52016-01-22 12:30:12 -070032
33 Args:
34 fn: The filename of the file to hash.
35 max_length: The number of bytes to hash. If the file has more
36 bytes than this, they will be ignored. If None or omitted, the
37 entire file will be hashed.
38
39 Returns:
40 The hash of the file content, as a binary string.
Stephen Warren75e731e2016-01-26 13:41:30 -070041 """
Stephen Warrenef824f52016-01-22 12:30:12 -070042
43 with open(fn, 'rb') as fh:
44 if max_length:
45 params = [max_length]
46 else:
47 params = []
48 data = fh.read(*params)
49 return md5sum_data(data)
50
51class PersistentRandomFile(object):
Stephen Warren75e731e2016-01-26 13:41:30 -070052 """Generate and store information about a persistent file containing
53 random data."""
Stephen Warrenef824f52016-01-22 12:30:12 -070054
55 def __init__(self, u_boot_console, fn, size):
Stephen Warren75e731e2016-01-26 13:41:30 -070056 """Create or process the persistent file.
Stephen Warrenef824f52016-01-22 12:30:12 -070057
58 If the file does not exist, it is generated.
59
60 If the file does exist, its content is hashed for later comparison.
61
62 These files are always located in the "persistent data directory" of
63 the current test run.
64
65 Args:
66 u_boot_console: A console connection to U-Boot.
67 fn: The filename (without path) to create.
68 size: The desired size of the file in bytes.
69
70 Returns:
71 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070072 """
Stephen Warrenef824f52016-01-22 12:30:12 -070073
74 self.fn = fn
75
76 self.abs_fn = u_boot_console.config.persistent_data_dir + '/' + fn
77
78 if os.path.exists(self.abs_fn):
79 u_boot_console.log.action('Persistent data file ' + self.abs_fn +
80 ' already exists')
81 self.content_hash = md5sum_file(self.abs_fn)
82 else:
83 u_boot_console.log.action('Generating ' + self.abs_fn +
84 ' (random, persistent, %d bytes)' % size)
85 data = os.urandom(size)
86 with open(self.abs_fn, 'wb') as fh:
87 fh.write(data)
88 self.content_hash = md5sum_data(data)
89
90def attempt_to_open_file(fn):
Stephen Warren75e731e2016-01-26 13:41:30 -070091 """Attempt to open a file, without throwing exceptions.
Stephen Warrenef824f52016-01-22 12:30:12 -070092
93 Any errors (exceptions) that occur during the attempt to open the file
94 are ignored. This is useful in order to test whether a file (in
95 particular, a device node) exists and can be successfully opened, in order
96 to poll for e.g. USB enumeration completion.
97
98 Args:
99 fn: The filename to attempt to open.
100
101 Returns:
102 An open file handle to the file, or None if the file could not be
103 opened.
Stephen Warren75e731e2016-01-26 13:41:30 -0700104 """
Stephen Warrenef824f52016-01-22 12:30:12 -0700105
106 try:
107 return open(fn, 'rb')
108 except:
109 return None
110
111def wait_until_open_succeeds(fn):
Stephen Warren75e731e2016-01-26 13:41:30 -0700112 """Poll until a file can be opened, or a timeout occurs.
Stephen Warrenef824f52016-01-22 12:30:12 -0700113
114 Continually attempt to open a file, and return when this succeeds, or
115 raise an exception after a timeout.
116
117 Args:
118 fn: The filename to attempt to open.
119
120 Returns:
121 An open file handle to the file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700122 """
Stephen Warrenef824f52016-01-22 12:30:12 -0700123
124 for i in xrange(100):
125 fh = attempt_to_open_file(fn)
126 if fh:
127 return fh
128 time.sleep(0.1)
129 raise Exception('File could not be opened')
130
131def wait_until_file_open_fails(fn, ignore_errors):
Stephen Warren75e731e2016-01-26 13:41:30 -0700132 """Poll until a file cannot be opened, or a timeout occurs.
Stephen Warrenef824f52016-01-22 12:30:12 -0700133
134 Continually attempt to open a file, and return when this fails, or
135 raise an exception after a timeout.
136
137 Args:
138 fn: The filename to attempt to open.
139 ignore_errors: Indicate whether to ignore timeout errors. If True, the
140 function will simply return if a timeout occurs, otherwise an
141 exception will be raised.
142
143 Returns:
144 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700145 """
Stephen Warrenef824f52016-01-22 12:30:12 -0700146
147 for i in xrange(100):
148 fh = attempt_to_open_file(fn)
149 if not fh:
150 return
151 fh.close()
152 time.sleep(0.1)
153 if ignore_errors:
154 return
155 raise Exception('File can still be opened')
156
157def run_and_log(u_boot_console, cmd, ignore_errors=False):
Stephen Warren75e731e2016-01-26 13:41:30 -0700158 """Run a command and log its output.
Stephen Warrenef824f52016-01-22 12:30:12 -0700159
160 Args:
161 u_boot_console: A console connection to U-Boot.
Simon Glassba8116c2016-07-31 17:35:05 -0600162 cmd: The command to run, as an array of argv[], or a string.
163 If a string, note that it is split up so that quoted spaces
164 will not be preserved. E.g. "fred and" becomes ['"fred', 'and"']
Stephen Warrenef824f52016-01-22 12:30:12 -0700165 ignore_errors: Indicate whether to ignore errors. If True, the function
166 will simply return if the command cannot be executed or exits with
167 an error code, otherwise an exception will be raised if such
168 problems occur.
169
170 Returns:
Simon Glassdb16a3d2016-07-03 09:40:39 -0600171 The output as a string.
Stephen Warren75e731e2016-01-26 13:41:30 -0700172 """
Simon Glassba8116c2016-07-31 17:35:05 -0600173 if isinstance(cmd, str):
174 cmd = cmd.split()
Stephen Warrenef824f52016-01-22 12:30:12 -0700175 runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
Simon Glassdb16a3d2016-07-03 09:40:39 -0600176 output = runner.run(cmd, ignore_errors=ignore_errors)
Stephen Warrenef824f52016-01-22 12:30:12 -0700177 runner.close()
Simon Glassdb16a3d2016-07-03 09:40:39 -0600178 return output
Stephen Warrenf7743ce2016-01-21 16:05:30 -0700179
Simon Glassbe86ac62016-07-03 09:40:41 -0600180def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg):
Simon Glassd5deca02016-07-31 17:35:04 -0600181 """Run a command that is expected to fail.
Simon Glassbe86ac62016-07-03 09:40:41 -0600182
183 This runs a command and checks that it fails with the expected return code
184 and exception method. If not, an exception is raised.
185
186 Args:
187 u_boot_console: A console connection to U-Boot.
188 cmd: The command to run, as an array of argv[].
189 retcode: Expected non-zero return code from the command.
Simon Glassd5deca02016-07-31 17:35:04 -0600190 msg: String that should be contained within the command's output.
Simon Glassbe86ac62016-07-03 09:40:41 -0600191 """
192 try:
193 runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
194 runner.run(cmd)
195 except Exception as e:
Simon Glass95adb802016-07-31 17:35:03 -0600196 assert(retcode == runner.exit_status)
Simon Glassbe86ac62016-07-03 09:40:41 -0600197 assert(msg in runner.output)
198 else:
Simon Glass95adb802016-07-31 17:35:03 -0600199 raise Exception("Expected an exception with retcode %d message '%s',"
200 "but it was not raised" % (retcode, msg))
Simon Glassbe86ac62016-07-03 09:40:41 -0600201 finally:
202 runner.close()
203
Stephen Warrenf7743ce2016-01-21 16:05:30 -0700204ram_base = None
205def find_ram_base(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700206 """Find the running U-Boot's RAM location.
Stephen Warrenf7743ce2016-01-21 16:05:30 -0700207
208 Probe the running U-Boot to determine the address of the first bank
209 of RAM. This is useful for tests that test reading/writing RAM, or
210 load/save files that aren't associated with some standard address
211 typically represented in an environment variable such as
212 ${kernel_addr_r}. The value is cached so that it only needs to be
213 actively read once.
214
215 Args:
216 u_boot_console: A console connection to U-Boot.
217
218 Returns:
219 The address of U-Boot's first RAM bank, as an integer.
Stephen Warren75e731e2016-01-26 13:41:30 -0700220 """
Stephen Warrenf7743ce2016-01-21 16:05:30 -0700221
222 global ram_base
223 if u_boot_console.config.buildconfig.get('config_cmd_bdi', 'n') != 'y':
224 pytest.skip('bdinfo command not supported')
225 if ram_base == -1:
226 pytest.skip('Previously failed to find RAM bank start')
227 if ram_base is not None:
228 return ram_base
229
230 with u_boot_console.log.section('find_ram_base'):
231 response = u_boot_console.run_command('bdinfo')
232 for l in response.split('\n'):
Daniel Schwierzeckda631742016-07-06 12:44:22 +0200233 if '-> start' in l or 'memstart =' in l:
Stephen Warrenf7743ce2016-01-21 16:05:30 -0700234 ram_base = int(l.split('=')[1].strip(), 16)
235 break
236 if ram_base is None:
237 ram_base = -1
238 raise Exception('Failed to find RAM bank start in `bdinfo`')
239
240 return ram_base
Stephen Warrena3b8f992017-10-26 18:23:35 -0600241
242class PersistentFileHelperCtxMgr(object):
243 """A context manager for Python's "with" statement, which ensures that any
244 generated file is deleted (and hence regenerated) if its mtime is older
245 than the mtime of the Python module which generated it, and gets an mtime
246 newer than the mtime of the Python module which generated after it is
247 generated. Objects of this type should be created by factory function
248 persistent_file_helper rather than directly."""
249
250 def __init__(self, log, filename):
251 """Initialize a new object.
252
253 Args:
254 log: The Logfile object to log to.
255 filename: The filename of the generated file.
256
257 Returns:
258 Nothing.
259 """
260
261 self.log = log
262 self.filename = filename
263
264 def __enter__(self):
265 frame = inspect.stack()[1]
266 module = inspect.getmodule(frame[0])
267 self.module_filename = module.__file__
268 self.module_timestamp = os.path.getmtime(self.module_filename)
269
270 if os.path.exists(self.filename):
271 filename_timestamp = os.path.getmtime(self.filename)
272 if filename_timestamp < self.module_timestamp:
273 self.log.action('Removing stale generated file ' +
274 self.filename)
275 os.unlink(self.filename)
276
277 def __exit__(self, extype, value, traceback):
278 if extype:
279 try:
280 os.path.unlink(self.filename)
281 except:
282 pass
283 return
284 logged = False
285 for i in range(20):
286 filename_timestamp = os.path.getmtime(self.filename)
287 if filename_timestamp > self.module_timestamp:
288 break
289 if not logged:
290 self.log.action(
291 'Waiting for generated file timestamp to increase')
292 logged = True
293 os.utime(self.filename)
294 time.sleep(0.1)
295
296def persistent_file_helper(u_boot_log, filename):
297 """Manage the timestamps and regeneration of a persistent generated
298 file. This function creates a context manager for Python's "with"
299 statement
300
301 Usage:
302 with persistent_file_helper(u_boot_console.log, filename):
303 code to generate the file, if it's missing.
304
305 Args:
306 u_boot_log: u_boot_console.log.
307 filename: The filename of the generated file.
308
309 Returns:
310 A context manager object.
311 """
312
313 return PersistentFileHelperCtxMgr(u_boot_log, filename)