...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/container_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  	"context"
    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"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    28  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    30  )
    31  
    32  var _ = Describe("container kill", func() {
    33  	defer mock.With("MockContainerdClient", &test.MockClient{})()
    34  	logger, err := log.NewDefaultZapLogger()
    35  	Expect(err).To(BeNil())
    36  	s, _ := newDaemonServer(&crclients.CrClientConfig{
    37  		Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
    38  
    39  	Context("ContainerKill", func() {
    40  		It("should work", func() {
    41  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    42  				Action: &pb.ContainerAction{
    43  					Action: pb.ContainerAction_KILL,
    44  				},
    45  				ContainerId: "containerd://container-id",
    46  			})
    47  			Expect(err).To(BeNil())
    48  		})
    49  
    50  		It("should fail on wrong action type", func() {
    51  			const wrongActionType = 9527
    52  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    53  				Action: &pb.ContainerAction{
    54  					Action: pb.ContainerAction_Action(wrongActionType),
    55  				},
    56  				ContainerId: "containerd://container-id",
    57  			})
    58  			Expect(err).ToNot(BeNil())
    59  			Expect(err.Error()).To(ContainSubstring("not kill"))
    60  		})
    61  
    62  		It("should fail on container kill", func() {
    63  			const errorStr = "mock error on container kill"
    64  			defer mock.With("KillError", errors.New(errorStr))()
    65  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    66  				Action: &pb.ContainerAction{
    67  					Action: pb.ContainerAction_KILL,
    68  				},
    69  				ContainerId: "containerd://container-id",
    70  			})
    71  			Expect(err).ToNot(BeNil())
    72  			Expect(err.Error()).To(Equal(errorStr))
    73  		})
    74  	})
    75  })
    76