Skip to content

Commit

Permalink
add notifier unit tests to runtime package
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Aug 10, 2020
1 parent ee73d71 commit c068097
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 9 deletions.
4 changes: 3 additions & 1 deletion pkg/runtime/executor.go
Expand Up @@ -97,7 +97,9 @@ func (e *Executor) Execute() (normalized interface{}, err error) {
// evaluate policies

// send notifications, if configured
e.SendNotifications(normalized)
if err = e.SendNotifications(normalized); err != nil {
return normalized, err
}

// successful
return normalized, nil
Expand Down
68 changes: 61 additions & 7 deletions pkg/runtime/executor_test.go
Expand Up @@ -24,6 +24,8 @@ import (
iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/iac-providers/output"
tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12"
"github.com/accurics/terrascan/pkg/notifications"
"github.com/accurics/terrascan/pkg/notifications/webhook"
)

var (
Expand Down Expand Up @@ -85,6 +87,22 @@ func TestExecute(t *testing.T) {
},
wantErr: nil,
},
{
name: "test SendNofitications no error",
executor: Executor{
iacProvider: MockIacProvider{err: nil},
notifiers: []notifications.Notifier{&MockNotifier{err: nil}},
},
wantErr: nil,
},
{
name: "test SendNofitications no error",
executor: Executor{
iacProvider: MockIacProvider{err: nil},
notifiers: []notifications.Notifier{&MockNotifier{err: mockNotifierErr}},
},
wantErr: mockNotifierErr,
},
}

for _, tt := range table {
Expand All @@ -104,6 +122,7 @@ func TestInit(t *testing.T) {
executor Executor
wantErr error
wantIacProvider iacProvider.IacProvider
wantNotifiers []notifications.Notifier
}{
{
name: "valid filePath",
Expand All @@ -116,16 +135,51 @@ func TestInit(t *testing.T) {
},
wantErr: nil,
wantIacProvider: &tfv12.TfV12{},
wantNotifiers: []notifications.Notifier{},
},
{
name: "valid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: "aws",
iacType: "terraform",
iacVersion: "v12",
configFile: "./testdata/webhook.toml",
},
wantErr: nil,
wantIacProvider: &tfv12.TfV12{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config not present",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: "aws",
iacType: "terraform",
iacVersion: "v12",
configFile: "./testdata/does-not-exist",
},
wantErr: fmt.Errorf("config file not present"),
wantIacProvider: &tfv12.TfV12{},
},
}

for _, tt := range table {
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
t.Run(tt.name, func(t *testing.T) {
gotErr := tt.executor.Init()
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(tt.executor.iacProvider, tt.wantIacProvider) {
t.Errorf("got: '%v', want: '%v'", tt.executor.iacProvider, tt.wantIacProvider)
}
for i, notifier := range tt.executor.notifiers {
if !reflect.DeepEqual(reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i])) {
t.Errorf("got: '%v', want: '%v'", reflect.TypeOf(notifier), reflect.TypeOf(tt.wantNotifiers[i]))
}
}
})
}
}
9 changes: 8 additions & 1 deletion pkg/runtime/notifications.go
Expand Up @@ -16,13 +16,20 @@

package runtime

import (
"github.com/accurics/terrascan/pkg/utils"
)

// SendNotifications sends notifications via all the configured notifiers
func (e *Executor) SendNotifications(data interface{}) {
func (e *Executor) SendNotifications(data interface{}) error {
var allErrs error
// send notifications using configured notifiers
for _, notifier := range e.notifiers {
err := notifier.SendNotification(data)
if err != nil {
allErrs = utils.WrapError(err, allErrs)
continue
}
}
return allErrs
}
59 changes: 59 additions & 0 deletions pkg/runtime/notifications_test.go
@@ -0,0 +1,59 @@
package runtime

import (
"fmt"
"reflect"
"testing"

"github.com/accurics/terrascan/pkg/notifications"
)

// MockNotifier mocks notifications.Notifier interface
type MockNotifier struct {
err error
}

var (
mockNotifierErr = fmt.Errorf("mock notification error")
)

func (m MockNotifier) Init(config interface{}) error {
return m.err
}

func (m MockNotifier) SendNotification(config interface{}) error {
return m.err
}

func TestSendNotifications(t *testing.T) {

table := []struct {
name string
executor Executor
wantErr error
}{
{
name: "no notifier error",
executor: Executor{
notifiers: []notifications.Notifier{&MockNotifier{err: nil}},
},
wantErr: nil,
},
{
name: "no notifier error",
executor: Executor{
notifiers: []notifications.Notifier{&MockNotifier{err: mockNotifierErr}},
},
wantErr: mockNotifierErr,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
gotErr := tt.executor.SendNotifications("some data")
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("incorrect error; got: '%v', want: '%v'", gotErr, tt.wantErr)
}
})
}
}
6 changes: 6 additions & 0 deletions pkg/runtime/testdata/invalid-notifier.toml
@@ -0,0 +1,6 @@
# terrascan configuration file

# notifications configuration
[notifications]
[notifications.invalid]
url = "https://httpbin.org/post"
6 changes: 6 additions & 0 deletions pkg/runtime/testdata/webhook.toml
@@ -0,0 +1,6 @@
# terrascan configuration file

# notifications configuration
[notifications]
[notifications.webhook]
url = "https://httpbin.org/post"

0 comments on commit c068097

Please sign in to comment.