-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests for runtime package and refactor accordingly
- Loading branch information
Yusuf Kanchwala
committed
Jul 26, 2020
1 parent
1fac82b
commit fb4e51a
Showing
5 changed files
with
369 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
cloudProvider "github.com/accurics/terrascan/pkg/cloud-providers" | ||
awsProvider "github.com/accurics/terrascan/pkg/cloud-providers/aws" | ||
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" | ||
) | ||
|
||
var ( | ||
errMockLoadIacDir = fmt.Errorf("mock LoadIacDir") | ||
errMockLoadIacFile = fmt.Errorf("mock LoadIacFile") | ||
errMockCreateNormalizedJSON = fmt.Errorf("mock CreateNormalizedJSON") | ||
) | ||
|
||
// MockIacProvider mocks IacProvider interface | ||
type MockIacProvider struct { | ||
output output.AllResourceConfigs | ||
err error | ||
} | ||
|
||
func (m MockIacProvider) LoadIacDir(dir string) (output.AllResourceConfigs, error) { | ||
return m.output, m.err | ||
} | ||
|
||
func (m MockIacProvider) LoadIacFile(file string) (output.AllResourceConfigs, error) { | ||
return m.output, m.err | ||
} | ||
|
||
// MockCloudProvider mocks CloudProvider interface | ||
type MockCloudProvider struct { | ||
err error | ||
} | ||
|
||
func (m MockCloudProvider) CreateNormalizedJSON(data output.AllResourceConfigs) (mockInterface interface{}, err error) { | ||
return data, m.err | ||
} | ||
|
||
func TestExecute(t *testing.T) { | ||
|
||
table := []struct { | ||
name string | ||
executor Executor | ||
wantErr error | ||
}{ | ||
{ | ||
name: "test LoadIacDir", | ||
executor: Executor{ | ||
dirPath: "./testdata/testdir", | ||
iacProvider: MockIacProvider{err: errMockLoadIacDir}, | ||
}, | ||
wantErr: errMockLoadIacDir, | ||
}, | ||
{ | ||
name: "test LoadIacFile", | ||
executor: Executor{ | ||
filePath: "./testdata/testfile", | ||
iacProvider: MockIacProvider{err: errMockLoadIacFile}, | ||
}, | ||
wantErr: errMockLoadIacFile, | ||
}, | ||
{ | ||
name: "test CreateNormalizedJSON error", | ||
executor: Executor{ | ||
filePath: "./testdata/testfile", | ||
iacProvider: MockIacProvider{err: nil}, | ||
cloudProvider: MockCloudProvider{err: errMockCreateNormalizedJSON}, | ||
}, | ||
wantErr: errMockCreateNormalizedJSON, | ||
}, | ||
{ | ||
name: "test CreateNormalizedJSON", | ||
executor: Executor{ | ||
filePath: "./testdata/testfile", | ||
iacProvider: MockIacProvider{err: nil}, | ||
cloudProvider: MockCloudProvider{err: nil}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotErr := tt.executor.Execute() | ||
if !reflect.DeepEqual(gotErr, tt.wantErr) { | ||
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestInit(t *testing.T) { | ||
|
||
table := []struct { | ||
name string | ||
executor Executor | ||
wantErr error | ||
wantIacProvider iacProvider.IacProvider | ||
wantCloudProvider cloudProvider.CloudProvider | ||
}{ | ||
{ | ||
name: "valid filePath", | ||
executor: Executor{ | ||
filePath: "./testdata/testfile", | ||
dirPath: "", | ||
cloudType: "aws", | ||
iacType: "terraform", | ||
iacVersion: "v12", | ||
}, | ||
wantErr: nil, | ||
wantIacProvider: &tfv12.TfV12{}, | ||
wantCloudProvider: &awsProvider.AWSProvider{}, | ||
}, | ||
} | ||
|
||
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) | ||
} | ||
if !reflect.DeepEqual(tt.executor.cloudProvider, tt.wantCloudProvider) { | ||
t.Errorf("got: '%v', want: '%v'", tt.executor.cloudProvider, tt.wantCloudProvider) | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/accurics/terrascan/pkg/utils" | ||
"go.uber.org/zap" | ||
|
||
CloudProvider "github.com/accurics/terrascan/pkg/cloud-providers" | ||
IacProvider "github.com/accurics/terrascan/pkg/iac-providers" | ||
) | ||
|
||
var ( | ||
errEmptyIacPath = fmt.Errorf("empty iac path, either use '-f' or '-d' option") | ||
errIncorrectIacPath = fmt.Errorf("cannot accept both '-f' and '-d' options together") | ||
errDirNotExists = fmt.Errorf("directory does not exist") | ||
errFileNotExists = fmt.Errorf("file does not exist") | ||
errIacNotSupported = fmt.Errorf("iac type or version not supported") | ||
errCloudNotSupported = fmt.Errorf("cloud type not supported") | ||
) | ||
|
||
// ValidateInputs validates the inputs to the executor object | ||
func (e *Executor) ValidateInputs() error { | ||
|
||
// terrascan can accept either a file or a directory | ||
if e.filePath == "" && e.dirPath == "" { | ||
zap.S().Errorf("no IaC path specified; use '-f' for file or '-d' for directory") | ||
return errEmptyIacPath | ||
} | ||
if e.filePath != "" && e.dirPath != "" { | ||
zap.S().Errorf("cannot accept both '-f %s' and '-d %s' options together", e.filePath, e.dirPath) | ||
return errIncorrectIacPath | ||
} | ||
|
||
if e.dirPath != "" { | ||
// if directory, check if directory exists | ||
absDirPath, err := utils.GetAbsPath(e.dirPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := os.Stat(absDirPath); err != nil { | ||
zap.S().Errorf("directory '%s' does not exist", absDirPath) | ||
return errDirNotExists | ||
} | ||
zap.S().Debugf("directory '%s' exists", absDirPath) | ||
} else { | ||
|
||
// if file path, check if file exists | ||
absFilePath, err := utils.GetAbsPath(e.filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := os.Stat(absFilePath); err != nil { | ||
zap.S().Errorf("file '%s' does not exist", absFilePath) | ||
return errFileNotExists | ||
} | ||
zap.S().Debugf("file '%s' exists", absFilePath) | ||
} | ||
|
||
// check if Iac type is supported | ||
if !IacProvider.IsIacSupported(e.iacType, e.iacVersion) { | ||
zap.S().Errorf("iac type '%s', version '%s' not supported", e.iacType, e.iacVersion) | ||
return errIacNotSupported | ||
} | ||
zap.S().Debugf("iac type '%s', version '%s' is supported", e.iacType, e.iacVersion) | ||
|
||
// check if cloud type is supported | ||
if !CloudProvider.IsCloudSupported(e.cloudType) { | ||
zap.S().Errorf("cloud type '%s' not supported", e.cloudType) | ||
return errCloudNotSupported | ||
} | ||
zap.S().Debugf("cloud type '%s' supported", e.cloudType) | ||
|
||
// check if policy type is supported | ||
|
||
// successful | ||
zap.S().Debug("input validation successful") | ||
return nil | ||
} |
Oops, something went wrong.