...

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

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

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