-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomondoc.go
95 lines (72 loc) · 2.05 KB
/
gomondoc.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gomondoctest
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
"testing"
)
//Inspired by https://developers.almamedia.fi/2014/painless-mongodb-testing-with-docker-and-golang/
const mongoImage = "mongo"
type Gomondoc struct {
t *testing.T
containerID string
}
func NewGomondoc(t *testing.T) *Gomondoc {
checkDocker(t)
if ok, err := dockerHasImage(mongoImage); !ok || err != nil {
if err != nil {
t.Fatalf("Error checking that the docker image %v is installed in the system.", mongoImage)
}
installDockerImage(t)
}
return &Gomondoc{t, ""}
}
func checkDocker(t *testing.T) {
if _, err := exec.LookPath("docker"); err != nil {
t.Skip("Docker is not installed on the system.")
}
}
func (e *Gomondoc) RunMongo() {
log.Printf("Executing docker mongo image.")
out, err := exec.Command("docker", "run", "--name", "some-mongo", "-p", "27017:27017", "-d", "mongo").Output()
if err != nil {
e.t.Fatal("Error running mongo image.", err)
}
e.containerID = strings.TrimSpace(string(out))
if e.containerID == "" {
e.t.Fatal("Error getting id of docker container.")
}
}
func (md *Gomondoc) StopMongo() {
log.Printf("Stopping docker image.")
out, err := exec.Command("docker", "stop", md.containerID).Output()
if err != nil {
md.t.Fatalf("Error stopping docker container. %v", out)
}
out, err = exec.Command("docker", "rm", md.containerID).Output()
if err != nil {
md.t.Fatalf("Error removing docker container. %v", out)
}
}
func installDockerImage(t *testing.T) {
log.Printf("Pulling docker image %s ...", mongoImage)
if err := dockerPull(mongoImage); err != nil {
t.Skipf("Error pulling %s: %v", mongoImage, err)
}
}
func dockerPull(name string) interface{} {
out, err := exec.Command("docker", "pull", name).CombinedOutput()
if err != nil {
err = fmt.Errorf("%v: %s", err, out)
}
return err
}
func dockerHasImage(name string) (ok bool, err error) {
out, err := exec.Command("docker", "images", "--filter=reference=mongo:latest").Output()
if err != nil {
return
}
return bytes.Contains(out, []byte(name)), nil
}