...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/server_test.go

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

     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 chaosdaemon
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	"github.com/prometheus/client_golang/prometheus"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    26  )
    27  
    28  var _ = Describe("netem server", func() {
    29  	Context("newDaemonServer", func() {
    30  		It("should work", func() {
    31  			defer mock.With("MockContainerdClient", &test.MockClient{})()
    32  			_, err := newDaemonServer(crclients.ContainerRuntimeContainerd)
    33  			Expect(err).To(BeNil())
    34  		})
    35  
    36  		It("should fail on CreateContainerRuntimeInfoClient", func() {
    37  			_, err := newDaemonServer("invalid-runtime")
    38  			Expect(err).ToNot(BeNil())
    39  		})
    40  	})
    41  
    42  	Context("newGRPCServer", func() {
    43  		It("should work", func() {
    44  			defer mock.With("MockContainerdClient", &test.MockClient{})()
    45  			_, err := newGRPCServer(crclients.ContainerRuntimeContainerd, &MockRegisterer{}, tlsConfig{})
    46  			Expect(err).To(BeNil())
    47  		})
    48  
    49  		It("should panic", func() {
    50  			Ω(func() {
    51  				defer mock.With("MockContainerdClient", &test.MockClient{})()
    52  				defer mock.With("PanicOnMustRegister", "mock panic")()
    53  				_, err := newGRPCServer(crclients.ContainerRuntimeContainerd, &MockRegisterer{}, tlsConfig{})
    54  				Expect(err).To(BeNil())
    55  			}).Should(Panic())
    56  		})
    57  	})
    58  })
    59  
    60  type MockRegisterer struct {
    61  	RegisterGatherer
    62  }
    63  
    64  func (*MockRegisterer) MustRegister(...prometheus.Collector) {
    65  	if err := mock.On("PanicOnMustRegister"); err != nil {
    66  		panic(err)
    67  	}
    68  }
    69