...

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 2020 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 chaosdaemon
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    25  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    27  )
    28  
    29  var _ = Describe("container kill", func() {
    30  	defer mock.With("MockContainerdClient", &test.MockClient{})()
    31  	s, _ := newDaemonServer(crclients.ContainerRuntimeContainerd)
    32  
    33  	Context("ContainerKill", func() {
    34  		It("should work", func() {
    35  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    36  				Action: &pb.ContainerAction{
    37  					Action: pb.ContainerAction_KILL,
    38  				},
    39  				ContainerId: "containerd://container-id",
    40  			})
    41  			Expect(err).To(BeNil())
    42  		})
    43  
    44  		It("should fail on wrong action type", func() {
    45  			const wrongActionType = 9527
    46  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    47  				Action: &pb.ContainerAction{
    48  					Action: pb.ContainerAction_Action(wrongActionType),
    49  				},
    50  				ContainerId: "containerd://container-id",
    51  			})
    52  			Expect(err).ToNot(BeNil())
    53  			Expect(err.Error()).To(ContainSubstring("not kill"))
    54  		})
    55  
    56  		It("should fail on container kill", func() {
    57  			const errorStr = "mock error on container kill"
    58  			defer mock.With("KillError", errors.New(errorStr))()
    59  			_, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
    60  				Action: &pb.ContainerAction{
    61  					Action: pb.ContainerAction_KILL,
    62  				},
    63  				ContainerId: "containerd://container-id",
    64  			})
    65  			Expect(err).ToNot(BeNil())
    66  			Expect(err.Error()).To(Equal(errorStr))
    67  		})
    68  	})
    69  })
    70