github.com/crowdsecurity/crowdsec@v1.6.1/pkg/parser/enrich_date_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"testing"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/crowdsecurity/go-cs-lib/cstest"
    10  
    11  	"github.com/crowdsecurity/crowdsec/pkg/types"
    12  )
    13  
    14  func TestDateParse(t *testing.T) {
    15  	tests := []struct {
    16  		name        string
    17  		evt         types.Event
    18  		expectedErr string
    19  		expected    string
    20  	}{
    21  		{
    22  			name: "RFC3339",
    23  			evt: types.Event{
    24  				StrTime: "2019-10-12T07:20:50.52Z",
    25  			},
    26  			expected: "2019-10-12T07:20:50.52Z",
    27  		},
    28  		{
    29  			name: "02/Jan/2006:15:04:05 -0700",
    30  			evt: types.Event{
    31  				StrTime: "02/Jan/2006:15:04:05 -0700",
    32  			},
    33  			expected: "2006-01-02T15:04:05-07:00",
    34  		},
    35  		{
    36  			name: "Dec 17 08:17:43",
    37  			evt: types.Event{
    38  				StrTime:       "2011 X 17 zz 08X17X43 oneone Dec",
    39  				StrTimeFormat: "2006 X 2 zz 15X04X05 oneone Jan",
    40  			},
    41  			expected: "2011-12-17T08:17:43Z",
    42  		},
    43  	}
    44  
    45  	logger := log.WithFields(log.Fields{
    46  		"test": "test",
    47  	})
    48  	for _, tt := range tests {
    49  		tt := tt
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			strTime, err := ParseDate(tt.evt.StrTime, &tt.evt, nil, logger)
    52  			cstest.RequireErrorContains(t, err, tt.expectedErr)
    53  			if tt.expectedErr != "" {
    54  				return
    55  			}
    56  			assert.Equal(t, tt.expected, strTime["MarshaledTime"])
    57  		})
    58  	}
    59  }