Skip to content

Commit

Permalink
Fixing tests in case Hub is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
tandr committed Sep 28, 2021
1 parent 1a5e924 commit 6dd99f1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/blackducksoftware/hub-client-go
go 1.17

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/h2non/gock v1.0.14
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.3.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect
)

replace github.com/h2non/gock => gopkg.in/h2non/gock.v1 v1.0.14
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -14,7 +15,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 h1:c20P3CcPbopVp2f7099WLOqSNKURf30Z0uq66HpijZY=
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/h2non/gock.v1 v1.0.14 h1:fTeu9fcUvSnLNacYvYI54h+1/XEteDyHvrVCZEEEYNM=
gopkg.in/h2non/gock.v1 v1.0.14/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
2 changes: 1 addition & 1 deletion hubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestCreateAndDeleteProject(t *testing.T) {
q := fmt.Sprintf("name:%s", projectName)
projectList, err := client.ListProjects(&hubapi.GetListOptions{Q: &q})
if err != nil {
t.Error(err)
t.Fatal(err)
}
projects := []hubapi.Project{}
for _, project := range projectList.Items {
Expand Down
7 changes: 5 additions & 2 deletions hubclient/component-client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/blackducksoftware/hub-client-go/hubapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClient_GetComponentVersion(t *testing.T) {
Expand All @@ -29,7 +30,8 @@ func TestClient_GetComponentVersion(t *testing.T) {
listOptions := &hubapi.GetListOptions{Q: &option}

componentList, err := client.ListComponents(listOptions)
assert.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, componentList)
assert.True(t, len(componentList.Items) > 0, "Expected at least one componentlist item")

item := componentList.Items[0]
Expand Down Expand Up @@ -59,7 +61,8 @@ func TestClient_GetComponentVersionFromVariant(t *testing.T) {
listOptions := &hubapi.GetListOptions{Q: &option}

componentList, err := client.ListComponents(listOptions)
assert.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, componentList)
assert.True(t, len(componentList.Items) > 0, "Expected at least one componentlist item")

componentVariant := componentList.Items[0]
Expand Down
3 changes: 2 additions & 1 deletion hubclient/currentuser-client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func createTestClient(t *testing.T) *Client {
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestClient_CurrentUser(t *testing.T) {

assert.NoError(t, err, "unable to get current user")

assert.NotNil(t, currentUser, "current user was nil")
require.NotNil(t, currentUser, "current user was nil")
assert.NotEmpty(t, currentUser.UserName, "username was empty")
assert.NotEmpty(t, currentUser.FirstName, "firstname was empty")
assert.NotEmpty(t, currentUser.LastName, "lastname was empty")
Expand Down
12 changes: 6 additions & 6 deletions hubclient/rapid-scans-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Client) StartRapidScan(bdioHeaderContent string) (error, string) {
bdioUploadEndpoint, err := c.HttpPostString(rapidScansURL, bdioHeaderContent, hubapi.ContentTypeRapidScanRequest, http.StatusCreated)

if err != nil {
log.Errorf("Error kicking off a rapid scan.", err)
log.Error("Error kicking off a rapid scan.", err)
return err, ""
}

Expand All @@ -51,15 +51,15 @@ func (c *Client) UploadBdioFiles(bdioUploadEndpoint string, bdioContents []strin
for _, bdioContent := range bdioContents {
err := c.HttpPutString(bdioUploadEndpoint, bdioContent, hubapi.ContentTypeRapidScanRequest, http.StatusAccepted)
if err != nil {
log.Errorf("Error uploading bdio files.", err)
log.Error("Error uploading bdio files.", err)
return err
}
}

c.SetHeaderValue(headerBdMode, bdModeFinish)
err := c.HttpPutString(bdioUploadEndpoint, "", hubapi.ContentTypeRapidScanRequest, http.StatusAccepted)
if err != nil {
log.Errorf("Error uploading bdio files.", err)
log.Error("Error uploading bdio files.", err)
return err
}

Expand All @@ -86,7 +86,7 @@ func (c *Client) PollRapidScanResults(rapidScanEndpoint string, interval, timeou
if err != nil {
ticker.Stop()
timeoutTimer.Stop()
log.Errorf("Error fetching rapid scan result", err)
log.Error("Error fetching rapid scan result", err)
return err, nil
}

Expand All @@ -107,7 +107,7 @@ func (c *Client) PollRapidScanResults(rapidScanEndpoint string, interval, timeou
err, statusCode := c.fetchResults(rapidScanEndpoint, offset, pageLimit, &body)

if err != nil {
log.Errorf("Error fetching rapid scan result", err)
log.Error("Error fetching rapid scan result", err)
return err, result
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func parseBody(body string) (error, *hubapi.RapidScanResult) {
err := json.Unmarshal([]byte(body), &pagedResult)

if err != nil {
log.Errorf("Error parsing rapid scan result", err)
log.Error("Error parsing rapid scan result", err)
return err, nil
}

Expand Down
2 changes: 2 additions & 0 deletions hubclient/vulnerability-client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/blackducksoftware/hub-client-go/hubapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClient_GetVulnerability(t *testing.T) {
Expand All @@ -31,6 +32,7 @@ func TestClient_GetVulnerability(t *testing.T) {
Href: url,
})
assert.NoError(t, err)
require.NotNil(t, vulnerability)

assert.Equal(t, "NVD", vulnerability.Source)
assert.Equal(t, "CVE-2014-0114", vulnerability.Name)
Expand Down

0 comments on commit 6dd99f1

Please sign in to comment.