Skip to content

Commit 3ca7933

Browse files
committed
feat: implement Destroy and Get endpoints for PersistentCache
Signed-off-by: BruceAko <chongzhi@hust.edu.cn>
1 parent 128f2d9 commit 3ca7933

File tree

8 files changed

+366
-18
lines changed

8 files changed

+366
-18
lines changed

manager/handlers/persistent_cache.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,61 @@ import (
2424
"d7y.io/dragonfly/v2/manager/types"
2525
)
2626

27+
// @Summary Destroy PersistentCache
28+
// @Description Destroy PersistentCache by id
29+
// @Tags PersistentCache
30+
// @Accept json
31+
// @Produce json
32+
// @Param scheduler_cluster_id path string true "scheduler cluster id"
33+
// @Param task_id path string true "task id"
34+
// @Success 200
35+
// @Failure 400
36+
// @Failure 404
37+
// @Failure 500
38+
// @Router /api/v1/persistent-caches/{scheduler_cluster_id}/{task_id} [delete]
39+
func (h *Handlers) DestroyPersistentCache(ctx *gin.Context) {
40+
var params types.PersistentCacheParams
41+
if err := ctx.ShouldBindUri(&params); err != nil {
42+
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
43+
return
44+
}
45+
46+
if err := h.service.DestroyPersistentCache(ctx.Request.Context(), params); err != nil {
47+
ctx.Error(err) // nolint: errcheck
48+
return
49+
}
50+
51+
ctx.Status(http.StatusOK)
52+
}
53+
54+
// @Summary Get PersistentCache
55+
// @Description Get PersistentCache by id
56+
// @Tags PersistentCache
57+
// @Accept json
58+
// @Produce json
59+
// @Param scheduler_cluster_id path string true "scheduler cluster id"
60+
// @Param task_id path string true "task id"
61+
// @Success 200 {object} types.GetPersistentCacheResponse
62+
// @Failure 400
63+
// @Failure 404
64+
// @Failure 500
65+
// @Router /api/v1/persistent-caches/{scheduler_cluster_id}/{task_id} [get]
66+
func (h *Handlers) GetPersistentCache(ctx *gin.Context) {
67+
var params types.PersistentCacheParams
68+
if err := ctx.ShouldBindUri(&params); err != nil {
69+
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
70+
return
71+
}
72+
73+
persistentCache, err := h.service.GetPersistentCache(ctx.Request.Context(), params)
74+
if err != nil {
75+
ctx.Error(err) // nolint: errcheck
76+
return
77+
}
78+
79+
ctx.JSON(http.StatusOK, persistentCache)
80+
}
81+
2782
// @Summary Get PersistentCaches
2883
// @Description Get PersistentCaches
2984
// @Tags PersistentCache
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright 2025 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+
"net/http"
21+
"net/http/httptest"
22+
"testing"
23+
24+
"github.com/gin-gonic/gin"
25+
"github.com/stretchr/testify/assert"
26+
"go.uber.org/mock/gomock"
27+
28+
"d7y.io/dragonfly/v2/manager/service/mocks"
29+
"d7y.io/dragonfly/v2/manager/types"
30+
)
31+
32+
var (
33+
mockPersistentCacheParams = types.PersistentCacheParams{
34+
SchedulerClusterID: 1,
35+
TaskID: "task-1",
36+
}
37+
mockGetPersistentCacheResponse = &types.GetPersistentCacheResponse{
38+
TaskID: "task-1",
39+
State: "SUCCESS",
40+
}
41+
mockGetPersistentCachesResponse = []types.GetPersistentCacheResponse{
42+
{
43+
TaskID: "task-1",
44+
State: "SUCCESS",
45+
},
46+
}
47+
)
48+
49+
func mockPersistentCacheRouter(h *Handlers) *gin.Engine {
50+
r := gin.Default()
51+
r.DELETE("/persistent-caches/:scheduler_cluster_id/:task_id", h.DestroyPersistentCache)
52+
r.GET("/persistent-caches/:scheduler_cluster_id/:task_id", h.GetPersistentCache)
53+
r.GET("/persistent-caches", h.GetPersistentCaches)
54+
return r
55+
}
56+
57+
func TestHandlers_DestroyPersistentCache(t *testing.T) {
58+
tests := []struct {
59+
name string
60+
req *http.Request
61+
mock func(ms *mocks.MockServiceMockRecorder)
62+
expect func(t *testing.T, w *httptest.ResponseRecorder)
63+
}{
64+
{
65+
name: "unprocessable entity",
66+
req: httptest.NewRequest(http.MethodDelete, "/persistent-caches/invalid/task-1", nil),
67+
mock: func(ms *mocks.MockServiceMockRecorder) {},
68+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
69+
assert := assert.New(t)
70+
assert.Equal(http.StatusUnprocessableEntity, w.Code)
71+
},
72+
},
73+
{
74+
name: "success",
75+
req: httptest.NewRequest(http.MethodDelete, "/persistent-caches/1/task-1", nil),
76+
mock: func(ms *mocks.MockServiceMockRecorder) {
77+
ms.DestroyPersistentCache(gomock.Any(), gomock.Eq(mockPersistentCacheParams)).Return(nil).Times(1)
78+
},
79+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
80+
assert := assert.New(t)
81+
assert.Equal(http.StatusOK, w.Code)
82+
},
83+
},
84+
}
85+
for _, tc := range tests {
86+
t.Run(tc.name, func(t *testing.T) {
87+
ctl := gomock.NewController(t)
88+
defer ctl.Finish()
89+
svc := mocks.NewMockService(ctl)
90+
w := httptest.NewRecorder()
91+
h := New(svc)
92+
mockRouter := mockPersistentCacheRouter(h)
93+
94+
tc.mock(svc.EXPECT())
95+
mockRouter.ServeHTTP(w, tc.req)
96+
tc.expect(t, w)
97+
})
98+
}
99+
}
100+
101+
func TestHandlers_GetPersistentCache(t *testing.T) {
102+
tests := []struct {
103+
name string
104+
req *http.Request
105+
mock func(ms *mocks.MockServiceMockRecorder)
106+
expect func(t *testing.T, w *httptest.ResponseRecorder)
107+
}{
108+
{
109+
name: "unprocessable entity",
110+
req: httptest.NewRequest(http.MethodGet, "/persistent-caches/invalid/task-1", nil),
111+
mock: func(ms *mocks.MockServiceMockRecorder) {},
112+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
113+
assert := assert.New(t)
114+
assert.Equal(http.StatusUnprocessableEntity, w.Code)
115+
},
116+
},
117+
{
118+
name: "success",
119+
req: httptest.NewRequest(http.MethodGet, "/persistent-caches/1/task-1", nil),
120+
mock: func(ms *mocks.MockServiceMockRecorder) {
121+
ms.GetPersistentCache(gomock.Any(), gomock.Eq(mockPersistentCacheParams)).Return(mockGetPersistentCacheResponse, nil).Times(1)
122+
},
123+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
124+
assert := assert.New(t)
125+
assert.Equal(http.StatusOK, w.Code)
126+
assert.Contains(w.Body.String(), `"task_id":"task-1"`)
127+
assert.Contains(w.Body.String(), `"state":"SUCCESS"`)
128+
},
129+
},
130+
}
131+
for _, tc := range tests {
132+
t.Run(tc.name, func(t *testing.T) {
133+
ctl := gomock.NewController(t)
134+
defer ctl.Finish()
135+
svc := mocks.NewMockService(ctl)
136+
w := httptest.NewRecorder()
137+
h := New(svc)
138+
mockRouter := mockPersistentCacheRouter(h)
139+
140+
tc.mock(svc.EXPECT())
141+
mockRouter.ServeHTTP(w, tc.req)
142+
tc.expect(t, w)
143+
})
144+
}
145+
}
146+
147+
func TestHandlers_GetPersistentCaches(t *testing.T) {
148+
tests := []struct {
149+
name string
150+
req *http.Request
151+
mock func(ms *mocks.MockServiceMockRecorder)
152+
expect func(t *testing.T, w *httptest.ResponseRecorder)
153+
}{
154+
{
155+
name: "success",
156+
req: httptest.NewRequest(http.MethodGet, "/persistent-caches?page=1&per_page=10", nil),
157+
mock: func(ms *mocks.MockServiceMockRecorder) {
158+
ms.GetPersistentCaches(gomock.Any(), gomock.Any()).Return(mockGetPersistentCachesResponse, int64(1), nil).Times(1)
159+
},
160+
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
161+
assert := assert.New(t)
162+
assert.Equal(http.StatusOK, w.Code)
163+
assert.Contains(w.Body.String(), `"task_id":"task-1"`)
164+
assert.Contains(w.Body.String(), `"state":"SUCCESS"`)
165+
assert.Contains(w.Header().Get("Link"), "page=1")
166+
},
167+
},
168+
}
169+
for _, tc := range tests {
170+
t.Run(tc.name, func(t *testing.T) {
171+
ctl := gomock.NewController(t)
172+
defer ctl.Finish()
173+
svc := mocks.NewMockService(ctl)
174+
w := httptest.NewRecorder()
175+
h := New(svc)
176+
mockRouter := mockPersistentCacheRouter(h)
177+
178+
tc.mock(svc.EXPECT())
179+
mockRouter.ServeHTTP(w, tc.req)
180+
tc.expect(t, w)
181+
})
182+
}
183+
}

manager/router/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ func Init(cfg *config.Config, logDir string, service service.Service, database *
235235

236236
// Persistent Cache.
237237
pc := apiv1.Group("/persistent-caches", jwt.MiddlewareFunc(), rbac)
238-
//pc.DELETE(":id", h.DestroyPersistentCache)
239-
//pc.GET(":id", h.GetPersistentCache)
238+
pc.DELETE(":id", h.DestroyPersistentCache)
239+
pc.GET(":id", h.GetPersistentCache)
240240
pc.GET("", h.GetPersistentCaches)
241241

242242
// Open API router.

manager/service/mocks/service_mock.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)