Skip to content

Commit cec7aa9

Browse files
Refactor tests for new Agency state functions, add new tests with NumberOfShards > 1
1 parent 1c77297 commit cec7aa9

File tree

1 file changed

+78
-200
lines changed

1 file changed

+78
-200
lines changed

pkg/deployment/agency/state_test.go

Lines changed: 78 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -73,237 +73,115 @@ func Test_Unmarshal_LongData(t *testing.T) {
7373
}
7474

7575
func Test_IsDBServerInSync(t *testing.T) {
76-
const dbName, colID, sh1, sh2 = "db1", "1001", "shard1", "shard2"
77-
const sid1, sid2, sid3, sid4 = "PRMR-1", "PRMR-2", "PRMR-3", "PRMR-4"
78-
var s = State{
79-
Supervision: StateSupervision{},
80-
Plan: StatePlan{
81-
Collections: map[string]StatePlanDBCollections{
82-
dbName: map[string]StatePlanCollection{
83-
colID: {
84-
Shards: map[string]ShardServers{
85-
sh1: []string{sid1, sid2},
86-
sh2: []string{sid1, sid2, sid3},
87-
},
88-
},
89-
},
90-
},
91-
},
92-
Current: StateCurrent{
93-
Collections: map[string]StateCurrentDBCollections{
94-
dbName: map[string]StateCurrentDBCollection{
95-
colID: map[string]StateCurrentDBShard{
96-
sh1: {Servers: []string{sid2, sid3}},
97-
sh2: {Servers: []string{sid1, sid2, sid3}},
98-
},
99-
},
100-
},
101-
},
102-
}
76+
var state = GenerateState(t, NewDatabaseRandomGenerator().RandomCollection().
77+
WithShard().WithPlan("A", "B").WithCurrent("B", "C").Add().
78+
WithShard().WithPlan("A", "B", "C").WithCurrent("A", "B", "C").Add().
79+
WithWriteConcern(1).Add().Add(),
80+
)
10381
var expected = map[string]bool{
104-
sid1: false, // in plan, not synced
105-
sid2: true, // in plan, synced
106-
sid3: true, // not in plan, synced
107-
sid4: true, // not in plan, not synced
82+
"A": false, // in plan, not synced
83+
"B": true, // in plan, synced
84+
"C": true, // not in plan, synced
85+
"D": true, // not in plan, not synced
10886
}
109-
11087
for serverID, inSync := range expected {
111-
require.Equalf(t, inSync, s.IsDBServerInSync(serverID), "server %s", serverID)
112-
}
113-
}
114-
115-
func Test_IsDBServerIsReadyToRestart(t *testing.T) {
116-
const dbName, colID, sh1, sh2, sh3 = "db1", "1001", "shard1", "shard2", "shard3"
117-
const sid1, sid2, sid3, sid4 = "PRMR-1", "PRMR-2", "PRMR-3", "PRMR-4"
118-
var wc1, wc2, wc3 = 1, 2, 3
119-
120-
var testCases = map[string]struct {
121-
state State
122-
expected map[string]bool
123-
}{
124-
"write-concern-1": {
125-
state: State{
126-
Supervision: StateSupervision{},
127-
Plan: StatePlan{
128-
Collections: map[string]StatePlanDBCollections{
129-
dbName: map[string]StatePlanCollection{
130-
colID: {
131-
WriteConcern: &wc1,
132-
Shards: map[string]ShardServers{
133-
sh1: []string{sid1, sid2},
134-
sh2: []string{sid1, sid2, sid3},
135-
},
136-
},
137-
},
138-
},
139-
},
140-
Current: StateCurrent{
141-
Collections: map[string]StateCurrentDBCollections{
142-
dbName: map[string]StateCurrentDBCollection{
143-
colID: map[string]StateCurrentDBShard{
144-
sh1: {Servers: []string{sid2, sid3}},
145-
sh2: {Servers: []string{sid1, sid2, sid3}},
146-
},
147-
},
148-
},
149-
},
150-
},
151-
expected: map[string]bool{
152-
sid1: true,
153-
sid2: true,
154-
sid3: true,
155-
sid4: true,
156-
},
157-
},
158-
"write-concern-2": {
159-
state: State{
160-
Supervision: StateSupervision{},
161-
Plan: StatePlan{
162-
Collections: map[string]StatePlanDBCollections{
163-
dbName: map[string]StatePlanCollection{
164-
colID: {
165-
WriteConcern: &wc2,
166-
Shards: map[string]ShardServers{
167-
sh1: []string{sid1, sid2},
168-
sh2: []string{sid1, sid2, sid3},
169-
sh3: []string{sid1, sid3},
170-
},
171-
},
172-
},
173-
},
174-
},
175-
Current: StateCurrent{
176-
Collections: map[string]StateCurrentDBCollections{
177-
dbName: map[string]StateCurrentDBCollection{
178-
colID: map[string]StateCurrentDBShard{
179-
sh1: {Servers: []string{sid3}},
180-
sh2: {Servers: []string{sid1, sid2, sid3}},
181-
sh3: {Servers: []string{}},
182-
},
183-
},
184-
},
185-
},
186-
},
187-
expected: map[string]bool{
188-
sid1: false,
189-
sid2: false,
190-
sid3: true,
191-
sid4: true,
192-
},
193-
},
194-
"write-concern-3": {
195-
state: State{
196-
Supervision: StateSupervision{},
197-
Plan: StatePlan{
198-
Collections: map[string]StatePlanDBCollections{
199-
dbName: map[string]StatePlanCollection{
200-
colID: {
201-
WriteConcern: &wc3,
202-
Shards: map[string]ShardServers{
203-
sh1: []string{sid1, sid2, sid3},
204-
sh2: []string{sid1, sid2, sid3},
205-
},
206-
},
207-
},
208-
},
209-
},
210-
Current: StateCurrent{
211-
Collections: map[string]StateCurrentDBCollections{
212-
dbName: map[string]StateCurrentDBCollection{
213-
colID: map[string]StateCurrentDBShard{
214-
sh1: {Servers: []string{sid3}},
215-
sh2: {Servers: []string{sid1, sid2, sid3}},
216-
},
217-
},
218-
},
219-
},
220-
},
221-
expected: map[string]bool{
222-
sid1: false,
223-
sid2: false,
224-
sid3: true,
225-
sid4: true,
226-
},
227-
},
228-
}
229-
230-
for name, testCase := range testCases {
231-
t.Run(name, func(t *testing.T) {
232-
for server, readyToRestart := range testCase.expected {
233-
require.Equalf(t, readyToRestart, testCase.state.IsDBServerReadyToRestart(server), "server %s", server)
234-
}
235-
})
88+
require.Equalf(t, inSync, state.IsDBServerInSync(serverID), "server %s", serverID)
23689
}
237-
23890
}
23991

