Skip to content

Commit

Permalink
Merge pull request #258 from accurics/terrascan-v1.0-test-codecov-pr-…
Browse files Browse the repository at this point in the history
…comments

add unit tests for cloud-provider package
  • Loading branch information
kanchwala-yusuf committed Jul 27, 2020
2 parents 60d243d + a705368 commit 98d2a60
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/cloud-providers/cloud-provider.go
Expand Up @@ -7,14 +7,18 @@ import (
"go.uber.org/zap"
)

var (
errCloudNotSupported = fmt.Errorf("cloud type not supported")
)

// NewCloudProvider returns a new CloudProvider
func NewCloudProvider(cloudType string) (cloudProvider CloudProvider, err error) {

// get CloudProvider from supportedCloudProviders
cloudProviderObject, supported := supportedCloudProviders[supportedCloudType(cloudType)]
if !supported {
zap.S().Errorf("cloud type '%s' not supported", cloudType)
return cloudProvider, fmt.Errorf("cloud type not supported")
return cloudProvider, errCloudNotSupported
}

return reflect.New(cloudProviderObject).Interface().(CloudProvider), nil
Expand Down
72 changes: 72 additions & 0 deletions pkg/cloud-providers/providers_test.go
@@ -0,0 +1,72 @@
package cloudprovider

import (
"reflect"
"testing"

awsProvider "github.com/accurics/terrascan/pkg/cloud-providers/aws"
)

func TestNewCloudProvider(t *testing.T) {

table := []struct {
name string
cloudType supportedCloudType
want CloudProvider
wantErr error
}{
{
name: "aws provider",
cloudType: aws,
want: &awsProvider.AWSProvider{},
wantErr: nil,
},
{
name: "not supported cloud type",
cloudType: "not-supported",
want: nil,
wantErr: errCloudNotSupported,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
got, gotErr := NewCloudProvider(string(tt.cloudType))
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got: '%v', want: '%v'", got, tt.want)
}
})
}
}

func TestIsCloudSupported(t *testing.T) {

table := []struct {
name string
cloudType supportedCloudType
want bool
}{
{
name: "aws provider",
cloudType: aws,
want: true,
},
{
name: "not supported cloud type",
cloudType: "not-supported",
want: false,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
got := IsCloudSupported(string(tt.cloudType))
if got != tt.want {
t.Errorf("got: '%v', want: '%v'", got, tt.want)
}
})
}
}

0 comments on commit 98d2a60

Please sign in to comment.