Skip to content

Commit

Permalink
new test + fix duplicate entries as notified in PR #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Issif committed Oct 4, 2019
1 parent 4e4462f commit bf286c6
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 22 deletions.
2 changes: 1 addition & 1 deletion outputs/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestNewAlertmanagerPayload(t *testing.T) {
expectedOutput := `[{"labels":{"proc_name":"falcosidekick","rule":"Test rule","source":"falco","user_name":"falcosidekick"},"annotations":{"info":"This is a test from falcosidekick","summary":"Test rule"}}]`
expectedOutput := `[{"labels":{"proc_name":"falcosidekick","rule":"Test rule","source":"falco"},"annotations":{"info":"This is a test from falcosidekick","summary":"Test rule"}}]`

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
Expand Down
2 changes: 1 addition & 1 deletion outputs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/falcosecurity/falcosidekick/types"
)

var falcoTestInput = `{"output":"This is a test from falcosidekick","priority":"Debug","rule":"Test rule", "time":"2001-01-01T01:10:00Z","output_fields": {"proc.name":"falcosidekick","user.name":"falcosidekick", "proc.tty": 1234}}`
var falcoTestInput = `{"output":"This is a test from falcosidekick","priority":"Debug","rule":"Test rule", "time":"2001-01-01T01:10:00Z","output_fields": {"proc.name":"falcosidekick", "proc.tty": 1234}}`

func TestNewClient(t *testing.T) {
u, _ := url.Parse("http://localhost")
Expand Down
2 changes: 2 additions & 0 deletions outputs/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func newDatadogPayload(falcopayload types.FalcoPayload) datadogPayload {
switch j.(type) {
case string:
tags = append(tags, i+":"+j.(string))
default:
continue
}
}
d.Tags = tags
Expand Down
2 changes: 1 addition & 1 deletion outputs/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestNewDatadogPayload(t *testing.T) {
expectedOutput := `{"title":"Test rule","text":"This is a test from falcosidekick","alert_type":"info","source_type_name":"falco","tags":["proc.name:falcosidekick","user.name:falcosidekick"]}`
expectedOutput := `{"title":"Test rule","text":"This is a test from falcosidekick","alert_type":"info","source_type_name":"falco","tags":["proc.name:falcosidekick"]}`

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
Expand Down
2 changes: 2 additions & 0 deletions outputs/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func newInfluxdbPayload(falcopayload types.FalcoPayload, config *types.Configura
switch j.(type) {
case string:
s += "," + i + "=" + strings.Replace(j.(string), " ", "_", -1)
default:
continue
}
}

Expand Down
2 changes: 1 addition & 1 deletion outputs/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestNewInfluxdbPayload(t *testing.T) {
expectedOutput := `"events,rule=Test_rule,priority=Debug,proc.name=falcosidekick,user.name=falcosidekick value=\"This is a test from falcosidekick\""`
expectedOutput := `"events,rule=Test_rule,priority=Debug,proc.name=falcosidekick value=\"This is a test from falcosidekick\""`

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
Expand Down
2 changes: 2 additions & 0 deletions outputs/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func newLokiPayload(falcopayload types.FalcoPayload, config *types.Configuration
switch j.(type) {
case string:
s += strings.Replace(strings.Replace(strings.Replace(i, ".", "", -1), "]", "", -1), "[", "", -1) + "=\"" + j.(string) + "\","
default:
continue
}
}
s += "rule=\"" + falcopayload.Rule + "\","
Expand Down
32 changes: 32 additions & 0 deletions outputs/loki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package outputs

import (
"encoding/json"
"reflect"
"testing"

"github.com/falcosecurity/falcosidekick/types"
)

func TestNewLokiPayload(t *testing.T) {
expectedOutput := lokiPayload{
Streams: []lokiStream{
lokiStream{
Labels: "{procname=\"falcosidekick\",rule=\"Test rule\",priority=\"Debug\"}",
Entries: []lokiEntry{
lokiEntry{
Ts: "2001-01-01T01:10:00Z",
Line: "This is a test from falcosidekick",
},
},
},
},
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
output := newLokiPayload(f, &types.Configuration{})
if !reflect.DeepEqual(output, expectedOutput) {
t.Fatalf("\nexpected payload: \n%#v\ngot: \n%#v\n", expectedOutput, output)
}
}
17 changes: 9 additions & 8 deletions outputs/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type opsgeniePayload struct {
Message string `json:"message"`
User string `json:"entity,omitempty"`
Entity string `json:"entity,omitempty"`
Description string `json:"description,omitempty"`
Details map[string]string `json:"details,omitempty"`
Priority string `json:"priority,omitempty"`
Expand All @@ -19,31 +19,32 @@ func newOpsgeniePayload(falcopayload types.FalcoPayload, config *types.Configura
switch j.(type) {
case string:
details[i] = j.(string)
default:
continue
}
}

var prio string
switch strings.ToLower(falcopayload.Priority) {
case "emergency", "alert", "critical":
case "emergency", "alert":
prio = "P1"
case "error":
case "critical":
prio = "P2"
case "warning":
case "error":
prio = "P3"
case "notice", "informationnal":
case "warning":
prio = "P4"
default:
prio = "P5"
}

ogpayload := opsgeniePayload{
return opsgeniePayload{
Message: falcopayload.Output,
User: "Falcosidekick",
Entity: "Falcosidekick",
Description: falcopayload.Rule,
Details: details,
Priority: prio,
}
return ogpayload
}

// OpsgeniePost posts event to OpsGenie
Expand Down
28 changes: 28 additions & 0 deletions outputs/opsgenie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package outputs

import (
"encoding/json"
"reflect"
"testing"

"github.com/falcosecurity/falcosidekick/types"
)

func TestNewOpsgeniePayload(t *testing.T) {
expectedOutput := opsgeniePayload{
Message: "This is a test from falcosidekick",
Entity: "Falcosidekick",
Description: "Test rule",
Details: map[string]string{
"proc.name": "falcosidekick",
},
Priority: "P5",
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
output := newOpsgeniePayload(f, &types.Configuration{})
if !reflect.DeepEqual(output, expectedOutput) {
t.Fatalf("\nexpected payload: \n%#v\ngot: \n%#v\n", expectedOutput, output)
}
}
7 changes: 1 addition & 6 deletions outputs/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestNewSlackPayload(t *testing.T) {
expectedOutput := slackPayload{
Username: "Falco Sidekick",
Username: "Falcosidekick",
IconURL: "https://raw.githubusercontent.com/falcosecurity/falcosidekick/master/imgs/falcosidekick.png",
Attachments: []slackAttachment{
slackAttachment{
Expand All @@ -24,11 +24,6 @@ func TestNewSlackPayload(t *testing.T) {
Value: "falcosidekick",
Short: true,
},
slackAttachmentField{
Title: "user.name",
Value: "falcosidekick",
Short: true,
},
slackAttachmentField{
Title: "rule",
Value: "Test rule",
Expand Down
4 changes: 0 additions & 4 deletions outputs/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ func TestNewTeamsPayload(t *testing.T) {
Name: "proc.name",
Value: "falcosidekick",
},
teamsFact{
Name: "user.name",
Value: "falcosidekick",
},
teamsFact{
Name: "rule",
Value: "Test rule",
Expand Down

0 comments on commit bf286c6

Please sign in to comment.