bitbucket.org/number571/tendermint@v0.8.14/test/e2e/runner/cleanup.go (about)

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