Beispiel #1
0
func (as ApiService) addCheck(c echo.Context) error {
	var check types.CheckSpec
	if err := c.Bind(&check); err != nil {
		return err
	}

	check.ServiceID = c.Param("service_name")

	// Converting int to time.Duration
	check.Interval = check.Interval * time.Second
	check.Timeout = check.Timeout * time.Second

	if err := as.balancer.AddCheck(check); err != nil {
		return err
	}

	return c.JSON(http.StatusCreated, check)
}
Beispiel #2
0
func (s *ApiTestSuite) TestAddCheck() {
	spec := types.CheckSpec{ServiceID: s.service.Name, Interval: 5 * time.Second, Timeout: 5 * time.Second}
	s.balancer.On("AddCheck", spec).Return(nil)

	spec.ServiceID = s.service.GetId()
	expectedBody, err := json.Marshal(spec)
	assert.Nil(s.T(), err)

	s.r.POST("/services/"+s.service.Name+"/check").
		SetJSON(gofight.D{
			"interval": 5,
			"timeout":  5,
		}).
		Run(s.api.echo, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
			s.balancer.AssertExpectations(s.T())

			assert.Equal(s.T(), http.StatusCreated, r.Code)
			assert.Equal(s.T(), string(expectedBody), r.Body.String())
		})
}