Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
test: fix args
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed Oct 30, 2023
1 parent acfc507 commit 5255848
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 38 deletions.
18 changes: 9 additions & 9 deletions pkg/http/api/v1/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestRequestHandler_AcceptScanRequest(t *testing.T) {
name: "Should accept scan request",
enqueuerExpectation: &mock.Expectation{
Method: "Enqueue",
Args: []interface{}{validScanRequest},
Args: []interface{}{mock.Anything, validScanRequest},
ReturnArgs: []interface{}{job.ScanJob{ID: "job:123"}, nil},
},
requestBody: validScanRequestJSON,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestRequestHandler_AcceptScanRequest(t *testing.T) {
name: "Should respond with error 500 when enqueuing scan request fails",
enqueuerExpectation: &mock.Expectation{
Method: "Enqueue",
Args: []interface{}{validScanRequest},
Args: []interface{}{mock.Anything, validScanRequest},
ReturnArgs: []interface{}{job.ScanJob{}, errors.New("queue is down")},
},
requestBody: validScanRequestJSON,
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with error 500 when retrieving scan job fails",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{}, errors.New("data store is down")},
},
expectedStatus: http.StatusInternalServerError,
Expand All @@ -218,7 +218,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with error 404 when scan job cannot be found",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{(*job.ScanJob)(nil), nil},
},
expectedStatus: http.StatusNotFound,
Expand All @@ -233,7 +233,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with found status 302 when scan job is %s", job.Queued),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Queued,
Expand All @@ -245,7 +245,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with found status 302 when scan job is %s", job.Pending),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Pending,
Expand All @@ -257,7 +257,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with error 500 when scan job is %s", job.Failed),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Failed,
Expand All @@ -276,7 +276,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: fmt.Sprintf("Should respond with error 500 when scan job is NOT %s", job.Finished),
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: 666,
Expand All @@ -295,7 +295,7 @@ func TestRequestHandler_GetScanReport(t *testing.T) {
name: "Should respond with vulnerabilities report",
storeExpectation: &mock.Expectation{
Method: "Get",
Args: []interface{}{"job:123"},
Args: []interface{}{mock.Anything, "job:123"},
ReturnArgs: []interface{}{&job.ScanJob{
ID: "job:123",
Status: job.Finished,
Expand Down
5 changes: 3 additions & 2 deletions pkg/mock/enqueuer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/harbor"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/job"
"github.com/stretchr/testify/mock"
Expand All @@ -14,7 +15,7 @@ func NewEnqueuer() *Enqueuer {
return &Enqueuer{}
}

func (em *Enqueuer) Enqueue(request harbor.ScanRequest) (job.ScanJob, error) {
args := em.Called(request)
func (em *Enqueuer) Enqueue(ctx context.Context, request harbor.ScanRequest) (job.ScanJob, error) {
args := em.Called(ctx, request)
return args.Get(0).(job.ScanJob), args.Error(1)
}
3 changes: 3 additions & 0 deletions pkg/mock/expectation.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package mock

import (
"github.com/stretchr/testify/mock"
"testing"

"github.com/aquasecurity/harbor-scanner-trivy/pkg/trivy"
)

const Anything = mock.Anything

// Expectation represents an expectation of a method being called and its return values.
type Expectation struct {
Method string
Expand Down
17 changes: 9 additions & 8 deletions pkg/mock/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/harbor"
"github.com/aquasecurity/harbor-scanner-trivy/pkg/job"
"github.com/stretchr/testify/mock"
Expand All @@ -14,22 +15,22 @@ func NewStore() *Store {
return &Store{}
}

func (s *Store) Create(scanJob job.ScanJob) error {
args := s.Called(scanJob)
func (s *Store) Create(ctx context.Context, scanJob job.ScanJob) error {
args := s.Called(ctx, scanJob)
return args.Error(0)
}

func (s *Store) Get(scanJobID string) (*job.ScanJob, error) {
args := s.Called(scanJobID)
func (s *Store) Get(ctx context.Context, scanJobID string) (*job.ScanJob, error) {
args := s.Called(ctx, scanJobID)
return args.Get(0).(*job.ScanJob), args.Error(1)
}

func (s *Store) UpdateStatus(scanJobID string, newStatus job.ScanJobStatus, error ...string) error {
args := s.Called(scanJobID, newStatus, error)
func (s *Store) UpdateStatus(ctx context.Context, scanJobID string, newStatus job.ScanJobStatus, error ...string) error {
args := s.Called(ctx, scanJobID, newStatus, error)
return args.Error(0)
}

func (s *Store) UpdateReport(scanJobID string, report harbor.ScanReport) error {
args := s.Called(scanJobID, report)
func (s *Store) UpdateReport(ctx context.Context, scanJobID string, report harbor.ScanReport) error {
args := s.Called(ctx, scanJobID, report)
return args.Error(0)
}
16 changes: 9 additions & 7 deletions pkg/scan/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scan

import (
"context"
"fmt"
"testing"

Expand All @@ -12,7 +13,8 @@ import (
"golang.org/x/xerrors"
)

func TestController_Scan(t *testing.T) {
func TestContoller_Scan(t *testing.T) {
ctx := context.Background()
artifact := harbor.Artifact{
Repository: "library/mongo",
Digest: "sha256:917f5b7f4bef1b35ee90f03033f33a81002511c1e0767fd44276d4bd9cd2fa8e",
Expand Down Expand Up @@ -44,17 +46,17 @@ func TestController_Scan(t *testing.T) {
storeExpectation: []*mock.Expectation{
{
Method: "UpdateStatus",
Args: []interface{}{"job:123", job.Pending, []string(nil)},
Args: []interface{}{ctx, "job:123", job.Pending, []string(nil)},
ReturnArgs: []interface{}{nil},
},
{
Method: "UpdateReport",
Args: []interface{}{"job:123", harborReport},
Args: []interface{}{ctx, "job:123", harborReport},
ReturnArgs: []interface{}{nil},
},
{
Method: "UpdateStatus",
Args: []interface{}{"job:123", job.Finished, []string(nil)},
Args: []interface{}{ctx, "job:123", job.Finished, []string(nil)},
ReturnArgs: []interface{}{nil},
},
},
Expand Down Expand Up @@ -96,12 +98,12 @@ func TestController_Scan(t *testing.T) {
storeExpectation: []*mock.Expectation{
{
Method: "UpdateStatus",
Args: []interface{}{"job:123", job.Pending, []string(nil)},
Args: []interface{}{ctx, "job:123", job.Pending, []string(nil)},
ReturnArgs: []interface{}{nil},
},
{
Method: "UpdateStatus",
Args: []interface{}{"job:123", job.Failed, []string{"running trivy wrapper: out of memory"}},
Args: []interface{}{ctx, "job:123", job.Failed, []string{"running trivy wrapper: out of memory"}},
ReturnArgs: []interface{}{nil},
},
},
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestController_Scan(t *testing.T) {
mock.ApplyExpectations(t, wrapper, tc.wrapperExpectation)
mock.ApplyExpectations(t, transformer, tc.transformerExpectation)

err := NewController(store, wrapper, transformer).Scan(tc.scanJobID, tc.scanRequest)
err := NewController(store, wrapper, transformer).Scan(ctx, tc.scanJobID, tc.scanRequest)
assert.Equal(t, tc.expectedError, err)

store.AssertExpectations(t)
Expand Down
8 changes: 4 additions & 4 deletions test/integration/api/rest_api_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build integration
// +build integration

package api

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestRestApi(t *testing.T) {

t.Run("POST /api/v1/scan", func(t *testing.T) {
// given
enqueuer.On("Enqueue", harbor.ScanRequest{
enqueuer.On("Enqueue", mock.Anything, harbor.ScanRequest{
Registry: harbor.Registry{
URL: "https://core.harbor.domain",
Authorization: "Bearer JWTTOKENGOESHERE",
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestRestApi(t *testing.T) {
assert.Equal(t, http.StatusAccepted, rs.StatusCode)
assert.Equal(t, "application/vnd.scanner.adapter.scan.response+json; version=1.0", rs.Header.Get("Content-Type"))

bodyBytes, err := ioutil.ReadAll(rs.Body)
bodyBytes, err := io.ReadAll(rs.Body)
require.NoError(t, err)

assert.JSONEq(t, `{"id": "job:123"}`, string(bodyBytes))
Expand All @@ -95,7 +95,7 @@ func TestRestApi(t *testing.T) {
// given
now := time.Now()

store.On("Get", "job:123").Return(&job.ScanJob{
store.On("Get", mock.Anything, "job:123").Return(&job.ScanJob{
ID: "job:123",
Status: job.Finished,
Report: harbor.ScanReport{
Expand Down
16 changes: 8 additions & 8 deletions test/integration/persistence/redis/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ func TestStore(t *testing.T) {
t.Run("CRUD", func(t *testing.T) {
scanJobID := "123"

err := store.Create(job.ScanJob{
err := store.Create(ctx, job.ScanJob{
ID: scanJobID,
Status: job.Queued,
})
require.NoError(t, err, "saving scan job should not fail")

j, err := store.Get(scanJobID)
j, err := store.Get(ctx, scanJobID)
require.NoError(t, err, "getting scan job should not fail")
assert.Equal(t, &job.ScanJob{
ID: scanJobID,
Status: job.Queued,
}, j)

err = store.UpdateStatus(scanJobID, job.Pending)
err = store.UpdateStatus(ctx, scanJobID, job.Pending)
require.NoError(t, err, "updating scan job status should not fail")

j, err = store.Get(scanJobID)
j, err = store.Get(ctx, scanJobID)
require.NoError(t, err, "getting scan job should not fail")
assert.Equal(t, &job.ScanJob{
ID: scanJobID,
Expand All @@ -89,20 +89,20 @@ func TestStore(t *testing.T) {
},
}

err = store.UpdateReport(scanJobID, scanReport)
err = store.UpdateReport(ctx, scanJobID, scanReport)
require.NoError(t, err, "updating scan job reports should not fail")

j, err = store.Get(scanJobID)
j, err = store.Get(ctx, scanJobID)
require.NoError(t, err, "retrieving scan job should not fail")
require.NotNil(t, j, "retrieved scan job must not be nil")
assert.Equal(t, scanReport, j.Report)

err = store.UpdateStatus(scanJobID, job.Finished)
err = store.UpdateStatus(ctx, scanJobID, job.Finished)
require.NoError(t, err)

time.Sleep(parseDuration(t, "12s"))

j, err = store.Get(scanJobID)
j, err = store.Get(ctx, scanJobID)
require.NoError(t, err, "retrieve scan job should not fail")
require.Nil(t, j, "retrieved scan job should be nil, i.e. expired")
})
Expand Down

0 comments on commit 5255848

Please sign in to comment.