1
1
// Copyright 2014 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
jujutesting "github.com/juju/testing"
10
13
jc "github.com/juju/testing/checkers"
12
15
gc "launchpad.net/gocheck"
17
"launchpad.net/juju-core/agent/mongo"
14
18
coretesting "launchpad.net/juju-core/testing"
15
"launchpad.net/juju-core/testing/testbase"
16
19
"launchpad.net/juju-core/upstart"
19
22
type EnsureAdminSuite struct {
23
28
var _ = gc.Suite(&EnsureAdminSuite{})
25
30
func (s *EnsureAdminSuite) SetUpTest(c *gc.C) {
26
s.PatchValue(&upstartConfInstall, func(conf *upstart.Conf) error {
29
s.PatchValue(&upstartServiceStart, func(svc *upstart.Service) error {
32
s.PatchValue(&upstartServiceStop, func(svc *upstart.Service) error {
31
s.BaseSuite.SetUpTest(c)
34
s.PatchValue(mongo.UpstartConfInstall, func(conf *upstart.Conf) error {
37
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
41
s.PatchValue(mongo.UpstartServiceStop, func(svc *upstart.Service) error {
38
48
inst := &coretesting.MgoInstance{}
39
49
err := inst.Start(true)
40
50
c.Assert(err, gc.IsNil)
51
defer inst.DestroyWithLog()
42
52
dialInfo := inst.DialInfo()
54
// Mock out mongod, so the --noauth execution doesn't
55
// do anything nasty. Also mock out the Signal method.
56
jujutesting.PatchExecutableAsEchoArgs(c, s, "mongod")
57
mongodDir := filepath.SplitList(os.Getenv("PATH"))[0]
58
s.PatchValue(&mongo.JujuMongodPath, filepath.Join(mongodDir, "mongod"))
59
s.PatchValue(mongo.ProcessSignal, func(*os.Process, os.Signal) error {
43
63
// First call succeeds, as there are no users yet.
44
64
added, err := s.ensureAdminUser(c, dialInfo, "whomever", "whatever")
45
65
c.Assert(err, gc.IsNil)
46
66
c.Assert(added, jc.IsTrue)
68
// EnsureAdminUser should have stopped the mongo service,
69
// started a new mongod with --noauth, and then finally
70
// started the service back up.
71
c.Assert(s.serviceStarts, gc.Equals, 1)
72
c.Assert(s.serviceStops, gc.Equals, 1)
73
_, portString, err := net.SplitHostPort(dialInfo.Addrs[0])
74
c.Assert(err, gc.IsNil)
75
jujutesting.AssertEchoArgs(c, "mongod",
79
"--sslPEMKeyFile", "server.pem",
80
"--sslPEMKeyPassword", "ignored",
81
"--bind_ip", "127.0.0.1",
47
89
// Second call succeeds, as the admin user is already there.
48
90
added, err = s.ensureAdminUser(c, dialInfo, "whomever", "whatever")
49
91
c.Assert(err, gc.IsNil)
50
92
c.Assert(added, jc.IsFalse)
94
// There should have been no additional start/stop.
95
c.Assert(s.serviceStarts, gc.Equals, 1)
96
c.Assert(s.serviceStops, gc.Equals, 1)
53
99
func (s *EnsureAdminSuite) TestEnsureAdminUserError(c *gc.C) {