/
cleanup_garden.go
52 lines (44 loc) · 1.4 KB
/
cleanup_garden.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package helpers
import (
"fmt"
"strings"
"time"
"code.cloudfoundry.org/garden"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func CleanupGarden(gardenClient garden.Client) []error {
containers, err := gardenClient.Containers(nil)
Expect(err).NotTo(HaveOccurred())
fmt.Fprintf(ginkgo.GinkgoWriter, "cleaning up %d Garden containers", len(containers))
// even if containers fail to destroy, stop garden, but still report the
// errors
destroyContainerErrors := []error{}
for _, container := range containers {
info, _ := container.Info()
fmt.Fprintf(ginkgo.GinkgoWriter, "cleaning up container %s (%s)", container.Handle(), info.ContainerPath)
RETRY_LOOP:
// try to Destroy the container up to 3 times
for i := 0; i < 3; i++ {
err := gardenClient.Destroy(container.Handle())
switch {
case err == nil:
// move on if Destroy succeeds
break RETRY_LOOP
case strings.Contains(err.Error(), "unknown handle"):
// move on if container doesn't exist
break RETRY_LOOP
case strings.Contains(err.Error(), "container already being destroyed"):
// move on if container is already being destroyed
break RETRY_LOOP
case i == 2:
// record an error if Destroy failed 3 times
destroyContainerErrors = append(destroyContainerErrors, err)
default:
// try Destroy again otherwise
time.Sleep(50 * time.Millisecond)
}
}
}
return destroyContainerErrors
}