@@ -27,7 +27,6 @@ import (
27
27
"net/http/httptest"
28
28
"testing"
29
29
30
- "github.com/go-logr/logr"
31
30
"github.com/google/go-github/v64/github"
32
31
"github.com/onsi/gomega"
33
32
corev1 "k8s.io/api/core/v1"
@@ -762,7 +761,7 @@ func Test_handlePayload(t *testing.T) {
762
761
expectedResponseCode : http .StatusOK ,
763
762
},
764
763
{
765
- name : "resources determined by CEL expressions" ,
764
+ name : "resources filtered with CEL expressions" ,
766
765
headers : map [string ]string {
767
766
"Content-Type" : "application/json; charset=utf-8" ,
768
767
},
@@ -775,11 +774,17 @@ func Test_handlePayload(t *testing.T) {
775
774
SecretRef : meta.LocalObjectReference {
776
775
Name : "token" ,
777
776
},
778
- ResourceExpressions : []string {
779
- `{"name": "test-resource-1", "kind": "Receiver", "apiVersion": "notification.toolkit.fluxcd.io/v1"}` ,
780
- `[{"name": body.image.split(':',2)[0] + '-2', "namespace": "tested", "kind": "Receiver", "apiVersion": "notification.toolkit.fluxcd.io/v1"}]` ,
781
- `body.resources.map(r, {"name": r, "kind": "Receiver", "apiVersion": "notification.toolkit.fluxcd.io/v1"})` ,
777
+ Resources : []apiv1.CrossNamespaceObjectReference {
778
+ {
779
+ APIVersion : apiv1 .GroupVersion .String (),
780
+ Kind : apiv1 .ReceiverKind ,
781
+ Name : "*" ,
782
+ MatchLabels : map [string ]string {
783
+ "label" : "production" ,
784
+ },
785
+ },
782
786
},
787
+ ResourceFilter : `resource.metadata.name in ["test-resource-1", "test-resource-2"]` ,
783
788
},
784
789
Status : apiv1.ReceiverStatus {
785
790
WebhookPath : apiv1 .ReceiverWebhookPath ,
@@ -794,13 +799,6 @@ func Test_handlePayload(t *testing.T) {
794
799
"token" : []byte ("token" ),
795
800
},
796
801
},
797
- payload : map [string ]interface {}{
798
- "image" : "test-resource:1.2.1" ,
799
- "resources" : []string {
800
- "test-resource-3" ,
801
- "test-resource-4" ,
802
- },
803
- },
804
802
resources : []client.Object {
805
803
& apiv1.Receiver {
806
804
TypeMeta : metav1.TypeMeta {
@@ -809,6 +807,9 @@ func Test_handlePayload(t *testing.T) {
809
807
},
810
808
ObjectMeta : metav1.ObjectMeta {
811
809
Name : "test-resource-1" ,
810
+ Labels : map [string ]string {
811
+ "label" : "production" ,
812
+ },
812
813
},
813
814
},
814
815
& apiv1.Receiver {
@@ -819,6 +820,9 @@ func Test_handlePayload(t *testing.T) {
819
820
ObjectMeta : metav1.ObjectMeta {
820
821
Name : "test-resource-2" ,
821
822
Namespace : "tested" ,
823
+ Labels : map [string ]string {
824
+ "label" : "production" ,
825
+ },
822
826
},
823
827
},
824
828
& apiv1.Receiver {
@@ -828,6 +832,9 @@ func Test_handlePayload(t *testing.T) {
828
832
},
829
833
ObjectMeta : metav1.ObjectMeta {
830
834
Name : "test-resource-3" ,
835
+ Labels : map [string ]string {
836
+ "label" : "production" ,
837
+ },
831
838
},
832
839
},
833
840
& apiv1.Receiver {
@@ -837,10 +844,13 @@ func Test_handlePayload(t *testing.T) {
837
844
},
838
845
ObjectMeta : metav1.ObjectMeta {
839
846
Name : "test-resource-4" ,
847
+ Labels : map [string ]string {
848
+ "label" : "production" ,
849
+ },
840
850
},
841
851
},
842
852
},
843
- expectedResourcesAnnotated : 4 , // TODO: This should really check more than just the count.
853
+ expectedResourcesAnnotated : 2 , // TODO: This should really check more than just the count.
844
854
expectedResponseCode : http .StatusOK ,
845
855
},
846
856
{
@@ -857,9 +867,17 @@ func Test_handlePayload(t *testing.T) {
857
867
SecretRef : meta.LocalObjectReference {
858
868
Name : "token" ,
859
869
},
860
- ResourceExpressions : []string {
861
- `{"name": ["test-resource-1"], "kind": "Receiver", "apiVersion": "notification.toolkit.fluxcd.io/v1"}` ,
870
+ Resources : []apiv1.CrossNamespaceObjectReference {
871
+ {
872
+ APIVersion : apiv1 .GroupVersion .String (),
873
+ Kind : apiv1 .ReceiverKind ,
874
+ Name : "*" ,
875
+ MatchLabels : map [string ]string {
876
+ "label" : "production" ,
877
+ },
878
+ },
862
879
},
880
+ ResourceFilter : `resource.name == "test-resource-1"` ,
863
881
},
864
882
Status : apiv1.ReceiverStatus {
865
883
WebhookPath : apiv1 .ReceiverWebhookPath ,
@@ -874,13 +892,6 @@ func Test_handlePayload(t *testing.T) {
874
892
"token" : []byte ("token" ),
875
893
},
876
894
},
877
- payload : map [string ]interface {}{
878
- "image" : "test-resource:1.2.1" ,
879
- "resources" : []string {
880
- "test-resource-3" ,
881
- "test-resource-4" ,
882
- },
883
- },
884
895
resources : []client.Object {
885
896
& apiv1.Receiver {
886
897
TypeMeta : metav1.TypeMeta {
@@ -889,11 +900,14 @@ func Test_handlePayload(t *testing.T) {
889
900
},
890
901
ObjectMeta : metav1.ObjectMeta {
891
902
Name : "test-resource-1" ,
903
+ Labels : map [string ]string {
904
+ "label" : "production" ,
905
+ },
892
906
},
893
907
},
894
908
},
895
909
expectedResourcesAnnotated : 0 , // TODO: This should really check more than just the count.
896
- expectedResponseCode : http .StatusBadRequest ,
910
+ expectedResponseCode : http .StatusInternalServerError ,
897
911
},
898
912
}
899
913
@@ -920,11 +934,11 @@ func Test_handlePayload(t *testing.T) {
920
934
}
921
935
922
936
client := builder .Build ()
923
- s := newReceiverHandler (
924
- logger . NewLogger (logger. Options {}) ,
925
- client ,
926
- false ,
927
- )
937
+ s := ReceiverServer {
938
+ port : "" ,
939
+ logger : logger . NewLogger (logger. Options {}) ,
940
+ kubeClient : client ,
941
+ }
928
942
929
943
data , err := json .Marshal (tt .payload )
930
944
if err != nil {
@@ -962,71 +976,6 @@ func Test_handlePayload(t *testing.T) {
962
976
}
963
977
}
964
978
965
- func TestReceiverServer (t * testing.T ) {
966
- receiver := & apiv1.Receiver {
967
- ObjectMeta : metav1.ObjectMeta {
968
- Name : "test-receiver" ,
969
- Namespace : "default" ,
970
- },
971
- Spec : apiv1.ReceiverSpec {
972
- Type : apiv1 .GenericReceiver ,
973
- SecretRef : meta.LocalObjectReference {
974
- Name : "token" ,
975
- },
976
- ResourceExpressions : []string {
977
- `{"name": "test-receiver", "kind": "Receiver", "apiVersion": "notification.toolkit.fluxcd.io/v1"}` ,
978
- },
979
- },
980
- Status : apiv1.ReceiverStatus {
981
- WebhookPath : apiv1 .ReceiverWebhookPath ,
982
- Conditions : []metav1.Condition {
983
- {
984
- Type : meta .ReadyCondition ,
985
- Status : metav1 .ConditionTrue ,
986
- },
987
- },
988
- },
989
- }
990
- secret := & corev1.Secret {
991
- ObjectMeta : metav1.ObjectMeta {
992
- Name : "token" ,
993
- Namespace : "default" ,
994
- },
995
- Data : map [string ][]byte {
996
- "token" : []byte ("token" ),
997
- },
998
- }
999
-
1000
- k8sClient := buildTestClient (receiver , secret )
1001
-
1002
- rs := newReceiverHandler (logr .Discard (), k8sClient , false )
1003
- srv := httptest .NewServer (rs )
1004
- defer srv .Close ()
1005
-
1006
- payload := map [string ]any {
1007
- "image" : "test-resource:1.2.1" ,
1008
- }
1009
-
1010
- body , err := json .Marshal (payload )
1011
- if err != nil {
1012
- t .Fatal (err )
1013
- }
1014
- req , err := http .NewRequest (http .MethodPost , srv .URL + apiv1 .ReceiverWebhookPath , bytes .NewBuffer (body ))
1015
- if err != nil {
1016
- t .Fatal (err )
1017
- }
1018
- req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
1019
-
1020
- resp , err := srv .Client ().Do (req )
1021
- if err != nil {
1022
- t .Fatal (err )
1023
- }
1024
-
1025
- if resp .StatusCode != http .StatusOK {
1026
- t .Errorf ("got StatusCode %v, want %v" , resp .StatusCode , http .StatusOK )
1027
- }
1028
- }
1029
-
1030
979
func buildTestClient (objs ... client.Object ) client.Client {
1031
980
scheme := runtime .NewScheme ()
1032
981
apiv1 .AddToScheme (scheme )
@@ -1035,6 +984,5 @@ func buildTestClient(objs ...client.Object) client.Client {
1035
984
return fake .NewClientBuilder ().
1036
985
WithScheme (scheme ).
1037
986
WithObjects (objs ... ).
1038
- WithStatusSubresource (& apiv1.Receiver {}).
1039
987
WithIndex (& apiv1.Receiver {}, WebhookPathIndexKey , IndexReceiverWebhookPath ).Build ()
1040
988
}
0 commit comments