...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/time_server_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/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
    21  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  
    26  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    28  )
    29  
    30  var _ = Describe("time server", func() {
    31  	defer mock.With("MockContainerdClient", &test.MockClient{})()
    32  	s, _ := newDaemonServer(crclients.ContainerRuntimeContainerd)
    33  
    34  	Context("SetTimeOffset", func() {
    35  		It("should work", func() {
    36  			// Inject nil error to ignore any error
    37  			const ignore = true
    38  			defer mock.With("ModifyTimeError", ignore)()
    39  
    40  			_, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
    41  				ContainerId: "containerd://container-id",
    42  			})
    43  			Expect(err).To(BeNil())
    44  		})
    45  
    46  		It("should fail on get pid", func() {
    47  			const errorStr = "mock error on load container"
    48  			defer mock.With("LoadContainerError", errors.New(errorStr))()
    49  
    50  			_, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
    51  				ContainerId: "containerd://container-id",
    52  			})
    53  			Expect(err).ToNot(BeNil())
    54  			Expect(err.Error()).To(Equal(errorStr))
    55  		})
    56  
    57  		It("should fail on modify time", func() {
    58  			const errorStr = "mock error on modify time"
    59  			defer mock.With("ModifyTimeError", errors.New(errorStr))()
    60  
    61  			_, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
    62  				ContainerId: "containerd://container-id",
    63  			})
    64  			Expect(err).ToNot(BeNil())
    65  			Expect(err.Error()).To(Equal(errorStr))
    66  		})
    67  	})
    68  
    69  	Context("RecoverTimeOffset", func() {
    70  		It("should work", func() {
    71  			// Inject nil error to ignore any error
    72  			const ignore = true
    73  			defer mock.With("ModifyTimeError", ignore)()
    74  
    75  			_, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
    76  				ContainerId: "containerd://container-id",
    77  			})
    78  			Expect(err).To(BeNil())
    79  		})
    80  
    81  		It("should fail on get pid", func() {
    82  			const errorStr = "mock error on load container"
    83  			defer mock.With("LoadContainerError", errors.New(errorStr))()
    84  
    85  			_, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
    86  				ContainerId: "containerd://container-id",
    87  			})
    88  			Expect(err).ToNot(BeNil())
    89  			Expect(err.Error()).To(Equal(errorStr))
    90  		})
    91  
    92  		It("should fail on modify time", func() {
    93  			const errorStr = "mock error on modify time"
    94  			defer mock.With("ModifyTimeError", errors.New(errorStr))()
    95  
    96  			_, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
    97  				ContainerId: "containerd://container-id",
    98  			})
    99  			Expect(err).ToNot(BeNil())
   100  			Expect(err.Error()).To(Equal(errorStr))
   101  		})
   102  	})
   103  })
   104