github.com/secure-build/gitlab-runner@v12.5.0+incompatible/helpers/timeperiod/period_test.go (about)

     1  package timeperiod
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var daysOfWeek = map[time.Weekday]string{
    12  	time.Monday:    "mon",
    13  	time.Tuesday:   "tue",
    14  	time.Wednesday: "wed",
    15  	time.Thursday:  "thu",
    16  	time.Friday:    "fri",
    17  	time.Saturday:  "sat",
    18  	time.Sunday:    "sun",
    19  }
    20  
    21  func testTimePeriods(t *testing.T, seconds int, getCurrentTime func(now time.Time) time.Time, inPeriod bool) {
    22  	location, _ := time.LoadLocation("Local")
    23  	now := time.Date(2017, time.February, 22, 14, 59, seconds, 0, location)
    24  
    25  	minute := now.Minute()
    26  	hour := now.Hour()
    27  	dayofWeek := now.Weekday()
    28  	day := daysOfWeek[dayofWeek]
    29  	periodPattern := fmt.Sprintf("* %d %d * * %s *", minute, hour, day)
    30  
    31  	timePeriods, err := TimePeriods([]string{periodPattern}, location.String())
    32  	assert.NoError(t, err)
    33  	timePeriods.GetCurrentTime = func() time.Time {
    34  		return getCurrentTime(now)
    35  	}
    36  
    37  	t.Logf("Testing periodPattern '%s' with time '%s' and currentTime '%s'", periodPattern, now, timePeriods.GetCurrentTime())
    38  	if inPeriod {
    39  		assert.True(t, timePeriods.InPeriod(), "It should be inside of the period")
    40  	} else {
    41  		assert.False(t, timePeriods.InPeriod(), "It should be outside of the period")
    42  	}
    43  }
    44  
    45  func TestInPeriod(t *testing.T) {
    46  	testTimePeriods(t, 0, func(now time.Time) time.Time { return now }, true)
    47  	// TODO: Decide if this case should be fixed, and how to do this
    48  	testTimePeriods(t, 59, func(now time.Time) time.Time { return now }, false)
    49  	testTimePeriods(t, 0, func(now time.Time) time.Time { return now.Add(time.Hour * 48) }, false)
    50  	testTimePeriods(t, 0, func(now time.Time) time.Time { return now.Add(time.Hour * 4) }, false)
    51  	testTimePeriods(t, 0, func(now time.Time) time.Time { return now.Add(time.Minute * 4) }, false)
    52  }
    53  
    54  func TestInvalidTimezone(t *testing.T) {
    55  	_, err := TimePeriods([]string{}, "InvalidTimezone/String")
    56  	assert.Error(t, err)
    57  }
    58  
    59  func testTimeperiodsWithTimezone(t *testing.T, period, timezone string, month time.Month, day, hour, minute int, inPeriod bool) {
    60  	timePeriods, _ := TimePeriods([]string{period}, timezone)
    61  	timePeriods.GetCurrentTime = func() time.Time {
    62  		return time.Date(2017, month, day, hour, minute, 0, 0, time.UTC)
    63  	}
    64  
    65  	now := timePeriods.GetCurrentTime()
    66  	nowInLocation := now.In(timePeriods.location)
    67  	t.Log(fmt.Sprintf("Checking timeperiod '%s' in timezone '%s' for %s (%s)", period, timezone, now, nowInLocation))
    68  
    69  	if inPeriod {
    70  		assert.True(t, timePeriods.InPeriod(), "It should be inside of the period")
    71  	} else {
    72  		assert.False(t, timePeriods.InPeriod(), "It should be outside of the period")
    73  	}
    74  }
    75  
    76  func TestTimeperiodsWithTimezone(t *testing.T) {
    77  	period := "* * 10-17 * * * *"
    78  	timezone := "Europe/Berlin"
    79  
    80  	// inside or outside of the timeperiod, basing on DST status
    81  	testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 16, 30, true)
    82  	testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 16, 30, false)
    83  
    84  	// always inside of the timeperiod
    85  	testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 14, 30, true)
    86  	testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 14, 30, true)
    87  
    88  	// always outside of the timeperiod
    89  	testTimeperiodsWithTimezone(t, period, timezone, time.January, 1, 20, 30, false)
    90  	testTimeperiodsWithTimezone(t, period, timezone, time.July, 1, 20, 30, false)
    91  }