driftctl/pkg/alerter/alerter_test.go

162 lines
3.2 KiB
Go
Raw Normal View History

2020-12-16 12:02:02 +00:00
package alerter
import (
"reflect"
"testing"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/pkg/resource"
2020-12-16 12:02:02 +00:00
)
func TestAlerter_Alert(t *testing.T) {
cases := []struct {
name string
alerts Alerts
expected Alerts
}{
{
name: "TestNoAlerts",
alerts: nil,
expected: Alerts{},
},
{
name: "TestWithSingleAlert",
alerts: Alerts{
"fakeres.foobar": []Alert{
&FakeAlert{"This is an alert", false},
2020-12-16 12:02:02 +00:00
},
},
expected: Alerts{
"fakeres.foobar": []Alert{
&FakeAlert{"This is an alert", false},
2020-12-16 12:02:02 +00:00
},
},
},
{
name: "TestWithMultipleAlerts",
alerts: Alerts{
"fakeres.foobar": []Alert{
&FakeAlert{"This is an alert", false},
&FakeAlert{"This is a second alert", true},
2020-12-16 12:02:02 +00:00
},
"fakeres.barfoo": []Alert{
&FakeAlert{"This is a third alert", true},
2020-12-16 12:02:02 +00:00
},
},
expected: Alerts{
"fakeres.foobar": []Alert{
&FakeAlert{"This is an alert", false},
&FakeAlert{"This is a second alert", true},
2020-12-16 12:02:02 +00:00
},
"fakeres.barfoo": []Alert{
&FakeAlert{"This is a third alert", true},
2020-12-16 12:02:02 +00:00
},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
alerter := NewAlerter()
for k, v := range c.alerts {
for _, a := range v {
alerter.SendAlert(k, a)
}
}
2021-01-11 16:33:23 +00:00
if eq := reflect.DeepEqual(alerter.Retrieve(), c.expected); !eq {
t.Errorf("Got %+v, expected %+v", alerter.Retrieve(), c.expected)
2020-12-16 12:02:02 +00:00
}
})
}
}
func TestAlerter_IgnoreResources(t *testing.T) {
cases := []struct {
name string
alerts Alerts
2021-08-09 14:03:04 +00:00
resource *resource.Resource
2020-12-16 12:02:02 +00:00
expected bool
}{
{
name: "TestNoAlerts",
alerts: Alerts{},
2021-08-09 14:03:04 +00:00
resource: &resource.Resource{
2020-12-16 12:02:02 +00:00
Type: "fakeres",
Id: "foobar",
},
expected: false,
},
{
name: "TestShouldNotBeIgnoredWithAlerts",
alerts: Alerts{
"fakeres": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
"fakeres.foobar": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
"fakeres.barfoo": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
"other.resource": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
},
2021-08-09 14:03:04 +00:00
resource: &resource.Resource{
2020-12-16 12:02:02 +00:00
Type: "fakeres",
Id: "foobar",
},
expected: false,
},
{
name: "TestShouldBeIgnoredWithAlertsOnWildcard",
alerts: Alerts{
"fakeres": {
&FakeAlert{"Should be ignored", true},
2020-12-16 12:02:02 +00:00
},
"other.foobaz": {
&FakeAlert{"Should be ignored", true},
2020-12-16 12:02:02 +00:00
},
"other.resource": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
},
2021-08-09 14:03:04 +00:00
resource: &resource.Resource{
2020-12-16 12:02:02 +00:00
Type: "fakeres",
Id: "foobar",
},
expected: true,
},
{
name: "TestShouldBeIgnoredWithAlertsOnResource",
alerts: Alerts{
"fakeres": {
&FakeAlert{"Should be ignored", true},
2020-12-16 12:02:02 +00:00
},
"other.foobaz": {
&FakeAlert{"Should be ignored", true},
2020-12-16 12:02:02 +00:00
},
"other.resource": {
&FakeAlert{"Should not be ignored", false},
2020-12-16 12:02:02 +00:00
},
},
2021-08-09 14:03:04 +00:00
resource: &resource.Resource{
2020-12-16 12:02:02 +00:00
Type: "other",
Id: "foobaz",
},
expected: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
alerter := NewAlerter()
alerter.SetAlerts(c.alerts)
if got := alerter.IsResourceIgnored(c.resource); got != c.expected {
t.Errorf("Got %+v, expected %+v", got, c.expected)
}
})
}
}