~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/testing/mgo.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
var (
31
31
        // MgoServer is a shared mongo server used by tests.
32
 
        MgoServer = &MgoInstance{}
 
32
        MgoServer = &MgoInstance{ssl: true}
33
33
)
34
34
 
35
35
type MgoInstance struct {
48
48
        // dir holds the directory that MongoDB is running in.
49
49
        dir string
50
50
 
 
51
        // ssl determines whether the MongoDB server will use TLS
 
52
        ssl bool
 
53
 
51
54
        // Params is a list of additional parameters that will be passed to
52
55
        // the mongod application
53
56
        Params []string
75
78
}
76
79
 
77
80
// Start starts a MongoDB server in a temporary directory.
78
 
func (inst *MgoInstance) Start() error {
 
81
func (inst *MgoInstance) Start(ssl bool) error {
79
82
        dbdir, err := ioutil.TempDir("", "test-mgo")
80
83
        if err != nil {
81
84
                return err
82
85
        }
 
86
 
 
87
        // give them all the same keyfile so they can talk appropriately
 
88
        keyFilePath := filepath.Join(dbdir, "keyfile")
 
89
        err = ioutil.WriteFile(keyFilePath, []byte("not very secret"), 0600)
 
90
        if err != nil {
 
91
                return fmt.Errorf("cannot write key file: %v", err)
 
92
        }
 
93
 
83
94
        pemPath := filepath.Join(dbdir, "server.pem")
84
95
        err = ioutil.WriteFile(pemPath, []byte(ServerCert+ServerKey), 0600)
85
96
        if err != nil {
88
99
        inst.port = FindTCPPort()
89
100
        inst.addr = fmt.Sprintf("localhost:%d", inst.port)
90
101
        inst.dir = dbdir
 
102
        inst.ssl = ssl
91
103
        if err := inst.run(); err != nil {
92
104
                inst.addr = ""
93
105
                inst.port = 0
103
115
        if inst.server != nil {
104
116
                panic("mongo server is already running")
105
117
        }
 
118
 
106
119
        mgoport := strconv.Itoa(inst.port)
107
120
        mgoargs := []string{
108
121
                "--auth",
109
122
                "--dbpath", inst.dir,
110
 
                "--sslOnNormalPorts",
111
 
                "--sslPEMKeyFile", filepath.Join(inst.dir, "server.pem"),
112
 
                "--sslPEMKeyPassword", "ignored",
113
123
                "--port", mgoport,
114
124
                "--nssize", "1",
115
125
                "--noprealloc",
116
126
                "--smallfiles",
117
127
                "--nojournal",
118
128
                "--nounixsocket",
 
129
                "--oplogSize", "10",
 
130
                "--keyFile", filepath.Join(inst.dir, "keyfile"),
 
131
        }
 
132
        if inst.ssl {
 
133
                mgoargs = append(mgoargs,
 
134
                        "--sslOnNormalPorts",
 
135
                        "--sslPEMKeyFile", filepath.Join(inst.dir, "server.pem"),
 
136
                        "--sslPEMKeyPassword", "ignored")
119
137
        }
120
138
        if inst.Params != nil {
121
139
                mgoargs = append(mgoargs, inst.Params...)
168
186
// testing what happens when a state server goes down.
169
187
func (inst *MgoInstance) Restart() {
170
188
        inst.kill()
171
 
        if err := inst.Start(); err != nil {
 
189
        if err := inst.Start(inst.ssl); err != nil {
172
190
                panic(err)
173
191
        }
174
192
}
176
194
// MgoTestPackage should be called to register the tests for any package that
177
195
// requires a MongoDB server.
178
196
func MgoTestPackage(t *stdtesting.T) {
179
 
        if err := MgoServer.Start(); err != nil {
 
197
        MgoTestPackageSsl(t, true)
 
198
}
 
199
 
 
200
func MgoTestPackageSsl(t *stdtesting.T, ssl bool) {
 
201
        if err := MgoServer.Start(ssl); err != nil {
180
202
                t.Fatal(err)
181
203
        }
182
204
        defer MgoServer.Destroy()
296
318
                // happen when tests fail.
297
319
                log.Noticef("testing: restarting MongoDB server after unauthorized access")
298
320
                inst.Destroy()
299
 
                if err := inst.Start(); err != nil {
 
321
                if err := inst.Start(inst.ssl); err != nil {
300
322
                        panic(err)
301
323
                }
302
324
                return