Skip to content

Commit

Permalink
Add --pass-on-timeout to the scan command
Browse files Browse the repository at this point in the history
  • Loading branch information
viktigpetterr committed Apr 12, 2023
1 parent 3f45e91 commit 48a313a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/go-retryablehttp"
)

var NoResErr = errors.New("failed to get response")
var NoResErr = errors.New("failed to get response. Check out the Debricked status page: https://status.debricked.com/")

func get(uri string, debClient *DebClient, retry bool, format string) (*http.Response, error) {
request, err := newRequest("GET", *debClient.host+uri, debClient.jwtToken, format, nil)
Expand Down
15 changes: 13 additions & 2 deletions internal/client/testdata/deb_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type DebClientMock struct {
realDebClient *client.DebClient
responseQueue []MockResponse
responseUriQueue map[string][]MockResponse
serviceUp bool
}

func (mock *DebClientMock) SetServiceUp(serviceUp bool) {
mock.serviceUp = serviceUp
}

func NewDebClientMock() *DebClientMock {
Expand All @@ -22,13 +27,15 @@ func NewDebClientMock() *DebClientMock {
return &DebClientMock{
realDebClient: debClient,
responseQueue: []MockResponse{},
responseUriQueue: map[string][]MockResponse{}}
responseUriQueue: map[string][]MockResponse{},
serviceUp: true,
}
}

func (mock *DebClientMock) Get(uri string, format string) (*http.Response, error) {
response, err := mock.popResponse(mock.RemoveQueryParamsFromUri(uri))

if response != nil {
if response != nil || !mock.serviceUp {
return response, err
}

Expand Down Expand Up @@ -82,6 +89,10 @@ func (mock *DebClientMock) RemoveQueryParamsFromUri(uri string) string {
}

func (mock *DebClientMock) popResponse(uri string) (*http.Response, error) {
if !mock.serviceUp {
return nil, client.NoResErr
}

var responseMock MockResponse
uriQueue, existsInUriQueue := mock.responseUriQueue[uri]
existsInQueue := len(mock.responseQueue) != 0
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNewRootCmd(t *testing.T) {
}
}
assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag)
assert.Len(t, viperKeys, 12)
assert.Len(t, viperKeys, 13)
}

func TestPreRun(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var commitAuthor string
var repositoryUrl string
var integrationName string
var exclusions = file.DefaultExclusions()
var passOnDowntime bool

const (
RepositoryFlag = "repository"
Expand All @@ -30,6 +31,7 @@ const (
RepositoryUrlFlag = "repository-url"
IntegrationFlag = "integration"
ExclusionFlag = "exclusion"
PassOnTimeOut = "pass-on-timeout"
)

var scanCmdError error
Expand Down Expand Up @@ -82,12 +84,15 @@ Special Terms | Meaning
Examples:
$ debricked scan . `+exampleFlags)
cmd.Flags().BoolVarP(&passOnDowntime, PassOnTimeOut, "p", false, "pass scan if there is a service access timeout")

viper.MustBindEnv(RepositoryFlag)
viper.MustBindEnv(CommitFlag)
viper.MustBindEnv(BranchFlag)
viper.MustBindEnv(CommitAuthorFlag)
viper.MustBindEnv(RepositoryUrlFlag)
viper.MustBindEnv(IntegrationFlag)
viper.MustBindEnv(PassOnTimeOut)

return cmd
}
Expand All @@ -107,6 +112,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
CommitAuthor: viper.GetString(CommitAuthorFlag),
RepositoryUrl: viper.GetString(RepositoryUrlFlag),
IntegrationName: viper.GetString(IntegrationFlag),
PassOnTimeOut: viper.GetBool(PassOnTimeOut),
}
if s != nil {
scanCmdError = (*s).Scan(options)
Expand Down
13 changes: 12 additions & 1 deletion internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type DebrickedOptions struct {
CommitAuthor string
RepositoryUrl string
IntegrationName string
PassOnTimeOut bool
}

func NewDebrickedScanner(c *client.IDebClient, ciService ci.IService) (*DebrickedScanner, error) {
Expand Down Expand Up @@ -93,7 +94,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {

result, err := dScanner.scan(dOptions, *gitMetaObject)
if err != nil {
return err
return dScanner.handleScanError(err, dOptions.PassOnTimeOut)
}

if result == nil {
Expand Down Expand Up @@ -132,6 +133,16 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
return result, nil
}

func (dScanner *DebrickedScanner) handleScanError(err error, passOnTimeOut bool) error {
if err == client.NoResErr && passOnTimeOut {
fmt.Println(err)

return nil
}

return err
}

// SetWorkingDirectory sets working directory in accordance with the path option
func SetWorkingDirectory(d *DebrickedOptions) error {
absPath, _ := filepath.Abs(d.Path)
Expand Down
54 changes: 52 additions & 2 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ func TestScan(t *testing.T) {

path := testdataYarn
repositoryName := path
commitName := "testdata/yarn-commit"
cwd, _ := os.Getwd()
// reset working directory that has been manipulated in scanner.Scan
defer resetWd(t, cwd)
opts := DebrickedOptions{
Path: path,
Exclusions: nil,
RepositoryName: repositoryName,
CommitName: commitName,
CommitName: "commit",
BranchName: "",
CommitAuthor: "",
RepositoryUrl: "",
Expand Down Expand Up @@ -457,6 +456,57 @@ func TestSetWorkingDirectory(t *testing.T) {
}
}

func TestScanServiceDowntime(t *testing.T) {
var debClient client.IDebClient
clientMock := testdata.NewDebClientMock()
clientMock.SetServiceUp(false)
debClient = clientMock

var ciService ci.IService = ci.NewService(nil)

scanner, _ := NewDebrickedScanner(&debClient, ciService)

path := testdataYarn
repositoryName := path
commitName := "testdata/yarn-commit"
cwd, _ := os.Getwd()
// reset working directory that has been manipulated in scanner.Scan
defer resetWd(t, cwd)
opts := DebrickedOptions{
Path: path,
Exclusions: nil,
RepositoryName: repositoryName,
CommitName: commitName,
PassOnTimeOut: true,
}

rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := scanner.Scan(opts)

_ = w.Close()
output, _ := io.ReadAll(r)
os.Stdout = rescueStdout

if err != nil {
t.Error("failed to assert that scan ran without errors. Error:", err)
}
assert.Contains(t, string(output), client.NoResErr.Error())
resetWd(t, cwd)

opts.PassOnTimeOut = false
rescueStdout = os.Stdout
os.Stdout = w

err = scanner.Scan(opts)

_ = w.Close()
os.Stdout = rescueStdout
assert.ErrorIs(t, err, client.NoResErr)
}

func addMockedFormatsResponse(clientMock *testdata.DebClientMock) {
formats := []file.Format{{
Regex: "",
Expand Down

0 comments on commit 48a313a

Please sign in to comment.