|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package spool |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/elastic/beats/libbeat/beat" |
| 25 | + "github.com/elastic/beats/libbeat/common" |
| 26 | + "github.com/elastic/beats/libbeat/publisher" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func TestEncodeDecode(t *testing.T) { |
| 31 | + tests := map[string]codecID{ |
| 32 | + "jsob": codecJSON, |
| 33 | + "ubjson": codecUBJSON, |
| 34 | + "cborl": codecCBORL, |
| 35 | + } |
| 36 | + |
| 37 | + event := publisher.Event{ |
| 38 | + Content: beat.Event{ |
| 39 | + Timestamp: time.Now().Round(0), |
| 40 | + Fields: common.MapStr{ |
| 41 | + "time": time.Now().UTC().Round(time.Millisecond), |
| 42 | + "commontime": common.Time(time.Now().UTC().Round(time.Millisecond)), |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + expected := publisher.Event{ |
| 47 | + Content: beat.Event{ |
| 48 | + Timestamp: event.Content.Timestamp, |
| 49 | + Fields: common.MapStr{ |
| 50 | + "time": event.Content.Fields["time"].(time.Time).Format(time.RFC3339Nano), |
| 51 | + "commontime": event.Content.Fields["commontime"].(common.Time).String(), |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for name, codec := range tests { |
| 57 | + t.Run(name, func(t *testing.T) { |
| 58 | + encoder, err := newEncoder(codec) |
| 59 | + assert.NoError(t, err) |
| 60 | + |
| 61 | + encoded, err := encoder.encode(&event) |
| 62 | + assert.NoError(t, err) |
| 63 | + |
| 64 | + decoder := newDecoder() |
| 65 | + decoder.buf = encoded |
| 66 | + |
| 67 | + observed, err := decoder.Decode() |
| 68 | + assert.NoError(t, err) |
| 69 | + |
| 70 | + assert.Equal(t, expected, observed) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments