Skip to content

Commit

Permalink
Start cleaning up condition tests (#5960)
Browse files Browse the repository at this point in the history
This simplifies part of the condition tests to use table tests. Like this the expected result and the condition itself are close together in the code which makes it easier to read.

Cleanup was triggered when looking at #5954 and realised it hard to follow in a diff.
  • Loading branch information
ruflin authored and exekias committed Jan 4, 2018
1 parent 1dc9113 commit 1a007c4
Showing 1 changed file with 50 additions and 67 deletions.
117 changes: 50 additions & 67 deletions libbeat/processors/condition_test.go
Expand Up @@ -23,7 +23,7 @@ func (c *countFilter) Run(e *beat.Event) (*beat.Event, error) {

func (c *countFilter) String() string { return "count" }

func TestConditions(t *testing.T) {
func TestCreateConditions(t *testing.T) {
logp.TestingSetup()

configs := []ConditionConfig{
Expand Down Expand Up @@ -71,85 +71,66 @@ func GetConditions(t *testing.T, configs []ConditionConfig) []Condition {
return conds
}

func TestEqualsCondition(t *testing.T) {
func TestCondition(t *testing.T) {
logp.TestingSetup()

configs := []ConditionConfig{
tests := []struct {
config ConditionConfig
result bool
}{
{
Equals: &ConditionFields{fields: map[string]interface{}{
"type": "process",
}},
config: ConditionConfig{
Equals: &ConditionFields{fields: map[string]interface{}{
"type": "process",
}},
},
result: true,
},

{
Equals: &ConditionFields{fields: map[string]interface{}{
"type": "process",
"proc.pid": 305,
}},
config: ConditionConfig{
Equals: &ConditionFields{fields: map[string]interface{}{
"type": "process",
"proc.pid": 305,
}},
},
result: true,
},

{
Range: &ConditionFields{fields: map[string]interface{}{
"proc.cpu.total_p.gt": 0.5,
}},
},
}

conds := GetConditions(t, configs)

event := &beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"proc": common.MapStr{
"cmdline": "/usr/libexec/secd",
"cpu": common.MapStr{
"start_time": "Apr10",
"system": 1988,
"total": 6029,
"total_p": 0.08,
"user": 4041,
},
"name": "secd",
"pid": 305,
"ppid": 1,
"state": "running",
"username": "monica",
config: ConditionConfig{
Range: &ConditionFields{fields: map[string]interface{}{
"proc.cpu.total_p.gt": 0.5,
}},
},
"type": "process",
}}

assert.True(t, conds[0].Check(event))
assert.True(t, conds[1].Check(event))
assert.False(t, conds[2].Check(event))
}

func TestContainsCondition(t *testing.T) {
logp.TestingSetup()

configs := []ConditionConfig{
result: false,
},
{
Contains: &ConditionFields{fields: map[string]interface{}{
"proc.name": "sec",
"proc.username": "monica",
}},
config: ConditionConfig{
Contains: &ConditionFields{fields: map[string]interface{}{
"proc.name": "sec",
"proc.username": "monica",
}},
},
result: true,
},

{
Contains: &ConditionFields{fields: map[string]interface{}{
"type": "process",
"proc.name": "secddd",
}},
config: ConditionConfig{
Contains: &ConditionFields{fields: map[string]interface{}{
"type": "process",
"proc.name": "secddd",
}},
},
result: false,
},

{
Contains: &ConditionFields{fields: map[string]interface{}{
"proc.keywords": "bar",
}},
config: ConditionConfig{
Contains: &ConditionFields{fields: map[string]interface{}{
"proc.keywords": "bar",
}},
},
result: true,
},
}

conds := GetConditions(t, configs)

event := &beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
Expand All @@ -173,9 +154,11 @@ func TestContainsCondition(t *testing.T) {
},
}

assert.True(t, conds[0].Check(event))
assert.False(t, conds[1].Check(event))
assert.True(t, conds[2].Check(event))
for _, test := range tests {
cond, err := NewCondition(&test.config)
assert.Nil(t, err)
assert.Equal(t, test.result, cond.Check(event))
}
}

func TestRegexpCondition(t *testing.T) {
Expand Down

0 comments on commit 1a007c4

Please sign in to comment.