Skip to content

Commit

Permalink
run refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Dec 16, 2020
1 parent 49562fe commit 209fb65
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 16 deletions.
51 changes: 35 additions & 16 deletions pkg/cli/run.go
Expand Up @@ -17,6 +17,7 @@
package cli

import (
"errors"
"flag"
"os"
"path/filepath"
Expand All @@ -43,16 +44,12 @@ func Run(iacType, iacVersion string, cloudType []string,
defer os.RemoveAll(tempDir)

// download remote repository
d := downloader.NewDownloader()
path, err := d.DownloadWithType(remoteType, remoteURL, tempDir)
if err == downloader.ErrEmptyURLType {
// url and type empty, proceed with regular scanning
zap.S().Debugf("remote url and type not configured, proceeding with regular scanning")
} else if err != nil {
// some error while downloading remote repository
path, err := downloadRemoteRepository(remoteType, remoteURL, tempDir)
if err != nil {
return
} else {
// successfully downloaded remote repository
}

if path != "" {
iacDirPath = path
}

Expand All @@ -69,6 +66,33 @@ func Run(iacType, iacVersion string, cloudType []string,
return
}

// write results to console
err = writeResults(results, useColors, verbose, configOnly, humanOutputFormat)
if err != nil {
zap.S().Error("failed to write results", zap.Error(err))
return
}

if results.Violations.ViolationStore.Summary.ViolatedPolicies != 0 && flag.Lookup("test.v") == nil {
os.RemoveAll(tempDir)
os.Exit(3)
}
}

func downloadRemoteRepository(remoteType, remoteURL, tempDir string) (string, error) {
d := downloader.NewDownloader()
path, err := d.DownloadWithType(remoteType, remoteURL, tempDir)
if err == downloader.ErrEmptyURLType {
// url and type empty, proceed with regular scanning
zap.S().Debugf("remote url and type not configured, proceeding with regular scanning")
} else if err != nil {
// some error while downloading remote repository
return path, err
}
return path, nil
}

func writeResults(results runtime.Output, useColors, verbose, configOnly bool, format string) error {
// add verbose flag to the scan summary
results.Violations.ViolationStore.Summary.ShowViolationDetails = verbose

Expand All @@ -79,16 +103,11 @@ func Run(iacType, iacVersion string, cloudType []string,
// if --config-only flag is set, then exit with an error
// asking the user to use yaml or json output format
if strings.EqualFold(format, humanOutputFormat) {
zap.S().Error("please use yaml or json output format when using --config-only flag")
return
return errors.New("please use yaml or json output format when using --config-only flag")
}
writer.Write(format, results.ResourceConfig, outputWriter)
} else {
writer.Write(format, results.Violations, outputWriter)
}

if results.Violations.ViolationStore.Summary.ViolatedPolicies != 0 && flag.Lookup("test.v") == nil {
os.RemoveAll(tempDir)
os.Exit(3)
}
return nil
}
119 changes: 119 additions & 0 deletions pkg/cli/run_test.go
Expand Up @@ -17,7 +17,15 @@
package cli

import (
"os"
"path/filepath"
"testing"

"github.com/accurics/terrascan/pkg/iac-providers/output"
"github.com/accurics/terrascan/pkg/policy"
"github.com/accurics/terrascan/pkg/results"
"github.com/accurics/terrascan/pkg/runtime"
"github.com/accurics/terrascan/pkg/utils"
)

func TestRun(t *testing.T) {
Expand Down Expand Up @@ -79,3 +87,114 @@ func TestRun(t *testing.T) {
})
}
}

func TestWriteResults(t *testing.T) {
testInput := runtime.Output{
ResourceConfig: output.AllResourceConfigs{},
Violations: policy.EngineOutput{
ViolationStore: &results.ViolationStore{},
},
}
type args struct {
results runtime.Output
useColors bool
verbose bool
configOnly bool
format string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "config only true with human readable output format",
args: args{
results: testInput,
configOnly: true,
format: "human",
},
wantErr: true,
},
{
name: "config only true with non human readable output format",
args: args{
results: testInput,
configOnly: true,
format: "json",
},
wantErr: false,
},
{
name: "config only false",
args: args{
results: testInput,
configOnly: false,
format: "human",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := writeResults(tt.args.results, tt.args.useColors, tt.args.verbose, tt.args.configOnly, tt.args.format); (err != nil) != tt.wantErr {
t.Errorf("writeResults() error = gotErr: %v, wantErr: %v", err, tt.wantErr)
}
})
}
}

func TestDownloadRemoteRepository(t *testing.T) {
testTempdir := filepath.Join(os.TempDir(), utils.GenRandomString(6))

type args struct {
remoteType string
remoteURL string
tempDir string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "blank input paramters",
args: args{
remoteType: "",
remoteURL: "",
tempDir: "",
},
},
{
name: "invalid input parameters",
args: args{
remoteType: "test",
remoteURL: "test",
tempDir: "test",
},
wantErr: true,
},
{
name: "valid inputs paramters",
args: args{
remoteType: "git",
remoteURL: "github.com/accurics/terrascan",
tempDir: testTempdir,
},
want: testTempdir,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := downloadRemoteRepository(tt.args.remoteType, tt.args.remoteURL, tt.args.tempDir)
if (err != nil) != tt.wantErr {
t.Errorf("downloadRemoteRepository() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("downloadRemoteRepository() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 209fb65

Please sign in to comment.