|
| 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 | +} |
0 commit comments