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.

69 lines
1.7 KiB

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