github.com/crowdsecurity/crowdsec@v1.6.1/pkg/acquisition/modules/loki/timestamp_test.go (about)

     1  package loki
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  func TestTimestampFail(t *testing.T) {
    11  	var tt timestamp
    12  	err := yaml.Unmarshal([]byte("plop"), tt)
    13  	if err == nil {
    14  		t.Fail()
    15  	}
    16  }
    17  
    18  func TestTimestampTime(t *testing.T) {
    19  	var tt timestamp
    20  	const ts string = "2022-06-14T12:56:39+02:00"
    21  	err := yaml.Unmarshal([]byte(ts), &tt)
    22  	if err != nil {
    23  		t.Error(err)
    24  		t.Fail()
    25  	}
    26  	if ts != time.Time(tt).Format(time.RFC3339) {
    27  		t.Fail()
    28  	}
    29  }
    30  
    31  func TestTimestampDuration(t *testing.T) {
    32  	var tt timestamp
    33  	err := yaml.Unmarshal([]byte("3h"), &tt)
    34  	if err != nil {
    35  		t.Error(err)
    36  		t.Fail()
    37  	}
    38  	d, err := time.ParseDuration("3h")
    39  	if err != nil {
    40  		t.Error(err)
    41  		t.Fail()
    42  	}
    43  	z := time.Now().Add(-d)
    44  	if z.Round(time.Second) != time.Time(tt).Round(time.Second) {
    45  		t.Fail()
    46  	}
    47  }