...

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

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

     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package docker
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"fmt"
    20  
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  
    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("docker client", func() {
    29  	Context("DockerClient GetPidFromContainerID", func() {
    30  		It("should return the magic number 9527", func() {
    31  			defer func() {
    32  				err := mock.With("pid", int(9527))()
    33  				Expect(err).To(BeNil())
    34  			}()
    35  
    36  			m := &test.MockClient{}
    37  			c := DockerClient{client: m}
    38  			pid, err := c.GetPidFromContainerID(context.TODO(), "docker://valid-container-id")
    39  			Expect(err).To(BeNil())
    40  			Expect(pid).To(Equal(uint32(9527)))
    41  		})
    42  
    43  		It("should error with wrong protocol", func() {
    44  			m := &test.MockClient{}
    45  			c := DockerClient{client: m}
    46  			_, err := c.GetPidFromContainerID(context.TODO(), "containerd://this-is-a-wrong-protocol")
    47  			Expect(err).NotTo(BeNil())
    48  			Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", dockerProtocolPrefix)))
    49  		})
    50  
    51  		It("should error on ContainerInspectError", func() {
    52  			errorStr := "this is a mocked error"
    53  			defer func() {
    54  				err := mock.With("ContainerInspectError", errors.New(errorStr))()
    55  				Expect(err).NotTo(BeNil())
    56  			}()
    57  			m := &test.MockClient{}
    58  			c := DockerClient{client: m}
    59  			_, err := c.GetPidFromContainerID(context.TODO(), "docker://valid-container-id")
    60  			Expect(err).NotTo(BeNil())
    61  			Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
    62  		})
    63  	})
    64  
    65  	Context("DockerClient ContainerKillByContainerID", func() {
    66  		It("should work", func() {
    67  			m := &test.MockClient{}
    68  			c := DockerClient{client: m}
    69  			err := c.ContainerKillByContainerID(context.TODO(), "docker://valid-container-id")
    70  			Expect(err).To(BeNil())
    71  		})
    72  
    73  		It("should error on ContainerKill", func() {
    74  			errorStr := "this is a mocked error on ContainerKill"
    75  			m := &test.MockClient{}
    76  			c := DockerClient{client: m}
    77  			defer func() {
    78  				err := mock.With("ContainerKillError", errors.New(errorStr))()
    79  				Expect(err).ToNot(BeNil())
    80  			}()
    81  			err := c.ContainerKillByContainerID(context.TODO(), "docker://valid-container-id")
    82  			Expect(err).ToNot(BeNil())
    83  			Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
    84  		})
    85  
    86  		It("should error on wrong protocol", func() {
    87  			m := &test.MockClient{}
    88  			c := DockerClient{client: m}
    89  			err := c.ContainerKillByContainerID(context.TODO(), "containerd://this-is-a-wrong-protocol")
    90  			Expect(err).ToNot(BeNil())
    91  			Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", dockerProtocolPrefix)))
    92  		})
    93  
    94  		It("should error on short protocol", func() {
    95  			m := &test.MockClient{}
    96  			c := DockerClient{client: m}
    97  			err := c.ContainerKillByContainerID(context.TODO(), "dock:")
    98  			Expect(err).ToNot(BeNil())
    99  			Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a docker container id"))
   100  		})
   101  	})
   102  
   103  })
   104