This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mgotest.go
182 lines (165 loc) · 3.77 KB
/
mgotest.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Package mgotest provides standalone test instances of mongo sutable for use
// in tests.
package mgotest
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"text/template"
"time"
"gopkg.in/mgo.v2"
"github.com/facebookgo/freeport"
"github.com/facebookgo/testname"
"github.com/facebookgo/waitout"
)
var mgoWaitingForConnections = []byte("waiting for connections on port")
var configTemplate, configTemplateErr = template.New("config").Parse(`
bind_ip = 127.0.0.1
dbpath = {{.DBPath}}
nohttpinterface = true
nojournal = true
noprealloc = true
nounixsocket = true
nssize = 8
port = {{.Port}}
quiet = true
smallfiles = true
{{if .ReplSet}}
oplogSize = 1
replSet = rs
{{end}}
`)
func init() {
if configTemplateErr != nil {
panic(configTemplateErr)
}
}
// Fatalf is satisfied by testing.T or testing.B.
type Fatalf interface {
Fatalf(format string, args ...interface{})
}
// Server is a unique instance of a mongod.
type Server struct {
Port int
DBPath string
ReplSet bool
StopTimeout time.Duration
T Fatalf
cmd *exec.Cmd
testName string
}
// Start the server, this will return once the server has been started.
func (s *Server) Start(args ...string) {
if s.Port == 0 {
port, err := freeport.Get()
if err != nil {
s.T.Fatalf(err.Error())
}
s.Port = port
}
if s.testName == "" {
s.T.Fatalf("Cannot determine name for test")
}
dir, err := ioutil.TempDir("", "mgotest-dbpath-"+s.testName)
if err != nil {
s.T.Fatalf(err.Error())
}
s.DBPath = dir
cf, err := ioutil.TempFile(s.DBPath, "config-")
if err != nil {
s.T.Fatalf(err.Error())
}
if err := configTemplate.Execute(cf, s); err != nil {
s.T.Fatalf(err.Error())
}
if err := cf.Close(); err != nil {
s.T.Fatalf(err.Error())
}
waiter := waitout.New(mgoWaitingForConnections)
args = append(args, "--config", cf.Name(), "--setParameter", "enableTestCommands=1")
s.cmd = exec.Command("mongod", args...)
s.cmd.Env = envPlusLcAll()
if os.Getenv("MGOTEST_VERBOSE") == "1" {
s.cmd.Stdout = io.MultiWriter(os.Stdout, waiter)
s.cmd.Stderr = os.Stderr
} else {
s.cmd.Stdout = waiter
}
if err := s.cmd.Start(); err != nil {
s.T.Fatalf(err.Error())
}
waiter.Wait()
}
// Stop the server, this will also remove all data.
func (s *Server) Stop() {
fin := make(chan struct{})
go func() {
defer close(fin)
s.cmd.Process.Kill()
s.cmd.Process.Wait()
os.RemoveAll(s.DBPath)
}()
select {
case <-fin:
case <-time.After(s.StopTimeout):
}
}
// URL for the mongo server, suitable for use with mgo.Dial.
func (s *Server) URL() string {
return fmt.Sprintf("127.0.0.1:%d", s.Port)
}
// Session for the mongo server.
func (s *Server) Session() *mgo.Session {
session, err := mgo.Dial(s.URL())
if err != nil {
s.T.Fatalf(err.Error())
}
return session
}
// NewStartedServer creates a new server starts it.
func NewStartedServer(t Fatalf, args ...string) *Server {
for {
s := &Server{
T: t,
StopTimeout: 15 * time.Second,
testName: testname.Get("MGO"),
}
start := make(chan struct{})
go func() {
defer close(start)
s.Start(args...)
}()
select {
case <-start:
return s
case <-time.After(30 * time.Second):
}
}
}
// NewReplSetServer creates a new server starts it with ReplSet enabled.
func NewReplSetServer(t Fatalf, args ...string) *Server {
for {
s := &Server{
T: t,
StopTimeout: 15 * time.Second,
ReplSet: true,
testName: testname.Get("MGO"),
}
start := make(chan struct{})
go func() {
defer close(start)
s.Start(args...)
}()
select {
case <-start:
return s
case <-time.After(30 * time.Second):
}
}
}
func envPlusLcAll() []string {
env := os.Environ()
return append(env, "LC_ALL=C")
}