File: test_storage_compatibility.py

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (236 lines) | stat: -rw-r--r-- 8,365 bytes parent folder | download | duplicates (3)
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
import argparse
import os
import subprocess
import re
import csv
from pathlib import Path
import sys

parser = argparse.ArgumentParser(description='Run a full benchmark using the CLI and report the results.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--old-cli', action='store', help='Path to the CLI of the old DuckDB version to test')
group.add_argument('--versions', type=str, action='store', help='DuckDB versions to test')
parser.add_argument('--new-unittest', action='store', help='Path to the new unittester to run', required=True)
parser.add_argument('--new-cli', action='store', help='Path to the new unittester to run', default=None)
parser.add_argument('--compatibility', action='store', help='Storage compatibility version', default='v1.0.0')
parser.add_argument(
    '--test-config', action='store', help='Test config script to run', default='test/configs/storage_compatibility.json'
)
parser.add_argument('--db-name', action='store', help='Database name to write to', default='bwc_storage_test.db')
parser.add_argument('--abort-on-failure', action='store_true', help='Abort on first failure', default=False)
parser.add_argument('--start-offset', type=int, action='store', help='Test start offset', default=None)
parser.add_argument('--end-offset', type=int, action='store', help='Test end offset', default=None)
parser.add_argument('--no-summarize-failures', action='store_true', help='Skip failure summary', default=False)
parser.add_argument('--list-versions', action='store_true', help='Only list versions to test', default=False)
parser.add_argument(
    '--run-empty-tests',
    action='store_true',
    help='Run tests that don' 't have a CREATE TABLE or CREATE VIEW statement',
    default=False,
)

args, extra_args = parser.parse_known_args()

programs_to_test = []
if args.versions is not None:
    version_splits = args.versions.split('|')
    for version in version_splits:
        cli_path = os.path.join(Path.home(), '.duckdb', 'cli', version, 'duckdb')
        if not os.path.isfile(cli_path):
            if os.system(f'curl https://install.duckdb.org | DUCKDB_VERSION={version} sh'):
                raise Exception(f"CURL install for DuckDB version: {version} failed")
        programs_to_test.append(cli_path)
else:
    programs_to_test.append(args.old_cli)

unittest_program = args.new_unittest
db_name = args.db_name
new_cli = args.new_unittest.replace('test/unittest', 'duckdb') if args.new_cli is None else args.new_cli
summarize_failures = not args.no_summarize_failures

# Use the '-l' parameter to output the list of tests to run
proc = subprocess.run(
    [unittest_program, '--test-config', args.test_config, '-l'] + extra_args,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)
stdout = proc.stdout.decode('utf8').strip()
stderr = proc.stderr.decode('utf8').strip()
if len(stderr) > 0:
    print("Failed to run program " + unittest_program)
    print("Returncode:", proc.returncode)
    print(stdout)
    print(stderr)
    exit(1)


# The output is in the format of 'PATH\tGROUP', we're only interested in the PATH portion
test_cases = []
first_line = True
for line in stdout.splitlines():
    if first_line:
        first_line = False
        continue
    if len(line.strip()) == 0:
        continue
    splits = line.rsplit('\t', 1)
    test_cases.append(splits[0])

test_cases.sort()
if args.compatibility != 'v1.0.0':
    raise Exception("Only v1.0.0 is supported for now (FIXME)")


def escape_cmd_arg(arg):
    if '"' in arg or '\'' in arg or ' ' in arg or '\\' in arg:
        arg = arg.replace('\\', '\\\\')
        arg = arg.replace('"', '\\"')
        arg = arg.replace("'", "\\'")
        return f'"{arg}"'
    return arg


error_container = []


