From 75879ab1d7f9b17e1fa7cee5eb5889bb8f3b3fbc Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Fri, 23 Oct 2020 10:17:15 +0200 Subject: [PATCH] test: tag E2E Docker resources and autoremove them (#5558) Fixes #5555. --- test/e2e/runner/cleanup.go | 59 ++++++++++++++++++++++++++++---------- test/e2e/runner/setup.go | 4 +++ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/test/e2e/runner/cleanup.go b/test/e2e/runner/cleanup.go index 951e0d612..0af49db7d 100644 --- a/test/e2e/runner/cleanup.go +++ b/test/e2e/runner/cleanup.go @@ -11,44 +11,73 @@ import ( // Cleanup removes the Docker Compose containers and testnet directory. func Cleanup(testnet *e2e.Testnet) error { - if testnet.Dir == "" { - return errors.New("no directory set") + err := cleanupDocker() + if err != nil { + return err } - _, err := os.Stat(testnet.Dir) - if os.IsNotExist(err) { - return nil - } else if err != nil { + err = cleanupDir(testnet.Dir) + if err != nil { return err } + return nil +} +// cleanupDocker removes all E2E resources (with label e2e=True), regardless +// of testnet. +func cleanupDocker() error { logger.Info("Removing Docker containers and networks") - err = execCompose(testnet.Dir, "stop") + + // GNU xargs requires the -r flag to not run when input is empty, macOS + // does this by default. Ugly, but works. + xargsR := `$(if [[ $OSTYPE == "linux-gnu"* ]]; then echo -n "-r"; fi)` + + err := exec("bash", "-c", fmt.Sprintf( + "docker container ls -q --filter label=e2e | xargs %v docker container rm -f", xargsR)) + if err != nil { + return err + } + + err = exec("bash", "-c", fmt.Sprintf( + "docker network ls -q --filter label=e2e | xargs %v docker network rm", xargsR)) if err != nil { return err } + return nil +} + +// cleanupDir cleans up a testnet directory +func cleanupDir(dir string) error { + if dir == "" { + return errors.New("no directory set") + } + + _, err := os.Stat(dir) + if os.IsNotExist(err) { + return nil + } else if err != nil { + return err + } + + logger.Info(fmt.Sprintf("Removing testnet directory %q", dir)) + // On Linux, some local files in the volume will be owned by root since Tendermint // runs as root inside the container, so we need to clean them up from within a // container running as root too. - absDir, err := filepath.Abs(testnet.Dir) + absDir, err := filepath.Abs(dir) if err != nil { return err } - err = execDocker("run", "--entrypoint", "", "-v", fmt.Sprintf("%v:/network", absDir), + err = execDocker("run", "--rm", "--entrypoint", "", "-v", fmt.Sprintf("%v:/network", absDir), "tendermint/e2e-node", "sh", "-c", "rm -rf /network/*/") if err != nil { return err } - err = execCompose(testnet.Dir, "down") + err = os.RemoveAll(dir) if err != nil { return err } - logger.Info(fmt.Sprintf("Removing testnet directory %q", testnet.Dir)) - err = os.RemoveAll(testnet.Dir) - if err != nil { - return err - } return nil } diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index e77fcd7ee..c22eee725 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -122,6 +122,8 @@ func MakeDockerCompose(testnet *e2e.Testnet) ([]byte, error) { networks: {{ .Name }}: + labels: + e2e: true driver: bridge {{- if .IPv6 }} enable_ipv6: true @@ -134,6 +136,8 @@ networks: services: {{- range .Nodes }} {{ .Name }}: + labels: + e2e: true container_name: {{ .Name }} image: tendermint/e2e-node {{- if eq .ABCIProtocol "builtin" }}