Skip to content

Commit bc9fdb8

Browse files
cw-Guomarcofranssen
authored andcommitted
refactor fix lint issue
Signed-off-by: chengweiguo <[email protected]>
1 parent c06dece commit bc9fdb8

File tree

1 file changed

+81
-93
lines changed

1 file changed

+81
-93
lines changed

tests/e2e/fluentd/label_selector_test.go

Lines changed: 81 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package fluentd
33
import (
44
"context"
55
"fmt"
6+
"sync"
67
"time"
78

9+
fluentdv1alpha1 "github.com/fluent/fluent-operator/v3/apis/fluentd/v1alpha1"
10+
cfgrender "github.com/fluent/fluent-operator/v3/apis/fluentd/v1alpha1/tests"
11+
"github.com/fluent/fluent-operator/v3/tests/utils"
812
. "github.com/onsi/ginkgo/v2"
913
. "github.com/onsi/gomega"
1014
"k8s.io/apimachinery/pkg/types"
1115
"sigs.k8s.io/controller-runtime/pkg/client"
12-
"sigs.k8s.io/yaml"
13-
14-
fluentdv1alpha1 "github.com/fluent/fluent-operator/v3/apis/fluentd/v1alpha1"
15-
"github.com/fluent/fluent-operator/v3/tests/utils"
1616
)
1717

