-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory_test.go
134 lines (115 loc) · 4.4 KB
/
factory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attributesprocessor
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor/internal/metadata"
)
func TestFactory_Type(t *testing.T) {
factory := NewFactory()
assert.Equal(t, factory.Type(), metadata.Type)
}
func TestFactory_CreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.Equal(t, cfg, &Config{})
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
}
func TestValidateConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.Error(t, component.ValidateConfig(cfg))
}
func TestFactoryCreateTracesProcessor_InvalidActions(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
oCfg := cfg.(*Config)
// Missing key
oCfg.Actions = []attraction.ActionKeyValue{
{Key: "", Value: 123, Action: attraction.UPSERT},
}
ap, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.Error(t, err)
assert.Nil(t, ap)
// Invalid target type
oCfg.Actions = []attraction.ActionKeyValue{
{Key: "http.status_code", ConvertedType: "array", Action: attraction.CONVERT},
}
ap2, err2 := factory.CreateTracesProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.Error(t, err2)
assert.Equal(t, "error creating AttrProc due to invalid value \"array\" in field \"converted_type\" for action \"convert\" at the 0-th action", err2.Error())
assert.Nil(t, ap2)
}
func TestFactoryCreateTracesProcessor(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
oCfg := cfg.(*Config)
oCfg.Actions = []attraction.ActionKeyValue{
{Key: "a key", Action: attraction.DELETE},
}
tp, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.NotNil(t, tp)
assert.NoError(t, err)
oCfg.Actions = []attraction.ActionKeyValue{
{Action: attraction.DELETE},
}
tp, err = factory.CreateTracesProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.Nil(t, tp)
assert.Error(t, err)
}
func TestFactory_CreateMetricsProcessor(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
cfg.(*Config).Actions = []attraction.ActionKeyValue{
{Key: "fake_key", Action: attraction.INSERT, Value: "100"},
}
mp, err := factory.CreateMetricsProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
require.NotNil(t, mp)
require.NoError(t, err)
cfg.(*Config).Actions = []attraction.ActionKeyValue{
{Key: "fake_key", Action: attraction.UPSERT},
}
// Upsert should fail on non-existent key
mp, err = factory.CreateMetricsProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
require.Nil(t, mp)
require.Error(t, err)
}
func TestFactoryCreateLogsProcessor_InvalidActions(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
oCfg := cfg.(*Config)
// Missing key
oCfg.Actions = []attraction.ActionKeyValue{
{Key: "", Value: 123, Action: attraction.UPSERT},
}
ap, err := factory.CreateLogsProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.Error(t, err)
assert.Nil(t, ap)
}
func TestFactoryCreateLogsProcessor(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
oCfg := cfg.(*Config)
oCfg.Actions = []attraction.ActionKeyValue{
{Key: "a key", Action: attraction.DELETE},
}
tp, err := factory.CreateLogsProcessor(
context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.NotNil(t, tp)
assert.NoError(t, err)
oCfg.Actions = []attraction.ActionKeyValue{
{Action: attraction.DELETE},
}
tp, err = factory.CreateLogsProcessor(
context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.Nil(t, tp)
assert.Error(t, err)
}