...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apiserver/event/event_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apiserver/event

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package event
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/gin-gonic/gin"
    27  	"github.com/jinzhu/gorm"
    28  	. "github.com/onsi/ginkgo/v2"
    29  	. "github.com/onsi/gomega"
    30  	"github.com/pkg/errors"
    31  	"github.com/stretchr/testify/mock"
    32  
    33  	config "github.com/chaos-mesh/chaos-mesh/pkg/config"
    34  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/core"
    35  	pkgmock "github.com/chaos-mesh/chaos-mesh/pkg/mock"
    36  )
    37  
    38  // MockEventService is a mock of core.EventStore
    39  type MockEventService struct {
    40  	mock.Mock
    41  }
    42  
    43  func (m *MockEventService) List(context.Context) ([]*core.Event, error) {
    44  	panic("implement me")
    45  }
    46  
    47  func (m *MockEventService) ListByExperiment(context.Context, string, string, string) ([]*core.Event, error) {
    48  	panic("implement me")
    49  }
    50  
    51  func (m *MockEventService) ListByUID(context.Context, string) ([]*core.Event, error) {
    52  	panic("implement me")
    53  }
    54  
    55  func (m *MockEventService) ListByUIDs(context.Context, []string) ([]*core.Event, error) {
    56  	panic("implement me")
    57  }
    58  
    59  func (m *MockEventService) ListByFilter(ctx context.Context, filter core.Filter) ([]*core.Event, error) {
    60  	var res []*core.Event
    61  	var err error
    62  	if filter.ObjectID == "testUID" {
    63  		event := &core.Event{
    64  			ID:        0,
    65  			CreatedAt: time.Time{},
    66  			Kind:      "testKind",
    67  			Type:      "testType",
    68  			Reason:    "testReason",
    69  			Message:   "testMessage",
    70  			Name:      "testName",
    71  			Namespace: "testNamespace",
    72  			ObjectID:  "testUID",
    73  		}
    74  		res = append(res, event)
    75  	} else {
    76  		err = errors.New("test err")
    77  	}
    78  	return res, err
    79  }
    80  
    81  func (m *MockEventService) Find(_ context.Context, id uint) (*core.Event, error) {
    82  	var res *core.Event
    83  	var err error
    84  	if id == 0 {
    85  		res = &core.Event{
    86  			ID:        0,
    87  			CreatedAt: time.Time{},
    88  			Kind:      "testKind",
    89  			Type:      "testType",
    90  			Reason:    "testReason",
    91  			Message:   "testMessage",
    92  			Name:      "testName",
    93  			Namespace: "testNamespace",
    94  			ObjectID:  "testUID",
    95  		}
    96  	} else {
    97  		if id == 1 {
    98  			err = gorm.ErrRecordNotFound
    99  		} else {
   100  			err = errors.New("test err")
   101  		}
   102  	}
   103  	return res, err
   104  }
   105  
   106  func (m *MockEventService) Create(context.Context, *core.Event) error {
   107  	panic("implement me")
   108  }
   109  
   110  func (m *MockEventService) DeleteByUIDs(context.Context, []string) error {
   111  	panic("implement me")
   112  }
   113  
   114  func (m *MockEventService) DeleteByCreateTime(context.Context, time.Duration) error {
   115  	panic("implement me")
   116  }
   117  
   118  func (m *MockEventService) DeleteByUID(context.Context, string) error {
   119  	panic("implement me")
   120  }
   121  
   122  func (m *MockEventService) DeleteByTime(context.Context, string, string) error {
   123  	panic("implement me")
   124  }
   125  
   126  func (m *MockEventService) DeleteByDuration(context.Context, time.Duration) error {
   127  	panic("implement me")
   128  }
   129  
   130  func TestEvent(t *testing.T) {
   131  	RegisterFailHandler(Fail)
   132  	RunSpecs(t, "Event Suite")
   133  }
   134  
   135  var _ = Describe("event", func() {
   136  	var router *gin.Engine
   137  	BeforeEach(func() {
   138  		pkgmock.With("AuthMiddleware", true)
   139  
   140  		mockes := new(MockEventService)
   141  
   142  		var s = Service{
   143  			conf: &config.ChaosDashboardConfig{
   144  				ClusterScoped: true,
   145  			},
   146  			event: mockes,
   147  		}
   148  		router = gin.Default()
   149  		r := router.Group("/api")
   150  		endpoint := r.Group("/events")
   151  		endpoint.GET("", s.list)
   152  		endpoint.GET("/:id", s.get)
   153  	})
   154  
   155  	AfterEach(func() {
   156  		// Add any teardown steps that needs to be executed after each test
   157  		pkgmock.Reset("AuthMiddleware")
   158  	})
   159  
   160  	Context("ListEvents", func() {
   161  		It("success", func() {
   162  			response := []*core.Event{
   163  				{
   164  					ID:        0,
   165  					CreatedAt: time.Time{},
   166  					Kind:      "testKind",
   167  					Type:      "testType",
   168  					Reason:    "testReason",
   169  					Message:   "testMessage",
   170  					Name:      "testName",
   171  					Namespace: "testNamespace",
   172  					ObjectID:  "testUID",
   173  				},
   174  			}
   175  			rr := httptest.NewRecorder()
   176  			request, _ := http.NewRequest(http.MethodGet, "/api/events?object_id=testUID", nil)
   177  			router.ServeHTTP(rr, request)
   178  			Expect(rr.Code).Should(Equal(http.StatusOK))
   179  			responseBody, err := json.Marshal(response)
   180  			Expect(err).ShouldNot(HaveOccurred())
   181  			Expect(rr.Body.Bytes()).Should(Equal(responseBody))
   182  		})
   183  
   184  		It("test err", func() {
   185  			rr := httptest.NewRecorder()
   186  			request, _ := http.NewRequest(http.MethodGet, "/api/events?object_id=err", nil)
   187  			router.ServeHTTP(rr, request)
   188  			Expect(rr.Code).Should(Equal(http.StatusInternalServerError))
   189  		})
   190  	})
   191  
   192  	Context("GetEvent", func() {
   193  		It("not found", func() {
   194  			rr := httptest.NewRecorder()
   195  			request, _ := http.NewRequest(http.MethodGet, "/api/events/1", nil)
   196  			router.ServeHTTP(rr, request)
   197  			Expect(rr.Code).Should(Equal(http.StatusNotFound))
   198  		})
   199  	})
   200  })
   201