def handle_failure(test, cmd, msg, stdout, stderr, returncode):
    print(f"==============FAILURE============", file=sys.stderr)
    print(test, file=sys.stderr)
    print(f"==============MESSAGE============", file=sys.stderr)
    print(msg, file=sys.stderr)
    print(f"==============COMMAND============", file=sys.stderr)
    cmd_str = ''
    for entry in cmd:
        cmd_str += escape_cmd_arg(entry) + ' '
    print(cmd_str.strip(), file=sys.stderr)
    print(f"==============RETURNCODE=========", file=sys.stderr)
    print(str(returncode), file=sys.stderr)
    print(f"==============STDOUT=============", file=sys.stderr)
    print(stdout, file=sys.stderr)
    print(f"==============STDERR=============", file=sys.stderr)
    print(stderr, file=sys.stderr)
    print(f"=================================", file=sys.stderr)
    if args.abort_on_failure:
        exit(1)
    else:
        error_container.append({'test': test, 'stderr': stderr})


def run_program(cmd, description):
    proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout = proc.stdout.decode('utf8').strip()
    stderr = proc.stderr.decode('utf8').strip()
    if proc.returncode != 0:
        return {
            'test': test,
            'cmd': cmd,
            'msg': f'Failed to {description}',
            'stdout': stdout,
            'stderr': stderr,
            'returncode': proc.returncode,
        }
    return None


def try_run_program(cmd, description):
    result = run_program(cmd, description)
    if result is None:
        return True
    handle_failure(**result)
    return False


index = 0
start = 0 if args.start_offset is None else args.start_offset
end = len(test_cases) if args.end_offset is None else args.end_offset
for i in range(start, end):
    test = test_cases[i]
    skipped = ''
    if not args.run_empty_tests:
        with open(test, 'r') as f:
            test_contents = f.read().lower()
        if 'create table' not in test_contents and 'create view' not in test_contents:
            skipped = ' (SKIPPED)'

    print(f'[{i}/{len(test_cases)}]: {test}{skipped}')
    if skipped != '':
        continue
    # remove the old db
    try:
        os.remove(db_name)
    except:
        pass
    cmd = [unittest_program, '--test-config', args.test_config, test]
    if not try_run_program(cmd, 'Run Test'):
        print("Failed to Run Test")
        continue

    if not os.path.isfile(db_name):
        # db not created
        print(f"Failed to create a database file by the name of {db_name}")
        continue

    cmd = [
        programs_to_test[-1],
        db_name,
        '-c',
        '.headers off',
        '-csv',
        '-c',
        '.output table_list.csv',
        '-c',
        'SHOW ALL TABLES',
    ]
    if not try_run_program(cmd, 'List Tables'):
        print("Failed to List Tables")
        continue

    tables = []
    with open('table_list.csv', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            tables.append((row[1], row[2]))
    # no tables / views
    if len(tables) == 0:
        print("No tables/views were created, skipping")
        continue

    # read all tables / views
    failures = []
    for cli in programs_to_test:
        cmd = [cli, db_name]
        for table in tables:
            schema_name = table[0].replace('"', '""')
            table_name = table[1].replace('"', '""')
            cmd += ['-c', f'FROM "{schema_name}"."{table_name}"']
        failure = run_program(cmd, 'Query Tables')
        if failure is not None:
            failures.append(failure)
    if len(failures) > 0:
        # we failed to query the tables
        # this MIGHT be expected - e.g. we might have views that reference stale state (e.g. files that are deleted)
        # try to run it with the new CLI - if this succeeds we have a problem
        new_cmd = [new_cli] + cmd[1:]
        new_failure = run_program(new_cmd, 'Query Tables (New)')
        if new_failure is None:
            # we succeeded with the new CLI - report the failure
            for failure in failures:
                handle_failure(**failure)
        continue

if len(error_container) == 0:
    exit(0)

if summarize_failures:
    print(
        '''\n\n====================================================
================  FAILURES SUMMARY  ================
====================================================\n
''',
        file=sys.stderr,
    )
    for i, error in enumerate(error_container, start=1):
        print(f"\n{i}:", error["test"], "\n", file=sys.stderr)
        print(error["stderr"], file=sys.stderr)

exit(1)