1# REQUIRES: curl
2# RUN: rm -rf %t
3# RUN: mkdir %t
4# # Query the python server for artifacts
5# RUN: env DEBUGINFOD_CACHE_PATH=%t %python %s --server-path %S/Inputs \
6# RUN:   --tool-cmd 'llvm-debuginfod-find --dump --executable abcdef' | \
7# RUN:   FileCheck %s --check-prefix=EXECUTABLE
8# RUN: env DEBUGINFOD_CACHE_PATH=%t %python %s --server-path %S/Inputs \
9# RUN:   --tool-cmd 'llvm-debuginfod-find --dump --source=/directory/file.c abcdef' | \
10# RUN:   FileCheck %s --check-prefix=SOURCE
11# RUN: env DEBUGINFOD_CACHE_PATH=%t %python %s --server-path %S/Inputs \
12# RUN:   --tool-cmd 'llvm-debuginfod-find --dump --debuginfo abcdef' | \
13# RUN:   FileCheck %s --check-prefix=DEBUGINFO
14
15# EXECUTABLE: fake_executable
16# SOURCE: int foo = 0;
17# DEBUGINFO: fake_debuginfo
18
19# # The artifacts should still be present in the cache without needing to query
20# # the server.
21# RUN: env DEBUGINFOD_CACHE_PATH=%t llvm-debuginfod-find --dump --executable abcdef | \
22# RUN:   FileCheck %s --check-prefix=EXECUTABLE
23# RUN: env DEBUGINFOD_CACHE_PATH=%t llvm-debuginfod-find --dump \
24# RUN:   --source=/directory/file.c abcdef | \
25# RUN:   FileCheck %s --check-prefix=SOURCE
26# RUN: env DEBUGINFOD_CACHE_PATH=%t llvm-debuginfod-find --dump --debuginfo abcdef | \
27# RUN:   FileCheck %s --check-prefix=DEBUGINFO
28
29
30# This script is used to test the debuginfod client within a host tool.
31# It first stands up a Python HTTP static file server and then executes the tool.
32# This way the tool can make debuginfod HTTP requests to the static file server.
33import argparse
34import threading
35import http.server
36import functools
37import subprocess
38import sys
39import os
40
41
42# Serves files at the server_path, then runs the tool with specified args.
43# Sets the DEBUGINFOD_CACHE_PATH env var to point at the given cache_directory.
44# Sets the DEBUGINFOD_URLS env var to point at the local server.
45def test_tool(server_path, tool_args):
46    httpd = http.server.ThreadingHTTPServer(
47        ('',0), functools.partial(
48            http.server.SimpleHTTPRequestHandler,
49            directory=server_path))
50    port = httpd.server_port
51    thread = threading.Thread(target=httpd.serve_forever)
52    try:
53        thread.start()
54        env = os.environ
55        env['DEBUGINFOD_URLS'] = 'http://localhost:%s' % port
56        process = subprocess.Popen(
57            tool_args, env=env)
58        code = process.wait()
59        if code != 0:
60            print('nontrivial return code %s' % code)
61            return 1
62    finally:
63        httpd.shutdown()
64        thread.join()
65    return 0
66
67def main():
68    parser = argparse.ArgumentParser()
69    parser.add_argument('--server-path', default='./')
70    parser.add_argument('--tool-cmd', required=True, type=str)
71    args = parser.parse_args()
72    result = test_tool(args.server_path,
73        args.tool_cmd.split())
74    sys.exit(result)
75
76if __name__ == '__main__':
77    main()
78