|
| 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 model |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + |
| 26 | + "github.com/elastic/elastic-agent-libs/mapstr" |
| 27 | +) |
| 28 | + |
| 29 | +func TestEventFieldsEmpty(t *testing.T) { |
| 30 | + event := APMEvent{} |
| 31 | + beatEvent := event.BeatEvent() |
| 32 | + assert.Empty(t, beatEvent.Fields) |
| 33 | +} |
| 34 | + |
| 35 | +func TestEventFields(t *testing.T) { |
| 36 | + tests := map[string]struct { |
| 37 | + Event Event |
| 38 | + Output mapstr.M |
| 39 | + }{ |
| 40 | + "withOutcome": { |
| 41 | + Event: Event{Outcome: "success"}, |
| 42 | + Output: mapstr.M{ |
| 43 | + "outcome": "success", |
| 44 | + }, |
| 45 | + }, |
| 46 | + "withAction": { |
| 47 | + Event: Event{Action: "process-started"}, |
| 48 | + Output: mapstr.M{ |
| 49 | + "action": "process-started", |
| 50 | + }, |
| 51 | + }, |
| 52 | + "withDataset": { |
| 53 | + Event: Event{Dataset: "access-log"}, |
| 54 | + Output: mapstr.M{ |
| 55 | + "dataset": "access-log", |
| 56 | + }, |
| 57 | + }, |
| 58 | + "withSeverity": { |
| 59 | + Event: Event{Severity: 1}, |
| 60 | + Output: mapstr.M{ |
| 61 | + "severity": int64(1), |
| 62 | + }, |
| 63 | + }, |
| 64 | + "withDuration": { |
| 65 | + Event: Event{Duration: time.Minute}, |
| 66 | + Output: mapstr.M{ |
| 67 | + "duration": int64(60000000000), |
| 68 | + }, |
| 69 | + }, |
| 70 | + "withOutcomeActionDataset": { |
| 71 | + Event: Event{Outcome: "success", Action: "process-started", Dataset: "access-log"}, |
| 72 | + Output: mapstr.M{ |
| 73 | + "outcome": "success", |
| 74 | + "action": "process-started", |
| 75 | + "dataset": "access-log", |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for name, tc := range tests { |
| 81 | + t.Run(name, func(t *testing.T) { |
| 82 | + event := APMEvent{Event: tc.Event} |
| 83 | + beatEvent := event.BeatEvent() |
| 84 | + assert.Equal(t, tc.Output, beatEvent.Fields["event"]) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments