github.com/thanos-io/thanos@v0.32.5/pkg/model/timeduration_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package model_test
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/prometheus/prometheus/model/timestamp"
    11  	"gopkg.in/alecthomas/kingpin.v2"
    12  
    13  	"github.com/efficientgo/core/testutil"
    14  	"github.com/thanos-io/thanos/pkg/model"
    15  )
    16  
    17  func TestTimeOrDurationValue(t *testing.T) {
    18  	t.Run("positive", func(t *testing.T) {
    19  		cmd := kingpin.New("test", "test")
    20  
    21  		minTime := model.TimeOrDuration(cmd.Flag("min-time", "Start of time range limit to serve"))
    22  
    23  		maxTime := model.TimeOrDuration(cmd.Flag("max-time", "End of time range limit to serve").
    24  			Default("9999-12-31T23:59:59Z"))
    25  
    26  		_, err := cmd.Parse([]string{"--min-time", "10s"})
    27  		if err != nil {
    28  			t.Fatal(err)
    29  		}
    30  
    31  		testutil.Equals(t, "10s", minTime.String())
    32  		testutil.Equals(t, "9999-12-31 23:59:59 +0000 UTC", maxTime.String())
    33  
    34  		prevTime := timestamp.FromTime(time.Now())
    35  		afterTime := timestamp.FromTime(time.Now().Add(15 * time.Second))
    36  
    37  		testutil.Assert(t, minTime.PrometheusTimestamp() > prevTime, "minTime prometheus timestamp is less than time now.")
    38  		testutil.Assert(t, minTime.PrometheusTimestamp() < afterTime, "minTime prometheus timestamp is more than time now + 15s")
    39  
    40  		testutil.Assert(t, maxTime.PrometheusTimestamp() == 253402300799000, "maxTime is not equal to 253402300799000")
    41  	})
    42  
    43  	t.Run("negative", func(t *testing.T) {
    44  		cmd := kingpin.New("test-negative", "test-negative")
    45  		var minTime model.TimeOrDurationValue
    46  		cmd.Flag("min-time", "Start of time range limit to serve").SetValue(&minTime)
    47  		_, err := cmd.Parse([]string{"--min-time=-10s"})
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  		testutil.Equals(t, "-10s", minTime.String())
    52  
    53  		prevTime := timestamp.FromTime(time.Now().Add(-15 * time.Second))
    54  		afterTime := timestamp.FromTime(time.Now())
    55  		testutil.Assert(t, minTime.PrometheusTimestamp() > prevTime, "minTime prometheus timestamp is less than time now - 15s.")
    56  		testutil.Assert(t, minTime.PrometheusTimestamp() < afterTime, "minTime prometheus timestamp is more than time now.")
    57  	})
    58  }