From e57de8ab53584f98437012baa07c6926e85c4612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 25 Mar 2022 16:10:50 +0100 Subject: [PATCH] Manager: Tests, allow mocking requests that are not Worker-authenticated --- internal/manager/api_impl/jobs_test.go | 3 ++- internal/manager/api_impl/support_test.go | 7 +++---- internal/manager/api_impl/workers_test.go | 12 ++++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/manager/api_impl/jobs_test.go b/internal/manager/api_impl/jobs_test.go index 93e7edea..40c767b5 100644 --- a/internal/manager/api_impl/jobs_test.go +++ b/internal/manager/api_impl/jobs_test.go @@ -65,7 +65,8 @@ func TestTaskUpdate(t *testing.T) { mf.logStorage.EXPECT().Write(gomock.Any(), jobID, taskID, "line1\nline2\n") // Do the call. - echoCtx := mf.prepareMockedJSONRequest(&worker, taskUpdate) + echoCtx := mf.prepareMockedJSONRequest(taskUpdate) + requestWorkerStore(echoCtx, &worker) err := mf.flamenco.TaskUpdate(echoCtx, taskID) // Check the saved task. diff --git a/internal/manager/api_impl/support_test.go b/internal/manager/api_impl/support_test.go index eb928c12..d2b205de 100644 --- a/internal/manager/api_impl/support_test.go +++ b/internal/manager/api_impl/support_test.go @@ -48,26 +48,25 @@ func newMockedFlamenco(mockCtrl *gomock.Controller) mockedFlamenco { } // prepareMockedJSONRequest returns an `echo.Context` that has a JSON request body attached to it. -func (mf *mockedFlamenco) prepareMockedJSONRequest(worker *persistence.Worker, requestBody interface{}) echo.Context { +func (mf *mockedFlamenco) prepareMockedJSONRequest(requestBody interface{}) echo.Context { bodyBytes, err := json.MarshalIndent(requestBody, "", " ") if err != nil { panic(err) } - c := mf.prepareMockedRequest(worker, bytes.NewBuffer(bodyBytes)) + c := mf.prepareMockedRequest(bytes.NewBuffer(bodyBytes)) c.Request().Header.Add(echo.HeaderContentType, "application/json") return c } // prepareMockedJSONRequest returns an `echo.Context` that has an empty request body attached to it. -func (mf *mockedFlamenco) prepareMockedRequest(worker *persistence.Worker, body io.Reader) echo.Context { +func (mf *mockedFlamenco) prepareMockedRequest(body io.Reader) echo.Context { e := echo.New() req := httptest.NewRequest(http.MethodPost, "/", body) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - requestWorkerStore(c, worker) return c } diff --git a/internal/manager/api_impl/workers_test.go b/internal/manager/api_impl/workers_test.go index c45105dd..90d48540 100644 --- a/internal/manager/api_impl/workers_test.go +++ b/internal/manager/api_impl/workers_test.go @@ -23,7 +23,8 @@ func TestTaskScheduleHappy(t *testing.T) { mf := newMockedFlamenco(mockCtrl) worker := testWorker() - echo := mf.prepareMockedRequest(&worker, nil) + echo := mf.prepareMockedRequest(nil) + requestWorkerStore(echo, &worker) // Expect a call into the persistence layer, which should return a scheduled task. job := persistence.Job{ @@ -54,7 +55,8 @@ func TestTaskScheduleNonActiveStatus(t *testing.T) { // Explicitly NO expected calls to the persistence layer. Since the worker is // not in a state that allows task execution, there should be no DB queries. - echoCtx := mf.prepareMockedRequest(&worker, nil) + echoCtx := mf.prepareMockedRequest(nil) + requestWorkerStore(echoCtx, &worker) err := mf.flamenco.ScheduleTask(echoCtx) assert.NoError(t, err) @@ -73,7 +75,8 @@ func TestTaskScheduleOtherStatusRequested(t *testing.T) { // Explicitly NO expected calls to the persistence layer. Since the worker is // not in a state that allows task execution, there should be no DB queries. - echoCtx := mf.prepareMockedRequest(&worker, nil) + echoCtx := mf.prepareMockedRequest(nil) + requestWorkerStore(echoCtx, &worker) err := mf.flamenco.ScheduleTask(echoCtx) assert.NoError(t, err) @@ -112,7 +115,8 @@ func TestWorkerSignoffTaskRequeue(t *testing.T) { // Signing off should be handled completely, even when the HTTP connection // breaks. This means using a different context than the one passed by Echo. - echo := mf.prepareMockedRequest(&worker, nil) + echo := mf.prepareMockedRequest(nil) + requestWorkerStore(echo, &worker) expectCtx := gomock.Not(gomock.Eq(echo.Request().Context())) // Expect worker's tasks to be re-queued.