From 5255848234d1e7dcf0ac21f2d4c4cb979a5e831b Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Mon, 30 Oct 2023 14:58:50 +0900 Subject: [PATCH] test: fix args Signed-off-by: knqyf263 --- pkg/http/api/v1/handler_test.go | 18 +++++++++--------- pkg/mock/enqueuer.go | 5 +++-- pkg/mock/expectation.go | 3 +++ pkg/mock/store.go | 17 +++++++++-------- pkg/scan/controller_test.go | 16 +++++++++------- test/integration/api/rest_api_test.go | 8 ++++---- .../persistence/redis/store_test.go | 16 ++++++++-------- 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/pkg/http/api/v1/handler_test.go b/pkg/http/api/v1/handler_test.go index 32c49aee..3d8aedd8 100644 --- a/pkg/http/api/v1/handler_test.go +++ b/pkg/http/api/v1/handler_test.go @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/pkg/mock/enqueuer.go b/pkg/mock/enqueuer.go index 952f0baf..612f4a1a 100644 --- a/pkg/mock/enqueuer.go +++ b/pkg/mock/enqueuer.go @@ -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" @@ -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) } diff --git a/pkg/mock/expectation.go b/pkg/mock/expectation.go index f7e27ae0..d00dec37 100644 --- a/pkg/mock/expectation.go +++ b/pkg/mock/expectation.go @@ -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 diff --git a/pkg/mock/store.go b/pkg/mock/store.go index 180a302d..8b0fc271 100644 --- a/pkg/mock/store.go +++ b/pkg/mock/store.go @@ -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" @@ -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) } diff --git a/pkg/scan/controller_test.go b/pkg/scan/controller_test.go index ed9de751..a3e5616c 100644 --- a/pkg/scan/controller_test.go +++ b/pkg/scan/controller_test.go @@ -1,6 +1,7 @@ package scan import ( + "context" "fmt" "testing" @@ -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", @@ -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}, }, }, @@ -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}, }, }, @@ -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) diff --git a/test/integration/api/rest_api_test.go b/test/integration/api/rest_api_test.go index 7fbfa2d0..02e87f86 100644 --- a/test/integration/api/rest_api_test.go +++ b/test/integration/api/rest_api_test.go @@ -1,10 +1,10 @@ //go:build integration -// +build integration package api import ( "fmt" + "io" "io/ioutil" "net/http" "net/http/httptest" @@ -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", @@ -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)) @@ -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{ diff --git a/test/integration/persistence/redis/store_test.go b/test/integration/persistence/redis/store_test.go index 16d89ec0..610ceec9 100644 --- a/test/integration/persistence/redis/store_test.go +++ b/test/integration/persistence/redis/store_test.go @@ -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, @@ -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") })