...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/schedule/cron/utils_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/schedule/cron

     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 cron
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/utils/pointer"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  )
    28  
    29  func TestGetRecentUnmetScheduleTime(t *testing.T) {
    30  	g := NewGomegaWithT(t)
    31  
    32  	type testCase struct {
    33  		now               string
    34  		lastScheduleTime  string
    35  		creationTimeStamp string
    36  		schedule          string
    37  		missedRun         *string
    38  		nextRun           *string
    39  		err               error
    40  	}
    41  
    42  	zeroTime := "0001-01-01T00:00:00.000Z"
    43  	testCases := []testCase{
    44  		{
    45  			now:               "2021-04-28T05:59:43.5Z",
    46  			lastScheduleTime:  "2021-04-28T05:59:38.0Z",
    47  			creationTimeStamp: zeroTime,
    48  			schedule:          "@every 5s",
    49  			missedRun:         pointer.StringPtr("2021-04-28T05:59:43.0Z"),
    50  			nextRun:           pointer.StringPtr("2021-04-28T05:59:48.0Z"),
    51  			err:               nil,
    52  		},
    53  		{
    54  			now:               "2021-04-28T06:49:35.079Z",
    55  			lastScheduleTime:  "2021-04-28T06:49:35.000Z",
    56  			creationTimeStamp: zeroTime,
    57  			schedule:          "@every 5s",
    58  			missedRun:         nil,
    59  			nextRun:           pointer.StringPtr("2021-04-28T06:49:40.000Z"),
    60  			err:               nil,
    61  		},
    62  		{
    63  			now:               "2021-04-28T06:49:35.079Z",
    64  			lastScheduleTime:  zeroTime,
    65  			creationTimeStamp: "2021-04-28T06:49:35.000Z",
    66  			schedule:          "@every 5s",
    67  			missedRun:         nil,
    68  			nextRun:           pointer.StringPtr("2021-04-28T06:49:40.000Z"),
    69  			err:               nil,
    70  		},
    71  		{
    72  			now:               "2021-04-28T06:49:38.079Z",
    73  			lastScheduleTime:  zeroTime,
    74  			creationTimeStamp: "2021-04-28T06:49:35.000Z",
    75  			schedule:          "@every 5s",
    76  			missedRun:         nil,
    77  			nextRun:           pointer.StringPtr("2021-04-28T06:49:40.000Z"),
    78  			err:               nil,
    79  		},
    80  		{
    81  			now:               "2021-04-28T06:49:40.079Z",
    82  			lastScheduleTime:  zeroTime,
    83  			creationTimeStamp: "2021-04-28T06:49:35.000Z",
    84  			schedule:          "@every 5s",
    85  			missedRun:         pointer.StringPtr("2021-04-28T06:49:40.000Z"),
    86  			nextRun:           pointer.StringPtr("2021-04-28T06:49:45.000Z"),
    87  			err:               nil,
    88  		},
    89  	}
    90  	for _, t := range testCases {
    91  		now, err := time.Parse(time.RFC3339, t.now)
    92  		g.Expect(err).To(BeNil())
    93  		lastScheduleTime, err := time.Parse(time.RFC3339, t.lastScheduleTime)
    94  		g.Expect(err).To(BeNil())
    95  		createTimeStamp, err := time.Parse(time.RFC3339, t.creationTimeStamp)
    96  		g.Expect(err).To(BeNil())
    97  
    98  		schedule := v1alpha1.Schedule{
    99  			ObjectMeta: metav1.ObjectMeta{
   100  				CreationTimestamp: metav1.Time{
   101  					Time: createTimeStamp,
   102  				},
   103  			},
   104  			Spec: v1alpha1.ScheduleSpec{
   105  				Schedule: t.schedule,
   106  			},
   107  			Status: v1alpha1.ScheduleStatus{
   108  				LastScheduleTime: metav1.Time{
   109  					Time: lastScheduleTime,
   110  				},
   111  			},
   112  		}
   113  		missedRun, nextRun, err := getRecentUnmetScheduleTime(&schedule, now)
   114  
   115  		expectedMissedRun := BeNil()
   116  		expectedNextRun := BeNil()
   117  		expectedErr := BeNil()
   118  		if t.missedRun != nil {
   119  			missedRun, err := time.Parse(time.RFC3339, *t.missedRun)
   120  			g.Expect(err).To(BeNil())
   121  			expectedMissedRun = Equal(&missedRun)
   122  		}
   123  		if t.nextRun != nil {
   124  			nextRun, err := time.Parse(time.RFC3339, *t.nextRun)
   125  			g.Expect(err).To(BeNil())
   126  			expectedNextRun = Equal(&nextRun)
   127  		}
   128  		if t.err != nil {
   129  			expectedErr = Equal(t.err)
   130  		}
   131  
   132  		g.Expect(err).To(expectedErr)
   133  		g.Expect(missedRun).To(expectedMissedRun)
   134  		g.Expect(nextRun).To(expectedNextRun)
   135  	}
   136  }
   137