forked from signalfx/splunk-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
132 lines (109 loc) · 3.6 KB
/
config_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
// Copyright Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows
package scriptedinputsreceiver
import (
"path"
"testing"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/split"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap/confmaptest"
)
func tmpScript(t *testing.T) {
scripts["aasd"] = "tmp"
t.Cleanup(func() {
delete(scripts, "aasd")
})
}
func TestValidConfig(t *testing.T) {
configs, err := confmaptest.LoadConf(path.Join(".", "testdata", "config.yaml"))
require.NoError(t, err)
require.NotNil(t, configs)
assert.Equal(t, 1, len(configs.ToStringMap()))
cm, err := configs.Sub("scripted_inputs/cpu")
require.NoError(t, err)
cfg := createDefaultConfig()
err = cm.Unmarshal(&cfg)
require.NoError(t, err)
require.Equal(t, &Config{
InputConfig: helper.InputConfig{
AttributerConfig: helper.AttributerConfig{
Attributes: map[string]helper.ExprStringConfig{},
},
IdentifierConfig: helper.IdentifierConfig{
Resource: map[string]helper.ExprStringConfig{},
},
WriterConfig: helper.WriterConfig{
BasicConfig: helper.BasicConfig{
OperatorID: "scripted_inputs",
OperatorType: "scripted_inputs",
}, OutputIDs: []string(nil)},
},
Multiline: split.Config{
LineStartPattern: "",
LineEndPattern: "",
},
ScriptName: "cpu",
Encoding: "utf-8",
Source: "",
SourceType: "",
CollectionInterval: "60s",
MaxLogSize: 1048576,
AddAttributes: false,
interval: 0,
}, cfg)
require.NoError(t, cfg.Validate())
}
func TestCreateConfig(t *testing.T) {
tmpScript(t)
config := Config{}
config.ScriptName = "aasd"
config.OperatorType = "test-operator"
built, err := config.Build(componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
assert.NotNil(t, config, "failed to create default config")
assert.NotNil(t, built, "failed to create default config")
}
func TestCreateWithSmallLogSize(t *testing.T) {
config := Config{}
config.ScriptName = "df"
config.OperatorType = "test-operator"
config.MaxLogSize = 2
err := config.Validate()
assert.Equal(t, err.Error(), "invalid value for parameter 'max_log_size', must be equal to or greater than 65536 bytes")
}
func TestCreateWithMissingExecFile(t *testing.T) {
config := Config{}
config.OperatorType = "test-operator"
config.MaxLogSize = 2
err := config.Validate()
assert.Equal(t, err.Error(), "'script_name' must be specified")
}
func TestCreateWithNonEmptyMultiline(t *testing.T) {
tmpScript(t)
config := Config{}
config.ScriptName = "aasd"
config.OperatorType = "test-operator"
config.Multiline = split.Config{
LineStartPattern: "a",
LineEndPattern: "",
}
built, err := config.Build(componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
assert.NotNil(t, config, "failed to create default config")
assert.NotNil(t, built, "failed to create default config")
}