-
Notifications
You must be signed in to change notification settings - Fork 523
/
storage_test.go
208 lines (181 loc) · 5.65 KB
/
storage_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
// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package db
import (
"github.com/globocom/config"
"labix.org/v2/mgo"
. "launchpad.net/gocheck"
"reflect"
"sync"
"testing"
"time"
)
type hasUniqueIndexChecker struct{}
func (c *hasUniqueIndexChecker) Info() *CheckerInfo {
return &CheckerInfo{Name: "HasUniqueField", Params: []string{"collection", "key"}}
}
func (c *hasUniqueIndexChecker) Check(params []interface{}, names []string) (bool, string) {
collection, ok := params[0].(*mgo.Collection)
if !ok {
return false, "first parameter should be a mgo collection"
}
key, ok := params[1].([]string)
if !ok {
return false, "second parameter should be the key, as used for mgo index declaration (slice of strings)"
}
indexes, err := collection.Indexes()
if err != nil {
return false, "failed to get collection indexes: " + err.Error()
}
for _, index := range indexes {
if reflect.DeepEqual(index.Key, key) {
return index.Unique, ""
}
}
return false, ""
}
var HasUniqueIndex Checker = &hasUniqueIndexChecker{}
func Test(t *testing.T) { TestingT(t) }
type S struct{}
var _ = Suite(&S{})
func (s *S) SetUpSuite(c *C) {
ticker.Stop()
}
func (s *S) TearDownSuite(c *C) {
storage, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
defer storage.session.Close()
storage.session.DB("tsuru_storage_test").DropDatabase()
}
func (s *S) TearDownTest(c *C) {
conn = make(map[string]*session)
}
func (s *S) TestOpenConnectsToTheDatabase(c *C) {
storage, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
defer storage.session.Close()
c.Assert(storage.session.Ping(), IsNil)
}
func (s *S) TestOpenStoresConnectionInThePool(c *C) {
storage, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
defer storage.session.Close()
c.Assert(storage.session, Equals, conn["127.0.0.1:27017"].s)
}
func (s *S) TestOpenReusesConnection(c *C) {
storage, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
defer storage.session.Close()
storage2, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
c.Assert(storage.session, Equals, storage2.session)
}
func (s *S) TestOpenReconnects(c *C) {
storage, err := Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
storage.session.Close()
storage, err = Open("127.0.0.1:27017", "tsuru_storage_test")
c.Assert(err, IsNil)
err = storage.session.Ping()
c.Assert(err, IsNil)
}
func (s *S) TestOpenConnectionRefused(c *C) {
storage, err := Open("127.0.0.1:27018", "tsuru_storage_test")
c.Assert(storage, IsNil)
c.Assert(err, NotNil)
}
func (s *S) TestConn(c *C) {
config.Set("database:url", "127.0.0.1:27017")
defer config.Unset("database:url")
config.Set("database:name", "tsuru_storage_test")
defer config.Unset("database:name")
storage, err := Conn()
c.Assert(err, IsNil)
defer storage.session.Close()
err = storage.session.Ping()
c.Assert(err, IsNil)
}
func (s *S) TestConnMissingDatabaseUrl(c *C) {
storage, err := Conn()
c.Assert(storage, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `configuration error: key "database:url" not found`)
}
func (s *S) TestConnMissingDatabaseName(c *C) {
config.Set("database:url", "127.0.0.1:27017")
defer config.Unset("database:url")
storage, err := Conn()
c.Assert(storage, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `configuration error: key "database:name" not found`)
}
func (s *S) TestCollection(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
collection := storage.Collection("users")
c.Assert(collection.FullName, Equals, storage.dbname+".users")
}
func (s *S) TestUsers(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
users := storage.Users()
usersc := storage.Collection("users")
c.Assert(users, DeepEquals, usersc)
c.Assert(users, HasUniqueIndex, []string{"email"})
}
func (s *S) TestApps(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
apps := storage.Apps()
appsc := storage.Collection("apps")
c.Assert(apps, DeepEquals, appsc)
c.Assert(apps, HasUniqueIndex, []string{"name"})
}
func (s *S) TestServices(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
services := storage.Services()
servicesc := storage.Collection("services")
c.Assert(services, DeepEquals, servicesc)
}
func (s *S) TestServiceInstances(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
serviceInstances := storage.ServiceInstances()
serviceInstancesc := storage.Collection("service_instances")
c.Assert(serviceInstances, DeepEquals, serviceInstancesc)
}
func (s *S) TestMethodTeamsShouldReturnTeamsCollection(c *C) {
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
defer storage.session.Close()
teams := storage.Teams()
teamsc := storage.Collection("teams")
c.Assert(teams, DeepEquals, teamsc)
}
func (s *S) TestRetire(c *C) {
defer func() {
if r := recover(); !c.Failed() && r == nil {
c.Errorf("Should panic in ping, but did not!")
}
}()
storage, _ := Open("127.0.0.1:27017", "tsuru_storage_test")
sess := conn["127.0.0.1:27017"]
sess.used = sess.used.Add(-1 * 2 * period)
conn["127.0.0.1:27017"] = sess
var ticker time.Ticker
ch := make(chan time.Time, 1)
ticker.C = ch
ch <- time.Now()
var wg sync.WaitGroup
wg.Add(1)
go func() {
retire(&ticker)
wg.Done()
}()
close(ch)
wg.Wait()
_, ok := conn["127.0.0.1:27017"]
c.Check(ok, Equals, false)
storage.session.Ping()
}