From 209fb65a270a80900f54322eceb162515613bf46 Mon Sep 17 00:00:00 2001 From: Pankaj Patil Date: Wed, 16 Dec 2020 15:09:06 +0530 Subject: [PATCH] run refactor and tests --- pkg/cli/run.go | 51 +++++++++++++------ pkg/cli/run_test.go | 119 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 16 deletions(-) diff --git a/pkg/cli/run.go b/pkg/cli/run.go index 8c12d8374..b0918f068 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -17,6 +17,7 @@ package cli import ( + "errors" "flag" "os" "path/filepath" @@ -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 } @@ -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 @@ -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 } diff --git a/pkg/cli/run_test.go b/pkg/cli/run_test.go index f2d0f28a5..589f2d453 100644 --- a/pkg/cli/run_test.go +++ b/pkg/cli/run_test.go @@ -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) { @@ -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) + } + }) + } +}