Skip to content

Commit

Permalink
add unit test for notifications package
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Aug 10, 2020
1 parent c068097 commit 9007143
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hashicorp/hcl/v2 v2.3.0
github.com/hashicorp/terraform v0.12.28
github.com/pelletier/go-toml v1.8.0
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.3.2
github.com/zclconf/go-cty v1.2.1
go.uber.org/zap v1.9.1
Expand Down
17 changes: 14 additions & 3 deletions pkg/notifications/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"reflect"

"github.com/accurics/terrascan/pkg/utils"
"github.com/pelletier/go-toml"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -87,24 +88,34 @@ func NewNotifiers(configFile string) ([]Notifier, error) {
notifierTypes := keyTomlConfig.Keys()

// create notifiers
var allErrs error
for _, nType := range notifierTypes {

if !IsNotifierSupported(nType) {
zap.S().Errorf("notifier type '%s' not supported", nType)
allErrs = utils.WrapError(errNotifierNotSupported, allErrs)
continue
}

// check if toml config present for notifier type
nTypeConfig := keyTomlConfig.Get(nType)
if nTypeConfig == nil {
if nTypeConfig.(*toml.Tree).String() == "" {
zap.S().Errorf("notifier '%v' config not present", nType)
return notifiers, errTomlKeyNotPresent
allErrs = utils.WrapError(errTomlKeyNotPresent, allErrs)
continue
}

// create a new notifier
n, err := NewNotifier(nType)
if err != nil {
allErrs = utils.WrapError(err, allErrs)
continue
}

// populate data
err = n.Init(nTypeConfig)
if err != nil {
allErrs = utils.WrapError(err, allErrs)
continue
}

Expand All @@ -113,7 +124,7 @@ func NewNotifiers(configFile string) ([]Notifier, error) {
}

// return list of notifiers
return notifiers, nil
return notifiers, allErrs
}

// IsNotifierSupported returns true/false depending on whether the notifier
Expand Down
91 changes: 91 additions & 0 deletions pkg/notifications/notifiers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package notifications

import (
"reflect"
"testing"

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

func TestNewNotifier(t *testing.T) {

table := []struct {
name string
nType string
wantType Notifier
wantErr error
}{
{
name: "valid notifier",
nType: "webhook",
wantType: &webhook.Webhook{},
wantErr: nil,
},
{
name: "invalid notifier",
nType: "notthere",
wantErr: errNotifierNotSupported,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
gotType, gotErr := NewNotifier(tt.nType)
if !reflect.DeepEqual(gotType, tt.wantType) {
t.Errorf("got: '%v', want: '%v'", gotType, tt.wantType)
}
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("incorrect error; got: '%v', want: '%v'", gotErr, tt.wantErr)
}
})
}
}

func TestNewNotifiers(t *testing.T) {

table := []struct {
name string
configFile string
wantErr error
}{
{
name: "config not present",
configFile: "notthere",
wantErr: errNotPresent,
},
{
name: "invalid toml",
configFile: "testdata/invalid.toml",
wantErr: errTomlLoadConfig,
},
{
name: "key not present",
configFile: "testdata/nokey.toml",
wantErr: errTomlKeyNotPresent,
},
{
name: "invalid notifier",
configFile: "testdata/invalid-notifier-type.toml",
wantErr: errNotifierNotSupported,
},
{
name: "empty notifier config",
configFile: "testdata/empty-notifier-config.toml",
wantErr: errTomlKeyNotPresent,
},
{
name: "invalid notifier config",
configFile: "testdata/invalid-notifier-config.toml",
wantErr: nil,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
_, gotErr := NewNotifiers(tt.configFile)
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("incorrect error; got: '%v', want: '%v'", gotErr, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/notifications/testdata/empty-notifier-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[notifications]
[notifications.webhook]
4 changes: 4 additions & 0 deletions pkg/notifications/testdata/invalid-notifier-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[notifications]
[notifications.webhook]
key1 = "val1"
key2 = "val2"
6 changes: 6 additions & 0 deletions pkg/notifications/testdata/invalid-notifier-type.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# terrascan configuration file

# notifications configuration
[notifications]
[notifications.invalid]
url = "https://httpbin.org/post"
1 change: 1 addition & 0 deletions pkg/notifications/testdata/invalid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am an invalid toml
2 changes: 2 additions & 0 deletions pkg/notifications/testdata/nokey.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[somefield]
somekey = "somevalue"
14 changes: 14 additions & 0 deletions pkg/runtime/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ func TestInit(t *testing.T) {
wantIacProvider: &tfv12.TfV12{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "invalid notifier",
executor: Executor{
filePath: "./testdata/testfile",
dirPath: "",
cloudType: "aws",
iacType: "terraform",
iacVersion: "v12",
configFile: "testdata/invalid-notifier.toml",
},
wantErr: fmt.Errorf("notifier not supported"),
wantIacProvider: &tfv12.TfV12{},
wantNotifiers: []notifications.Notifier{&webhook.Webhook{}},
},
{
name: "config not present",
executor: Executor{
Expand Down

0 comments on commit 9007143

Please sign in to comment.