From 2f07b96333eb687e6f9228c9c3b968435ae94810 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sun, 19 Jan 2020 10:07:01 -0500 Subject: [PATCH] adding test, cancelfunc (for go vet) --- audit/audit_test.go | 20 +++++++++++++ audit/repo.go | 71 +++++++++++++++++++++++++-------------------- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/audit/audit_test.go b/audit/audit_test.go index 736cc36a..50727e2a 100644 --- a/audit/audit_test.go +++ b/audit/audit_test.go @@ -213,6 +213,26 @@ func TestAudit(t *testing.T) { }, wantEmpty: true, }, + { + description: "test local repo one aws leak timeout", + opts: options.Options{ + RepoPath: "../test_data/test_repos/test_repo_1", + Report: "../test_data/test_local_repo_one_aws_leak.json.got", + ReportFormat: "json", + Timeout: "10ns", + }, + wantEmpty: true, + }, + { + description: "test local repo one aws leak long timeout", + opts: options.Options{ + RepoPath: "../test_data/test_repos/test_repo_1", + Report: "../test_data/test_local_repo_one_aws_leak.json.got", + ReportFormat: "json", + Timeout: "2m", + }, + wantPath: "../test_data/test_local_repo_one_aws_leak.json", + }, } for _, test := range tests { diff --git a/audit/repo.go b/audit/repo.go index d84b033d..3f519af6 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -40,7 +40,9 @@ type Repo struct { // for those repo audits. config config.Config - ctx context.Context + // ctx is used to signal timeouts to running goroutines + ctx context.Context + cancel context.CancelFunc Name string Manager *manager.Manager @@ -51,6 +53,7 @@ func NewRepo(m *manager.Manager) *Repo { return &Repo{ Manager: m, config: m.Config, + ctx: context.Background(), } } @@ -96,6 +99,10 @@ func (repo *Repo) AuditUncommitted() error { repo.config = cfg } + if err := repo.setupTimeout(); err != nil { + return err + } + auditTimeStart := time.Now() r, err := repo.Head() @@ -203,37 +210,6 @@ func (repo *Repo) AuditUncommitted() error { return nil } -// timeoutReached returns true if the timeout deadline has been met. This function should be used -// at the top of loops and before potentially long running goroutines (like checking inefficient regexes) -func (repo *Repo) timeoutReached() bool { - if repo.ctx.Err() == context.DeadlineExceeded { - return true - } - return false -} - -// setupTimeout parses the --timeout option and assigns a context with timeout to the manager -// which will exit early if the timeout has been met. -func (repo *Repo) setupTimeout() error { - if repo.Manager.Opts.Timeout == "" { - return nil - } - timeout, err := time.ParseDuration(repo.Manager.Opts.Timeout) - if err != nil { - return err - } - - repo.ctx, _ = context.WithTimeout(context.Background(), timeout) - - go func() { - select { - case <-repo.ctx.Done(): - log.Warnf("Timeout deadline exceeded: %s", timeout.String()) - } - }() - return nil -} - // Audit is responsible for scanning the entire history (default behavior) of a // git repo. Options that can change the behavior of this function include: --commit, --depth, --branch. // See options/options.go for an explanation on these options. @@ -385,3 +361,34 @@ func (repo *Repo) loadRepoConfig() (config.Config, error) { _, err = toml.DecodeReader(f, &tomlLoader) return tomlLoader.Parse() } + +// timeoutReached returns true if the timeout deadline has been met. This function should be used +// at the top of loops and before potentially long running goroutines (like checking inefficient regexes) +func (repo *Repo) timeoutReached() bool { + if repo.ctx.Err() == context.DeadlineExceeded { + return true + } + return false +} + +// setupTimeout parses the --timeout option and assigns a context with timeout to the manager +// which will exit early if the timeout has been met. +func (repo *Repo) setupTimeout() error { + if repo.Manager.Opts.Timeout == "" { + return nil + } + timeout, err := time.ParseDuration(repo.Manager.Opts.Timeout) + if err != nil { + return err + } + + repo.ctx, repo.cancel = context.WithTimeout(context.Background(), timeout) + + go func() { + select { + case <-repo.ctx.Done(): + log.Warnf("Timeout deadline exceeded: %s", timeout.String()) + } + }() + return nil +}