...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/router/route_table_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/router

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package router
    15  
    16  import (
    17  	"context"
    18  	"math/rand"
    19  	"reflect"
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	ctx "github.com/chaos-mesh/chaos-mesh/pkg/router/context"
    30  	end "github.com/chaos-mesh/chaos-mesh/pkg/router/endpoint"
    31  )
    32  
    33  func TestRouter(t *testing.T) {
    34  	RegisterFailHandler(Fail)
    35  
    36  	RunSpecsWithDefaultAndCustomReporters(t,
    37  		"Router Suit",
    38  		[]Reporter{envtest.NewlineReporter{}})
    39  }
    40  
    41  var _ = BeforeSuite(func(done Done) {
    42  	rand.Seed(GinkgoRandomSeed())
    43  
    44  	close(done)
    45  })
    46  
    47  type testEndpoint struct {
    48  	output *string
    49  }
    50  
    51  func (e *testEndpoint) Apply(ctx context.Context, req ctrl.Request, chaos v1alpha1.InnerObject) error {
    52  	return nil
    53  }
    54  
    55  func (e *testEndpoint) Recover(ctx context.Context, req ctrl.Request, chaos v1alpha1.InnerObject) error {
    56  	return nil
    57  }
    58  
    59  func (e *testEndpoint) Object() v1alpha1.InnerObject {
    60  	return &v1alpha1.NetworkChaos{}
    61  }
    62  
    63  var _ = Describe("Router", func() {
    64  	It("should register successfully", func() {
    65  		Register("hello", &v1alpha1.NetworkChaos{}, func(obj runtime.Object) bool {
    66  			return true
    67  		}, func(ctx ctx.Context) end.Endpoint {
    68  			return &testEndpoint{}
    69  		})
    70  
    71  		Expect(len(routeTable)).To(Equal(1))
    72  
    73  		typ := reflect.TypeOf(&v1alpha1.NetworkChaos{})
    74  		Expect(routeTable[typ]).NotTo(BeNil())
    75  	})
    76  })
    77