90
90
Namespace : namespace ,
91
91
Labels : map [string ]string {
92
92
"application" : "spilo" ,
93
- "cluster-name" : fmt . Sprintf ( "%s-2" , clusterName ) ,
93
+ "cluster-name" : clusterName ,
94
94
"team" : "acid" ,
95
95
},
96
96
OwnerReferences : []metav1.OwnerReference {
@@ -494,14 +494,13 @@ func TestSyncStreams(t *testing.T) {
494
494
OpConfig : config.Config {
495
495
PodManagementPolicy : "ordered_ready" ,
496
496
Resources : config.Resources {
497
- ClusterLabels : map [string ]string {"application" : "spilo" },
498
- ClusterNameLabel : "cluster-name" ,
499
- DefaultCPURequest : "300m" ,
500
- DefaultCPULimit : "300m" ,
501
- DefaultMemoryRequest : "300Mi" ,
502
- DefaultMemoryLimit : "300Mi" ,
503
- EnableOwnerReferences : util .True (),
504
- PodRoleLabel : "spilo-role" ,
497
+ ClusterLabels : map [string ]string {"application" : "spilo" },
498
+ ClusterNameLabel : "cluster-name" ,
499
+ DefaultCPURequest : "300m" ,
500
+ DefaultCPULimit : "300m" ,
501
+ DefaultMemoryRequest : "300Mi" ,
502
+ DefaultMemoryLimit : "300Mi" ,
503
+ PodRoleLabel : "spilo-role" ,
505
504
},
506
505
},
507
506
}, client , pg , logger , eventRecorder )
@@ -514,33 +513,17 @@ func TestSyncStreams(t *testing.T) {
514
513
err = cluster .syncStream (appId )
515
514
assert .NoError (t , err )
516
515
517
- // create a second stream with same spec but with different name
518
- createdStream , err := cluster .KubeClient .FabricEventStreams (namespace ).Create (
519
- context .TODO (), fes , metav1.CreateOptions {})
516
+ // sync the stream again
517
+ err = cluster .syncStream (appId )
520
518
assert .NoError (t , err )
521
- assert .Equal (t , createdStream .Spec .ApplicationId , appId )
522
519
523
- // check that two streams exist
520
+ // check that only one stream remains after sync
524
521
listOptions := metav1.ListOptions {
525
522
LabelSelector : cluster .labelsSet (true ).String (),
526
523
}
527
524
streams , err := cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
528
525
assert .NoError (t , err )
529
- assert .Equalf (t , 2 , len (streams .Items ), "unexpected number of streams found: got %d, but expected only 2" , len (streams .Items ))
530
-
531
- // sync the stream which should remove the redundant stream
532
- err = cluster .syncStream (appId )
533
- assert .NoError (t , err )
534
-
535
- // check that only one stream remains after sync
536
- streams , err = cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
537
- assert .NoError (t , err )
538
526
assert .Equalf (t , 1 , len (streams .Items ), "unexpected number of streams found: got %d, but expected only 1" , len (streams .Items ))
539
-
540
- // check owner references
541
- if ! reflect .DeepEqual (streams .Items [0 ].OwnerReferences , cluster .ownerReferences ()) {
542
- t .Errorf ("unexpected owner references, expected %#v, got %#v" , cluster .ownerReferences (), streams .Items [0 ].OwnerReferences )
543
- }
544
527
}
545
528
546
529
func TestSameStreams (t * testing.T ) {
@@ -663,13 +646,14 @@ func TestUpdateStreams(t *testing.T) {
663
646
OpConfig : config.Config {
664
647
PodManagementPolicy : "ordered_ready" ,
665
648
Resources : config.Resources {
666
- ClusterLabels : map [string ]string {"application" : "spilo" },
667
- ClusterNameLabel : "cluster-name" ,
668
- DefaultCPURequest : "300m" ,
669
- DefaultCPULimit : "300m" ,
670
- DefaultMemoryRequest : "300Mi" ,
671
- DefaultMemoryLimit : "300Mi" ,
672
- PodRoleLabel : "spilo-role" ,
649
+ ClusterLabels : map [string ]string {"application" : "spilo" },
650
+ ClusterNameLabel : "cluster-name" ,
651
+ DefaultCPURequest : "300m" ,
652
+ DefaultCPULimit : "300m" ,
653
+ DefaultMemoryRequest : "300Mi" ,
654
+ DefaultMemoryLimit : "300Mi" ,
655
+ EnableOwnerReferences : util .True (),
656
+ PodRoleLabel : "spilo-role" ,
673
657
},
674
658
},
675
659
}, client , pg , logger , eventRecorder )
@@ -678,10 +662,31 @@ func TestUpdateStreams(t *testing.T) {
678
662
context .TODO (), & pg , metav1.CreateOptions {})
679
663
assert .NoError (t , err )
680
664
681
- // create the stream
665
+ // create stream with different owner reference
666
+ fes .ObjectMeta .Name = fmt .Sprintf ("%s-12345" , pg .Name )
667
+ fes .ObjectMeta .Labels ["cluster-name" ] = pg .Name
668
+ createdStream , err := cluster .KubeClient .FabricEventStreams (namespace ).Create (
669
+ context .TODO (), fes , metav1.CreateOptions {})
670
+ assert .NoError (t , err )
671
+ assert .Equal (t , createdStream .Spec .ApplicationId , appId )
672
+
673
+ // sync the stream which should update the owner reference
682
674
err = cluster .syncStream (appId )
683
675
assert .NoError (t , err )
684
676
677
+ // check that only one stream exists after sync
678
+ listOptions := metav1.ListOptions {
679
+ LabelSelector : cluster .labelsSet (true ).String (),
680
+ }
681
+ streams , err := cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
682
+ assert .NoError (t , err )
683
+ assert .Equalf (t , 1 , len (streams .Items ), "unexpected number of streams found: got %d, but expected only 1" , len (streams .Items ))
684
+
685
+ // compare owner references
686
+ if ! reflect .DeepEqual (streams .Items [0 ].OwnerReferences , cluster .ownerReferences ()) {
687
+ t .Errorf ("unexpected owner references, expected %#v, got %#v" , cluster .ownerReferences (), streams .Items [0 ].OwnerReferences )
688
+ }
689
+
685
690
// change specs of streams and patch CRD
686
691
for i , stream := range pg .Spec .Streams {
687
692
if stream .ApplicationId == appId {
@@ -694,10 +699,7 @@ func TestUpdateStreams(t *testing.T) {
694
699
}
695
700
696
701
// compare stream returned from API with expected stream
697
- listOptions := metav1.ListOptions {
698
- LabelSelector : cluster .labelsSet (true ).String (),
699
- }
700
- streams := patchPostgresqlStreams (t , cluster , & pg .Spec , listOptions )
702
+ streams = patchPostgresqlStreams (t , cluster , & pg .Spec , listOptions )
701
703
result := cluster .generateFabricEventStream (appId )
702
704
if match , _ := cluster .compareStreams (& streams .Items [0 ], result ); ! match {
703
705
t .Errorf ("Malformed FabricEventStream after updating manifest, expected %#v, got %#v" , streams .Items [0 ], result )
@@ -716,9 +718,51 @@ func TestUpdateStreams(t *testing.T) {
716
718
if match , _ := cluster .compareStreams (& streams .Items [0 ], result ); ! match {
717
719
t .Errorf ("Malformed FabricEventStream after disabling event recovery, expected %#v, got %#v" , streams .Items [0 ], result )
718
720
}
721
+ }
719
722
720
- mockClient := k8sutil .NewMockKubernetesClient ()
721
- cluster .KubeClient .CustomResourceDefinitionsGetter = mockClient .CustomResourceDefinitionsGetter
723
+ func patchPostgresqlStreams (t * testing.T , cluster * Cluster , pgSpec * acidv1.PostgresSpec , listOptions metav1.ListOptions ) (streams * zalandov1.FabricEventStreamList ) {
724
+ patchData , err := specPatch (pgSpec )
725
+ assert .NoError (t , err )
726
+
727
+ pgPatched , err := cluster .KubeClient .Postgresqls (namespace ).Patch (
728
+ context .TODO (), cluster .Name , types .MergePatchType , patchData , metav1.PatchOptions {}, "spec" )
729
+ assert .NoError (t , err )
730
+
731
+ cluster .Postgresql .Spec = pgPatched .Spec
732
+ err = cluster .syncStream (appId )
733
+ assert .NoError (t , err )
734
+
735
+ streams , err = cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
736
+ assert .NoError (t , err )
737
+
738
+ return streams
739
+ }
740
+
741
+ func TestDeleteStreams (t * testing.T ) {
742
+ pg .Name = fmt .Sprintf ("%s-4" , pg .Name )
743
+ var cluster = New (
744
+ Config {
745
+ OpConfig : config.Config {
746
+ PodManagementPolicy : "ordered_ready" ,
747
+ Resources : config.Resources {
748
+ ClusterLabels : map [string ]string {"application" : "spilo" },
749
+ ClusterNameLabel : "cluster-name" ,
750
+ DefaultCPURequest : "300m" ,
751
+ DefaultCPULimit : "300m" ,
752
+ DefaultMemoryRequest : "300Mi" ,
753
+ DefaultMemoryLimit : "300Mi" ,
754
+ PodRoleLabel : "spilo-role" ,
755
+ },
756
+ },
757
+ }, client , pg , logger , eventRecorder )
758
+
759
+ _ , err := cluster .KubeClient .Postgresqls (namespace ).Create (
760
+ context .TODO (), & pg , metav1.CreateOptions {})
761
+ assert .NoError (t , err )
762
+
763
+ // create the stream
764
+ err = cluster .syncStream (appId )
765
+ assert .NoError (t , err )
722
766
723
767
// remove streams from manifest
724
768
pg .Spec .Streams = nil
@@ -729,26 +773,32 @@ func TestUpdateStreams(t *testing.T) {
729
773
appIds := getDistinctApplicationIds (pgUpdated .Spec .Streams )
730
774
cluster .cleanupRemovedStreams (appIds )
731
775
732
- streams , err = cluster . KubeClient . FabricEventStreams ( namespace ). List ( context . TODO (), listOptions )
733
- if len ( streams . Items ) > 0 || err != nil {
734
- t . Errorf ( "stream resource has not been removed or unexpected error %v" , err )
776
+ // check that streams have been deleted
777
+ listOptions := metav1. ListOptions {
778
+ LabelSelector : cluster . labelsSet ( true ). String (),
735
779
}
736
- }
737
-
738
- func patchPostgresqlStreams (t * testing.T , cluster * Cluster , pgSpec * acidv1.PostgresSpec , listOptions metav1.ListOptions ) (streams * zalandov1.FabricEventStreamList ) {
739
- patchData , err := specPatch (pgSpec )
780
+ streams , err := cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
740
781
assert .NoError (t , err )
782
+ assert .Equalf (t , 0 , len (streams .Items ), "unexpected number of streams found: got %d, but expected none" , len (streams .Items ))
741
783
742
- pgPatched , err := cluster .KubeClient .Postgresqls (namespace ).Patch (
743
- context .TODO (), cluster .Name , types .MergePatchType , patchData , metav1.PatchOptions {}, "spec" )
784
+ // create stream to test deleteStreams code
785
+ fes .ObjectMeta .Name = fmt .Sprintf ("%s-12345" , pg .Name )
786
+ fes .ObjectMeta .Labels ["cluster-name" ] = pg .Name
787
+ _ , err = cluster .KubeClient .FabricEventStreams (namespace ).Create (
788
+ context .TODO (), fes , metav1.CreateOptions {})
744
789
assert .NoError (t , err )
745
790
746
- cluster . Postgresql . Spec = pgPatched . Spec
791
+ // sync it once to cluster struct
747
792
err = cluster .syncStream (appId )
748
793
assert .NoError (t , err )
749
794
795
+ // we need a mock client because deleteStreams checks for CRD existance
796
+ mockClient := k8sutil .NewMockKubernetesClient ()
797
+ cluster .KubeClient .CustomResourceDefinitionsGetter = mockClient .CustomResourceDefinitionsGetter
798
+ cluster .deleteStreams ()
799
+
800
+ // check that streams have been deleted
750
801
streams , err = cluster .KubeClient .FabricEventStreams (namespace ).List (context .TODO (), listOptions )
751
802
assert .NoError (t , err )
752
-
753
- return streams
803
+ assert .Equalf (t , 0 , len (streams .Items ), "unexpected number of streams found: got %d, but expected none" , len (streams .Items ))
754
804
}
0 commit comments