代码拉取完成,页面将自动刷新
package routers
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/store"
"github.com/stretchr/testify/mock"
)
type mockEventController struct {
mock.Mock
}
func (m *mockEventController) Create(ctx context.Context, check *corev2.Event) error {
return m.Called(ctx, check).Error(0)
}
func (m *mockEventController) CreateOrReplace(ctx context.Context, check *corev2.Event) error {
return m.Called(ctx, check).Error(0)
}
func (m *mockEventController) Delete(ctx context.Context, entity, check string) error {
return m.Called(ctx, entity, check).Error(0)
}
func (m *mockEventController) Get(ctx context.Context, entity, check string) (*corev2.Event, error) {
args := m.Called(ctx, entity, check)
return args.Get(0).(*corev2.Event), args.Error(1)
}
func (m *mockEventController) List(ctx context.Context, pred *store.SelectionPredicate) ([]corev2.Resource, error) {
args := m.Called(ctx, pred)
return args.Get(0).([]corev2.Resource), args.Error(1)
}
func TestEventsRouter(t *testing.T) {
type controllerFunc func(*mockEventController)
// Setup the router
controller := &mockEventController{}
router := EventsRouter{controller: controller}
parentRouter := mux.NewRouter().PathPrefix(corev2.URLPrefix).Subrouter()
router.Mount(parentRouter)
empty := &corev2.Event{
Check: &corev2.Check{ObjectMeta: corev2.ObjectMeta{}},
Entity: &corev2.Entity{ObjectMeta: corev2.ObjectMeta{Namespace: "default"}},
}
fixture := corev2.FixtureEvent("foo", "check-cpu")
tests := []struct {
name string
method string
path string
body []byte
controllerFunc controllerFunc
wantStatusCode int
}{
{
name: "it returns 404 if a resource is not found",
method: http.MethodGet,
path: fixture.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("Get", mock.Anything, "foo", "check-cpu").
Return(empty, actions.NewErrorf(actions.NotFound)).
Once()
},
wantStatusCode: http.StatusNotFound,
},
{
name: "it returns 200 if a resource is found",
method: http.MethodGet,
path: fixture.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("Get", mock.Anything, "foo", "check-cpu").
Return(fixture, nil).
Once()
},
wantStatusCode: http.StatusOK,
},
{
name: "it returns 500 if the store encounters an error while listing events",
method: http.MethodGet,
path: empty.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("List", mock.Anything, mock.AnythingOfType("*store.SelectionPredicate")).
Return([]corev2.Resource{empty}, actions.NewErrorf(actions.InternalErr)).
Once()
},
wantStatusCode: http.StatusInternalServerError,
},
{
name: "it returns 200 and lists resources",
method: http.MethodGet,
path: empty.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("List", mock.Anything, mock.AnythingOfType("*store.SelectionPredicate")).
Return([]corev2.Resource{fixture}, nil).
Once()
},
wantStatusCode: http.StatusOK,
},
{
name: "it returns 400 if the payload to create is not decodable",
method: http.MethodPost,
path: empty.URIPath(),
body: []byte(`foo`),
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 400 if the event metadata to create is invalid",
method: http.MethodPost,
path: empty.URIPath(),
body: []byte(`{"entity": {"namespace":"acme"}}`),
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 400 if the event to create is not valid",
method: http.MethodPost,
path: empty.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("Create", mock.Anything, mock.Anything).
Return(actions.NewErrorf(actions.InvalidArgument)).
Once()
},
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 500 if the store returns an error while creating an event",
method: http.MethodPost,
path: empty.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("Create", mock.Anything, mock.Anything).
Return(actions.NewErrorf(actions.InternalErr)).
Once()
},
wantStatusCode: http.StatusInternalServerError,
},
{
name: "it returns 201 when an event is successfully created",
method: http.MethodPost,
path: empty.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("Create", mock.Anything, mock.Anything).
Return(nil).
Once()
},
wantStatusCode: http.StatusNoContent,
},
{
name: "it returns 400 if the payload to update is not decodable",
method: http.MethodPut,
path: fixture.URIPath(),
body: []byte(`foo`),
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 400 if the event metadata to update is invalid",
method: http.MethodPut,
path: fixture.URIPath(),
body: []byte(`{"entity": {"namespace":"acme"}}`),
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 400 if the event to update is not valid",
method: http.MethodPut,
path: fixture.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("CreateOrReplace", mock.Anything, mock.Anything).
Return(actions.NewErrorf(actions.InvalidArgument)).
Once()
},
wantStatusCode: http.StatusBadRequest,
},
{
name: "it returns 500 if the store returns an error while updating an event",
method: http.MethodPut,
path: fixture.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("CreateOrReplace", mock.Anything, mock.Anything).
Return(actions.NewErrorf(actions.InternalErr)).
Once()
},
wantStatusCode: http.StatusInternalServerError,
},
{
name: "it returns 201 when an event is successfully updated",
method: http.MethodPut,
path: fixture.URIPath(),
body: marshal(fixture),
controllerFunc: func(c *mockEventController) {
c.On("CreateOrReplace", mock.Anything, mock.Anything).
Return(nil).
Once()
},
wantStatusCode: http.StatusNoContent,
},
{
name: "it returns 404 if the event to delete does not exist",
method: http.MethodDelete,
path: fixture.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("Delete", mock.Anything, "foo", "check-cpu").
Return(actions.NewErrorf(actions.NotFound)).
Once()
},
wantStatusCode: http.StatusNotFound,
},
{
name: "it returns 500 if the store returns an error while deleting an event",
method: http.MethodDelete,
path: fixture.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("Delete", mock.Anything, "foo", "check-cpu").
Return(actions.NewErrorf(actions.InternalErr)).
Once()
},
wantStatusCode: http.StatusInternalServerError,
},
{
name: "it returns 204 if the event was deleted",
method: http.MethodDelete,
path: fixture.URIPath(),
controllerFunc: func(c *mockEventController) {
c.On("Delete", mock.Anything, "foo", "check-cpu").
Return(nil).
Once()
},
wantStatusCode: http.StatusNoContent,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Only start the HTTP server here to prevent data races in tests
server := httptest.NewServer(parentRouter)
defer server.Close()
if tt.controllerFunc != nil {
tt.controllerFunc(controller)
}
// Prepare the HTTP request
client := new(http.Client)
req, err := http.NewRequest(tt.method, server.URL+tt.path, bytes.NewReader(tt.body))
if err != nil {
t.Fatal(err)
}
// Perform the HTTP request
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
// Inspect the response code
if res.StatusCode != tt.wantStatusCode {
t.Errorf("EventsRouter StatusCode = %v, wantStatusCode %v", res.StatusCode, tt.wantStatusCode)
body, _ := ioutil.ReadAll(res.Body)
t.Errorf("error message: %q", string(body))
return
}
})
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。