[analyzer] SATest: Add an easy option to connect to docker for debugging

Summary:
Docker on its own has a pretty convenient way to run shell.
This method, however, requires target container to be currently running,
which is not a usual scenario for the test system.  For this purpose,
it is better to have a simple way to run the container, shell it, and
clean up at the end of it all.  New option `--shell` does exactly this.

Differential Revision: https://reviews.llvm.org/D81598
This commit is contained in:
Valeriy Savchenko 2020-06-08 12:49:31 +03:00
parent 061b5bf914
commit 11f287826f
2 changed files with 49 additions and 18 deletions

View File

@ -108,8 +108,10 @@ def docker(parser, args):
if args.build_image: if args.build_image:
docker_build_image() docker_build_image()
elif args.shell:
docker_shell(args)
else: else:
docker_run(args) sys.exit(docker_run(args, ' '.join(args.rest)))
def docker_build_image(): def docker_build_image():
@ -117,21 +119,40 @@ def docker_build_image():
shell=True)) shell=True))
def docker_run(args): def docker_shell(args):
sys.exit(call("docker run --rm --name satest " try:
"-v {llvm}:/llvm-project " # First we need to start the docker container in a waiting mode,
"-v {build}:/build " # so it doesn't do anything, but most importantly keeps working
"-v {clang}:/analyzer " # while the shell session is in progress.
"-v {scripts}:/scripts " docker_run(args, "--wait", "--detach")
"-v {projects}:/projects " # Since the docker container is running, we can actually connect to it
"satest-image:latest {args}" call("docker exec -it satest bash", shell=True)
.format(llvm=args.llvm_project_dir,
build=args.build_dir, except KeyboardInterrupt:
clang=args.clang_dir, pass
scripts=SCRIPTS_DIR,
projects=PROJECTS_DIR, finally:
args=' '.join(args.rest)), print("Please wait for docker to clean up")
shell=True)) call("docker stop satest", shell=True)
def docker_run(args, command, docker_args=""):
return call("docker run --rm --name satest "
"-v {llvm}:/llvm-project "
"-v {build}:/build "
"-v {clang}:/analyzer "
"-v {scripts}:/scripts "
"-v {projects}:/projects "
"{docker_args} "
"satest-image:latest {command}"
.format(llvm=args.llvm_project_dir,
build=args.build_dir,
clang=args.clang_dir,
scripts=SCRIPTS_DIR,
projects=PROJECTS_DIR,
docker_args=docker_args,
command=command),
shell=True)
def main(): def main():

View File

@ -2,13 +2,15 @@ import argparse
import os import os
import sys import sys
from typing import List, Tuple
from subprocess import call, check_call, CalledProcessError from subprocess import call, check_call, CalledProcessError
from time import sleep
from typing import List, Tuple
def main(): def main():
settings, rest = parse_arguments() settings, rest = parse_arguments()
if settings.wait:
wait()
if settings.build_llvm or settings.build_llvm_only: if settings.build_llvm or settings.build_llvm_only:
build_llvm() build_llvm()
if settings.build_llvm_only: if settings.build_llvm_only:
@ -16,8 +18,16 @@ def main():
sys.exit(test(rest)) sys.exit(test(rest))
def wait():
# It is an easy on CPU way of keeping the docker container running
# while the user has a terminal session in that container.
while True:
sleep(3600)
def parse_arguments() -> Tuple[argparse.Namespace, List[str]]: def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--wait', action='store_true')
parser.add_argument('--build-llvm', action='store_true') parser.add_argument('--build-llvm', action='store_true')
parser.add_argument('--build-llvm-only', action='store_true') parser.add_argument('--build-llvm-only', action='store_true')
return parser.parse_known_args() return parser.parse_known_args()