(t *testing.T)
| 43 | ) |
| 44 | |
| 45 | func TestClusterBasics(t *testing.T) { |
| 46 | ns := "test-ns" |
| 47 | handler := &ClusterHandler{s: store.NewClusterStore(engine.NewMock())} |
| 48 | |
| 49 | runCreate := func(t *testing.T, name string, expectedStatusCode int) { |
| 50 | testCreateRequest := &CreateClusterRequest{ |
| 51 | Name: name, |
| 52 | Nodes: []string{"127.0.0.1:1234", "127.0.0.1:1235", "127.0.0.1:1236", "127.0.0.1:1237"}, |
| 53 | Replicas: 2, |
| 54 | } |
| 55 | recorder := httptest.NewRecorder() |
| 56 | ctx := GetTestContext(recorder) |
| 57 | body, err := json.Marshal(testCreateRequest) |
| 58 | require.NoError(t, err) |
| 59 | |
| 60 | ctx.Header(consts.HeaderDontCheckClusterMode, "yes") |
| 61 | ctx.Request.Body = io.NopCloser(bytes.NewBuffer(body)) |
| 62 | ctx.Params = []gin.Param{{Key: "namespace", Value: ns}} |
| 63 | |
| 64 | handler.Create(ctx) |
| 65 | require.Equal(t, expectedStatusCode, recorder.Code) |
| 66 | } |
| 67 | |
| 68 | runGet := func(t *testing.T, name string, expectedStatusCode int) { |
| 69 | recorder := httptest.NewRecorder() |
| 70 | ctx := GetTestContext(recorder) |
| 71 | ctx.Set(consts.ContextKeyStore, handler.s) |
| 72 | ctx.Params = []gin.Param{{Key: "namespace", Value: ns}, {Key: "cluster", Value: name}} |
| 73 | |
| 74 | middleware.RequiredCluster(ctx) |
| 75 | if recorder.Code != http.StatusOK { |
| 76 | return |
| 77 | } |
| 78 | handler.Get(ctx) |
| 79 | require.Equal(t, expectedStatusCode, recorder.Code) |
| 80 | } |
| 81 | |
| 82 | runRemove := func(t *testing.T, name string, expectedStatusCode int) { |
| 83 | recorder := httptest.NewRecorder() |
| 84 | ctx := GetTestContext(recorder) |
| 85 | ctx.Set(consts.ContextKeyStore, handler.s) |
| 86 | ctx.Params = []gin.Param{{Key: "namespace", Value: ns}, {Key: "cluster", Value: name}} |
| 87 | |
| 88 | middleware.RequiredCluster(ctx) |
| 89 | if recorder.Code != http.StatusOK { |
| 90 | return |
| 91 | } |
| 92 | handler.Remove(ctx) |
| 93 | require.Equal(t, expectedStatusCode, recorder.Code) |
| 94 | } |
| 95 | |
| 96 | t.Run("create cluster", func(t *testing.T) { |
| 97 | runCreate(t, "test-cluster", http.StatusCreated) |
| 98 | runCreate(t, "test-cluster", http.StatusConflict) |
| 99 | }) |
| 100 | |
| 101 | t.Run("get cluster", func(t *testing.T) { |
| 102 | runGet(t, "test-cluster", http.StatusOK) |
nothing calls this directly
no test coverage detected