fix tests
This commit is contained in:
parent
c7614a327a
commit
1eaaa2075d
2 changed files with 61 additions and 22 deletions
|
@ -2,24 +2,34 @@ package alertcontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewAlertContext(t *testing.T) {
|
func TestNewAlertContext(t *testing.T) {
|
||||||
|
contextFileFullPath, err := filepath.Abs("./tests/context.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
contextToSend map[string][]string
|
contextToSend []csconfig.ContextToSend
|
||||||
valueLength int
|
valueLength int
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic config test",
|
name: "basic config test",
|
||||||
contextToSend: map[string][]string{
|
contextToSend: []csconfig.ContextToSend{
|
||||||
"test": {"evt.Parsed.source_ip"},
|
csconfig.ContextToSend{
|
||||||
|
SourceFile: contextFileFullPath,
|
||||||
|
Context: map[string][]string{
|
||||||
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
valueLength: 100,
|
valueLength: 100,
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
|
@ -35,18 +45,26 @@ func TestNewAlertContext(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventToContext(t *testing.T) {
|
func TestEventToContext(t *testing.T) {
|
||||||
|
contextFileFullPath, err := filepath.Abs("./tests/context.yaml")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
contextToSend map[string][]string
|
contextToSend []csconfig.ContextToSend
|
||||||
valueLength int
|
valueLength int
|
||||||
events []types.Event
|
events []types.Event
|
||||||
expectedResult models.Meta
|
expectedResult models.Meta
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic test",
|
name: "basic test",
|
||||||
contextToSend: map[string][]string{
|
contextToSend: []csconfig.ContextToSend{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
csconfig.ContextToSend{
|
||||||
"nonexistent_field": {"evt.Parsed.nonexist"},
|
SourceFile: contextFileFullPath,
|
||||||
|
Context: map[string][]string{
|
||||||
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
|
"nonexistent_field": {"evt.Parsed.nonexist"},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
valueLength: 100,
|
valueLength: 100,
|
||||||
events: []types.Event{
|
events: []types.Event{
|
||||||
|
@ -66,10 +84,14 @@ func TestEventToContext(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test many events",
|
name: "test many events",
|
||||||
contextToSend: map[string][]string{
|
contextToSend: []csconfig.ContextToSend{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
csconfig.ContextToSend{
|
||||||
"source_machine": {"evt.Parsed.source_machine"},
|
SourceFile: contextFileFullPath,
|
||||||
"cve": {"evt.Parsed.cve"},
|
Context: map[string][]string{
|
||||||
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
|
"source_machine": {"evt.Parsed.source_machine"},
|
||||||
|
"cve": {"evt.Parsed.cve"}},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
valueLength: 100,
|
valueLength: 100,
|
||||||
events: []types.Event{
|
events: []types.Event{
|
||||||
|
@ -112,11 +134,17 @@ func TestEventToContext(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test many events with result above max length (need truncate, keep only 2 on 3 elements)",
|
name: "test many events with result above max length (need truncate, keep only 2 on 3 elements)",
|
||||||
contextToSend: map[string][]string{
|
contextToSend: []csconfig.ContextToSend{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
csconfig.ContextToSend{
|
||||||
"source_machine": {"evt.Parsed.source_machine"},
|
SourceFile: contextFileFullPath,
|
||||||
"uri": {"evt.Parsed.uri"},
|
Context: map[string][]string{
|
||||||
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
|
"source_machine": {"evt.Parsed.source_machine"},
|
||||||
|
"uri": {"evt.Parsed.uri"},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
valueLength: 100,
|
valueLength: 100,
|
||||||
events: []types.Event{
|
events: []types.Event{
|
||||||
{
|
{
|
||||||
|
@ -158,10 +186,15 @@ func TestEventToContext(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "test one events with result above max length (need truncate on one element)",
|
name: "test one events with result above max length (need truncate on one element)",
|
||||||
contextToSend: map[string][]string{
|
contextToSend: []csconfig.ContextToSend{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
csconfig.ContextToSend{
|
||||||
"source_machine": {"evt.Parsed.source_machine"},
|
SourceFile: contextFileFullPath,
|
||||||
"uri": {"evt.Parsed.uri"},
|
Context: map[string][]string{
|
||||||
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
|
"source_machine": {"evt.Parsed.source_machine"},
|
||||||
|
"uri": {"evt.Parsed.uri"},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
valueLength: 100,
|
valueLength: 100,
|
||||||
events: []types.Event{
|
events: []types.Event{
|
||||||
|
|
|
@ -36,6 +36,9 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
contextFileFullPath, err := filepath.Abs("./tests/context.yaml")
|
contextFileFullPath, err := filepath.Abs("./tests/context.yaml")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
contextDirFullPath, err := filepath.Abs("./tests/context/")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input *Config
|
input *Config
|
||||||
|
@ -66,6 +69,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
Enable: ptr.Of(true),
|
Enable: ptr.Of(true),
|
||||||
AcquisitionDirPath: "",
|
AcquisitionDirPath: "",
|
||||||
ContextPath: contextFileFullPath,
|
ContextPath: contextFileFullPath,
|
||||||
|
ContextDir: contextDirFullPath,
|
||||||
AcquisitionFilePath: acquisFullPath,
|
AcquisitionFilePath: acquisFullPath,
|
||||||
ConfigDir: configDirFullPath,
|
ConfigDir: configDirFullPath,
|
||||||
DataDir: dataFullPath,
|
DataDir: dataFullPath,
|
||||||
|
@ -79,7 +83,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
SimulationFilePath: "./tests/simulation.yaml",
|
SimulationFilePath: "./tests/simulation.yaml",
|
||||||
ContextToSend: []ContextToSend{
|
ContextToSend: []ContextToSend{
|
||||||
ContextToSend{
|
ContextToSend{
|
||||||
SourceFile: "./tests/context.yaml",
|
SourceFile: contextFileFullPath,
|
||||||
Context: map[string][]string{
|
Context: map[string][]string{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
},
|
},
|
||||||
|
@ -115,6 +119,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
AcquisitionDirPath: acquisDirFullPath,
|
AcquisitionDirPath: acquisDirFullPath,
|
||||||
AcquisitionFilePath: acquisFullPath,
|
AcquisitionFilePath: acquisFullPath,
|
||||||
ContextPath: contextFileFullPath,
|
ContextPath: contextFileFullPath,
|
||||||
|
ContextDir: contextDirFullPath,
|
||||||
ConfigDir: configDirFullPath,
|
ConfigDir: configDirFullPath,
|
||||||
HubIndexFile: hubIndexFileFullPath,
|
HubIndexFile: hubIndexFileFullPath,
|
||||||
DataDir: dataFullPath,
|
DataDir: dataFullPath,
|
||||||
|
@ -126,7 +131,7 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
AcquisitionFiles: []string{acquisFullPath, acquisInDirFullPath},
|
AcquisitionFiles: []string{acquisFullPath, acquisInDirFullPath},
|
||||||
ContextToSend: []ContextToSend{
|
ContextToSend: []ContextToSend{
|
||||||
ContextToSend{
|
ContextToSend{
|
||||||
SourceFile: "./tests/context.yaml",
|
SourceFile: contextFileFullPath,
|
||||||
Context: map[string][]string{
|
Context: map[string][]string{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
},
|
},
|
||||||
|
@ -169,11 +174,12 @@ func TestLoadCrowdsec(t *testing.T) {
|
||||||
ParserRoutinesCount: 1,
|
ParserRoutinesCount: 1,
|
||||||
OutputRoutinesCount: 1,
|
OutputRoutinesCount: 1,
|
||||||
ContextValueLength: 10,
|
ContextValueLength: 10,
|
||||||
|
ContextDir: contextDirFullPath,
|
||||||
AcquisitionFiles: []string{},
|
AcquisitionFiles: []string{},
|
||||||
SimulationFilePath: "",
|
SimulationFilePath: "",
|
||||||
ContextToSend: []ContextToSend{
|
ContextToSend: []ContextToSend{
|
||||||
ContextToSend{
|
ContextToSend{
|
||||||
SourceFile: "./tests/context.yaml",
|
SourceFile: contextFileFullPath,
|
||||||
Context: map[string][]string{
|
Context: map[string][]string{
|
||||||
"source_ip": {"evt.Parsed.source_ip"},
|
"source_ip": {"evt.Parsed.source_ip"},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue