/
zk_control.go
45 lines (39 loc) · 974 Bytes
/
zk_control.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
package testutils
import (
"log"
"sync"
"github.com/docker/docker/client"
"github.com/pkg/errors"
)
// ZkControl allows testing code to manipulate a running ZK instance.
type ZkControl struct {
dockerClient *client.Client
containerID string
addr string
teardownOnce sync.Once
}
// Addr returns the address of the zookeeper node
func (z *ZkControl) Addr() string {
return z.addr
}
// Teardown destroys the ZK container
func (z *ZkControl) Teardown() error {
log.Println("Starting requested teardown of ZK container")
var err error
z.teardownOnce.Do(func() {
err = removeContainer(z.dockerClient, z.containerID)
if err == nil {
log.Println("Successfully removed ZK container")
}
})
if err != nil {
return errors.Wrap(err, "could not remove ZK container")
}
return nil
}
// TeardownPanic destroys the ZK container and panics if unsuccessful
func (z *ZkControl) TeardownPanic() {
if err := z.Teardown(); err != nil {
panic(err)
}
}