-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_test.go
542 lines (455 loc) · 15.4 KB
/
mongo_test.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mongo_test
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
stdtesting "testing"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
gc "launchpad.net/gocheck"
"github.com/juju/juju/mongo"
"github.com/juju/juju/network"
"github.com/juju/juju/service/common"
"github.com/juju/juju/service/upstart"
"github.com/juju/juju/state/apiserver/params"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/version"
)
func Test(t *stdtesting.T) { gc.TestingT(t) }
type MongoSuite struct {
coretesting.BaseSuite
mongodConfigPath string
mongodPath string
installError error
installed []upstart.Service
removeError error
removed []upstart.Service
}
var _ = gc.Suite(&MongoSuite{})
var testInfo = params.StateServingInfo{
StatePort: 25252,
Cert: "foobar-cert",
PrivateKey: "foobar-privkey",
SharedSecret: "foobar-sharedsecret",
}
func makeEnsureServerParams(dataDir, namespace string) mongo.EnsureServerParams {
return mongo.EnsureServerParams{
StateServingInfo: testInfo,
DataDir: dataDir,
Namespace: namespace,
}
}
func (s *MongoSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
// Try to make sure we don't execute any commands accidentally.
s.PatchEnvironment("PATH", "")
s.mongodPath = filepath.Join(c.MkDir(), "mongod")
err := ioutil.WriteFile(s.mongodPath, []byte("#!/bin/bash\n\nprintf %s 'db version v2.4.9'\n"), 0755)
c.Assert(err, gc.IsNil)
s.PatchValue(&mongo.JujuMongodPath, s.mongodPath)
// Patch "df" such that it always reports there's 1MB free.
s.PatchValue(mongo.AvailSpace, func(dir string) (float64, error) {
info, err := os.Stat(dir)
if err != nil {
return 0, err
}
if info.IsDir() {
return 1, nil
}
return 0, fmt.Errorf("not a directory")
})
s.PatchValue(mongo.MinOplogSizeMB, 1)
testPath := c.MkDir()
s.mongodConfigPath = filepath.Join(testPath, "mongodConfig")
s.PatchValue(mongo.MongoConfigPath, s.mongodConfigPath)
s.PatchValue(mongo.UpstartConfInstall, func(conf *upstart.Service) error {
s.installed = append(s.installed, *conf)
return s.installError
})
s.PatchValue(mongo.UpstartServiceStopAndRemove, func(svc *upstart.Service) error {
s.removed = append(s.removed, *svc)
return s.removeError
})
// Clear out the values that are set by the above patched functions.
s.removeError = nil
s.installError = nil
s.installed = nil
s.removed = nil
}
func (s *MongoSuite) TestJujuMongodPath(c *gc.C) {
obtained, err := mongo.Path()
c.Check(err, gc.IsNil)
c.Check(obtained, gc.Equals, s.mongodPath)
}
func (s *MongoSuite) TestDefaultMongodPath(c *gc.C) {
s.PatchValue(&mongo.JujuMongodPath, "/not/going/to/exist/mongod")
s.PatchEnvPathPrepend(filepath.Dir(s.mongodPath))
obtained, err := mongo.Path()
c.Check(err, gc.IsNil)
c.Check(obtained, gc.Equals, s.mongodPath)
}
func (s *MongoSuite) TestMakeJournalDirs(c *gc.C) {
dir := c.MkDir()
err := mongo.MakeJournalDirs(dir)
c.Assert(err, gc.IsNil)
testJournalDirs(dir, c)
}
func testJournalDirs(dir string, c *gc.C) {
journalDir := path.Join(dir, "journal")
c.Assert(journalDir, jc.IsDirectory)
info, err := os.Stat(filepath.Join(journalDir, "prealloc.0"))
c.Assert(err, gc.IsNil)
size := int64(1024 * 1024)
c.Assert(info.Size(), gc.Equals, size)
info, err = os.Stat(filepath.Join(journalDir, "prealloc.1"))
c.Assert(err, gc.IsNil)
c.Assert(info.Size(), gc.Equals, size)
info, err = os.Stat(filepath.Join(journalDir, "prealloc.2"))
c.Assert(err, gc.IsNil)
c.Assert(info.Size(), gc.Equals, size)
}
func (s *MongoSuite) TestEnsureServer(c *gc.C) {
dataDir := c.MkDir()
dbDir := filepath.Join(dataDir, "db")
namespace := "namespace"
mockShellCommand(c, &s.CleanupSuite, "apt-get")
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
testJournalDirs(dbDir, c)
assertInstalled := func() {
c.Assert(s.installed, gc.HasLen, 1)
service := s.installed[0]
c.Assert(service.Name, gc.Equals, "juju-db-namespace")
c.Assert(service.Conf.InitDir, gc.Equals, "/etc/init")
c.Assert(service.Conf.Desc, gc.Equals, "juju state database")
c.Assert(service.Conf.Cmd, gc.Matches, regexp.QuoteMeta(s.mongodPath)+".*")
// TODO(nate) set Out so that mongod output goes somewhere useful?
c.Assert(service.Conf.Out, gc.Equals, "")
}
assertInstalled()
contents, err := ioutil.ReadFile(s.mongodConfigPath)
c.Assert(err, gc.IsNil)
c.Assert(contents, jc.DeepEquals, []byte("ENABLE_MONGODB=no"))
contents, err = ioutil.ReadFile(mongo.SSLKeyPath(dataDir))
c.Assert(err, gc.IsNil)
c.Assert(string(contents), gc.Equals, testInfo.Cert+"\n"+testInfo.PrivateKey)
contents, err = ioutil.ReadFile(mongo.SharedSecretPath(dataDir))
c.Assert(err, gc.IsNil)
c.Assert(string(contents), gc.Equals, testInfo.SharedSecret)
// make sure that we log the version of mongodb as we get ready to
// start it
tlog := c.GetTestLog()
any := `(.|\n)*`
start := "^" + any
tail := any + "$"
c.Assert(tlog, gc.Matches, start+`using mongod: .*/mongod --version: "db version v2\.4\.9`+tail)
}
func (s *MongoSuite) TestEnsureServerServerExistsAndRunning(c *gc.C) {
dataDir := c.MkDir()
namespace := "namespace"
mockShellCommand(c, &s.CleanupSuite, "apt-get")
s.PatchValue(mongo.UpstartServiceExists, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceRunning, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
return fmt.Errorf("shouldn't be called")
})
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
c.Assert(s.installed, gc.HasLen, 0)
}
func (s *MongoSuite) TestEnsureServerServerExistsNotRunningIsStarted(c *gc.C) {
dataDir := c.MkDir()
namespace := "namespace"
mockShellCommand(c, &s.CleanupSuite, "apt-get")
s.PatchValue(mongo.UpstartServiceExists, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceRunning, func(svc *upstart.Service) bool {
return false
})
var started bool
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
started = true
return nil
})
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
c.Assert(s.installed, gc.HasLen, 0)
c.Assert(started, jc.IsTrue)
}
func (s *MongoSuite) TestEnsureServerServerExistsNotRunningStartError(c *gc.C) {
dataDir := c.MkDir()
namespace := "namespace"
mockShellCommand(c, &s.CleanupSuite, "apt-get")
s.PatchValue(mongo.UpstartServiceExists, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceRunning, func(svc *upstart.Service) bool {
return false
})
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
return fmt.Errorf("won't start")
})
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.ErrorMatches, `.*won't start`)
c.Assert(s.installed, gc.HasLen, 0)
}
func (s *MongoSuite) TestInstallMongod(c *gc.C) {
type installs struct {
series string
pkg string
}
tests := []installs{
{"precise", "mongodb-server"},
{"quantal", "mongodb-server"},
{"raring", "mongodb-server"},
{"saucy", "mongodb-server"},
{"trusty", "juju-mongodb"},
{"u-series", "juju-mongodb"},
}
mockShellCommand(c, &s.CleanupSuite, "add-apt-repository")
output := mockShellCommand(c, &s.CleanupSuite, "apt-get")
for _, test := range tests {
c.Logf("Testing %s", test.series)
dataDir := c.MkDir()
namespace := "namespace" + test.series
s.PatchValue(&version.Current.Series, test.series)
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
cmds := getMockShellCalls(c, output)
// quantal does an extra apt-get install for python software properties
// so we need to remember to skip that one
index := 0
if test.series == "quantal" {
index = 1
}
match := fmt.Sprintf(`.* install .*%s`, test.pkg)
c.Assert(strings.Join(cmds[index], " "), gc.Matches, match)
// remove the temp file between tests
c.Assert(os.Remove(output), gc.IsNil)
}
}
func (s *MongoSuite) TestInstallMongodServiceExists(c *gc.C) {
output := mockShellCommand(c, &s.CleanupSuite, "apt-get")
dataDir := c.MkDir()
namespace := "namespace"
s.PatchValue(mongo.UpstartServiceExists, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceRunning, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
return fmt.Errorf("shouldn't be called")
})
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
c.Assert(s.installed, gc.HasLen, 0)
// We still attempt to install mongodb, despite the service existing.
cmds := getMockShellCalls(c, output)
c.Assert(cmds, gc.HasLen, 1)
}
func (s *MongoSuite) TestUpstartServiceWithReplSet(c *gc.C) {
dataDir := c.MkDir()
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Conf.Cmd, "--replSet"), jc.IsTrue)
}
func (s *MongoSuite) TestUpstartServiceIPv6(c *gc.C) {
dataDir := c.MkDir()
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Conf.Cmd, "--ipv6"), jc.IsTrue)
}
func (s *MongoSuite) TestUpstartServiceWithJournal(c *gc.C) {
dataDir := c.MkDir()
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
journalPresent := strings.Contains(svc.Conf.Cmd, " --journal ") || strings.HasSuffix(svc.Conf.Cmd, " --journal")
c.Assert(journalPresent, jc.IsTrue)
}
func (s *MongoSuite) TestNoAuthCommandWithJournal(c *gc.C) {
dataDir := c.MkDir()
cmd, err := mongo.NoauthCommand(dataDir, 1234)
c.Assert(err, gc.IsNil)
var isJournalPresent bool
for _, value := range cmd.Args {
if value == "--journal" {
isJournalPresent = true
}
}
c.Assert(isJournalPresent, jc.IsTrue)
}
func (s *MongoSuite) TestRemoveService(c *gc.C) {
err := mongo.RemoveService("namespace")
c.Assert(err, gc.IsNil)
c.Assert(s.removed, jc.DeepEquals, []upstart.Service{{
Name: "juju-db-namespace",
Conf: common.Conf{InitDir: upstart.InitDir},
}})
}
func (s *MongoSuite) TestQuantalAptAddRepo(c *gc.C) {
dir := c.MkDir()
s.PatchEnvPathPrepend(dir)
failCmd(filepath.Join(dir, "add-apt-repository"))
mockShellCommand(c, &s.CleanupSuite, "apt-get")
// test that we call add-apt-repository only for quantal (and that if it
// fails, we return the error)
s.PatchValue(&version.Current.Series, "quantal")
err := mongo.EnsureServer(makeEnsureServerParams(dir, ""))
c.Assert(err, gc.ErrorMatches, "cannot install mongod: cannot add apt repository: exit status 1.*")
s.PatchValue(&version.Current.Series, "trusty")
failCmd(filepath.Join(dir, "mongod"))
err = mongo.EnsureServer(makeEnsureServerParams(dir, ""))
c.Assert(err, gc.IsNil)
}
func (s *MongoSuite) TestNoMongoDir(c *gc.C) {
// Make a non-existent directory that can nonetheless be
// created.
mockShellCommand(c, &s.CleanupSuite, "apt-get")
dataDir := filepath.Join(c.MkDir(), "dir", "data")
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, ""))
c.Check(err, gc.IsNil)
_, err = os.Stat(filepath.Join(dataDir, "db"))
c.Assert(err, gc.IsNil)
}
func (s *MongoSuite) TestServiceName(c *gc.C) {
name := mongo.ServiceName("foo")
c.Assert(name, gc.Equals, "juju-db-foo")
name = mongo.ServiceName("")
c.Assert(name, gc.Equals, "juju-db")
}
func (s *MongoSuite) TestSelectPeerAddress(c *gc.C) {
addresses := []network.Address{{
Value: "10.0.0.1",
Type: network.IPv4Address,
NetworkName: "cloud",
Scope: network.ScopeCloudLocal}, {
Value: "8.8.8.8",
Type: network.IPv4Address,
NetworkName: "public",
Scope: network.ScopePublic}}
address := mongo.SelectPeerAddress(addresses)
c.Assert(address, gc.Equals, "10.0.0.1")
}
func (s *MongoSuite) TestSelectPeerHostPort(c *gc.C) {
hostPorts := []network.HostPort{{
Address: network.Address{
Value: "10.0.0.1",
Type: network.IPv4Address,
NetworkName: "cloud",
Scope: network.ScopeCloudLocal,
},
Port: 37017}, {
Address: network.Address{
Value: "8.8.8.8",
Type: network.IPv4Address,
NetworkName: "public",
Scope: network.ScopePublic,
},
Port: 37017}}
address := mongo.SelectPeerHostPort(hostPorts)
c.Assert(address, gc.Equals, "10.0.0.1:37017")
}
func (s *MongoSuite) TestGenerateSharedSecret(c *gc.C) {
secret, err := mongo.GenerateSharedSecret()
c.Assert(err, gc.IsNil)
c.Assert(secret, gc.HasLen, 1024)
_, err = base64.StdEncoding.DecodeString(secret)
c.Assert(err, gc.IsNil)
}
func (s *MongoSuite) TestAddPPAInQuantal(c *gc.C) {
mockShellCommand(c, &s.CleanupSuite, "apt-get")
addAptRepoOut := mockShellCommand(c, &s.CleanupSuite, "add-apt-repository")
s.PatchValue(&version.Current.Series, "quantal")
dataDir := c.MkDir()
err := mongo.EnsureServer(makeEnsureServerParams(dataDir, ""))
c.Assert(err, gc.IsNil)
c.Assert(getMockShellCalls(c, addAptRepoOut), gc.DeepEquals, [][]string{{
"-y",
"ppa:juju/stable",
}})
}
// mockShellCommand creates a new command with the given
// name and contents, and patches $PATH so that it will be
// executed by preference. It returns the name of a file
// that is written by each call to the command - mockShellCalls
// can be used to retrieve the calls.
func mockShellCommand(c *gc.C, s *testing.CleanupSuite, name string) string {
dir := c.MkDir()
s.PatchEnvPathPrepend(dir)
// Note the shell script produces output of the form:
// +arg1+\n
// +arg2+\n
// ...
// +argn+\n
// -
//
// It would be nice if there was a simple way of unambiguously
// quoting shell arguments, but this will do as long
// as no argument contains a newline character.
outputFile := filepath.Join(dir, name+".out")
contents := `#!/bin/sh
{
for i in "$@"; do
echo +"$i"+
done
echo -
} >> ` + utils.ShQuote(outputFile) + `
`
err := ioutil.WriteFile(filepath.Join(dir, name), []byte(contents), 0755)
c.Assert(err, gc.IsNil)
return outputFile
}
// getMockShellCalls, given a file name returned by mockShellCommands,
// returns a slice containing one element for each call, each
// containing the arguments passed to the command.
// It will be confused if the arguments contain newlines.
func getMockShellCalls(c *gc.C, file string) [][]string {
data, err := ioutil.ReadFile(file)
if os.IsNotExist(err) {
return nil
}
c.Assert(err, gc.IsNil)
s := string(data)
parts := strings.Split(s, "\n-\n")
c.Assert(parts[len(parts)-1], gc.Equals, "")
var calls [][]string
for _, part := range parts[0 : len(parts)-1] {
calls = append(calls, splitCall(c, part))
}
return calls
}
// splitCall splits the output produced by a single call to the
// mocked shell function (see mockShellCommand) and
// splits it into its individual arguments.
func splitCall(c *gc.C, part string) []string {
var result []string
for _, arg := range strings.Split(part, "\n") {
c.Assert(arg, gc.Matches, `\+.*\+`)
arg = strings.TrimSuffix(arg, "+")
arg = strings.TrimPrefix(arg, "+")
result = append(result, arg)
}
return result
}
// failCmd creates an executable file at the given location that will do nothing
// except return an error.
func failCmd(path string) {
err := ioutil.WriteFile(path, []byte("#!/bin/bash --norc\nexit 1"), 0755)
if err != nil {
panic(err)
}
}