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.

83 lines
1.9 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. err = cleanupDir(testnet.Dir)
  16. if err != nil {
  17. return err
  18. }
  19. return nil
  20. }
  21. // cleanupDocker removes all E2E resources (with label e2e=True), regardless
  22. // of testnet.
  23. func cleanupDocker() error {
  24. logger.Info("Removing Docker containers and networks")
  25. // GNU xargs requires the -r flag to not run when input is empty, macOS
  26. // does this by default. Ugly, but works.
  27. xargsR := `$(if [[ $OSTYPE == "linux-gnu"* ]]; then echo -n "-r"; fi)`
  28. err := exec("bash", "-c", fmt.Sprintf(
  29. "docker container ls -q --filter label=e2e | xargs %v docker container rm -f", xargsR))
  30. if err != nil {
  31. return err
  32. }
  33. err = exec("bash", "-c", fmt.Sprintf(
  34. "docker network ls -q --filter label=e2e | xargs %v docker network rm", xargsR))
  35. if err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. // cleanupDir cleans up a testnet directory
  41. func cleanupDir(dir string) error {
  42. if dir == "" {
  43. return errors.New("no directory set")
  44. }
  45. _, err := os.Stat(dir)
  46. if os.IsNotExist(err) {
  47. return nil
  48. } else if err != nil {
  49. return err
  50. }
  51. logger.Info(fmt.Sprintf("Removing testnet directory %q", dir))
  52. // On Linux, some local files in the volume will be owned by root since Tendermint
  53. // runs as root inside the container, so we need to clean them up from within a
  54. // container running as root too.
  55. absDir, err := filepath.Abs(dir)
  56. if err != nil {
  57. return err
  58. }
  59. err = execDocker("run", "--rm", "--entrypoint", "", "-v", fmt.Sprintf("%v:/network", absDir),
  60. "tendermint/e2e-node", "sh", "-c", "rm -rf /network/*/")
  61. if err != nil {
  62. return err
  63. }
  64. err = os.RemoveAll(dir)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }