1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Integration tests."""
import argparse
import builtins
from collections import defaultdict
import difflib
from functools import wraps
import glob
import json
import os
import unittest
import sys
import tempfile
import pprint
from typing import List, Dict
import disable
import resultdb
def cmd_record(args: argparse.Namespace):
record_testcase(args.name, ['./disable'] + args.args)
def record_testcase(name: str, testcase_args: List[str]):
# While running the test, point CANNED_RESPONSE_FILE to a temporary that we
# can recover data from afterwards.
fd, temp_canned_response_file = tempfile.mkstemp()
os.fdopen(fd).close()
resultdb.CANNED_RESPONSE_FILE = temp_canned_response_file
original_open = builtins.open
builtins.open = opener(original_open)
try:
disable.main(testcase_args)
# TODO: We probably want to test failure cases as well. We can add an
# "exception" field to the testcase JSON and test that the same exception is
# raised.
finally:
builtins.open = original_open
with open(temp_canned_response_file) as f:
recorded_requests = f.read()
os.remove(temp_canned_response_file)
testcase = {
'args': testcase_args,
'requests': recorded_requests,
'read_data': TrackingFile.read_data,
'written_data': TrackingFile.written_data,
}
print(f'Recorded testcase {name}.\nDiff from this testcase is:\n')
print_diffs(TrackingFile.read_data, TrackingFile.written_data)
with open(os.path.join('tests', f'{name}.json'), 'w') as f:
json.dump(testcase, f, indent=2)
TrackingFile.read_data.clear()
TrackingFile.written_data.clear()
def print_diffs(read_data: Dict[str, str], written_data: Dict[str, str]):
def lines(s: str) -> List[str]:
return [line + '\n' for line in s.split('\n')]
for filename in read_data:
if filename in written_data:
before = lines(read_data[filename])
after = lines(written_data[filename])
sys.stdout.writelines(
difflib.unified_diff(before,
after,
fromfile=f'a/{filename}',
tofile=f'b/{filename}'))
def opener(old_open):
@wraps(old_open)
def tracking_open(path, mode='r', **kwargs):
if os.path.abspath(path).startswith(disable.SRC_ROOT):
return TrackingFile(old_open, path, mode, **kwargs)
return old_open(path, mode, **kwargs)
return tracking_open
class TrackingFile:
"""A file-like class that records what data was read/written."""
read_data = {}
written_data = defaultdict(str)
def __init__(self, old_open, path, mode, **kwargs):
self.path = path
if mode != 'w':
self.file = old_open(path, mode, **kwargs)
else:
self.file = None
def read(self, n_bytes=-1):
# It's easier to stash all the results if we only deal with the case where
# all the data is read at once. Right now we can get away with this as the
# tool only does this, but if that changes we'll need to support it here.
assert n_bytes == -1
data = self.file.read(n_bytes)
TrackingFile.read_data[src_root_relative(self.path)] = data
return data
def write(self, data):
# Don't actually write the data, since we're just recording a testcase.
TrackingFile.written_data[src_root_relative(self.path)] += data
def __enter__(self):
return self
def __exit__(self, e_type, e_val, e_tb):
if self.file is not None:
self.file.close()
self.file = None
def src_root_relative(path: str) -> str:
if os.path.abspath(path).startswith(disable.SRC_ROOT):
return os.path.relpath(path, disable.SRC_ROOT)
return path
class IntegrationTest(unittest.TestCase):
"""This class represents a data-driven integration test.
Given a list of arguments to pass to the test disabler, a set of ResultDB
requests and responses to replay, and the data read/written to the filesystem,
run the test disabler in a hermetic test environment and check that the output
is the same.
"""
def __init__(self, name, args, requests, read_data, written_data):
unittest.TestCase.__init__(self, methodName='test_one_testcase')
self.name = name
self.args = args
self.requests = requests
self.read_data = read_data
self.written_data = written_data
def test_one_testcase(self):
fd, temp_canned_response_file = tempfile.mkstemp(text=True)
os.fdopen(fd).close()
with open(temp_canned_response_file, 'w') as f:
f.write(self.requests)
resultdb.CANNED_RESPONSE_FILE = temp_canned_response_file
TrackingFile.read_data.clear()
TrackingFile.written_data.clear()
with tempfile.TemporaryDirectory() as temp_dir:
disable.SRC_ROOT = temp_dir
for filename, contents in self.read_data.items():
in_temp = os.path.join(temp_dir, filename)
os.makedirs(os.path.dirname(in_temp))
with open(in_temp, 'w') as f:
f.write(contents)
original_open = builtins.open
builtins.open = opener(original_open)
try:
disable.main(self.args)
finally:
os.remove(temp_canned_response_file)
builtins.open = original_open
for path, data in TrackingFile.written_data.items():
if path == temp_canned_response_file:
continue
relpath = src_root_relative(path)
self.assertIn(relpath, self.written_data)
self.assertEqual(data, self.written_data[relpath])
def shortDescription(self):
return self.name
def cmd_show(args: argparse.Namespace):
try:
with open(os.path.join('tests', f'{args.name}.json'), 'r') as f:
testcase = json.load(f)
except FileNotFoundError:
print(f"No such testcase '{args.name}'", file=sys.stderr)
sys.exit(1)
command_line = ' '.join(testcase['args'])
print(f'Testcase {args.name}, invokes disabler with:\n{command_line}\n\n')
# Pretty-print ResultDB RPC requests and corresponding responses.
requests = json.loads(testcase['requests'])
if len(requests) != 0:
print(f'Makes {len(requests)} request(s) to ResultDB:')
for request, response in requests.items():
n = request.index('/')
name = request[:n]
payload = json.loads(request[n + 1:])
print(f'\n{name}')
pprint.pprint(payload)
print('->')
pprint.pprint(json.loads(response))
print('\n')
# List all files read.
read_data = testcase['read_data']
if len(read_data) > 0:
print(f'Reads {len(read_data)} file(s):')
print('\n'.join(read_data))
print('\n')
# Show diff between read and written for all written files.
written_data = testcase['written_data']
if len(written_data) > 0:
print('Produces the following diffs:')
print_diffs(read_data, written_data)
def all_testcase_jsons():
for testcase in glob.glob('tests/*.json'):
with open(testcase, 'r') as f:
yield os.path.basename(testcase)[:-5], json.load(f)
def cmd_run(_args: argparse.Namespace):
testcases = []
for name, testcase_json in all_testcase_jsons():
testcases.append(
IntegrationTest(
name,
testcase_json['args'],
testcase_json['requests'],
testcase_json['read_data'],
testcase_json['written_data'],
))
test_runner = unittest.TextTestRunner()
test_runner.run(unittest.TestSuite(testcases))
def cmd_rerecord(_args: argparse.Namespace):
for name, testcase_json in all_testcase_jsons():
record_testcase(name, testcase_json['args'])
print('')
def main():
parser = argparse.ArgumentParser(
description='Record / replay integration tests.', )
subparsers = parser.add_subparsers()
record_parser = subparsers.add_parser('record', help='Record a testcase')
record_parser.add_argument('name',
type=str,
help='The name to give the testcase')
record_parser.add_argument(
'args',
type=str,
nargs='+',
help='The arguments to use for running the testcase.')
record_parser.set_defaults(func=cmd_record)
run_parser = subparsers.add_parser('run', help='Run all testcases')
run_parser.set_defaults(func=cmd_run)
show_parser = subparsers.add_parser('show', help='Describe a testcase')
show_parser.add_argument('name', type=str, help='The testcase to describe')
show_parser.set_defaults(func=cmd_show)
rerecord_parser = subparsers.add_parser(
'rerecord', help='Re-record all existing testcases')
rerecord_parser.set_defaults(func=cmd_rerecord)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()
|