...

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

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

     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 crclients
    17  
    18  import (
    19  	"fmt"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  	"github.com/pkg/errors"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    27  )
    28  
    29  var _ = Describe("chaosdaemon util", func() {
    30  	Context("CreateContainerRuntimeInfoClient", func() {
    31  		It("should work without socket path", func() {
    32  			_, err := CreateContainerRuntimeInfoClient(&CrClientConfig{Runtime: ContainerRuntimeDocker})
    33  			Expect(err).To(BeNil())
    34  			_, err = CreateContainerRuntimeInfoClient(&CrClientConfig{Runtime: ContainerRuntimeDocker})
    35  			Expect(err).To(BeNil())
    36  			defer func() {
    37  				err := mock.With("MockContainerdClient", &test.MockClient{})()
    38  				Expect(err).To(BeNil())
    39  			}()
    40  			_, err = CreateContainerRuntimeInfoClient(&CrClientConfig{Runtime: ContainerRuntimeContainerd})
    41  			Expect(err).To(BeNil())
    42  		})
    43  
    44  		It("should work with socket path", func() {
    45  			_, err := CreateContainerRuntimeInfoClient(&CrClientConfig{Runtime: ContainerRuntimeDocker})
    46  			Expect(err).To(BeNil())
    47  			_, err = CreateContainerRuntimeInfoClient(&CrClientConfig{
    48  				Runtime:    ContainerRuntimeDocker,
    49  				SocketPath: "/foo/bar/docker.socket"})
    50  			Expect(err).To(BeNil())
    51  			defer func() {
    52  				err := mock.With("MockContainerdClient", &test.MockClient{})()
    53  				Expect(err).To(BeNil())
    54  			}()
    55  			_, err = CreateContainerRuntimeInfoClient(&CrClientConfig{
    56  				Runtime:    ContainerRuntimeContainerd,
    57  				SocketPath: "/foo/bar/containerd.socket"})
    58  			Expect(err).To(BeNil())
    59  		})
    60  
    61  		It("should work with socket path and ns", func() {
    62  			defer func() {
    63  				err := mock.With("MockContainerdClient", &test.MockClient{})()
    64  				Expect(err).To(BeNil())
    65  			}()
    66  			_, err := CreateContainerRuntimeInfoClient(&CrClientConfig{
    67  				Runtime:      ContainerRuntimeContainerd,
    68  				SocketPath:   "/foo/bar/containerd.socket",
    69  				ContainerdNS: "chaos-mesh.org"})
    70  			Expect(err).To(BeNil())
    71  		})
    72  
    73  		It("should error on newContaineredClient", func() {
    74  			errorStr := "this is a mocked error"
    75  
    76  			defer func() {
    77  				err := mock.With("NewContainerdClientError", errors.New(errorStr))()
    78  				Expect(err).To(BeNil())
    79  			}()
    80  			_, err := CreateContainerRuntimeInfoClient(&CrClientConfig{Runtime: ContainerRuntimeContainerd})
    81  			Expect(err).ToNot(BeNil())
    82  			Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
    83  		})
    84  	})
    85  })
    86