Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Feb 4, 2024
1 parent 8773f6a commit 8b1acb6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
10 changes: 5 additions & 5 deletions pkg/mcs/resourcemanager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func changeLogLevel(c *gin.Context) {
// @Success 200 {string} string "Success"
// @Failure 400 {string} error
// @Failure 500 {string} error
// @Router /config/group [POST]
// @Router /config/group [post]
func (s *Service) postResourceGroup(c *gin.Context) {
var group rmpb.ResourceGroup
if err := c.ShouldBindJSON(&group); err != nil {
Expand Down Expand Up @@ -189,7 +189,7 @@ func (s *Service) putResourceGroup(c *gin.Context) {
// @Failure 404 {string} error
// @Param name path string true "groupName"
// @Param with_stats query bool false "whether to return statistics data."
// @Router /config/group/{name} [GET]
// @Router /config/group/{name} [get]
func (s *Service) getResourceGroup(c *gin.Context) {
withStats := strings.EqualFold(c.Query("with_stats"), "true")
group := s.manager.GetResourceGroup(c.Param("name"), withStats)
Expand All @@ -206,7 +206,7 @@ func (s *Service) getResourceGroup(c *gin.Context) {
// @Success 200 {string} json format of []rmserver.ResourceGroup
// @Failure 404 {string} error
// @Param with_stats query bool false "whether to return statistics data."
// @Router /config/groups [GET]
// @Router /config/groups [get]
func (s *Service) getResourceGroupList(c *gin.Context) {
withStats := strings.EqualFold(c.Query("with_stats"), "true")
groups := s.manager.GetResourceGroupList(withStats)
Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *Service) deleteResourceGroup(c *gin.Context) {
// @Summary Get the resource controller config.
// @Success 200 {string} json format of rmserver.ControllerConfig
// @Failure 400 {string} error
// @Router /config/controller [GET]
// @Router /config/controller [get]
func (s *Service) getControllerConfig(c *gin.Context) {
config := s.manager.GetControllerConfig()
c.IndentedJSON(http.StatusOK, config)
Expand All @@ -247,7 +247,7 @@ func (s *Service) getControllerConfig(c *gin.Context) {
// @Param config body object true "json params, rmserver.ControllerConfig"
// @Success 200 {string} string "Success!"
// @Failure 400 {string} error
// @Router /config/controller [POST]
// @Router /config/controller [post]
func (s *Service) setControllerConfig(c *gin.Context) {
conf := make(map[string]any)
if err := c.ShouldBindJSON(&conf); err != nil {
Expand Down
23 changes: 20 additions & 3 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (s *Service) RegisterOperatorsRouter() {
router := s.root.Group("operators")
router.GET("", getOperators)
router.POST("", createOperator)
router.DELETE("", deleteOperators)
router.GET("/:id", getOperatorByRegion)
router.DELETE("/:id", deleteOperatorByRegion)
router.GET("/records", getOperatorRecords)
Expand Down Expand Up @@ -315,7 +316,7 @@ func deleteRegionCacheByID(c *gin.Context) {
// @Success 200 {object} operator.OpWithStatus
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /operators/{id} [GET]
// @Router /operators/{id} [get]
func getOperatorByRegion(c *gin.Context) {
handler := c.MustGet(handlerKey).(*handler.Handler)
id := c.Param("id")
Expand All @@ -342,7 +343,7 @@ func getOperatorByRegion(c *gin.Context) {
// @Produce json
// @Success 200 {array} operator.Operator
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /operators [GET]
// @Router /operators [get]
func getOperators(c *gin.Context) {
handler := c.MustGet(handlerKey).(*handler.Handler)
var (
Expand Down Expand Up @@ -373,14 +374,30 @@ func getOperators(c *gin.Context) {
}
}

// @Tags operators
// @Summary Delete operators.
// @Produce json
// @Success 200 {string} string "All pending operator are canceled."
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /operators [DELETE]
func deleteOperators(c *gin.Context) {
handler := c.MustGet(handlerKey).(*handler.Handler)
if err := handler.RemoveOperators(); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}

c.String(http.StatusOK, "All pending operator are canceled.")
}

// @Tags operator
// @Summary Cancel a Region's pending operator.
// @Param region_id path int true "A Region's Id"
// @Produce json
// @Success 200 {string} string "The pending operator is canceled."
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /operators/{region_id} [delete]
// @Router /operators/{region_id} [DELETE]
func deleteOperatorByRegion(c *gin.Context) {
handler := c.MustGet(handlerKey).(*handler.Handler)
id := c.Param("id")
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/testutil/api_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func StringContain(re *require.Assertions, sub string) func([]byte, int, http.He
}
}

// StringContain is used to check whether response context contains given string.
func StringNotContain(re *require.Assertions, sub string) func([]byte, int, http.Header) {
return func(resp []byte, _ int, _ http.Header) {
re.NotContains(string(resp), sub, "resp: "+string(resp))
}
}

// StringEqual is used to check whether response context equal given string.
func StringEqual(re *require.Assertions, str string) func([]byte, int, http.Header) {
return func(resp []byte, _ int, _ http.Header) {
Expand Down
4 changes: 2 additions & 2 deletions server/api/service_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (h *serviceMiddlewareHandler) updateAudit(config *config.ServiceMiddlewareC
// @Success 200 {string} string
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "config item not found"
// @Router /service-middleware/config/rate-limit [POST]
// @Router /service-middleware/config/rate-limit [post]
func (h *serviceMiddlewareHandler) SetRateLimitConfig(w http.ResponseWriter, r *http.Request) {
var input map[string]any
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
Expand Down Expand Up @@ -236,7 +236,7 @@ func (h *serviceMiddlewareHandler) SetRateLimitConfig(w http.ResponseWriter, r *
// @Success 200 {string} string
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "config item not found"
// @Router /service-middleware/config/grpc-rate-limit [POST]
// @Router /service-middleware/config/grpc-rate-limit [post]
func (h *serviceMiddlewareHandler) SetGRPCRateLimitConfig(w http.ResponseWriter, r *http.Request) {
var input map[string]any
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/api/unsafe_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newUnsafeOperationHandler(svr *server.Server, rd *render.Render) *unsafeOpe
// Success 200 {string} string "Request has been accepted."
// Failure 400 {string} string "The input is invalid."
// Failure 500 {string} string "PD server failed to proceed the request."
// @Router /admin/unsafe/remove-failed-stores [POST]
// @Router /admin/unsafe/remove-failed-stores [post]
func (h *unsafeOperationHandler) RemoveFailedStores(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
var input map[string]any
Expand Down Expand Up @@ -81,7 +81,7 @@ func (h *unsafeOperationHandler) RemoveFailedStores(w http.ResponseWriter, r *ht
// @Summary Show the current status of failed stores removal.
// @Produce json
// Success 200 {object} []StageOutput
// @Router /admin/unsafe/remove-failed-stores/show [GET]
// @Router /admin/unsafe/remove-failed-stores/show [get]
func (h *unsafeOperationHandler) GetFailedStoresRemovalStatus(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
h.rd.JSON(w, http.StatusOK, rc.GetUnsafeRecoveryController().Show())
Expand Down
55 changes: 55 additions & 0 deletions tests/server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,58 @@ func (suite *operatorTestSuite) pauseRuleChecker(re *require.Assertions, cluster
re.NoError(err)
re.True(resp["paused"].(bool))
}

func (suite *operatorTestSuite) TestRemoveOperators() {
suite.env.RunTestInTwoModes(suite.checkRemoveOperators)
}

func (suite *operatorTestSuite) checkRemoveOperators(cluster *tests.TestCluster) {
re := suite.Require()
stores := []*metapb.Store{
{
Id: 1,
State: metapb.StoreState_Up,
NodeState: metapb.NodeState_Serving,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 2,
State: metapb.StoreState_Up,
NodeState: metapb.NodeState_Serving,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 3,
State: metapb.StoreState_Up,
NodeState: metapb.NodeState_Serving,
LastHeartbeat: time.Now().UnixNano(),
},
}

for _, store := range stores {
tests.MustPutStore(re, cluster, store)
}

suite.pauseRuleChecker(re, cluster)
r1 := core.NewTestRegionInfo(10, 1, []byte(""), []byte("b"), core.SetWrittenBytes(1000), core.SetReadBytes(1000), core.SetRegionConfVer(1), core.SetRegionVersion(1))
tests.MustPutRegionInfo(re, cluster, r1)
r2 := core.NewTestRegionInfo(20, 1, []byte("b"), []byte("c"), core.SetWrittenBytes(2000), core.SetReadBytes(0), core.SetRegionConfVer(2), core.SetRegionVersion(3))
tests.MustPutRegionInfo(re, cluster, r2)
r3 := core.NewTestRegionInfo(30, 1, []byte("c"), []byte("d"), core.SetWrittenBytes(500), core.SetReadBytes(800), core.SetRegionConfVer(3), core.SetRegionVersion(2))
tests.MustPutRegionInfo(re, cluster, r3)
r4 := core.NewTestRegionInfo(40, 1, []byte("d"), []byte(""), core.SetWrittenBytes(500), core.SetReadBytes(800), core.SetRegionConfVer(3), core.SetRegionVersion(2))
tests.MustPutRegionInfo(re, cluster, r4)

urlPrefix := fmt.Sprintf("%s/pd/api/v1", cluster.GetLeaderServer().GetAddr())
err := tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", urlPrefix), []byte(`{"name":"merge-region", "source_region_id": 10, "target_region_id": 20}`), tu.StatusOK(re))
re.NoError(err)
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", urlPrefix), []byte(`{"name":"merge-region", "source_region_id": 30, "target_region_id": 40}`), tu.StatusOK(re))
re.NoError(err)
url := fmt.Sprintf("%s/operators", urlPrefix)
err = tu.CheckGetJSON(testDialClient, url, nil, tu.StatusOK(re), tu.StringContain(re, "merge: region 10 to 20"), tu.StringContain(re, "merge: region 30 to 40"))
re.NoError(err)
err = tu.CheckDelete(testDialClient, url, tu.StatusOK(re))
re.NoError(err)
err = tu.CheckGetJSON(testDialClient, url, nil, tu.StatusOK(re), tu.StringNotContain(re, "merge: region 10 to 20"), tu.StringNotContain(re, "merge: region 30 to 40"))
re.NoError(err)
}

0 comments on commit 8b1acb6

Please sign in to comment.