~mfoord/juju-core/tests-replset

« back to all changes in this revision

Viewing changes to agent/mongo/upgrade_test.go

  • Committer: Michael Foord
  • Date: 2014-05-22 15:16:56 UTC
  • mfrom: (2745.1.30 juju-core)
  • Revision ID: michael.foord@canonical.com-20140522151656-1l0z4p9puke9drsp
Merge existing rsyslog work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Copyright 2014 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
3
 
4
 
package mongo
 
4
package mongo_test
5
5
 
6
6
import (
7
7
        "net"
 
8
        "os"
 
9
        "path/filepath"
8
10
        "strconv"
9
11
 
 
12
        jujutesting "github.com/juju/testing"
10
13
        jc "github.com/juju/testing/checkers"
11
14
        "labix.org/v2/mgo"
12
15
        gc "launchpad.net/gocheck"
13
16
 
 
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"
17
20
)
18
21
 
19
22
type EnsureAdminSuite struct {
20
 
        testbase.LoggingSuite
 
23
        coretesting.BaseSuite
 
24
        serviceStarts int
 
25
        serviceStops  int
21
26
}
22
27
 
23
28
var _ = gc.Suite(&EnsureAdminSuite{})
24
29
 
25
30
func (s *EnsureAdminSuite) SetUpTest(c *gc.C) {
26
 
        s.PatchValue(&upstartConfInstall, func(conf *upstart.Conf) error {
27
 
                return nil
28
 
        })
29
 
        s.PatchValue(&upstartServiceStart, func(svc *upstart.Service) error {
30
 
                return nil
31
 
        })
32
 
        s.PatchValue(&upstartServiceStop, func(svc *upstart.Service) error {
 
31
        s.BaseSuite.SetUpTest(c)
 
32
        s.serviceStarts = 0
 
33
        s.serviceStops = 0
 
34
        s.PatchValue(mongo.UpstartConfInstall, func(conf *upstart.Conf) error {
 
35
                return nil
 
36
        })
 
37
        s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
 
38
                s.serviceStarts++
 
39
                return nil
 
40
        })
 
41
        s.PatchValue(mongo.UpstartServiceStop, func(svc *upstart.Service) error {
 
42
                s.serviceStops++
33
43
                return nil
34
44
        })
35
45
}
38
48
        inst := &coretesting.MgoInstance{}
39
49
        err := inst.Start(true)
40
50
        c.Assert(err, gc.IsNil)
41
 
        defer inst.Destroy()
 
51
        defer inst.DestroyWithLog()
42
52
        dialInfo := inst.DialInfo()
 
53
 
 
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 {
 
60
                return nil
 
61
        })
 
62
 
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)
 
67
 
 
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",
 
76
                "--noauth",
 
77
                "--dbpath", "db",
 
78
                "--sslOnNormalPorts",
 
79
                "--sslPEMKeyFile", "server.pem",
 
80
                "--sslPEMKeyPassword", "ignored",
 
81
                "--bind_ip", "127.0.0.1",
 
82
                "--port", portString,
 
83
                "--noprealloc",
 
84
                "--syslog",
 
85
                "--smallfiles",
 
86
                "--journal",
 
87
        )
 
88
 
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)
 
93
 
 
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)
51
97
}
52
98
 
53
99
func (s *EnsureAdminSuite) TestEnsureAdminUserError(c *gc.C) {
73
119
        c.Assert(err, gc.IsNil)
74
120
        port, err := strconv.Atoi(portString)
75
121
        c.Assert(err, gc.IsNil)
76
 
        return EnsureAdminUser(EnsureAdminUserParams{
 
122
        return mongo.EnsureAdminUser(mongo.EnsureAdminUserParams{
77
123
                DialInfo: dialInfo,
78
124
                Port:     port,
79
125
                User:     user,