@@ -18,11 +18,143 @@ import (
1818 "fmt"
1919 "testing"
2020
21+ "github.com/MakeNowJust/heredoc"
22+ "github.com/andreyvit/diff"
23+ "github.com/banzaicloud/logging-operator/pkg/resources/fluentd"
2124 "github.com/banzaicloud/logging-operator/pkg/sdk/api/v1beta1"
2225 "github.com/banzaicloud/operator-tools/pkg/utils"
26+ "github.com/onsi/gomega"
27+ corev1 "k8s.io/api/core/v1"
2328 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2429)
2530
31+ func TestFlowMatch (t * testing.T ) {
32+ g := gomega .NewGomegaWithT (t )
33+ defer beforeEach (t )()
34+
35+ logging := testLogging ()
36+ output := testOutput ()
37+
38+ flow := & v1beta1.Flow {
39+ ObjectMeta : v1.ObjectMeta {
40+ Name : "test-flow" ,
41+ Namespace : output .Namespace ,
42+ },
43+ Spec : v1beta1.FlowSpec {
44+ Match : []v1beta1.Match {
45+ {
46+ Select : & v1beta1.Select {
47+ Labels : map [string ]string {
48+ "c" : "d" ,
49+ },
50+ },
51+ },
52+ },
53+ OutputRefs : []string {output .Name },
54+ },
55+ }
56+
57+ defer ensureCreated (t , logging )()
58+ defer ensureCreated (t , output )()
59+ defer ensureCreated (t , flow )()
60+
61+ secret := & corev1.Secret {}
62+ defer ensureCreatedEventually (t , controlNamespace , logging .QualifiedName (fluentd .AppSecretConfigName ), secret )()
63+
64+ g .Expect (diff .TrimLinesInString (string (secret .Data [fluentd .AppConfigKey ]))).Should (gomega .ContainSubstring (diff .TrimLinesInString (heredoc .Docf (`
65+ <match>
66+ labels c:d
67+ namespaces %s
68+ negate false
69+ </match>
70+ ` , flow .Namespace ))))
71+ }
72+
73+ func TestClusterFlowMatch (t * testing.T ) {
74+ g := gomega .NewGomegaWithT (t )
75+ defer beforeEach (t )()
76+
77+ logging := testLogging ()
78+ output := testClusterOutput ()
79+
80+ flow := & v1beta1.ClusterFlow {
81+ ObjectMeta : v1.ObjectMeta {
82+ Name : "test-flow" ,
83+ Namespace : logging .Spec .ControlNamespace ,
84+ },
85+ Spec : v1beta1.ClusterFlowSpec {
86+ Match : []v1beta1.ClusterMatch {
87+ {
88+ ClusterSelect : & v1beta1.ClusterSelect {
89+ Labels : map [string ]string {
90+ "c" : "d" ,
91+ },
92+ },
93+ },
94+ },
95+ OutputRefs : []string {output .Name },
96+ },
97+ }
98+
99+ defer ensureCreated (t , logging )()
100+ defer ensureCreated (t , output )()
101+ defer ensureCreated (t , flow )()
102+
103+ secret := & corev1.Secret {}
104+ defer ensureCreatedEventually (t , controlNamespace , logging .QualifiedName (fluentd .AppSecretConfigName ), secret )()
105+
106+ g .Expect (diff .TrimLinesInString (string (secret .Data [fluentd .AppConfigKey ]))).Should (gomega .ContainSubstring (diff .TrimLinesInString (heredoc .Docf (`
107+ <match>
108+ labels c:d
109+ namespaces
110+ negate false
111+ </match>
112+ ` ))))
113+ }
114+
115+ func TestClusterFlowMatchWithNamespaces (t * testing.T ) {
116+ g := gomega .NewGomegaWithT (t )
117+ defer beforeEach (t )()
118+
119+ logging := testLogging ()
120+ output := testClusterOutput ()
121+
122+ flow := & v1beta1.ClusterFlow {
123+ ObjectMeta : v1.ObjectMeta {
124+ Name : "test-flow" ,
125+ Namespace : logging .Spec .ControlNamespace ,
126+ },
127+ Spec : v1beta1.ClusterFlowSpec {
128+ Match : []v1beta1.ClusterMatch {
129+ {
130+ ClusterSelect : & v1beta1.ClusterSelect {
131+ Labels : map [string ]string {
132+ "c" : "d" ,
133+ },
134+ Namespaces : []string {"a" , "b" },
135+ },
136+ },
137+ },
138+ OutputRefs : []string {output .Name },
139+ },
140+ }
141+
142+ defer ensureCreated (t , logging )()
143+ defer ensureCreated (t , output )()
144+ defer ensureCreated (t , flow )()
145+
146+ secret := & corev1.Secret {}
147+ defer ensureCreatedEventually (t , controlNamespace , logging .QualifiedName (fluentd .AppSecretConfigName ), secret )()
148+
149+ g .Expect (diff .TrimLinesInString (string (secret .Data [fluentd .AppConfigKey ]))).Should (gomega .ContainSubstring (diff .TrimLinesInString (heredoc .Docf (`
150+ <match>
151+ labels c:d
152+ namespaces a,b
153+ negate false
154+ </match>
155+ ` ))))
156+ }
157+
26158func TestInvalidFlowIfMatchAndSelectorBothSet (t * testing.T ) {
27159 defer beforeEach (t )()
28160
0 commit comments