Debian Codegraph @

filter/config_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package filter
5
6 import (
7 "path/filepath"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/collector/confmap"
14 "go.opentelemetry.io/collector/confmap/confmaptest"
15 )
16
17 func readTestdataConfigYamls(t *testing.T, filename string) map[string][]Config {
18 testFile := filepath.Join("testdata", filename)
19 v, err := confmaptest.LoadConf(testFile)
20 require.NoError(t, err)
21
22 cfgs := map[string][]Config{}
23 require.NoErrorf(t, v.Unmarshal(&cfgs, confmap.WithIgnoreUnused()), "unable to unmarshal yaml from file %v", testFile)
24 return cfgs
25 }
26
27 func TestConfig(t *testing.T) {
28 actualConfigs := readTestdataConfigYamls(t, "config.yaml")
29 expectedConfigs := map[string][]Config{
30 "regexp/default": {
31 {
32 Regex: "one|two",
33 },
34 },
35 "strict/default": {
36 {
37 Strict: "strict",
38 },
39 },
40 }
41
42 for testName, actualCfg := range actualConfigs {
43 t.Run(testName, func(t *testing.T) {
44 expCfg, ok := expectedConfigs[testName]
45 assert.True(t, ok)
46 assert.Equal(t, expCfg, actualCfg)
47
48 for _, cfg := range actualCfg {
49 require.NoError(t, cfg.Validate())
50 }
51 fs := CreateFilter(actualCfg)
52 assert.NotNil(t, fs)
53 })
54 }
55 }
56
57 func TestMatches(t *testing.T) {
58 cfg := []Config{
59 {
60 Strict: "a",
61 },
62 {
63 Strict: "b",
64 },
65 {
66 Regex: "a|b|c",
67 },
68 }
69
70 for _, c := range cfg {
71 require.NoError(t, c.Validate())
72 }
73 fs := CreateFilter(cfg)
74
75 assert.True(t, fs.Matches("a"))
76 assert.True(t, fs.Matches("b"))
77 assert.True(t, fs.Matches("c"))
78 }
79
80 func TestConfigInvalid(t *testing.T) {
81 actualConfigs := readTestdataConfigYamls(t, "config_invalid.yaml")
82 expectedConfigs := map[string][]Config{
83 "invalid/regexp": {
84 {
85 Regex: "(.*[",
86 },
87 },
88 "invalid/config_empty": {
89 {
90 Regex: "",
91 Strict: "",
92 },
93 },
94 "invalid/config_both_set": {
95 {
96 Regex: "1",
97 Strict: "1",
98 },
99 },
100 }
101
102 for testName, actualCfg := range actualConfigs {
103 t.Run(testName, func(t *testing.T) {
104 expCfg, ok := expectedConfigs[testName]
105 assert.True(t, ok)
106 assert.Equal(t, expCfg, actualCfg)
107
108 for _, cfg := range actualCfg {
109 assert.Error(t, cfg.Validate())
110 }
111 })
112 }
113 }