1818
var (
19+
once sync.Once
20+
1921
// Fluentd instance for label selector tests
22+
Fluentd fluentdv1alpha1.Fluentd
2023
FluentdLabelSelectorRaw = `
2124
apiVersion: fluentd.fluent.io/v1alpha1
2225
kind: Fluentd
@@ -38,6 +41,7 @@ spec:
3841
`
3942

4043
// FluentdConfig with filterSelector and outputSelector
44+
FluentdConfig fluentdv1alpha1.FluentdConfig
4145
FluentdConfigLabelSelectorRaw = `
4246
apiVersion: fluentd.fluent.io/v1alpha1
4347
kind: FluentdConfig
@@ -58,6 +62,7 @@ spec:
5862
`
5963

6064
// Filter with matching labels
65+
FluentdFilter fluentdv1alpha1.Filter
6166
FilterLabelSelectorRaw = `
6267
apiVersion: fluentd.fluent.io/v1alpha1
6368
kind: Filter
@@ -78,6 +83,7 @@ spec:
7883
`
7984

8085
// Output with matching labels
86+
FluentdOutput fluentdv1alpha1.Output
8187
OutputLabelSelectorRaw = `
8288
apiVersion: fluentd.fluent.io/v1alpha1
8389
kind: Output
@@ -93,6 +99,7 @@ spec:
9399
`
94100

95101
// FluentdConfig with only filterSelector
102+
FluentdConfigFilterOnly fluentdv1alpha1.FluentdConfig
96103
FluentdConfigFilterOnlyRaw = `
97104
apiVersion: fluentd.fluent.io/v1alpha1
98105
kind: FluentdConfig
@@ -110,8 +117,9 @@ spec:
110117
output.fluentd.fluent.io/enabled: "true"
111118
`
112119

113-
// Grep Filter
114-
FilterGrepRaw = `
120+
// Grep Filter with matching labels
121+
FluentdFilterGrep fluentdv1alpha1.Filter
122+
FilterGrepRaw = `
115123
apiVersion: fluentd.fluent.io/v1alpha1
116124
kind: Filter
117125
metadata:
@@ -128,6 +136,7 @@ spec:
128136
`
129137

130138
// ClusterOutput for filter-only test
139+
FluentdClusterOutput fluentdv1alpha1.ClusterOutput
131140
ClusterOutputStdoutRaw = `
132141
apiVersion: fluentd.fluent.io/v1alpha1
133142
kind: ClusterOutput
@@ -141,6 +150,50 @@ spec:
141150
`
142151
)
143152

153+
func init() {
154+
once.Do(setupFluentdObjects)
155+
}
156+
157+
func setupFluentdObjects() {
158+
cfgrender.MustParseIntoObject(FluentdLabelSelectorRaw, &Fluentd)
159+
cfgrender.MustParseIntoObject(FluentdConfigLabelSelectorRaw, &FluentdConfig)
160+
cfgrender.MustParseIntoObject(FluentdConfigFilterOnlyRaw, &FluentdConfigFilterOnly)
161+
cfgrender.MustParseIntoObject(FilterLabelSelectorRaw, &FluentdFilter)
162+
cfgrender.MustParseIntoObject(OutputLabelSelectorRaw, &FluentdOutput)
163+
cfgrender.MustParseIntoObject(FilterGrepRaw, &FluentdFilterGrep)
164+
cfgrender.MustParseIntoObject(ClusterOutputStdoutRaw, &FluentdClusterOutput)
165+
}
166+
167+
// Helper function to run a fluentd label selector test
168+
func testFluentdLabelSelector(
169+
ctx context.Context,
170+
expectedConfig []byte,
171+
fluentd fluentdv1alpha1.Fluentd,
172+
objects []client.Object,
173+
) {
174+
// Create all objects
175+
err := CreateObjs(ctx, objects)
176+
Expect(err).NotTo(HaveOccurred())
177+
178+
// Wait for reconciliation
179+
time.Sleep(time.Second * 3)
180+
181+
// Get the generated configuration
182+
seckey := types.NamespacedName{
183+
Namespace: fluentd.Namespace,
184+
Name: fmt.Sprintf("%s-config", fluentd.Name),
185+
}
186+
config, err := GetCfgFromSecret(ctx, seckey)
187+
Expect(err).NotTo(HaveOccurred())
188+
189+
// Verify that the configuration matches expected
190+
Expect(string(expectedConfig)).To(Equal(config))
191+
192+
// Clean up
193+
err = DeleteObjs(ctx, objects)
194+
Expect(err).NotTo(HaveOccurred())
195+
}
196+
144197
// This test verifies the fix for the bug where filterSelector and outputSelector
145198
// were incorrectly writing to the inputs list instead of their respective lists.
146199
var _ = Describe("Test FluentdConfig with namespace-level filter and output selectors", func() {
@@ -157,96 +210,31 @@ var _ = Describe("Test FluentdConfig with namespace-level filter and output sele
157210

158211
Describe("Test namespace-level resources with label selectors", func() {
159212
It("E2E_FLUENTD_NAMESPACE_FILTER_OUTPUT_SELECTORS: FluentdConfig with filterSelector and outputSelector", func() {
160-
161-
// Parse YAML into objects
162-
var fluentd fluentdv1alpha1.Fluentd
163-
err := yaml.Unmarshal([]byte(FluentdLabelSelectorRaw), &fluentd)
164-
Expect(err).NotTo(HaveOccurred())
165-
166-
var fluentdConfig fluentdv1alpha1.FluentdConfig
167-
err = yaml.Unmarshal([]byte(FluentdConfigLabelSelectorRaw), &fluentdConfig)
168-
Expect(err).NotTo(HaveOccurred())
169-
170-
var testFilter fluentdv1alpha1.Filter
171-
err = yaml.Unmarshal([]byte(FilterLabelSelectorRaw), &testFilter)
172-
Expect(err).NotTo(HaveOccurred())
173-
174-
var testOutput fluentdv1alpha1.Output
175-
err = yaml.Unmarshal([]byte(OutputLabelSelectorRaw), &testOutput)
176-
Expect(err).NotTo(HaveOccurred())
177-
178-
// Create all objects
179-
objects := []client.Object{
180-
&fluentd,
181-
&fluentdConfig,
182-
&testFilter,
183-
&testOutput,
184-
}
185-
186-
err = CreateObjs(ctx, objects)
187-
Expect(err).NotTo(HaveOccurred())
188-
189-
// Wait for reconciliation
190-
time.Sleep(time.Second * 3)
191-
192-
// Get the generated configuration
193-
seckey := types.NamespacedName{
194-
Namespace: fluentd.Namespace,
195-
Name: fmt.Sprintf("%s-config", fluentd.Name),
196-
}
197-
config, err := GetCfgFromSecret(ctx, seckey)
198-
Expect(err).NotTo(HaveOccurred())
199-
200-
// Verify that the filter configuration is present
201-
// Before the fix, the filter would not be loaded because it was written to the inputs list
202-
Expect(string(utils.ExpectedFluentdNamespacedCfgFilterOutputSelector)).To(Equal(config))
203-
204-
// Clean up
205-
err = DeleteObjs(ctx, objects)
206-
Expect(err).NotTo(HaveOccurred())
213+
testFluentdLabelSelector(
214+
ctx,
215+
utils.ExpectedFluentdNamespacedCfgFilterOutputSelector,
216+
Fluentd,
217+
[]client.Object{
218+
&Fluentd,
219+
&FluentdConfig,
220+
&FluentdFilter,
221+
&FluentdOutput,
222+
},
223+
)
207224
})
208225

209226
It("E2E_FLUENTD_NAMESPACE_MIXED_SELECTORS: FluentdConfig with only filterSelector", func() {
210-
211-
// Parse YAML into objects
212-
var fluentd fluentdv1alpha1.Fluentd
213-
err := yaml.Unmarshal([]byte(FluentdLabelSelectorRaw), &fluentd)
214-
Expect(err).NotTo(HaveOccurred())
215-
216-
var fluentdConfig fluentdv1alpha1.FluentdConfig
217-
err = yaml.Unmarshal([]byte(FluentdConfigFilterOnlyRaw), &fluentdConfig)
218-
Expect(err).NotTo(HaveOccurred())
219-
220-
var grepFilter fluentdv1alpha1.Filter
221-
err = yaml.Unmarshal([]byte(FilterGrepRaw), &grepFilter)
222-
Expect(err).NotTo(HaveOccurred())
223-
224-
var clusterOutput fluentdv1alpha1.ClusterOutput
225-
err = yaml.Unmarshal([]byte(ClusterOutputStdoutRaw), &clusterOutput)
226-
Expect(err).NotTo(HaveOccurred())
227-
228-
objects := []client.Object{
229-
&fluentd,
230-
&fluentdConfig,
231-
&grepFilter,
232-
&clusterOutput,
233-
}
234-
235-
err = CreateObjs(ctx, objects)
236-
Expect(err).NotTo(HaveOccurred())
237-
238-
time.Sleep(time.Second * 3)
239-
240-
seckey := types.NamespacedName{
241-
Namespace: fluentd.Namespace,
242-
Name: fmt.Sprintf("%s-config", fluentd.Name),
243-
}
244-
config, err := GetCfgFromSecret(ctx, seckey)
245-
Expect(string(utils.ExpectedFluentdNamespacedCfgFilterSelector)).To(Equal(config))
246-
247-
// Clean up
248-
err = DeleteObjs(ctx, objects)
249-
Expect(err).NotTo(HaveOccurred())
227+
testFluentdLabelSelector(
228+
ctx,
229+
utils.ExpectedFluentdNamespacedCfgFilterSelector,
230+
Fluentd,
231+
[]client.Object{
232+
&Fluentd,
233+
&FluentdConfigFilterOnly,
234+
&FluentdFilterGrep,
235+
&FluentdClusterOutput,
236+
},
237+
)
250238
})
251239
})
252240
})

0 commit comments

Comments
 (0)