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{
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"This is an alert", false},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: Alerts{
|
|
|
|
"fakeres.foobar": []Alert{
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"This is an alert", false},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TestWithMultipleAlerts",
|
|
|
|
alerts: Alerts{
|
|
|
|
"fakeres.foobar": []Alert{
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"This is an alert", false},
|
|
|
|
&FakeAlert{"This is a second alert", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"fakeres.barfoo": []Alert{
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"This is a third alert", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: Alerts{
|
|
|
|
"fakeres.foobar": []Alert{
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"This is an alert", false},
|
|
|
|
&FakeAlert{"This is a second alert", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"fakeres.barfoo": []Alert{
|
2021-02-12 21:39:49 +00:00
|
|
|
&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": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should not be ignored", false},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"fakeres.foobar": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should not be ignored", false},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"fakeres.barfoo": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should not be ignored", false},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"other.resource": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&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": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should be ignored", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"other.foobaz": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should be ignored", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"other.resource": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&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": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should be ignored", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"other.foobaz": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&FakeAlert{"Should be ignored", true},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
"other.resource": {
|
2021-02-12 21:39:49 +00:00
|
|
|
&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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|