@@ -2,6 +2,7 @@ package cluster
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67
78 acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
@@ -17,7 +18,7 @@ func int32ToPointer(value int32) *int32 {
1718 return & value
1819}
1920
20- func deploymentUpdated (cluster * Cluster , err error ) error {
21+ func deploymentUpdated (cluster * Cluster , err error , reason SyncReason ) error {
2122 if cluster .ConnectionPooler .Deployment .Spec .Replicas == nil ||
2223 * cluster .ConnectionPooler .Deployment .Spec .Replicas != 2 {
2324 return fmt .Errorf ("Wrong nubmer of instances" )
@@ -26,7 +27,7 @@ func deploymentUpdated(cluster *Cluster, err error) error {
2627 return nil
2728}
2829
29- func objectsAreSaved (cluster * Cluster , err error ) error {
30+ func objectsAreSaved (cluster * Cluster , err error , reason SyncReason ) error {
3031 if cluster .ConnectionPooler == nil {
3132 return fmt .Errorf ("Connection pooler resources are empty" )
3233 }
@@ -42,14 +43,24 @@ func objectsAreSaved(cluster *Cluster, err error) error {
4243 return nil
4344}
4445
45- func objectsAreDeleted (cluster * Cluster , err error ) error {
46+ func objectsAreDeleted (cluster * Cluster , err error , reason SyncReason ) error {
4647 if cluster .ConnectionPooler != nil {
4748 return fmt .Errorf ("Connection pooler was not deleted" )
4849 }
4950
5051 return nil
5152}
5253
54+ func noEmptySync (cluster * Cluster , err error , reason SyncReason ) error {
55+ for _ , msg := range reason {
56+ if strings .HasPrefix (msg , "update [] from '<nil>' to '" ) {
57+ return fmt .Errorf ("There is an empty reason, %s" , msg )
58+ }
59+ }
60+
61+ return nil
62+ }
63+
5364func TestConnectionPoolerSynchronization (t * testing.T ) {
5465 testName := "Test connection pooler synchronization"
5566 var cluster = New (
@@ -91,15 +102,15 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
91102
92103 clusterNewDefaultsMock := * cluster
93104 clusterNewDefaultsMock .KubeClient = k8sutil .NewMockKubernetesClient ()
94- cluster .OpConfig .ConnectionPooler .Image = "pooler:2.0"
95- cluster .OpConfig .ConnectionPooler .NumberOfInstances = int32ToPointer (2 )
96105
97106 tests := []struct {
98- subTest string
99- oldSpec * acidv1.Postgresql
100- newSpec * acidv1.Postgresql
101- cluster * Cluster
102- check func (cluster * Cluster , err error ) error
107+ subTest string
108+ oldSpec * acidv1.Postgresql
109+ newSpec * acidv1.Postgresql
110+ cluster * Cluster
111+ defaultImage string
112+ defaultInstances int32
113+ check func (cluster * Cluster , err error , reason SyncReason ) error
103114 }{
104115 {
105116 subTest : "create if doesn't exist" ,
@@ -113,8 +124,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
113124 ConnectionPooler : & acidv1.ConnectionPooler {},
114125 },
115126 },
116- cluster : & clusterMissingObjects ,
117- check : objectsAreSaved ,
127+ cluster : & clusterMissingObjects ,
128+ defaultImage : "pooler:1.0" ,
129+ defaultInstances : 1 ,
130+ check : objectsAreSaved ,
118131 },
119132 {
120133 subTest : "create if doesn't exist with a flag" ,
@@ -126,8 +139,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
126139 EnableConnectionPooler : boolToPointer (true ),
127140 },
128141 },
129- cluster : & clusterMissingObjects ,
130- check : objectsAreSaved ,
142+ cluster : & clusterMissingObjects ,
143+ defaultImage : "pooler:1.0" ,
144+ defaultInstances : 1 ,
145+ check : objectsAreSaved ,
131146 },
132147 {
133148 subTest : "create from scratch" ,
@@ -139,8 +154,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
139154 ConnectionPooler : & acidv1.ConnectionPooler {},
140155 },
141156 },
142- cluster : & clusterMissingObjects ,
143- check : objectsAreSaved ,
157+ cluster : & clusterMissingObjects ,
158+ defaultImage : "pooler:1.0" ,
159+ defaultInstances : 1 ,
160+ check : objectsAreSaved ,
144161 },
145162 {
146163 subTest : "delete if not needed" ,
@@ -152,8 +169,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
152169 newSpec : & acidv1.Postgresql {
153170 Spec : acidv1.PostgresSpec {},
154171 },
155- cluster : & clusterMock ,
156- check : objectsAreDeleted ,
172+ cluster : & clusterMock ,
173+ defaultImage : "pooler:1.0" ,
174+ defaultInstances : 1 ,
175+ check : objectsAreDeleted ,
157176 },
158177 {
159178 subTest : "cleanup if still there" ,
@@ -163,8 +182,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
163182 newSpec : & acidv1.Postgresql {
164183 Spec : acidv1.PostgresSpec {},
165184 },
166- cluster : & clusterDirtyMock ,
167- check : objectsAreDeleted ,
185+ cluster : & clusterDirtyMock ,
186+ defaultImage : "pooler:1.0" ,
187+ defaultInstances : 1 ,
188+ check : objectsAreDeleted ,
168189 },
169190 {
170191 subTest : "update deployment" ,
@@ -182,8 +203,10 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
182203 },
183204 },
184205 },
185- cluster : & clusterMock ,
186- check : deploymentUpdated ,
206+ cluster : & clusterMock ,
207+ defaultImage : "pooler:1.0" ,
208+ defaultInstances : 1 ,
209+ check : deploymentUpdated ,
187210 },
188211 {
189212 subTest : "update image from changed defaults" ,
@@ -197,14 +220,40 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
197220 ConnectionPooler : & acidv1.ConnectionPooler {},
198221 },
199222 },
200- cluster : & clusterNewDefaultsMock ,
201- check : deploymentUpdated ,
223+ cluster : & clusterNewDefaultsMock ,
224+ defaultImage : "pooler:2.0" ,
225+ defaultInstances : 2 ,
226+ check : deploymentUpdated ,
227+ },
228+ {
229+ subTest : "there is no sync from nil to an empty spec" ,
230+ oldSpec : & acidv1.Postgresql {
231+ Spec : acidv1.PostgresSpec {
232+ EnableConnectionPooler : boolToPointer (true ),
233+ ConnectionPooler : nil ,
234+ },
235+ },
236+ newSpec : & acidv1.Postgresql {
237+ Spec : acidv1.PostgresSpec {
238+ EnableConnectionPooler : boolToPointer (true ),
239+ ConnectionPooler : & acidv1.ConnectionPooler {},
240+ },
241+ },
242+ cluster : & clusterMock ,
243+ defaultImage : "pooler:1.0" ,
244+ defaultInstances : 1 ,
245+ check : noEmptySync ,
202246 },
203247 }
204248 for _ , tt := range tests {
205- err := tt .cluster .syncConnectionPooler (tt .oldSpec , tt .newSpec , mockInstallLookupFunction )
249+ tt .cluster .OpConfig .ConnectionPooler .Image = tt .defaultImage
250+ tt .cluster .OpConfig .ConnectionPooler .NumberOfInstances =
251+ int32ToPointer (tt .defaultInstances )
252+
253+ reason , err := tt .cluster .syncConnectionPooler (tt .oldSpec ,
254+ tt .newSpec , mockInstallLookupFunction )
206255
207- if err := tt .check (tt .cluster , err ); err != nil {
256+ if err := tt .check (tt .cluster , err , reason ); err != nil {
208257 t .Errorf ("%s [%s]: Could not synchronize, %+v" ,
209258 testName , tt .subTest , err )
210259 }
0 commit comments