2020-11-30 09:37:17 +00:00
|
|
|
package acquisition
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-11 07:53:53 +00:00
|
|
|
"strings"
|
2020-11-30 09:37:17 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-11-30 09:37:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-10-17 15:32:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-30 09:37:17 +00:00
|
|
|
tomb "gopkg.in/tomb.v2"
|
2021-06-11 07:53:53 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2023-05-25 13:37:44 +00:00
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
type MockSource struct {
|
|
|
|
configuration.DataSourceCommonCfg `yaml:",inline"`
|
|
|
|
Toto string `yaml:"toto"`
|
|
|
|
logger *log.Entry
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:36:56 +00:00
|
|
|
func (f *MockSource) UnmarshalConfig(cfg []byte) error {
|
|
|
|
err := yaml.UnmarshalStrict(cfg, &f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:57:19 +00:00
|
|
|
func (f *MockSource) Configure(cfg []byte, logger *log.Entry, metricsLevel int) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
f.logger = logger
|
2022-11-30 16:36:56 +00:00
|
|
|
if err := f.UnmarshalConfig(cfg); err != nil {
|
|
|
|
return err
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if f.Mode == "" {
|
|
|
|
f.Mode = configuration.CAT_MODE
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if f.Mode != configuration.CAT_MODE && f.Mode != configuration.TAIL_MODE {
|
|
|
|
return fmt.Errorf("mode %s is not supported", f.Mode)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if f.Toto == "" {
|
|
|
|
return fmt.Errorf("expect non-empty toto")
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (f *MockSource) GetMode() string { return f.Mode }
|
|
|
|
func (f *MockSource) OneShotAcquisition(chan types.Event, *tomb.Tomb) error { return nil }
|
|
|
|
func (f *MockSource) StreamingAcquisition(chan types.Event, *tomb.Tomb) error { return nil }
|
|
|
|
func (f *MockSource) CanRun() error { return nil }
|
|
|
|
func (f *MockSource) GetMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockSource) GetAggregMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockSource) Dump() interface{} { return f }
|
|
|
|
func (f *MockSource) GetName() string { return "mock" }
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockSource) ConfigureByDSN(string, map[string]string, *log.Entry, string) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
return fmt.Errorf("not supported")
|
|
|
|
}
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockSource) GetUuid() string { return "" }
|
2021-06-11 07:53:53 +00:00
|
|
|
|
2022-08-26 11:31:49 +00:00
|
|
|
// copy the mocksource, but this one can't run
|
2021-06-11 07:53:53 +00:00
|
|
|
type MockSourceCantRun struct {
|
|
|
|
MockSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *MockSourceCantRun) CanRun() error { return fmt.Errorf("can't run bro") }
|
|
|
|
func (f *MockSourceCantRun) GetName() string { return "mock_cant_run" }
|
|
|
|
|
2022-08-26 11:31:49 +00:00
|
|
|
// appendMockSource is only used to add mock source for tests
|
2021-06-11 07:53:53 +00:00
|
|
|
func appendMockSource() {
|
|
|
|
if GetDataSourceIface("mock") == nil {
|
2022-11-30 16:36:56 +00:00
|
|
|
AcquisitionSources["mock"] = func() DataSource { return &MockSource{} }
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if GetDataSourceIface("mock_cant_run") == nil {
|
2022-11-30 16:36:56 +00:00
|
|
|
AcquisitionSources["mock_cant_run"] = func() DataSource { return &MockSourceCantRun{} }
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataSourceConfigure(t *testing.T) {
|
2021-06-11 07:53:53 +00:00
|
|
|
appendMockSource()
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
tests := []struct {
|
2021-06-11 07:53:53 +00:00
|
|
|
TestName string
|
2022-10-17 15:32:08 +00:00
|
|
|
String string
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError string
|
2020-11-30 09:37:17 +00:00
|
|
|
}{
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "basic_valid_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: cat
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: info
|
|
|
|
source: mock
|
|
|
|
toto: test_value1
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "basic_debug_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: cat
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: mock
|
|
|
|
toto: test_value1
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "basic_tailmode_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: tail
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: mock
|
|
|
|
toto: test_value1
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "bad_mode_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: ratata
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: mock
|
|
|
|
toto: test_value1
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "failed to configure datasource mock: mode ratata is not supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "bad_type_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: cat
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: tutu
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "cannot find source tutu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "mismatch_config",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: cat
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: mock
|
|
|
|
wowo: ajsajasjas
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "field wowo not found in type acquisition.MockSource",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "cant_run_error",
|
2022-10-17 15:32:08 +00:00
|
|
|
String: `
|
2021-06-11 07:53:53 +00:00
|
|
|
mode: cat
|
|
|
|
labels:
|
|
|
|
test: foobar
|
|
|
|
log_level: debug
|
|
|
|
source: mock_cant_run
|
|
|
|
wowo: ajsajasjas
|
2022-10-17 15:32:08 +00:00
|
|
|
`,
|
2023-06-27 08:13:13 +00:00
|
|
|
ExpectedError: "datasource 'mock_cant_run' is not available: can't run bro",
|
2021-06-11 07:53:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.TestName, func(t *testing.T) {
|
|
|
|
common := configuration.DataSourceCommonCfg{}
|
|
|
|
yaml.Unmarshal([]byte(tc.String), &common)
|
2024-03-13 13:57:19 +00:00
|
|
|
ds, err := DataSourceConfigure(common, configuration.METRICS_NONE)
|
2022-10-17 15:32:08 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.ExpectedError)
|
2022-12-21 11:20:01 +00:00
|
|
|
if tc.ExpectedError != "" {
|
2022-10-17 15:32:08 +00:00
|
|
|
return
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
switch tc.TestName {
|
|
|
|
case "basic_valid_config":
|
|
|
|
mock := (*ds).Dump().(*MockSource)
|
2024-01-05 14:26:13 +00:00
|
|
|
assert.Equal(t, "test_value1", mock.Toto)
|
|
|
|
assert.Equal(t, "cat", mock.Mode)
|
|
|
|
assert.Equal(t, log.InfoLevel, mock.logger.Logger.Level)
|
|
|
|
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
|
2022-10-17 15:32:08 +00:00
|
|
|
case "basic_debug_config":
|
|
|
|
mock := (*ds).Dump().(*MockSource)
|
2024-01-05 14:26:13 +00:00
|
|
|
assert.Equal(t, "test_value1", mock.Toto)
|
|
|
|
assert.Equal(t, "cat", mock.Mode)
|
|
|
|
assert.Equal(t, log.DebugLevel, mock.logger.Logger.Level)
|
|
|
|
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
|
2022-10-17 15:32:08 +00:00
|
|
|
case "basic_tailmode_config":
|
|
|
|
mock := (*ds).Dump().(*MockSource)
|
2024-01-05 14:26:13 +00:00
|
|
|
assert.Equal(t, "test_value1", mock.Toto)
|
|
|
|
assert.Equal(t, "tail", mock.Mode)
|
|
|
|
assert.Equal(t, log.DebugLevel, mock.logger.Logger.Level)
|
|
|
|
assert.Equal(t, map[string]string{"test": "foobar"}, mock.Labels)
|
2022-10-17 15:32:08 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadAcquisitionFromFile(t *testing.T) {
|
|
|
|
appendMockSource()
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
TestName string
|
|
|
|
Config csconfig.CrowdsecServiceCfg
|
|
|
|
ExpectedError string
|
|
|
|
ExpectedLen int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
TestName: "non_existent_file",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"does_not_exist"},
|
|
|
|
},
|
2022-10-17 15:32:08 +00:00
|
|
|
ExpectedError: "open does_not_exist: " + cstest.FileNotFoundMessage,
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedLen: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "invalid_yaml_file",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/badyaml.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "failed to yaml decode test_files/badyaml.yaml: yaml: unmarshal errors",
|
|
|
|
ExpectedLen: 0,
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "invalid_empty_yaml",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/emptyitem.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedLen: 0,
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "basic_valid",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/basic_filemode.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedLen: 2,
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "missing_labels",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/missing_labels.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "missing labels in test_files/missing_labels.yaml",
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "backward_compat",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/backward_compat.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedLen: 2,
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
{
|
|
|
|
TestName: "bad_type",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/bad_source.yaml"},
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
2021-06-11 07:53:53 +00:00
|
|
|
ExpectedError: "unknown data source does_not_exist in test_files/bad_source.yaml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "invalid_filetype_config",
|
|
|
|
Config: csconfig.CrowdsecServiceCfg{
|
|
|
|
AcquisitionFiles: []string{"test_files/bad_filetype.yaml"},
|
|
|
|
},
|
|
|
|
ExpectedError: "while configuring datasource of type file from test_files/bad_filetype.yaml",
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
2022-10-17 15:32:08 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.TestName, func(t *testing.T) {
|
2024-03-13 13:57:19 +00:00
|
|
|
dss, err := LoadAcquisitionFromFile(&tc.Config, nil)
|
2022-10-17 15:32:08 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.ExpectedError)
|
|
|
|
if tc.ExpectedError != "" {
|
|
|
|
return
|
|
|
|
}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
assert.Len(t, dss, tc.ExpectedLen)
|
|
|
|
})
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
test start acquisition :
|
|
|
|
- create mock parser in cat mode : start acquisition, check it returns, count items in chan
|
|
|
|
- create mock parser in tail mode : start acquisition, sleep, check item count, tomb kill it, wait for it to return
|
|
|
|
*/
|
|
|
|
|
|
|
|
type MockCat struct {
|
|
|
|
configuration.DataSourceCommonCfg `yaml:",inline"`
|
|
|
|
logger *log.Entry
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:57:19 +00:00
|
|
|
func (f *MockCat) Configure(cfg []byte, logger *log.Entry, metricsLevel int) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
f.logger = logger
|
|
|
|
if f.Mode == "" {
|
|
|
|
f.Mode = configuration.CAT_MODE
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if f.Mode != configuration.CAT_MODE {
|
|
|
|
return fmt.Errorf("mode %s is not supported", f.Mode)
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-30 16:36:56 +00:00
|
|
|
|
|
|
|
func (f *MockCat) UnmarshalConfig(cfg []byte) error { return nil }
|
|
|
|
func (f *MockCat) GetName() string { return "mock_cat" }
|
|
|
|
func (f *MockCat) GetMode() string { return "cat" }
|
2021-06-11 07:53:53 +00:00
|
|
|
func (f *MockCat) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) error {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
evt := types.Event{}
|
|
|
|
evt.Line.Src = "test"
|
|
|
|
out <- evt
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (f *MockCat) StreamingAcquisition(chan types.Event, *tomb.Tomb) error {
|
|
|
|
return fmt.Errorf("can't run in tail")
|
|
|
|
}
|
2022-02-15 11:50:33 +00:00
|
|
|
func (f *MockCat) CanRun() error { return nil }
|
|
|
|
func (f *MockCat) GetMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockCat) GetAggregMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockCat) Dump() interface{} { return f }
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockCat) ConfigureByDSN(string, map[string]string, *log.Entry, string) error {
|
2022-02-15 11:50:33 +00:00
|
|
|
return fmt.Errorf("not supported")
|
|
|
|
}
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockCat) GetUuid() string { return "" }
|
2021-06-11 07:53:53 +00:00
|
|
|
|
|
|
|
//----
|
|
|
|
|
|
|
|
type MockTail struct {
|
|
|
|
configuration.DataSourceCommonCfg `yaml:",inline"`
|
|
|
|
logger *log.Entry
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:57:19 +00:00
|
|
|
func (f *MockTail) Configure(cfg []byte, logger *log.Entry, metricsLevel int) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
f.logger = logger
|
|
|
|
if f.Mode == "" {
|
|
|
|
f.Mode = configuration.TAIL_MODE
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if f.Mode != configuration.TAIL_MODE {
|
|
|
|
return fmt.Errorf("mode %s is not supported", f.Mode)
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-30 16:36:56 +00:00
|
|
|
|
|
|
|
func (f *MockTail) UnmarshalConfig(cfg []byte) error { return nil }
|
|
|
|
func (f *MockTail) GetName() string { return "mock_tail" }
|
|
|
|
func (f *MockTail) GetMode() string { return "tail" }
|
2021-06-11 07:53:53 +00:00
|
|
|
func (f *MockTail) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) error {
|
|
|
|
return fmt.Errorf("can't run in cat mode")
|
|
|
|
}
|
|
|
|
func (f *MockTail) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
evt := types.Event{}
|
|
|
|
evt.Line.Src = "test"
|
|
|
|
out <- evt
|
|
|
|
}
|
2022-05-25 20:27:50 +00:00
|
|
|
<-t.Dying()
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2022-05-25 20:27:50 +00:00
|
|
|
return nil
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
func (f *MockTail) CanRun() error { return nil }
|
|
|
|
func (f *MockTail) GetMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockTail) GetAggregMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockTail) Dump() interface{} { return f }
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockTail) ConfigureByDSN(string, map[string]string, *log.Entry, string) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
return fmt.Errorf("not supported")
|
|
|
|
}
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockTail) GetUuid() string { return "" }
|
2021-06-11 07:53:53 +00:00
|
|
|
|
|
|
|
//func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb *tomb.Tomb) error {
|
|
|
|
|
|
|
|
func TestStartAcquisitionCat(t *testing.T) {
|
|
|
|
sources := []DataSource{
|
|
|
|
&MockCat{},
|
|
|
|
}
|
|
|
|
out := make(chan types.Event)
|
|
|
|
acquisTomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := StartAcquisition(sources, out, &acquisTomb); err != nil {
|
2022-03-10 12:53:33 +00:00
|
|
|
t.Errorf("unexpected error")
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
READLOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-out:
|
|
|
|
count++
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
break READLOOP
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2022-10-17 15:32:08 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 10, count)
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
func TestStartAcquisitionTail(t *testing.T) {
|
|
|
|
sources := []DataSource{
|
|
|
|
&MockTail{},
|
|
|
|
}
|
|
|
|
out := make(chan types.Event)
|
|
|
|
acquisTomb := tomb.Tomb{}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
go func() {
|
|
|
|
if err := StartAcquisition(sources, out, &acquisTomb); err != nil {
|
2022-03-10 12:53:33 +00:00
|
|
|
t.Errorf("unexpected error")
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
count := 0
|
|
|
|
READLOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-out:
|
|
|
|
count++
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
break READLOOP
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 15:32:08 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 10, count)
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
acquisTomb.Kill(nil)
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-10-17 15:32:08 +00:00
|
|
|
require.NoError(t, acquisTomb.Err(), "tomb is not dead")
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
type MockTailError struct {
|
|
|
|
MockTail
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *MockTailError) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
evt := types.Event{}
|
|
|
|
evt.Line.Src = "test"
|
|
|
|
out <- evt
|
|
|
|
}
|
|
|
|
t.Kill(fmt.Errorf("got error (tomb)"))
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return fmt.Errorf("got error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartAcquisitionTailError(t *testing.T) {
|
|
|
|
sources := []DataSource{
|
|
|
|
&MockTailError{},
|
|
|
|
}
|
|
|
|
out := make(chan types.Event)
|
|
|
|
acquisTomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := StartAcquisition(sources, out, &acquisTomb); err != nil && err.Error() != "got error (tomb)" {
|
2022-06-22 13:53:53 +00:00
|
|
|
t.Errorf("expected error, got '%s'", err)
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
READLOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-out:
|
|
|
|
count++
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
break READLOOP
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2022-10-17 15:32:08 +00:00
|
|
|
assert.Equal(t, 10, count)
|
2021-06-11 07:53:53 +00:00
|
|
|
//acquisTomb.Kill(nil)
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-10-17 15:32:08 +00:00
|
|
|
cstest.RequireErrorContains(t, acquisTomb.Err(), "got error (tomb)")
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockSourceByDSN struct {
|
|
|
|
configuration.DataSourceCommonCfg `yaml:",inline"`
|
2022-08-26 11:31:49 +00:00
|
|
|
Toto string `yaml:"toto"`
|
|
|
|
logger *log.Entry //nolint: unused
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 13:57:19 +00:00
|
|
|
func (f *MockSourceByDSN) UnmarshalConfig(cfg []byte) error { return nil }
|
|
|
|
func (f *MockSourceByDSN) Configure(cfg []byte, logger *log.Entry, metricsLevel int) error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
func (f *MockSourceByDSN) GetMode() string { return f.Mode }
|
|
|
|
func (f *MockSourceByDSN) OneShotAcquisition(chan types.Event, *tomb.Tomb) error { return nil }
|
|
|
|
func (f *MockSourceByDSN) StreamingAcquisition(chan types.Event, *tomb.Tomb) error { return nil }
|
|
|
|
func (f *MockSourceByDSN) CanRun() error { return nil }
|
|
|
|
func (f *MockSourceByDSN) GetMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockSourceByDSN) GetAggregMetrics() []prometheus.Collector { return nil }
|
|
|
|
func (f *MockSourceByDSN) Dump() interface{} { return f }
|
|
|
|
func (f *MockSourceByDSN) GetName() string { return "mockdsn" }
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockSourceByDSN) ConfigureByDSN(dsn string, labels map[string]string, logger *log.Entry, uuid string) error {
|
2021-06-11 07:53:53 +00:00
|
|
|
dsn = strings.TrimPrefix(dsn, "mockdsn://")
|
|
|
|
if dsn != "test_expect" {
|
|
|
|
return fmt.Errorf("unexpected value")
|
|
|
|
}
|
2024-01-05 14:26:13 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-29 14:04:17 +00:00
|
|
|
func (f *MockSourceByDSN) GetUuid() string { return "" }
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
func TestConfigureByDSN(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
dsn string
|
|
|
|
ExpectedError string
|
|
|
|
ExpectedResLen int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
dsn: "baddsn",
|
|
|
|
ExpectedError: "baddsn isn't valid dsn (no protocol)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dsn: "foobar://toto",
|
|
|
|
ExpectedError: "no acquisition for protocol foobar://",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dsn: "mockdsn://test_expect",
|
|
|
|
ExpectedResLen: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dsn: "mockdsn://bad",
|
|
|
|
ExpectedError: "unexpected value",
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
if GetDataSourceIface("mockdsn") == nil {
|
2022-11-30 16:36:56 +00:00
|
|
|
AcquisitionSources["mockdsn"] = func() DataSource { return &MockSourceByDSN{} }
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.dsn, func(t *testing.T) {
|
2023-03-29 14:04:17 +00:00
|
|
|
srcs, err := LoadAcquisitionFromDSN(tc.dsn, map[string]string{"type": "test_label"}, "")
|
2022-10-17 15:32:08 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.ExpectedError)
|
2022-02-15 11:50:33 +00:00
|
|
|
|
2022-10-17 15:32:08 +00:00
|
|
|
assert.Len(t, srcs, tc.ExpectedResLen)
|
|
|
|
})
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|