|
| 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 uri", |
| 131 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks/", 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.StatusNotFound, w.Code) |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "unprocessable entity query", |
| 140 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockUnprocessableSchedulerQuery, nil), |
| 141 | + mock: func(ms *mocks.MockServiceMockRecorder) {}, |
| 142 | + expect: func(t *testing.T, w *httptest.ResponseRecorder) { |
| 143 | + assert := assert.New(t) |
| 144 | + assert.Equal(http.StatusUnprocessableEntity, w.Code) |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "success", |
| 149 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks/"+mockPersistentCacheTaskID+"?"+mockGetPersistentCacheQuery, nil), |
| 150 | + mock: func(ms *mocks.MockServiceMockRecorder) { |
| 151 | + ms.GetPersistentCacheTask(gomock.Any(), gomock.Eq(mockSchedulerClusterID), gomock.Eq(mockPersistentCacheTaskID)).Return(mockPersistentCacheTask, nil).Times(1) |
| 152 | + }, |
| 153 | + expect: func(t *testing.T, w *httptest.ResponseRecorder) { |
| 154 | + assert := assert.New(t) |
| 155 | + assert.Equal(http.StatusOK, w.Code) |
| 156 | + var task types.PersistentCacheTask |
| 157 | + err := json.Unmarshal(w.Body.Bytes(), &task) |
| 158 | + assert.NoError(err) |
| 159 | + assert.Equal(mockPersistentCacheTaskID, task.ID) |
| 160 | + }, |
| 161 | + }, |
| 162 | + } |
| 163 | + for _, tc := range tests { |
| 164 | + t.Run(tc.name, func(t *testing.T) { |
| 165 | + ctl := gomock.NewController(t) |
| 166 | + defer ctl.Finish() |
| 167 | + svc := mocks.NewMockService(ctl) |
| 168 | + w := httptest.NewRecorder() |
| 169 | + h := New(svc) |
| 170 | + mockRouter := mockPersistentCacheTaskRouter(h) |
| 171 | + |
| 172 | + tc.mock(svc.EXPECT()) |
| 173 | + mockRouter.ServeHTTP(w, tc.req) |
| 174 | + tc.expect(t, w) |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestHandlers_GetPersistentCacheTasks(t *testing.T) { |
| 180 | + tests := []struct { |
| 181 | + name string |
| 182 | + req *http.Request |
| 183 | + mock func(ms *mocks.MockServiceMockRecorder) |
| 184 | + expect func(t *testing.T, w *httptest.ResponseRecorder) |
| 185 | + }{ |
| 186 | + { |
| 187 | + name: "unprocessable entity query", |
| 188 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockUnprocessableSchedulerQuery, nil), |
| 189 | + mock: func(ms *mocks.MockServiceMockRecorder) {}, |
| 190 | + expect: func(t *testing.T, w *httptest.ResponseRecorder) { |
| 191 | + assert := assert.New(t) |
| 192 | + assert.Equal(http.StatusUnprocessableEntity, w.Code) |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "get without pagination", |
| 197 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockGetPersistentCachesQuery, nil), |
| 198 | + mock: func(ms *mocks.MockServiceMockRecorder) { |
| 199 | + ms.GetPersistentCacheTasks(gomock.Any(), gomock.Any()).Return(mockPersistentCacheTasks, int64(len(mockPersistentCacheTasks)), nil).Times(1) |
| 200 | + }, |
| 201 | + expect: func(t *testing.T, w *httptest.ResponseRecorder) { |
| 202 | + assert := assert.New(t) |
| 203 | + assert.Equal(http.StatusOK, w.Code) |
| 204 | + var tasks []types.PersistentCacheTask |
| 205 | + err := json.Unmarshal(w.Body.Bytes(), &tasks) |
| 206 | + assert.NoError(err) |
| 207 | + assert.Len(tasks, 1) |
| 208 | + assert.Equal(mockPersistentCacheTaskID, tasks[0].ID) |
| 209 | + }, |
| 210 | + }, |
| 211 | + { |
| 212 | + name: "get with pagination", |
| 213 | + req: httptest.NewRequest(http.MethodGet, "/api/v1/persistent-cache-tasks?"+mockGetPersistentCachesQueryWithPage, nil), |
| 214 | + mock: func(ms *mocks.MockServiceMockRecorder) { |
| 215 | + ms.GetPersistentCacheTasks(gomock.Any(), gomock.Any()).Return(mockPersistentCacheTasks, int64(len(mockPersistentCacheTasks)), nil).Times(1) |
| 216 | + }, |
| 217 | + expect: func(t *testing.T, w *httptest.ResponseRecorder) { |
| 218 | + assert := assert.New(t) |
| 219 | + assert.Equal(http.StatusOK, w.Code) |
| 220 | + var tasks []types.PersistentCacheTask |
| 221 | + err := json.Unmarshal(w.Body.Bytes(), &tasks) |
| 222 | + assert.NoError(err) |
| 223 | + assert.Len(tasks, 1) |
| 224 | + assert.Equal(mockPersistentCacheTaskID, tasks[0].ID) |
| 225 | + }, |
| 226 | + }, |
| 227 | + } |
| 228 | + for _, tc := range tests { |
| 229 | + t.Run(tc.name, func(t *testing.T) { |
| 230 | + ctl := gomock.NewController(t) |
| 231 | + defer ctl.Finish() |
| 232 | + svc := mocks.NewMockService(ctl) |
| 233 | + w := httptest.NewRecorder() |
| 234 | + h := New(svc) |
| 235 | + mockRouter := mockPersistentCacheTaskRouter(h) |
| 236 | + |
| 237 | + tc.mock(svc.EXPECT()) |
| 238 | + mockRouter.ServeHTTP(w, tc.req) |
| 239 | + tc.expect(t, w) |
| 240 | + }) |
| 241 | + } |
| 242 | +} |
0 commit comments