(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func TestTimestamp(t *testing.T) { |
| 12 | fieldByName := func(name string) (baker.FieldIndex, bool) { |
| 13 | if name == "timestamp" { |
| 14 | return 10, true |
| 15 | } |
| 16 | return 0, false |
| 17 | } |
| 18 | |
| 19 | t.Run("ok", func(t *testing.T) { |
| 20 | // Init filter |
| 21 | cfg := baker.FilterParams{ |
| 22 | ComponentParams: baker.ComponentParams{ |
| 23 | DecodedConfig: &TimestampConfig{ |
| 24 | Field: "timestamp", |
| 25 | }, |
| 26 | FieldByName: fieldByName, |
| 27 | }, |
| 28 | } |
| 29 | |
| 30 | filter, err := NewTimestamp(cfg) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | |
| 35 | // Run the filter |
| 36 | ll := &baker.LogLine{FieldSeparator: ','} |
| 37 | filter.Process(ll, func(baker.Record) {}) |
| 38 | |
| 39 | got := ll.Get(10) |
| 40 | if got == nil { |
| 41 | t.Fatalf("got l.Get(10) == nil, want unix timestamp") |
| 42 | } |
| 43 | |
| 44 | // Check an actual timestamp has been set by the filter |
| 45 | i, err := strconv.ParseInt(string(got), 10, 64) |
| 46 | if err != nil { |
| 47 | t.Fatalf("error parsing timestamp: %v", err) |
| 48 | } |
| 49 | |
| 50 | // Check that the timestamp is a reasonable timestamp with respect to now |
| 51 | now := time.Now().UTC() |
| 52 | ts := time.Unix(i, 0).UTC() |
| 53 | if now.Sub(ts).Seconds() > 1 { |
| 54 | t.Fatalf(`timestamp if more than 1s in the past: "%v - %v > 1s"`, now, ts) |
| 55 | } |
| 56 | }) |
| 57 | |
| 58 | t.Run("field error", func(t *testing.T) { |
| 59 | // Init filter |
| 60 | cfg := baker.FilterParams{ |
| 61 | ComponentParams: baker.ComponentParams{ |
| 62 | DecodedConfig: &TimestampConfig{ |
| 63 | Field: "foobar", |
| 64 | }, |
| 65 | FieldByName: fieldByName, |
| 66 | }, |
| 67 | } |
| 68 |
nothing calls this directly
no test coverage detected