24092
func Test_IsDBServerIsReadyToRestart_New(t *testing.T) {
241-
type tc struct {
93+
type testCase struct {
24294
generator StateGenerator
24395
ready bool
244-
dbserver string
96+
dbServer string
24597
}
246-
247-
// TODO: Add More complex TC
248-
249-
tcs := map[string]tc{
250-
"DB Not in Plan": {
251-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A").WithCurrent("A").Add().WithWriteConcern(1).Add().Add(),
98+
newDBWithCol := func(writeConcern int) CollectionGeneratorInterface {
99+
return NewDatabaseRandomGenerator().RandomCollection().WithWriteConcern(writeConcern)
100+
}
101+
tcs := map[string]testCase{
102+
"not in Plan, in Current": {
103+
generator: newDBWithCol(1).WithShard().WithPlan("A").WithCurrent("B").Add().Add().Add(),
252104
ready: true,
253-
dbserver: "B",
105+
dbServer: "B",
254106
},
255-
"DB in Plan, but WC == RF": {
256-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A").WithCurrent("A").Add().WithWriteConcern(1).Add().Add(),
107+
"not in Plan, not in Current": {
108+
generator: newDBWithCol(1).WithShard().WithPlan("A").WithCurrent("A").Add().Add().Add(),
257109
ready: true,
258-
dbserver: "A",
110+
dbServer: "C",
259111
},
260-
"DB in Plan, only in Current": {
261-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B").WithCurrent("A").Add().WithWriteConcern(1).Add().Add(),
112+
"in Plan and WC == RF": {
113+
generator: newDBWithCol(1).WithShard().WithPlan("A").WithCurrent("A").Add().Add().Add(),
114+
ready: true,
115+
dbServer: "A",
116+
},
117+
"in Plan, the only in Current": {
118+
generator: newDBWithCol(1).WithShard().WithPlan("A", "B").WithCurrent("A").Add().Add().Add(),
262119
ready: false,
263-
dbserver: "A",
120+
dbServer: "A",
264121
},
265-
"DB in Plan, missing in Current": {
266-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B").WithCurrent("B").Add().WithWriteConcern(1).Add().Add(),
122+
"in Plan, missing in Current": {
123+
generator: newDBWithCol(1).WithShard().WithPlan("A", "B").WithCurrent("B").Add().Add().Add(),
267124
ready: true,
268-
dbserver: "A",
125+
dbServer: "A",
269126
},
270-
"DB in Plan, missing in Current but broken WC": {
271-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B", "C").WithCurrent("B").Add().WithWriteConcern(2).Add().Add(),
127+
"in Plan, missing in Current but broken WC": {
128+
generator: newDBWithCol(2).WithShard().WithPlan("A", "B", "C").WithCurrent("B").Add().Add().Add(),
272129
ready: false,
273-
dbserver: "A",
130+
dbServer: "A",
274131
},
275-
"DB in Plan, missing in Current with fine WC": {
276-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B", "C").WithCurrent("B", "C").Add().WithWriteConcern(2).Add().Add(),
132+
"in Plan, missing in Current with fine WC": {
133+
generator: newDBWithCol(2).WithShard().WithPlan("A", "B", "C").WithCurrent("B", "C").Add().Add().Add(),
277134
ready: true,
278-
dbserver: "A",
135+
dbServer: "A",
279136
},
280-
"DB in Plan, missing in Current with low WC": {
281-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().WithWriteConcern(1).Add().Add(),
137+
"in Plan, missing in Current with low WC": {
138+
generator: newDBWithCol(1).WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().Add().Add(),
282139
ready: true,
283-
dbserver: "A",
140+
dbServer: "A",
284141
},
285-
"DB in Plan, in Current with break WC": {
286-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().WithWriteConcern(2).Add().Add(),
142+
"in Plan, in Current but broken WC": {
143+
generator: newDBWithCol(2).WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().Add().Add(),
287144
ready: false,
288-
dbserver: "A",
145+
dbServer: "A",
289146
},
290-
"DB in Plan, only in Current, default WC": {
291-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B").WithCurrent("A").Add().Add().Add(),
292-
ready: false,
293-
dbserver: "A",
147+
"in Plan, all shards in sync": {
148+
generator: newDBWithCol(1).
149+
WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().
150+
WithShard().WithPlan("A", "D").WithCurrent("D", "A").Add().
151+
Add().Add(),
152+
ready: true,
153+
dbServer: "A",
294154
},
295-
"DB in Plan, in Current with default WC": {
296-
generator: NewDatabaseRandomGenerator().RandomCollection().WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().Add().Add(),
297-
ready: true,
298-
dbserver: "A",
155+
"in Plan, all shards in sync but broken WC": {
156+
generator: newDBWithCol(2).
157+
WithShard().WithPlan("A", "B", "C").WithCurrent("B", "A").Add().
158+
WithShard().WithPlan("A", "D").WithCurrent("D", "A").Add().
159+
Add().Add(),
160+
ready: false,
161+
dbServer: "A",
162+
},
163+
"in Plan, some shards not synced": {
164+
generator: newDBWithCol(1).
165+
WithShard().WithPlan("A", "B", "C").WithCurrent("A", "B", "C").Add().
166+
WithShard().WithPlan("A", "B", "C").WithCurrent("C").Add().
167+
Add().Add(),
168+
ready: true,
169+
dbServer: "A",
170+
},
171+
"in Plan, some shards not synced and broken WC": {
172+
generator: newDBWithCol(2).
173+
WithShard().WithPlan("A", "B", "C").WithCurrent("A", "B", "C").Add().
174+
WithShard().WithPlan("A", "B", "C").WithCurrent("C").Add().
175+
Add().Add(),
176+
ready: false,
177+
dbServer: "A",
299178
},
300179
}
301180

302-
for name, det := range tcs {
181+
for name, tc := range tcs {
303182
t.Run(name, func(t *testing.T) {
304-
s := GenerateState(t, det.generator)
305-
306-
require.Equal(t, det.ready, s.IsDBServerReadyToRestart(det.dbserver))
183+
s := GenerateState(t, tc.generator)
184+
require.Equal(t, tc.ready, s.IsDBServerReadyToRestart(tc.dbServer))
307185
})
308186
}
309187
}

0 commit comments

Comments
 (0)