...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/time/time_skew_linux_amd64_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/time

     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 time
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/tasks"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    27  	"github.com/chaos-mesh/chaos-mesh/test/pkg/timer"
    28  )
    29  
    30  // These test cases required bin/test/timer as its workload.
    31  // You could use make test-utils to build it.
    32  
    33  func TestModifyTime(t *testing.T) {
    34  	RegisterFailHandler(Fail)
    35  
    36  	RunSpecs(t, "Time Suit")
    37  }
    38  
    39  var _ = BeforeSuite(func(done Done) {
    40  	By("change working directory")
    41  
    42  	err := os.Chdir("../../")
    43  	Expect(err).NotTo(HaveOccurred())
    44  
    45  	close(done)
    46  })
    47  
    48  // These tests are written in BDD-style using Ginkgo framework. Refer to
    49  // http://onsi.github.io/ginkgo to learn more.
    50  
    51  var _ = Describe("ModifyTime", func() {
    52  	var t *timer.Timer
    53  	logger, err := log.NewDefaultZapLogger()
    54  	Expect(err).ShouldNot(HaveOccurred())
    55  	BeforeEach(func() {
    56  		var err error
    57  
    58  		t, err = timer.StartTimer()
    59  		Expect(err).ShouldNot(HaveOccurred())
    60  	})
    61  
    62  	AfterEach(func() {
    63  		err := t.Stop()
    64  		Expect(err).ShouldNot(HaveOccurred())
    65  	})
    66  
    67  	Context("Modify Time", func() {
    68  		It("should move forward successfully", func() {
    69  
    70  			Expect(t).NotTo(BeNil())
    71  			s, err := GetSkew(logger, NewConfig(10000, 0, 1))
    72  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    73  
    74  			now, err := t.GetTime()
    75  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    76  
    77  			sec := now.Unix()
    78  
    79  			err = s.Inject(tasks.SysPID(t.Pid()))
    80  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    81  
    82  			newTime, err := t.GetTime()
    83  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    84  
    85  			newSec := newTime.Unix()
    86  
    87  			Expect(newSec-sec).Should(BeNumerically(">=", 10000), "sec %d newSec %d", sec, newSec)
    88  			Expect(newSec-sec).Should(BeNumerically("<=", 10010), "sec %d newSec %d", sec, newSec)
    89  		})
    90  
    91  		It("should move backward successfully", func() {
    92  			Expect(t).NotTo(BeNil())
    93  			s, err := GetSkew(logger, NewConfig(10000, 0, 1))
    94  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    95  
    96  			err = s.Inject(tasks.SysPID(t.Pid()))
    97  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    98  
    99  			now, err := t.GetTime()
   100  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   101  
   102  			sec := now.Unix()
   103  
   104  			err = s.Recover(tasks.SysPID(t.Pid()))
   105  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   106  
   107  			newTime, err := t.GetTime()
   108  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   109  
   110  			newSec := newTime.Unix()
   111  			Expect(10000-(sec-newSec)).Should(BeNumerically("<=", 1), "sec %d newSec %d", sec, newSec)
   112  		})
   113  
   114  		It("should handle nsec overflow", func() {
   115  			Expect(t).NotTo(BeNil())
   116  
   117  			s, err := GetSkew(logger, NewConfig(0, 1000000000, 1))
   118  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   119  
   120  			now, err := t.GetTime()
   121  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   122  
   123  			sec := now.Unix()
   124  
   125  			err = s.Inject(tasks.SysPID(t.Pid()))
   126  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   127  
   128  			newTime, err := t.GetTime()
   129  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   130  
   131  			newSec := newTime.Unix()
   132  			Expect(newSec-sec).Should(BeNumerically(">=", 1), "sec %d newSec %d", sec, newSec)
   133  		})
   134  	})
   135  })
   136