forked from cloudfoundry/consuladapter
/
session_test.go
202 lines (167 loc) · 5.03 KB
/
session_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
package consuladapter_test
import (
"time"
"github.com/cloudfoundry-incubator/consuladapter"
"github.com/cloudfoundry-incubator/consuladapter/fakes"
"github.com/hashicorp/consul/api"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pivotal-golang/lager/lagertest"
)
// copied from consul to prevent unnecessary additional dependencies
const serfCheckID = "serfHealth"
var _ = Describe("Session", func() {
BeforeEach(startCluster)
AfterEach(stopCluster)
var client *api.Client
var sessionMgr *fakes.FakeSessionManager
var session *consuladapter.Session
var newSessionErr error
var logger *lagertest.TestLogger
var noChecks bool
BeforeEach(func() {
logger = lagertest.NewTestLogger("test")
client = clusterRunner.NewClient()
sessionMgr = newFakeSessionManager(client)
noChecks = false
})
JustBeforeEach(func() {
if noChecks {
session, newSessionErr = consuladapter.NewSessionNoChecks("a-session", 20*time.Second, client, sessionMgr)
} else {
session, newSessionErr = consuladapter.NewSession("a-session", 20*time.Second, client, sessionMgr)
}
})
AfterEach(func() {
if session != nil {
session.Destroy()
}
})
Describe("NewSession", func() {
Context("when consul is down", func() {
BeforeEach(stopCluster)
It("a session can be created", func() {
Expect(newSessionErr).NotTo(HaveOccurred())
Expect(session).NotTo(BeNil())
})
})
It("creates a new session", func() {
Expect(newSessionErr).NotTo(HaveOccurred())
Expect(session).NotTo(BeNil())
})
Context("when a consul session is created", func() {
JustBeforeEach(func() {
err := session.AcquireLock("foo", []byte{})
Expect(err).NotTo(HaveOccurred())
})
It("has checks", func() {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
session := findSession(session.ID(), entries)
Expect(session.Checks).To(ConsistOf(serfCheckID))
})
Context("with no checks", func() {
BeforeEach(func() {
noChecks = true
})
It("has no checks", func() {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
session := findSession(session.ID(), entries)
Expect(session.Checks).To(BeEmpty())
})
})
})
Describe("Session#Recreate", func() {
var newSession *consuladapter.Session
JustBeforeEach(func() {
err := session.AcquireLock("foo", []byte{})
Expect(err).NotTo(HaveOccurred())
})
Context("when the session exists", func() {
JustBeforeEach(func() {
var err error
newSession, err = session.Recreate()
Expect(err).NotTo(HaveOccurred())
})
It("destroys the current session", func() {
Eventually(func() *api.SessionEntry {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
return findSession(session.ID(), entries)
}).Should(BeNil())
})
It("has checks", func() {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
s := findSession(newSession.ID(), entries)
Expect(s.Checks).To(ConsistOf(serfCheckID))
})
Context("with no checks", func() {
BeforeEach(func() {
noChecks = true
})
It("has no checks", func() {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
session := findSession(newSession.ID(), entries)
Expect(session.Checks).To(BeEmpty())
})
})
})
Context("when the session does not exist", func() {
var newSession *consuladapter.Session
JustBeforeEach(func() {
var err error
session.Destroy()
newSession, err = session.Recreate()
Expect(err).NotTo(HaveOccurred())
})
It("has checks", func() {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
s := findSession(newSession.ID(), entries)
Expect(s.Checks).To(ConsistOf(serfCheckID))
})
})
})
Describe("Session#Destroy", func() {
JustBeforeEach(func() {
err := session.AcquireLock("a-key", []byte("a-value"))
Expect(err).NotTo(HaveOccurred())
Eventually(func() []*api.SessionEntry {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
return entries
}).Should(HaveLen(1))
session.Destroy()
})
It("destroys the session", func() {
Expect(sessionMgr.DestroyCallCount()).To(Equal(1))
id, _ := sessionMgr.DestroyArgsForCall(0)
Expect(id).To(Equal(session.ID()))
})
It("removes the session", func() {
Eventually(func() *api.SessionEntry {
entries, _, err := client.Session().List(nil)
Expect(err).NotTo(HaveOccurred())
return findSession(session.ID(), entries)
}).Should(BeNil())
})
It("sends a nil error", func() {
Eventually(session.Err()).Should(Receive(BeNil()))
})
It("can be called multiple times", func() {
session.Destroy()
})
})
})
})
func findSession(sessionID string, entries []*api.SessionEntry) *api.SessionEntry {
for _, e := range entries {
if e.ID == sessionID {
return e
}
}
return nil
}