...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/ipset_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  	"context"
    20  	"errors"
    21  	"io/ioutil"
    22  	"os"
    23  	"os/exec"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    26  
    27  	. "github.com/onsi/ginkgo"
    28  	. "github.com/onsi/gomega"
    29  
    30  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
    31  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    32  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    33  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    34  )
    35  
    36  var _ = Describe("ipset server", func() {
    37  	defer mock.With("MockContainerdClient", &test.MockClient{})()
    38  	logger, err := log.NewDefaultZapLogger()
    39  	Expect(err).To(BeNil())
    40  	s, _ := newDaemonServer(crclients.ContainerRuntimeContainerd, logger)
    41  
    42  	Context("createIPSet", func() {
    43  		It("should work", func() {
    44  			defer mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
    45  				Expect(cmd).To(Equal("/usr/local/bin/nsexec"))
    46  				Expect(args[0]).To(Equal("-n"))
    47  				Expect(args[1]).To(Equal("/proc/1/ns/net"))
    48  				Expect(args[2]).To(Equal("--"))
    49  				Expect(args[3]).To(Equal("ipset"))
    50  				Expect(args[4]).To(Equal("create"))
    51  				Expect(args[5]).To(Equal("name"))
    52  				Expect(args[6]).To(Equal("hash:net"))
    53  				return exec.Command("echo", "mock command")
    54  			})()
    55  			err := createIPSet(context.TODO(), logger, true, 1, "name")
    56  			Expect(err).To(BeNil())
    57  		})
    58  
    59  		It("should work since ipset exist", func() {
    60  			// The mockfail.sh will fail only once
    61  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
    62  echo $1
    63  cat > /tmp/mockfail.sh << EOF
    64  #! /bin/sh
    65  exit 0
    66  EOF
    67  exit 1
    68  			`), 0755)
    69  			Expect(err).To(BeNil())
    70  			defer os.Remove("/tmp/mockfail.sh")
    71  			defer mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
    72  				return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
    73  			})()
    74  			err = createIPSet(context.TODO(), logger, true, 1, "name")
    75  			Expect(err).To(BeNil())
    76  		})
    77  
    78  		It("shoud fail on the first command", func() {
    79  			// The mockfail.sh will fail
    80  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
    81  echo $1
    82  exit 1
    83  			`), 0755)
    84  			Expect(err).To(BeNil())
    85  			defer os.Remove("/tmp/mockfail.sh")
    86  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
    87  				return exec.Command("/tmp/mockfail.sh", "fail msg")
    88  			})()
    89  			err = createIPSet(context.TODO(), logger, true, 1, "name")
    90  			Expect(err).ToNot(BeNil())
    91  		})
    92  
    93  		It("shoud fail on the second command", func() {
    94  			// The mockfail.sh will fail
    95  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
    96  echo $1
    97  exit 1
    98  			`), 0755)
    99  			Expect(err).To(BeNil())
   100  			defer os.Remove("/tmp/mockfail.sh")
   101  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   102  				return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
   103  			})()
   104  			err = createIPSet(context.TODO(), logger, true, 1, "name")
   105  			Expect(err).ToNot(BeNil())
   106  		})
   107  	})
   108  
   109  	Context("addIpsToIPSet", func() {
   110  		It("should work", func() {
   111  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   112  				return exec.Command("echo", "mock command")
   113  			})()
   114  			err := addCIDRsToIPSet(context.TODO(), logger, true, 1, "name", []string{"1.1.1.1"})
   115  			Expect(err).To(BeNil())
   116  		})
   117  
   118  		It("should work since ip exist", func() {
   119  			// The mockfail.sh will fail
   120  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
   121  echo $1
   122  exit 1
   123  			`), 0755)
   124  			Expect(err).To(BeNil())
   125  			defer os.Remove("/tmp/mockfail.sh")
   126  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   127  				return exec.Command("/tmp/mockfail.sh", ipExistErr)
   128  			})()
   129  			err = addCIDRsToIPSet(context.TODO(), logger, true, 1, "name", []string{"1.1.1.1"})
   130  			Expect(err).To(BeNil())
   131  		})
   132  
   133  		It("should fail", func() {
   134  			// The mockfail.sh will fail
   135  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
   136  echo $1
   137  exit 1
   138  			`), 0755)
   139  			Expect(err).To(BeNil())
   140  			defer os.Remove("/tmp/mockfail.sh")
   141  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   142  				return exec.Command("/tmp/mockfail.sh", "fail msg")
   143  			})()
   144  			err = addCIDRsToIPSet(context.TODO(), logger, true, 1, "name", []string{"1.1.1.1"})
   145  			Expect(err).ToNot(BeNil())
   146  		})
   147  	})
   148  
   149  	Context("renameIPSet", func() {
   150  		It("should work", func() {
   151  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   152  				return exec.Command("echo", "mock command")
   153  			})()
   154  			err := renameIPSet(context.TODO(), logger, true, 1, "name", "newname")
   155  			Expect(err).To(BeNil())
   156  		})
   157  
   158  		It("should work since ipset exist", func() {
   159  			// The mockfail.sh will fail only once
   160  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
   161  echo $1
   162  cat > /tmp/mockfail.sh << EOF
   163  #! /bin/sh
   164  exit 0
   165  EOF
   166  exit 1
   167  			`), 0755)
   168  			Expect(err).To(BeNil())
   169  			defer os.Remove("/tmp/mockfail.sh")
   170  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   171  				return exec.Command("/tmp/mockfail.sh", ipsetNewNameExistErr)
   172  			})()
   173  			err = renameIPSet(context.TODO(), logger, true, 1, "name", "newname")
   174  			Expect(err).To(BeNil())
   175  		})
   176  
   177  		It("shoud fail on the first command", func() {
   178  			// The mockfail.sh will fail
   179  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
   180  echo $1
   181  exit 1
   182  			`), 0755)
   183  			Expect(err).To(BeNil())
   184  			defer os.Remove("/tmp/mockfail.sh")
   185  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   186  				return exec.Command("/tmp/mockfail.sh", "fail msg")
   187  			})()
   188  			err = renameIPSet(context.TODO(), logger, true, 1, "name", "newname")
   189  			Expect(err).ToNot(BeNil())
   190  		})
   191  
   192  		It("shoud fail on the second command", func() {
   193  			// The mockfail.sh will fail
   194  			err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
   195  echo $1
   196  exit 1
   197  			`), 0755)
   198  			Expect(err).To(BeNil())
   199  			defer os.Remove("/tmp/mockfail.sh")
   200  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   201  				return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
   202  			})()
   203  			err = renameIPSet(context.TODO(), logger, true, 1, "name", "newname")
   204  			Expect(err).ToNot(BeNil())
   205  		})
   206  	})
   207  
   208  	Context("FlushIPSets", func() {
   209  		It("should work", func() {
   210  			defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
   211  				return exec.Command("echo", "mock command")
   212  			})()
   213  			_, err := s.FlushIPSets(context.TODO(), &pb.IPSetsRequest{
   214  				Ipsets: []*pb.IPSet{{
   215  					Name:  "ipset-name",
   216  					Cidrs: []string{"1.1.1.1/32"},
   217  				}},
   218  				ContainerId: "containerd://container-id",
   219  				EnterNS:     true,
   220  			})
   221  			Expect(err).To(BeNil())
   222  		})
   223  
   224  		It("should fail on get pid", func() {
   225  			const errorStr = "mock get pid error"
   226  			defer mock.With("TaskError", errors.New(errorStr))()
   227  			_, err := s.FlushIPSets(context.TODO(), &pb.IPSetsRequest{
   228  				Ipsets: []*pb.IPSet{{
   229  					Name:  "ipset-name",
   230  					Cidrs: []string{"1.1.1.1/32"},
   231  				}},
   232  				ContainerId: "containerd://container-id",
   233  				EnterNS:     true,
   234  			})
   235  			Expect(err).ToNot(BeNil())
   236  			Expect(err.Error()).To(Equal(errorStr))
   237  		})
   238  	})
   239  })
   240