Skip to content

Commit 1edd631

Browse files
authored
feat: add unit tests for persistent cache task handlers (#4023)
Signed-off-by: BruceAko <chongzhi@hust.edu.cn>
1 parent f45351f commit 1edd631

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
* Copyright 2024 The Dragonfly Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package handlers
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"net/http"
23+
"net/http/httptest"
24+
"testing"
25+
"time"
26+
27+
"github.com/gin-gonic/gin"
28+
"github.com/stretchr/testify/assert"
29+
"go.uber.org/mock/gomock"
30+
31+
"d7y.io/dragonfly/v2/manager/service/mocks"
32+
"d7y.io/dragonfly/v2/manager/types"
33+
)
34+
35+
var (
36+
mockPersistentCacheTaskID = "task-id-123"
37+
mockSchedulerClusterID uint = 1
38+
mockUnprocessableSchedulerQuery = ""
39+
mockDestroyPersistentCacheQuery = fmt.Sprintf("scheduler_cluster_id=%d", mockSchedulerClusterID)
40+
mockGetPersistentCacheQuery = fmt.Sprintf("scheduler_cluster_id=%d", mockSchedulerClusterID)
41+
mockGetPersistentCachesQuery = fmt.Sprintf("scheduler_cluster_id=%d", mockSchedulerClusterID)
42+
mockGetPersistentCachesQueryWithPage = fmt.Sprintf("scheduler_cluster_id=%d&page=1&per_page=10", mockSchedulerClusterID)
43+
mockPersistentCacheTask = types.PersistentCacheTask{
44+
ID: mockPersistentCacheTaskID,
45+
PersistentReplicaCount: 3,
46+
Tag: "v1.0.0",
47+
Application: "app1",
48+
PieceLength: 1024,
49+
ContentLength: 4096,
50+
TotalPieceCount: 4,
51+
State: "Success",
52+
TTL: time.Hour * 24,
53+
CreatedAt: time.Now(),
54+
UpdatedAt: time.Now(),
55+
}
56+
mockPersistentCacheTasks = []types.PersistentCacheTask{mockPersistentCacheTask}
57+
)
58+
59+
func mockPersistentCacheTaskRouter(h *Handlers) *gin.Engine {
60+
r := gin.Default()
61+
apiv1 := r.Group("/api/v1")
62+
task := apiv1.Group("/persistent-cache-tasks")
63+
task.DELETE(":id", h.DestroyPersistentCacheTask)
64+
task.GET(":id", h.GetPersistentCacheTask)
65+
task.GET("", h.GetPersistentCacheTasks)
66+
return r
67+
}
68+
69+
func TestHandlers_DestroyPersistentCacheTask(t *testing.T) {
70+
tests := []struct {
71+
name string
72+
req *http.Request
73+
mock func(ms *mocks.MockServiceMockRecorder)
74+
expect func(t *testing.T, w *httptest.ResponseRecorder)
75+
}{
76+
{
77+
name: "unprocessable entity uri",
78+
req: httptest.NewRequest(http.MethodDelete, "/api/v1/persistent-cache-tasks/", nil),
79+
mock: func(ms *mocks.MockServiceMockRecorder) {},
80+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
81+
assert := assert.New(t)
82+
assert.Equal(http.StatusNotFound, w.Code)
83+
},
84+
},
85+
{
86+
name: "unprocessable entity query",
87+
req: httptest.NewRequest(http.MethodDelete, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockUnprocessableSchedulerQuery, nil),
88+
mock: func(ms *mocks.MockServiceMockRecorder) {},
89+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
90+
assert := assert.New(t)
91+
assert.Equal(http.StatusUnprocessableEntity, w.Code)
92+
},
93+
},
94+
{
95+
name: "success",
96+
req: httptest.NewRequest(http.MethodDelete, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockDestroyPersistentCacheQuery, nil),
97+
mock: func(ms *mocks.MockServiceMockRecorder) {
98+
ms.DestroyPersistentCacheTask(gomock.Any(), gomock.Eq(mockSchedulerClusterID), gomock.Eq(mockPersistentCacheTaskID)).Return(nil).Times(1)
99+
},
100+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
101+
assert := assert.New(t)
102+
assert.Equal(http.StatusOK, w.Code)
103+
},
104+
},
105+
}
106+
for _, tc := range tests {
107+
t.Run(tc.name, func(t *testing.T) {
108+
ctl := gomock.NewController(t)
109+
defer ctl.Finish()
110+
svc := mocks.NewMockService(ctl)
111+
w := httptest.NewRecorder()
112+
h := New(svc)
113+
mockRouter := mockPersistentCacheTaskRouter(h)
114+
115+
tc.mock(svc.EXPECT())
116+
mockRouter.ServeHTTP(w, tc.req)
117+
tc.expect(t, w)
118+
})
119+
}
120+
}
121+
122+
func TestHandlers_GetPersistentCacheTask(t *testing.T) {
123+
tests := []struct {
124+
name string
125+
req *http.Request
126+
mock func(ms *mocks.MockServiceMockRecorder)
127+
expect func(t *testing.T, w *httptest.ResponseRecorder)
128+
}{
129+
{
130+
name: "unprocessable entity query",
131+
req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockUnprocessableSchedulerQuery, nil),
132+
mock: func(ms *mocks.MockServiceMockRecorder) {},
133+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
134+
assert := assert.New(t)
135+
assert.Equal(http.StatusUnprocessableEntity, w.Code)
136+
},
137+
},
138+
{
139+
name: "success",
140+
req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockGetPersistentCacheQuery, nil),
141+
mock: func(ms *mocks.MockServiceMockRecorder) {
142+
ms.GetPersistentCacheTask(gomock.Any(), gomock.Eq(mockSchedulerClusterID), gomock.Eq(mockPersistentCacheTaskID)).Return(mockPersistentCacheTask, nil).Times(1)
143+
},
144+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
145+
assert := assert.New(t)
146+
assert.Equal(http.StatusOK, w.Code)
147+
var task types.PersistentCacheTask
148+
err := json.Unmarshal(w.Body.Bytes(), &task)
149+
assert.NoError(err)
150+
assert.Equal(mockPersistentCacheTaskID, task.ID)
151+
},
152+
},
153+
}
154+
for _, tc := range tests {
155+
t.Run(tc.name, func(t *testing.T) {
156+
ctl := gomock.NewController(t)
157+
defer ctl.Finish()
158+
svc := mocks.NewMockService(ctl)
159+
w := httptest.NewRecorder()
160+
h := New(svc)
161+
mockRouter := mockPersistentCacheTaskRouter(h)
162+
163+
tc.mock(svc.EXPECT())
164+
mockRouter.ServeHTTP(w, tc.req)
165+
tc.expect(t, w)
166+
})
167+
}
168+
}
169+
170+
func TestHandlers_GetPersistentCacheTasks(t *testing.T) {
171+
tests := []struct {
172+
name string
173+
req *http.Request
174+
mock func(ms *mocks.MockServiceMockRecorder)
175+
expect func(t *testing.T, w *httptest.ResponseRecorder)
176+
}{
177+
{
178+
name: "unprocessable entity query",
179+
req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockUnprocessableSchedulerQuery, nil),
180+
mock: func(ms *mocks.MockServiceMockRecorder) {},
181+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
182+
assert := assert.New(t)
183+
assert.Equal(http.StatusUnprocessableEntity, w.Code)
184+
},
185+
},
186+
{
187+
name: "get without pagination",
188+
req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockGetPersistentCachesQuery, nil),
189+
mock: func(ms *mocks.MockServiceMockRecorder) {
190+
ms.GetPersistentCacheTasks(gomock.Any(), gomock.Any()).Return(mockPersistentCacheTasks, int64(len(mockPersistentCacheTasks)), nil).Times(1)
191+
},
192+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
193+
assert := assert.New(t)
194+
assert.Equal(http.StatusOK, w.Code)
195+
var tasks []types.PersistentCacheTask
196+
err := json.Unmarshal(w.Body.Bytes(), &tasks)
197+
assert.NoError(err)
198+
assert.Len(tasks, 1)
199+
assert.Equal(mockPersistentCacheTaskID, tasks[0].ID)
200+
},
201+
},
202+
{
203+
name: "get with pagination",
204+
req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockGetPersistentCachesQueryWithPage, nil),
205+
mock: func(ms *mocks.MockServiceMockRecorder) {
206+
ms.GetPersistentCacheTasks(gomock.Any(), gomock.Any()).Return(mockPersistentCacheTasks, int64(len(mockPersistentCacheTasks)), nil).Times(1)
207+
},
208+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
209+
assert := assert.New(t)
210+
assert.Equal(http.StatusOK, w.Code)
211+
var tasks []types.PersistentCacheTask
212+
err := json.Unmarshal(w.Body.Bytes(), &tasks)
213+
assert.NoError(err)
214+
assert.Len(tasks, 1)
215+
assert.Equal(mockPersistentCacheTaskID, tasks[0].ID)
216+
},
217+
},
218+
}
219+
for _, tc := range tests {
220+
t.Run(tc.name, func(t *testing.T) {
221+
ctl := gomock.NewController(t)
222+
defer ctl.Finish()
223+
svc := mocks.NewMockService(ctl)
224+
w := httptest.NewRecorder()
225+
h := New(svc)
226+
mockRouter := mockPersistentCacheTaskRouter(h)
227+
228+
tc.mock(svc.EXPECT())
229+
mockRouter.ServeHTTP(w, tc.req)
230+
tc.expect(t, w)
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)