2020-12-16 12:02:02 +00:00
|
|
|
package alerter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resource2 "github.com/cloudskiff/driftctl/test/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
resource resource.Resource
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "TestNoAlerts",
|
|
|
|
alerts: Alerts{},
|
|
|
|
resource: &resource2.FakeResource{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
resource: &resource2.FakeResource{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
resource: &resource2.FakeResource{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
resource: &resource2.FakeResource{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|