You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/tendermint/tendermint/libs/log"
  8. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  9. )
  10. // Cleanup removes the Docker Compose containers and testnet directory.
  11. func Cleanup(logger log.Logger, testnet *e2e.Testnet) error {
  12. err := cleanupDocker(logger)
  13. if err != nil {
  14. return err
  15. }
  16. return cleanupDir(logger, testnet.Dir)
  17. }
  18. // cleanupDocker removes all E2E resources (with label e2e=True), regardless
  19. // of testnet.
  20. func cleanupDocker(logger log.Logger) error {
  21. logger.Info("Removing Docker containers and networks")
  22. // GNU xargs requires the -r flag to not run when input is empty, macOS
  23. // does this by default. Ugly, but works.
  24. xargsR := `$(if [[ $OSTYPE == "linux-gnu"* ]]; then echo -n "-r"; fi)`
  25. err := exec("bash", "-c", fmt.Sprintf(
  26. "docker container ls -qa --filter label=e2e | xargs %v docker container rm -f", xargsR))
  27. if err != nil {
  28. return err
  29. }
  30. return exec("bash", "-c", fmt.Sprintf(
  31. "docker network ls -q --filter label=e2e | xargs %v docker network rm", xargsR))
  32. }
  33. // cleanupDir cleans up a testnet directory
  34. func cleanupDir(logger log.Logger, dir string) error {
  35. if dir == "" {
  36. return errors.New("no directory set")
  37. }
  38. _, err := os.Stat(dir)
  39. if os.IsNotExist(err) {
  40. return nil
  41. } else if err != nil {
  42. return err
  43. }
  44. logger.Info(fmt.Sprintf("Removing testnet directory %q", dir))
  45. // On Linux, some local files in the volume will be owned by root since Tendermint
  46. // runs as root inside the container, so we need to clean them up from within a
  47. // container running as root too.
  48. absDir, err := filepath.Abs(dir)
  49. if err != nil {
  50. return err
  51. }
  52. err = execDocker("run", "--rm", "--entrypoint", "", "-v", fmt.Sprintf("%v:/network", absDir),
  53. "tendermint/e2e-node", "sh", "-c", "rm -rf /network/*/")
  54. if err != nil {
  55. return err
  56. }
  57. return os.RemoveAll(dir)
  58. }