269 lines
7.4 KiB
Go
269 lines
7.4 KiB
Go
/*
|
|
* Copyright 2024 The Dragonfly Authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"d7y.io/dragonfly/v2/manager/models"
|
|
"d7y.io/dragonfly/v2/manager/service/mocks"
|
|
"d7y.io/dragonfly/v2/manager/types"
|
|
)
|
|
|
|
var (
|
|
mockModelReqBody = `
|
|
{
|
|
"bio": "bio",
|
|
"state": "active"
|
|
}`
|
|
mockUpdateModelRequest = types.UpdateModelRequest{
|
|
BIO: "bio",
|
|
State: "active",
|
|
}
|
|
mockModel = &models.Model{
|
|
BaseModel: mockBaseModel,
|
|
Name: "name",
|
|
Type: "type",
|
|
BIO: "bio",
|
|
Version: "version",
|
|
State: "state",
|
|
Evaluation: nil,
|
|
SchedulerID: 8,
|
|
Scheduler: models.Scheduler{},
|
|
}
|
|
)
|
|
|
|
func mockModelRouter(h *Handlers) *gin.Engine {
|
|
r := gin.Default()
|
|
apiv1 := r.Group("/api/v1")
|
|
model := apiv1.Group("/models")
|
|
model.DELETE(":id", h.DestroyModel)
|
|
model.PATCH(":id", h.UpdateModel)
|
|
model.GET(":id", h.GetModel)
|
|
model.GET("", h.GetModels)
|
|
return r
|
|
}
|
|
|
|
func TestHandlers_DestroyModel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
mock func(ms *mocks.MockServiceMockRecorder)
|
|
expect func(t *testing.T, w *httptest.ResponseRecorder)
|
|
}{
|
|
{
|
|
name: "unprocessable entity",
|
|
req: httptest.NewRequest(http.MethodDelete, "/api/v1/models/test", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
req: httptest.NewRequest(http.MethodDelete, "/api/v1/models/2", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {
|
|
ms.DestroyModel(gomock.Any(), gomock.Eq(uint(2))).Return(nil).Times(1)
|
|
},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
defer ctl.Finish()
|
|
svc := mocks.NewMockService(ctl)
|
|
w := httptest.NewRecorder()
|
|
h := New(svc)
|
|
mockRouter := mockModelRouter(h)
|
|
|
|
tc.mock(svc.EXPECT())
|
|
mockRouter.ServeHTTP(w, tc.req)
|
|
tc.expect(t, w)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandlers_UpdateModel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
mock func(ms *mocks.MockServiceMockRecorder)
|
|
expect func(t *testing.T, w *httptest.ResponseRecorder)
|
|
}{
|
|
{
|
|
name: "unprocessable entity caused by uri",
|
|
req: httptest.NewRequest(http.MethodPatch, "/api/v1/models/test", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
},
|
|
},
|
|
{
|
|
name: "unprocessable entity caused by body",
|
|
req: httptest.NewRequest(http.MethodPatch, "/api/v1/models/2", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
req: httptest.NewRequest(http.MethodPatch, "/api/v1/models/2", strings.NewReader(mockModelReqBody)),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {
|
|
ms.UpdateModel(gomock.Any(), gomock.Eq(uint(2)), gomock.Eq(mockUpdateModelRequest)).Return(mockModel, nil).Times(1)
|
|
},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
model := models.Model{}
|
|
err := json.Unmarshal(w.Body.Bytes(), &model)
|
|
assert.NoError(err)
|
|
assert.Equal(mockModel, &model)
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
defer ctl.Finish()
|
|
svc := mocks.NewMockService(ctl)
|
|
w := httptest.NewRecorder()
|
|
h := New(svc)
|
|
mockRouter := mockModelRouter(h)
|
|
|
|
tc.mock(svc.EXPECT())
|
|
mockRouter.ServeHTTP(w, tc.req)
|
|
tc.expect(t, w)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandlers_GetModel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
mock func(ms *mocks.MockServiceMockRecorder)
|
|
expect func(t *testing.T, w *httptest.ResponseRecorder)
|
|
}{
|
|
{
|
|
name: "unprocessable entity",
|
|
req: httptest.NewRequest(http.MethodGet, "/api/v1/models/test", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
req: httptest.NewRequest(http.MethodGet, "/api/v1/models/2", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {
|
|
ms.GetModel(gomock.Any(), gomock.Eq(uint(2))).Return(mockModel, nil).Times(1)
|
|
},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
model := models.Model{}
|
|
err := json.Unmarshal(w.Body.Bytes(), &model)
|
|
assert.NoError(err)
|
|
assert.Equal(mockModel, &model)
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
defer ctl.Finish()
|
|
svc := mocks.NewMockService(ctl)
|
|
w := httptest.NewRecorder()
|
|
h := New(svc)
|
|
mockRouter := mockModelRouter(h)
|
|
|
|
tc.mock(svc.EXPECT())
|
|
mockRouter.ServeHTTP(w, tc.req)
|
|
tc.expect(t, w)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandlers_GetModels(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
mock func(ms *mocks.MockServiceMockRecorder)
|
|
expect func(t *testing.T, w *httptest.ResponseRecorder)
|
|
}{
|
|
{
|
|
name: "unprocessable entity",
|
|
req: httptest.NewRequest(http.MethodGet, "/api/v1/models?page=-1", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusUnprocessableEntity, w.Code)
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
req: httptest.NewRequest(http.MethodGet, "/api/v1/models?name=foo", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {
|
|
ms.GetModels(gomock.Any(), gomock.Eq(types.GetModelsQuery{
|
|
Name: "",
|
|
Page: 1,
|
|
PerPage: 10,
|
|
})).Return([]models.Model{*mockModel}, int64(1), nil).Times(1)
|
|
},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
model := models.Model{}
|
|
err := json.Unmarshal(w.Body.Bytes()[1:w.Body.Len()-1], &model)
|
|
assert.NoError(err)
|
|
assert.Equal(mockModel, &model)
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
defer ctl.Finish()
|
|
svc := mocks.NewMockService(ctl)
|
|
w := httptest.NewRecorder()
|
|
h := New(svc)
|
|
mockRouter := mockModelRouter(h)
|
|
|
|
tc.mock(svc.EXPECT())
|
|
mockRouter.ServeHTTP(w, tc.req)
|
|
tc.expect(t, w)
|
|
})
|
|
}
|
|
}
|