Skip to content

Commit 4ac3519

Browse files
committed
UTs for server.go
Signed-off-by: Ashima-Ashima1 <[email protected]>
1 parent 5ad177e commit 4ac3519

File tree

1 file changed

+76
-11
lines changed

1 file changed

+76
-11
lines changed

cos-csi-mounter/server/server_test.go

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ import (
1919
)
2020

2121
// Mock implementations
22-
type mockMounter struct {
22+
type MockMounterUtils struct {
2323
mock.Mock
2424
}
2525

26-
func (m *mockMounter) FuseMount(path string, mounter string, args []string) error {
26+
func (m *MockMounterUtils) FuseMount(path string, mounter string, args []string) error {
2727
argsCalled := m.Called(path, mounter, args)
2828
return argsCalled.Error(0)
2929
}
3030

31-
func (m *mockMounter) FuseUnmount(path string) error {
31+
func (m *MockMounterUtils) FuseUnmount(path string) error {
3232
argsCalled := m.Called(path)
3333
return argsCalled.Error(0)
3434
}
@@ -133,7 +133,7 @@ func TestNewRouter_HasExpectedRoutes(t *testing.T) {
133133
}
134134

135135
func TestHandleCosMount_InvalidJSON(t *testing.T) {
136-
mockMounter := new(mockMounter)
136+
mockMounter := new(MockMounterUtils)
137137
router := gin.Default()
138138
router.POST("/mount", handleCosMount(mockMounter, &MockMounterArgsParser{}))
139139

@@ -147,7 +147,7 @@ func TestHandleCosMount_InvalidJSON(t *testing.T) {
147147
}
148148

149149
func TestHandleCosMount_InvalidMounter(t *testing.T) {
150-
mockMounter := new(mockMounter)
150+
mockMounter := new(MockMounterUtils)
151151
router := gin.Default()
152152
router.POST("/mount", handleCosMount(mockMounter, &MockMounterArgsParser{}))
153153

@@ -170,7 +170,7 @@ func TestHandleCosMount_InvalidMounter(t *testing.T) {
170170
}
171171

172172
func TestHandleCosMount_MissingBucket(t *testing.T) {
173-
mockMounter := new(mockMounter)
173+
mockMounter := new(MockMounterUtils)
174174
router := gin.Default()
175175
router.POST("/mount", handleCosMount(mockMounter, &MockMounterArgsParser{}))
176176

@@ -194,7 +194,7 @@ func TestHandleCosMount_MissingBucket(t *testing.T) {
194194
func TestHandleCosMount_InvalidMounterArgs(t *testing.T) {
195195
gin.SetMode(gin.TestMode)
196196

197-
mockMounter := new(mockMounter)
197+
mockMounter := new(MockMounterUtils)
198198
mockParser := new(MockMounterArgsParser)
199199

200200
reqBody := []byte(`{
@@ -204,7 +204,6 @@ func TestHandleCosMount_InvalidMounterArgs(t *testing.T) {
204204
"args": {"flag": "--invalid"}
205205
}`)
206206

207-
// Unmarshal into MountRequest to match the call made to Parse()
208207
var request MountRequest
209208
err := json.Unmarshal(reqBody, &request)
210209
assert.NoError(t, err)
@@ -224,8 +223,74 @@ func TestHandleCosMount_InvalidMounterArgs(t *testing.T) {
224223
mockParser.AssertExpectations(t)
225224
}
226225

226+
func TestHandleCosMount_FuseMountFails(t *testing.T) {
227+
mockMounter := new(MockMounterUtils)
228+
mockParser := new(MockMounterArgsParser)
229+
230+
request := MountRequest{
231+
Bucket: "my-bucket",
232+
Path: "/mnt/test",
233+
Mounter: constants.S3FS,
234+
Args: json.RawMessage(`["--endpoint=https://s3.example.com"]`),
235+
}
236+
237+
expectedArgs := []string{"--endpoint=https://s3.example.com"}
238+
239+
mockParser.On("Parse", request).Return(expectedArgs, nil)
240+
mockMounter.On("FuseMount", request.Path, request.Mounter, expectedArgs).Return(fmt.Errorf("mount error"))
241+
242+
router := gin.Default()
243+
router.POST("/mount", handleCosMount(mockMounter, mockParser))
244+
245+
body, _ := json.Marshal(request)
246+
w := httptest.NewRecorder()
247+
req, _ := http.NewRequest("POST", "/mount", bytes.NewBuffer(body))
248+
req.Header.Set("Content-Type", "application/json")
249+
250+
router.ServeHTTP(w, req)
251+
252+
assert.Equal(t, http.StatusInternalServerError, w.Code)
253+
assert.Contains(t, w.Body.String(), "mount failed: mount error")
254+
255+
mockMounter.AssertExpectations(t)
256+
mockParser.AssertExpectations(t)
257+
}
258+
259+
func TestHandleCosMount_Success(t *testing.T) {
260+
mockMounter := new(MockMounterUtils)
261+
mockParser := new(MockMounterArgsParser)
262+
263+
request := MountRequest{
264+
Bucket: "my-bucket",
265+
Path: "/mnt/test",
266+
Mounter: constants.S3FS,
267+
Args: json.RawMessage(`["--endpoint=https://s3.example.com"]`),
268+
}
269+
270+
expectedArgs := []string{"--endpoint=https://s3.example.com"}
271+
272+
mockParser.On("Parse", request).Return(expectedArgs, nil)
273+
mockMounter.On("FuseMount", request.Path, request.Mounter, expectedArgs).Return(nil)
274+
275+
router := gin.Default()
276+
router.POST("/mount", handleCosMount(mockMounter, mockParser))
277+
278+
body, _ := json.Marshal(request)
279+
w := httptest.NewRecorder()
280+
req, _ := http.NewRequest("POST", "/mount", bytes.NewBuffer(body))
281+
req.Header.Set("Content-Type", "application/json")
282+
283+
router.ServeHTTP(w, req)
284+
285+
assert.Equal(t, http.StatusOK, w.Code)
286+
assert.Contains(t, w.Body.String(), "success")
287+
288+
mockMounter.AssertExpectations(t)
289+
mockParser.AssertExpectations(t)
290+
}
291+
227292
func TestHandleCosUnmount_InvalidJSON(t *testing.T) {
228-
mock := new(mockMounter)
293+
mock := new(MockMounterUtils)
229294
router := gin.Default()
230295
router.POST("/unmount", handleCosUnmount(mock))
231296

@@ -240,7 +305,7 @@ func TestHandleCosUnmount_InvalidJSON(t *testing.T) {
240305
}
241306

242307
func TestHandleCosUnmount_UnmountFailure(t *testing.T) {
243-
mock := new(mockMounter)
308+
mock := new(MockMounterUtils)
244309
mock.On("FuseUnmount", "/mnt/fail").Return(errors.New("mock failure"))
245310

246311
router := gin.Default()
@@ -261,7 +326,7 @@ func TestHandleCosUnmount_UnmountFailure(t *testing.T) {
261326
}
262327

263328
func TestHandleCosUnmount_Success(t *testing.T) {
264-
mock := new(mockMounter)
329+
mock := new(MockMounterUtils)
265330
mock.On("FuseUnmount", "/mnt/success").Return(nil)
266331

267332
router := gin.Default()

0 commit comments

Comments
 (0)