-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_cross_test.go
228 lines (197 loc) · 5.89 KB
/
manager_cross_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lease_test
import (
"time"
"github.com/juju/clock/testclock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
corelease "github.com/juju/juju/core/lease"
"github.com/juju/juju/worker/lease"
)
// Tests that check the manager handles leases across namespaces and
// models correctly.
type CrossSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&CrossSuite{})
func (s *CrossSuite) testClaims(c *gc.C, lease1, lease2 corelease.Key) {
fix := Fixture{
expectCalls: []call{{
method: "ClaimLease",
args: []interface{}{
lease1,
corelease.Request{Holder: "sgt-howie", Duration: time.Minute},
},
callback: func(leases map[corelease.Key]corelease.Info) {
leases[lease1] = corelease.Info{
Holder: "sgt-howie",
Expiry: offset(time.Second),
}
},
}, {
method: "ClaimLease",
args: []interface{}{
lease2,
corelease.Request{Holder: "rowan", Duration: time.Minute},
},
callback: func(leases map[corelease.Key]corelease.Info) {
leases[lease2] = corelease.Info{
Holder: "rowan",
Expiry: offset(time.Second),
}
},
}},
}
fix.RunTest(c, func(manager *lease.Manager, _ *testclock.Clock) {
claimer1, err := manager.Claimer(lease1.Namespace, lease1.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
claimer2, err := manager.Claimer(lease2.Namespace, lease2.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
err = claimer1.Claim(lease1.Lease, "sgt-howie", time.Minute)
c.Assert(err, jc.ErrorIsNil)
err = claimer2.Claim(lease2.Lease, "rowan", time.Minute)
c.Assert(err, jc.ErrorIsNil)
err = claimer1.Claim(lease1.Lease, "lord-summerisle", time.Minute)
c.Assert(err, gc.Equals, corelease.ErrClaimDenied)
})
}
func (s *CrossSuite) TestClaimAcrossNamespaces(c *gc.C) {
s.testClaims(c,
key("ns1", "model", "summerisle"),
key("ns2", "model", "summerisle"),
)
}
func (s *CrossSuite) TestClaimAcrossModels(c *gc.C) {
s.testClaims(c,
key("ns", "model1", "summerisle"),
key("ns", "model2", "summerisle"),
)
}
func (s *CrossSuite) testWaits(c *gc.C, lease1, lease2 corelease.Key) {
fix := Fixture{
leases: map[corelease.Key]corelease.Info{
lease1: {
Holder: "sgt-howie",
Expiry: offset(time.Second),
},
lease2: {
Holder: "willow",
Expiry: offset(time.Minute),
},
},
}
fix.RunTest(c, func(manager *lease.Manager, clock *testclock.Clock) {
b1 := newBlockTest(c, manager, lease1)
b2 := newBlockTest(c, manager, lease2)
b1.assertBlocked(c)
b2.assertBlocked(c)
clock.Advance(2 * time.Second)
err := b1.assertUnblocked(c)
c.Assert(err, jc.ErrorIsNil)
b2.assertBlocked(c)
})
}
func (s *CrossSuite) TestWaitAcrossNamespaces(c *gc.C) {
s.testWaits(c,
key("ns1", "model", "summerisle"),
key("ns2", "model", "summerisle"),
)
}
func (s *CrossSuite) TestWaitAcrossModels(c *gc.C) {
s.testWaits(c,
key("ns", "model1", "summerisle"),
key("ns", "model2", "summerisle"),
)
}
func (s *CrossSuite) testChecks(c *gc.C, lease1, lease2 corelease.Key) {
fix := Fixture{
leases: map[corelease.Key]corelease.Info{
lease1: {
Holder: "sgt-howie",
Expiry: offset(time.Second),
Trapdoor: corelease.LockedTrapdoor,
},
},
}
fix.RunTest(c, func(manager *lease.Manager, _ *testclock.Clock) {
checker1, err := manager.Checker(lease1.Namespace, lease1.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
checker2, err := manager.Checker(lease2.Namespace, lease2.ModelUUID)
c.Assert(err, jc.ErrorIsNil)
t1 := checker1.Token(lease1.Lease, "sgt-howie")
c.Assert(t1.Check(0, nil), gc.Equals, nil)
t2 := checker2.Token(lease2.Lease, "sgt-howie")
err = t2.Check(0, nil)
c.Assert(errors.Cause(err), gc.Equals, corelease.ErrNotHeld)
})
}
func (s *CrossSuite) TestCheckAcrossNamespaces(c *gc.C) {
s.testChecks(c,
key("ns1", "model", "summerisle"),
key("ns2", "model", "summerisle"),
)
}
func (s *CrossSuite) TestCheckAcrossModels(c *gc.C) {
s.testChecks(c,
key("ns", "model1", "summerisle"),
key("ns", "model2", "summerisle"),
)
}
func (s *CrossSuite) TestDifferentNamespaceValidation(c *gc.C) {
clock := testclock.NewClock(defaultClockStart)
store := NewStore(nil, nil, clock)
manager, err := lease.NewManager(lease.ManagerConfig{
Clock: clock,
Store: store,
Secretary: func(namespace string) (lease.Secretary, error) {
switch namespace {
case "ns1":
return Secretary{}, nil
case "ns2":
return OtherSecretary{}, nil
default:
return nil, errors.Errorf("bad namespace!")
}
},
MaxSleep: defaultMaxSleep,
Logger: loggo.GetLogger("lease_test"),
PrometheusRegisterer: noopRegisterer{},
})
c.Assert(err, jc.ErrorIsNil)
defer func() {
manager.Kill()
err := manager.Wait()
c.Check(err, jc.ErrorIsNil)
}()
defer store.Wait(c)
_, err = manager.Claimer("something-else", "model")
c.Assert(err, gc.ErrorMatches, "bad namespace!")
c1, err := manager.Claimer("ns1", "model")
c.Assert(err, jc.ErrorIsNil)
err = c1.Claim("INVALID", "sgt-howie", time.Minute)
c.Assert(err, gc.ErrorMatches, `cannot claim lease "INVALID": name not valid`)
c2, err := manager.Claimer("ns2", "model")
c.Assert(err, jc.ErrorIsNil)
err = c2.Claim("INVALID", "sgt-howie", time.Minute)
c.Assert(err, gc.ErrorMatches, `cannot claim lease "INVALID": lease name not valid`)
}
type OtherSecretary struct{}
// CheckLease is part of the lease.Secretary interface.
func (OtherSecretary) CheckLease(_ corelease.Key) error {
return errors.NotValidf("lease name")
}
// CheckHolder is part of the lease.Secretary interface.
func (OtherSecretary) CheckHolder(_ string) error {
return errors.NotValidf("holder name")
}
// CheckDuration is part of the lease.Secretary interface.
func (OtherSecretary) CheckDuration(duration time.Duration) error {
if duration != time.Hour {
return errors.NotValidf("time")
}
return nil
}