11package cluster
22
33import (
4- "encoding/json"
54 "fmt"
65 "reflect"
76 "strings"
@@ -167,6 +166,15 @@ var (
167166 }
168167)
169168
169+ func TestGatherApplicationIds (t * testing.T ) {
170+ testAppIds := []string {appId }
171+ appIds := gatherApplicationIds (pg .Spec .Streams )
172+
173+ if ! util .IsEqualIgnoreOrder (testAppIds , appIds ) {
174+ t .Errorf ("gathered applicationIds do not match, expected %#v, got %#v" , testAppIds , appIds )
175+ }
176+ }
177+
170178func TestGenerateFabricEventStream (t * testing.T ) {
171179 client , _ := newFakeK8sStreamClient ()
172180
@@ -206,12 +214,18 @@ func TestGenerateFabricEventStream(t *testing.T) {
206214 t .Errorf ("malformed FabricEventStream, expected %#v, got %#v" , fes , result )
207215 }
208216
209- // compare stream returned from API with expected stream
210217 listOptions := metav1.ListOptions {
211218 LabelSelector : cluster .labelsSet (true ).String (),
212219 }
213220 streams , err := cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
214221 assert .NoError (t , err )
222+
223+ // check if there is only one stream
224+ if len (streams .Items ) > 1 {
225+ t .Errorf ("too many stream CRDs found: got %d, but expected only one" , len (streams .Items ))
226+ }
227+
228+ // compare stream returned from API with expected stream
215229 if match , _ := sameStreams (streams .Items [0 ].Spec .EventStreams , fes .Spec .EventStreams ); ! match {
216230 t .Errorf ("malformed FabricEventStream returned from API, expected %#v, got %#v" , fes , streams .Items [0 ])
217231 }
@@ -220,9 +234,15 @@ func TestGenerateFabricEventStream(t *testing.T) {
220234 err = cluster .createOrUpdateStreams ()
221235 assert .NoError (t , err )
222236
223- // compare stream resturned from API with generated stream
224237 streams , err = cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
225238 assert .NoError (t , err )
239+
240+ // check if there is still only one stream
241+ if len (streams .Items ) > 1 {
242+ t .Errorf ("too many stream CRDs found after sync: got %d, but expected only one" , len (streams .Items ))
243+ }
244+
245+ // compare stream resturned from API with generated stream
226246 if match , _ := sameStreams (streams .Items [0 ].Spec .EventStreams , result .Spec .EventStreams ); ! match {
227247 t .Errorf ("returned FabricEventStream differs from generated one, expected %#v, got %#v" , result , streams .Items [0 ])
228248 }
@@ -335,31 +355,26 @@ func TestUpdateFabricEventStream(t *testing.T) {
335355 _ , err = cluster .createStatefulSet ()
336356 assert .NoError (t , err )
337357
358+ // now create the stream
338359 err = cluster .createOrUpdateStreams ()
339360 assert .NoError (t , err )
340361
341- var pgSpec acidv1.PostgresSpec
342- pgSpec .Streams = []acidv1.Stream {
343- {
344- ApplicationId : appId ,
345- Database : dbName ,
346- Tables : map [string ]acidv1.StreamTable {
347- "data.bar" : acidv1.StreamTable {
348- EventType : "stream-type-c" ,
349- IdColumn : k8sutil .StringToPointer ("b_id" ),
350- PayloadColumn : k8sutil .StringToPointer ("b_payload" ),
351- },
352- },
353- BatchSize : k8sutil .UInt32ToPointer (uint32 (250 )),
354- },
362+ // change specs of streams and patch CRD
363+ for i , stream := range pg .Spec .Streams {
364+ if stream .ApplicationId == appId {
365+ streamTable := stream .Tables ["data.bar" ]
366+ streamTable .EventType = "stream-type-c"
367+ stream .Tables ["data.bar" ] = streamTable
368+ stream .BatchSize = k8sutil .UInt32ToPointer (uint32 (250 ))
369+ pg .Spec .Streams [i ] = stream
370+ }
355371 }
356- patch , err := json .Marshal (struct {
357- PostgresSpec interface {} `json:"spec"`
358- }{& pgSpec })
372+
373+ patchData , err := specPatch (pg .Spec )
359374 assert .NoError (t , err )
360375
361376 pgPatched , err := cluster .KubeClient .Postgresqls (namespace ).Patch (
362- context .TODO (), cluster .Name , types .MergePatchType , patch , metav1.PatchOptions {}, "spec" )
377+ context .TODO (), cluster .Name , types .MergePatchType , patchData , metav1.PatchOptions {}, "spec" )
363378 assert .NoError (t , err )
364379
365380 cluster .Postgresql .Spec = pgPatched .Spec
0 commit comments