@@ -144,6 +144,13 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
144144 client , _ := newFakeK8sSyncClient ()
145145 clusterName := "acid-test-cluster"
146146 namespace := "default"
147+ testSlots := map [string ]map [string ]string {
148+ "slot1" : {
149+ "type" : "logical" ,
150+ "plugin" : "wal2json" ,
151+ "database" : "foo" ,
152+ },
153+ }
147154
148155 ctrl := gomock .NewController (t )
149156 defer ctrl .Finish ()
@@ -208,11 +215,26 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
208215
209216 // simulate existing config that differs from cluster.Spec
210217 tests := []struct {
211- subtest string
212- patroni acidv1.Patroni
213- pgParams map [string ]string
214- restartPrimary bool
218+ subtest string
219+ patroni acidv1.Patroni
220+ desiredSlots map [string ]map [string ]string
221+ removedSlots map [string ]map [string ]string
222+ pgParams map [string ]string
223+ shouldBePatched bool
224+ restartPrimary bool
215225 }{
226+ {
227+ subtest : "Patroni and Postgresql.Parameters do not differ" ,
228+ patroni : acidv1.Patroni {
229+ TTL : 20 ,
230+ },
231+ pgParams : map [string ]string {
232+ "log_min_duration_statement" : "200" ,
233+ "max_connections" : "50" ,
234+ },
235+ shouldBePatched : false ,
236+ restartPrimary : false ,
237+ },
216238 {
217239 subtest : "Patroni and Postgresql.Parameters differ - restart replica first" ,
218240 patroni : acidv1.Patroni {
@@ -222,7 +244,8 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
222244 "log_min_duration_statement" : "500" , // desired 200
223245 "max_connections" : "100" , // desired 50
224246 },
225- restartPrimary : false ,
247+ shouldBePatched : true ,
248+ restartPrimary : false ,
226249 },
227250 {
228251 subtest : "multiple Postgresql.Parameters differ - restart replica first" ,
@@ -231,7 +254,8 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
231254 "log_min_duration_statement" : "500" , // desired 200
232255 "max_connections" : "100" , // desired 50
233256 },
234- restartPrimary : false ,
257+ shouldBePatched : true ,
258+ restartPrimary : false ,
235259 },
236260 {
237261 subtest : "desired max_connections bigger - restart replica first" ,
@@ -240,7 +264,8 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
240264 "log_min_duration_statement" : "200" ,
241265 "max_connections" : "30" , // desired 50
242266 },
243- restartPrimary : false ,
267+ shouldBePatched : true ,
268+ restartPrimary : false ,
244269 },
245270 {
246271 subtest : "desired max_connections smaller - restart master first" ,
@@ -249,19 +274,105 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
249274 "log_min_duration_statement" : "200" ,
250275 "max_connections" : "100" , // desired 50
251276 },
252- restartPrimary : true ,
277+ shouldBePatched : true ,
278+ restartPrimary : true ,
279+ },
280+ {
281+ subtest : "slot does not exist but is desired" ,
282+ patroni : acidv1.Patroni {
283+ TTL : 20 ,
284+ },
285+ desiredSlots : testSlots ,
286+ pgParams : map [string ]string {
287+ "log_min_duration_statement" : "200" ,
288+ "max_connections" : "50" ,
289+ },
290+ shouldBePatched : true ,
291+ restartPrimary : false ,
292+ },
293+ {
294+ subtest : "slot exist, nothing specified in manifest" ,
295+ patroni : acidv1.Patroni {
296+ TTL : 20 ,
297+ Slots : map [string ]map [string ]string {
298+ "slot1" : {
299+ "type" : "logical" ,
300+ "plugin" : "pgoutput" ,
301+ "database" : "foo" ,
302+ },
303+ },
304+ },
305+ pgParams : map [string ]string {
306+ "log_min_duration_statement" : "200" ,
307+ "max_connections" : "50" ,
308+ },
309+ shouldBePatched : false ,
310+ restartPrimary : false ,
311+ },
312+ {
313+ subtest : "slot is removed from manifest" ,
314+ patroni : acidv1.Patroni {
315+ TTL : 20 ,
316+ Slots : map [string ]map [string ]string {
317+ "slot1" : {
318+ "type" : "logical" ,
319+ "plugin" : "pgoutput" ,
320+ "database" : "foo" ,
321+ },
322+ },
323+ },
324+ removedSlots : testSlots ,
325+ pgParams : map [string ]string {
326+ "log_min_duration_statement" : "200" ,
327+ "max_connections" : "50" ,
328+ },
329+ shouldBePatched : true ,
330+ restartPrimary : false ,
331+ },
332+ {
333+ subtest : "slot plugin differs" ,
334+ patroni : acidv1.Patroni {
335+ TTL : 20 ,
336+ Slots : map [string ]map [string ]string {
337+ "slot1" : {
338+ "type" : "logical" ,
339+ "plugin" : "pgoutput" ,
340+ "database" : "foo" ,
341+ },
342+ },
343+ },
344+ desiredSlots : testSlots ,
345+ pgParams : map [string ]string {
346+ "log_min_duration_statement" : "200" ,
347+ "max_connections" : "50" ,
348+ },
349+ shouldBePatched : true ,
350+ restartPrimary : false ,
253351 },
254352 }
255353
256354 for _ , tt := range tests {
355+ if len (tt .desiredSlots ) > 0 {
356+ cluster .Spec .Patroni .Slots = tt .desiredSlots
357+ }
358+ if len (tt .removedSlots ) > 0 {
359+ for slotName , removedSlot := range tt .removedSlots {
360+ cluster .replicationSlots [slotName ] = removedSlot
361+ }
362+ }
363+
257364 configPatched , requirePrimaryRestart , err := cluster .checkAndSetGlobalPostgreSQLConfiguration (mockPod , tt .patroni , cluster .Spec .Patroni , tt .pgParams , cluster .Spec .Parameters )
258365 assert .NoError (t , err )
259- if configPatched != true {
366+ if configPatched != tt . shouldBePatched {
260367 t .Errorf ("%s - %s: expected config update did not happen" , testName , tt .subtest )
261368 }
262369 if requirePrimaryRestart != tt .restartPrimary {
263370 t .Errorf ("%s - %s: wrong master restart strategy, got restart %v, expected restart %v" , testName , tt .subtest , requirePrimaryRestart , tt .restartPrimary )
264371 }
372+
373+ // reset slots for next tests
374+ cluster .Spec .Patroni .Slots = nil
375+ cluster .replicationSlots = make (map [string ]interface {})
265376 }
266377
267378 testsFailsafe := []struct {
@@ -342,7 +453,7 @@ func TestCheckAndSetGlobalPostgreSQLConfiguration(t *testing.T) {
342453 effectiveVal : util .True (),
343454 desiredVal : true ,
344455 shouldBePatched : false , // should not require patching
345- restartPrimary : true ,
456+ restartPrimary : false ,
346457 },
347458 }
348459
0 commit comments