Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: use testify to check the test results #108

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/nats-io/nats.go v1.10.0
github.com/prometheus/client_golang v1.8.0
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
google.golang.org/api v0.35.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand Down
19 changes: 9 additions & 10 deletions outputs/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewAlertmanagerPayload(t *testing.T) {
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)
s, _ := json.Marshal(newAlertmanagerPayload(f))
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))
s, err := json.Marshal(newAlertmanagerPayload(f))
require.Nil(t, err)

var o1, o2 alertmanagerPayload
json.Unmarshal([]byte(expectedOutput), &o1)
json.Unmarshal(s, &o2)
var o1, o2 []alertmanagerPayload
require.Nil(t, json.Unmarshal([]byte(expectedOutput), &o1))
require.Nil(t, json.Unmarshal(s, &o2))

if !reflect.DeepEqual(o1, o2) {
// t.Fatalf("\nexpected payload: \n%v\ngot: \n%v\n", o1, o2)
t.Fatalf("\nexpected payload: \n%v\ngot: \n%v\n", expectedOutput, string(s))
}
require.Equal(t, o1, o2)
}
27 changes: 12 additions & 15 deletions outputs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

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}}`
Expand All @@ -22,14 +23,11 @@ func TestNewClient(t *testing.T) {

testClientOutput := Client{OutputType: "test", EndpointURL: u, Config: config, Stats: stats, PromStats: promStats}
_, err := NewClient("test", "localhost/%*$¨^!/:;", config, stats, promStats, nil, nil)
if err == nil {
t.Fatalf("error while creating client object : %v\n", err)
}
require.NotNil(t, err)

nc, _ := NewClient("test", "http://localhost", config, stats, promStats, nil, nil)
if !reflect.DeepEqual(&testClientOutput, nc) {
t.Fatalf("expected: %v, got: %v\n", testClientOutput, nc)
}
nc, err := NewClient("test", "http://localhost", config, stats, promStats, nil, nil)
require.Nil(t, err)
require.Equal(t, &testClientOutput, nc)
}

func TestPost(t *testing.T) {
Expand Down Expand Up @@ -57,8 +55,6 @@ func TestPost(t *testing.T) {
}
}))

nc, _ := NewClient("", "", &types.Configuration{}, &types.Statistics{}, &types.PromStatistics{}, nil, nil)

for i, j := range map[string]error{
"/200": nil, "/400": ErrHeaderMissing,
"/401": ErrClientAuthenticationError,
Expand All @@ -68,10 +64,11 @@ func TestPost(t *testing.T) {
"/429": ErrTooManyRequest,
"/502": errors.New("502 Bad Gateway"),
} {
nc, _ = NewClient("", ts.URL+i, &types.Configuration{}, &types.Statistics{}, &types.PromStatistics{}, nil, nil)
err := nc.Post("")
if !reflect.DeepEqual(err, j) {
t.Fatalf("expected error: %v, got: %v\n", j, err)
}
nc, err := NewClient("", ts.URL+i, &types.Configuration{}, &types.Statistics{}, &types.PromStatistics{}, nil, nil)
require.Nil(t, err)
require.NotEmpty(t, nc)

errPost := nc.Post("")
require.Equal(t, errPost, j)
}
}
11 changes: 5 additions & 6 deletions outputs/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewDatadogPayload(t *testing.T) {
Expand All @@ -16,10 +17,8 @@ func TestNewDatadogPayload(t *testing.T) {
s, _ := json.Marshal(newDatadogPayload(f))

var o1, o2 datadogPayload
json.Unmarshal([]byte(expectedOutput), &o1)
json.Unmarshal(s, &o2)
require.Nil(t, json.Unmarshal([]byte(expectedOutput), &o1))
require.Nil(t, json.Unmarshal(s, &o2))

if !reflect.DeepEqual(o1, o2) {
t.Fatalf("\nexpected payload: \n%v\ngot: \n%v\n", expectedOutput, string(s))
}
require.Equal(t, o1, o2)
}
12 changes: 7 additions & 5 deletions outputs/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (
"testing"

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewInfluxdbPayload(t *testing.T) {
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)
influxdbPayload, _ := json.Marshal(newInfluxdbPayload(f, &types.Configuration{}))
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))

influxdbPayload, err := json.Marshal(newInfluxdbPayload(f, &types.Configuration{}))
require.Nil(t, err)

if string(influxdbPayload) != expectedOutput {
t.Fatalf("\nexpected payload: \n%v\ngot: \n%v\n", expectedOutput, string(influxdbPayload))
}
require.Equal(t, string(influxdbPayload), expectedOutput)
}
9 changes: 4 additions & 5 deletions outputs/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewLokiPayload(t *testing.T) {
Expand All @@ -24,10 +25,8 @@ func TestNewLokiPayload(t *testing.T) {
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
require.Nil(t, 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)
}
require.Equal(t, output, expectedOutput)
}
14 changes: 8 additions & 6 deletions outputs/mattermost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestMattermostPayload(t *testing.T) {
Expand Down Expand Up @@ -47,17 +48,18 @@ func TestMattermostPayload(t *testing.T) {
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))
config := &types.Configuration{
Mattermost: types.MattermostOutputConfig{
Username: "Falcosidekick",
Icon: "https://raw.githubusercontent.com/falcosecurity/falcosidekick/master/imgs/falcosidekick.png",
},
}

config.Mattermost.MessageFormatTemplate, _ = template.New("").Parse("Rule: {{ .Rule }} Priority: {{ .Priority }}")
var err error
config.Mattermost.MessageFormatTemplate, err = template.New("").Parse("Rule: {{ .Rule }} Priority: {{ .Priority }}")
require.Nil(t, err)

output := newMattermostPayload(f, config)
if !reflect.DeepEqual(output, expectedOutput) {
t.Fatalf("\nexpected payload: \n%#v\ngot: \n%#v\n", expectedOutput, output)
}
require.Equal(t, output, expectedOutput)
}
9 changes: 4 additions & 5 deletions outputs/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewOpsgeniePayload(t *testing.T) {
Expand All @@ -20,10 +21,8 @@ func TestNewOpsgeniePayload(t *testing.T) {
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
require.Nil(t, 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)
}
require.Equal(t, output, expectedOutput)
}
15 changes: 8 additions & 7 deletions outputs/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewSlackPayload(t *testing.T) {
Expand Down Expand Up @@ -47,18 +48,18 @@ func TestNewSlackPayload(t *testing.T) {
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))
config := &types.Configuration{
Slack: types.SlackOutputConfig{
Username: "Falcosidekick",
Icon: DefaultIconURL,
},
}

config.Slack.MessageFormatTemplate, _ = template.New("").Parse("Rule: {{ .Rule }} Priority: {{ .Priority }}")
output := newSlackPayload(f, config)
var err error
config.Slack.MessageFormatTemplate, err = template.New("").Parse("Rule: {{ .Rule }} Priority: {{ .Priority }}")
require.Nil(t, err)

if !reflect.DeepEqual(output, expectedOutput) {
t.Fatalf("\nexpected payload: \n%#v\ngot: \n%#v\n", expectedOutput, output)
}
output := newSlackPayload(f, config)
require.Equal(t, output, expectedOutput)
}
11 changes: 5 additions & 6 deletions outputs/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package outputs

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

"github.com/falcosecurity/falcosidekick/types"

"github.com/stretchr/testify/require"
)

func TestNewTeamsPayload(t *testing.T) {
Expand Down Expand Up @@ -38,10 +39,8 @@ func TestNewTeamsPayload(t *testing.T) {
}

var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
output := newTeamsPayload(f, &types.Configuration{})
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))

if !reflect.DeepEqual(output, expectedOutput) {
t.Fatalf("\nexpected payload: \n%#v\ngot: \n%#v\n", expectedOutput, output)
}
output := newTeamsPayload(f, &types.Configuration{})
require.Equal(t, output, expectedOutput)
}