Skip to content

Commit f8d46ea

Browse files
pepovtarokkk
authored andcommitted
add tests to cover match configs
1 parent 12ee67e commit f8d46ea

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

controllers/logging_controller_match_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
26158
func TestInvalidFlowIfMatchAndSelectorBothSet(t *testing.T) {
27159
defer beforeEach(t)()
28160

controllers/logging_controller_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ func ensureCreatedEventually(t *testing.T, ns, name string, object runtime.Objec
551551
}
552552
}
553553

554-
555554
func expectError(t *testing.T, expected string) {
556555
err := wait.Poll(time.Second, time.Second*3, func() (bool, error) {
557556
select {
@@ -583,6 +582,20 @@ func testOutput() *v1beta1.Output {
583582
}
584583
}
585584

585+
func testClusterOutput() *v1beta1.ClusterOutput {
586+
return &v1beta1.ClusterOutput{
587+
ObjectMeta: v1.ObjectMeta{
588+
Name: "test-output",
589+
Namespace: controlNamespace,
590+
},
591+
Spec: v1beta1.ClusterOutputSpec{
592+
OutputSpec: v1beta1.OutputSpec{
593+
NullOutputConfig: output.NewNullOutputConfig(),
594+
},
595+
},
596+
}
597+
}
598+
586599
func testLogging() *v1beta1.Logging {
587600
return &v1beta1.Logging{
588601
ObjectMeta: v1.ObjectMeta{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
emperror.dev/errors v0.7.0
77
github.com/MakeNowJust/heredoc v1.0.0
8+
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
89
github.com/banzaicloud/logging-operator/pkg/sdk v0.0.0
910
github.com/banzaicloud/operator-tools v0.8.0
1011
github.com/coreos/prometheus-operator v0.34.0

0 commit comments

Comments
 (0)