...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/time/time_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/go-logr/zapr"
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  	"go.uber.org/zap"
    26  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/test/pkg/timer"
    29  )
    30  
    31  func TestModifyTime(t *testing.T) {
    32  	RegisterFailHandler(Fail)
    33  
    34  	RunSpecsWithDefaultAndCustomReporters(t,
    35  		"Time Suit",
    36  		[]Reporter{printer.NewlineReporter{}})
    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  	By("register logger")
    46  	zapLog, err := zap.NewDevelopment()
    47  	Expect(err).NotTo(HaveOccurred())
    48  	log := zapr.NewLogger(zapLog)
    49  	RegisterLogger(log)
    50  
    51  	close(done)
    52  })
    53  
    54  // These tests are written in BDD-style using Ginkgo framework. Refer to
    55  // http://onsi.github.io/ginkgo to learn more.
    56  
    57  var _ = Describe("ModifyTime", func() {
    58  
    59  	var t *timer.Timer
    60  
    61  	BeforeEach(func() {
    62  		var err error
    63  
    64  		t, err = timer.StartTimer()
    65  		Expect(err).ShouldNot(HaveOccurred())
    66  	})
    67  
    68  	AfterEach(func() {
    69  		err := t.Stop()
    70  		Expect(err).ShouldNot(HaveOccurred())
    71  	})
    72  
    73  	Context("Modify Time", func() {
    74  		It("should move forward successfully", func() {
    75  			Expect(t).NotTo(BeNil())
    76  
    77  			now, err := t.GetTime()
    78  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    79  
    80  			sec := now.Unix()
    81  
    82  			err = ModifyTime(t.Pid(), 10000, 0, 1)
    83  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    84  
    85  			newTime, err := t.GetTime()
    86  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    87  
    88  			newSec := newTime.Unix()
    89  
    90  			Expect(newSec-sec).Should(BeNumerically(">=", 10000), "sec %d newSec %d", sec, newSec)
    91  		})
    92  
    93  		It("should move backward successfully", func() {
    94  			Expect(t).NotTo(BeNil())
    95  
    96  			now, err := t.GetTime()
    97  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    98  
    99  			sec := now.Unix()
   100  
   101  			err = ModifyTime(t.Pid(), -10000, 0, 1)
   102  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   103  
   104  			newTime, err := t.GetTime()
   105  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   106  
   107  			newSec := newTime.Unix()
   108  
   109  			Expect(10000-(sec-newSec)).Should(BeNumerically("<=", 1), "sec %d newSec %d", sec, newSec)
   110  		})
   111  
   112  		It("should handle nsec overflow", func() {
   113  			Expect(t).NotTo(BeNil())
   114  
   115  			now, err := t.GetTime()
   116  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   117  
   118  			sec := now.Unix()
   119  
   120  			err = ModifyTime(t.Pid(), 0, 1000000000, 1)
   121  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   122  
   123  			newTime, err := t.GetTime()
   124  			Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   125  
   126  			newSec := newTime.Unix()
   127  
   128  			Expect(newSec-sec).Should(BeNumerically(">=", 1), "sec %d newSec %d", sec, newSec)
   129  		})
   130  	})
   131  })
   132