...

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/v2"
    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/log"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    27  )
    28  
    29  var _ = Describe("netem server", func() {
    30  	logger, err := log.NewDefaultZapLogger()
    31  	Expect(err).To(BeNil())
    32  
    33  	Context("newDaemonServer", func() {
    34  		It("should work without socket path", func() {
    35  			_, err := newDaemonServer(&crclients.CrClientConfig{Runtime: crclients.ContainerRuntimeDocker}, nil, logger)
    36  			Expect(err).To(BeNil())
    37  			defer mock.With("MockContainerdClient", &test.MockClient{})()
    38  			_, err = newDaemonServer(&crclients.CrClientConfig{Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
    39  			Expect(err).To(BeNil())
    40  		})
    41  
    42  		It("should work with socket path", func() {
    43  			_, err := newDaemonServer(&crclients.CrClientConfig{
    44  				Runtime:    crclients.ContainerRuntimeDocker,
    45  				SocketPath: "/foo/bar/docker.socket"}, nil, logger)
    46  			Expect(err).To(BeNil())
    47  		})
    48  
    49  		It("should work with socket path and ns", func() {
    50  			defer mock.With("MockContainerdClient", &test.MockClient{})()
    51  			_, err := newDaemonServer(&crclients.CrClientConfig{Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
    52  			Expect(err).To(BeNil())
    53  			_, err = newDaemonServer(&crclients.CrClientConfig{
    54  				Runtime:      crclients.ContainerRuntimeContainerd,
    55  				SocketPath:   "/foo/bar/containerd.socket",
    56  				ContainerdNS: "chaos-mesh.org"}, nil, logger)
    57  			Expect(err).To(BeNil())
    58  		})
    59  
    60  		It("should fail on CreateContainerRuntimeInfoClient", func() {
    61  			_, err := newDaemonServer(&crclients.CrClientConfig{Runtime: "invalid-runtime"}, nil, logger)
    62  			Expect(err).ToNot(BeNil())
    63  		})
    64  	})
    65  
    66  	Context("newGRPCServer", func() {
    67  		It("should work", func() {
    68  			defer mock.With("MockContainerdClient", &test.MockClient{})()
    69  			daemonServer, err := newDaemonServer(&crclients.CrClientConfig{Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
    70  			Expect(err).To(BeNil())
    71  			_, err = newGRPCServer(daemonServer, &MockRegisterer{}, tlsConfig{})
    72  			Expect(err).To(BeNil())
    73  		})
    74  
    75  		It("should panic", func() {
    76  			Ω(func() {
    77  				defer mock.With("MockContainerdClient", &test.MockClient{})()
    78  				defer mock.With("PanicOnMustRegister", "mock panic")()
    79  				daemonServer, err := newDaemonServer(&crclients.CrClientConfig{Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
    80  				Expect(err).To(BeNil())
    81  				_, err = newGRPCServer(daemonServer, &MockRegisterer{}, tlsConfig{})
    82  				Expect(err).To(BeNil())
    83  			}).Should(Panic())
    84  		})
    85  	})
    86  })
    87  
    88  type MockRegisterer struct {
    89  	RegisterGatherer
    90  }
    91  
    92  func (*MockRegisterer) MustRegister(...prometheus.Collector) {
    93  	if err := mock.On("PanicOnMustRegister"); err != nil {
    94  		panic(err)
    95  	}
    96  }
    97