Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: juicer <[email protected]>
  • Loading branch information
cw-Guo committed Oct 22, 2023
1 parent cf3b539 commit 16b883d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<source>
@type forward
bind 0.0.0.0
port 24224
</source>
<match **>
@id main
@type label_router
<route>
@label @a2170d34e9940ec56d328100e375c43e
<match>
namespaces default,kube-system
</match>
</route>
</match>
<label @a2170d34e9940ec56d328100e375c43e>
<filter **>
@id ClusterFluentdConfig-cluster-fluentd-config::cluster::clusterfilter::fluentd-filter-0
@type record_transformer
auto_typecast true
enable_ruby true
renew_record true
<record>
kubernetes_ns ${record["kubernetes"]["namespace_name"]}
</record>
</filter>
<match **>
@id ClusterFluentdConfig-cluster-fluentd-config::cluster::clusteroutput::fluentd-output-cluster-0
@type elasticsearch
host elasticsearch-logging-data.kubesphere-logging-system.svc
logstash_format true
logstash_prefix ks-logstash-log
port 9200
</match>
</label>
27 changes: 27 additions & 0 deletions apis/fluentd/v1alpha1/tests/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,30 @@ func Test_DuplicateRemovalCRSpecs(t *testing.T) {
i++
}
}

func Test_RecordTransformer(t *testing.T) {
g := NewGomegaWithT(t)
sl := plugins.NewSecretLoader(nil, Fluentd.Namespace, logr.Logger{})

psr := fluentdv1alpha1.NewGlobalPluginResources("main")
psr.CombineGlobalInputsPlugins(sl, Fluentd.Spec.GlobalInputs)

clustercfgRouter, err := psr.BuildCfgRouter(&FluentdClusterFluentdConfig1)
g.Expect(err).NotTo(HaveOccurred())
clusterFilters := []fluentdv1alpha1.ClusterFilter{FluentdClusterRecordTransformerFilter}
clusterOutputs := []fluentdv1alpha1.ClusterOutput{FluentdClusterOutputCluster}
clustercfgResources, _ := psr.PatchAndFilterClusterLevelResources(sl, FluentdClusterFluentdConfig1.GetCfgId(), clusterFilters, clusterOutputs)
err = psr.WithCfgResources(*clustercfgRouter.Label, clustercfgResources)
g.Expect(err).NotTo(HaveOccurred())

// we should not see any permutations in serialized config
i := 0
for i < maxRuntimes {
config, errs := psr.RenderMainConfig(false)
// fmt.Println(config)
g.Expect(errs).NotTo(HaveOccurred())
g.Expect(string(getExpectedCfg("./expected/fluentd-cluster-cfg-filter-recordTransformer.cfg"))).To(Equal(config))

i++
}
}
20 changes: 20 additions & 0 deletions apis/fluentd/v1alpha1/tests/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ spec:
value: ${record["kubernetes"]["namespace_name"]}
`

FluentdClusterRecordTransformerFilter fluentdv1alpha1.ClusterFilter
FluentdClusterRecordTransformerRaw = `
apiVersion: fluentd.fluent.io/v1alpha1
kind: ClusterFilter
metadata:
name: fluentd-filter
labels:
filter.fluentd.fluent.io/enabled: "true"
spec:
filters:
- recordTransformer:
enableRuby: true
autoTypeCast: true
renewRecord: true
records:
- key: kubernetes_ns
value: ${record["kubernetes"]["namespace_name"]}
`

FluentdClusterOutputBuffer fluentdv1alpha1.ClusterOutput
FluentdClusterOutputBufferRaw = `
apiVersion: fluentd.fluent.io/v1alpha1
Expand Down Expand Up @@ -562,6 +581,7 @@ func init() {
ParseIntoObject(FluentdConfigUser1Raw, &FluentdConfigUser1)
ParseIntoObject(FluentdConfig1Raw, &FluentdConfig1)
ParseIntoObject(FluentdClusterFilter1Raw, &FluentdClusterFilter1)
ParseIntoObject(FluentdClusterRecordTransformerRaw, &FluentdClusterRecordTransformerFilter)
ParseIntoObject(FluentdClusterOutputClusterRaw, &FluentdClusterOutputCluster)
ParseIntoObject(FluentdClusterOutputLogOperatorRaw, &FluentdClusterOutputLogOperator)
ParseIntoObject(FluentdClusterOutputBufferRaw, &FluentdClusterOutputBuffer)
Expand Down

0 comments on commit 16b883d

Please sign in to comment.