-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[metricbeat] [auditbeat] Add formatted index option to metricbeat / a…
- Loading branch information
Showing
7 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package module | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestProcessorsForConfig(t *testing.T) { | ||
testCases := map[string]struct { | ||
beatInfo beat.Info | ||
configStr string | ||
event beat.Event | ||
expectedFields map[string]string | ||
}{ | ||
"Simple static index": { | ||
configStr: "index: 'test'", | ||
expectedFields: map[string]string{ | ||
"@metadata.raw_index": "test", | ||
}, | ||
}, | ||
"Index with agent info + timestamp": { | ||
beatInfo: beat.Info{Beat: "TestBeat", Version: "3.9.27"}, | ||
configStr: "index: 'beat-%{[agent.name]}-%{[agent.version]}-%{+yyyy.MM.dd}'", | ||
event: beat.Event{Timestamp: time.Date(1999, time.December, 31, 23, 0, 0, 0, time.UTC)}, | ||
expectedFields: map[string]string{ | ||
"@metadata.raw_index": "beat-TestBeat-3.9.27-1999.12.31", | ||
}, | ||
}, | ||
} | ||
for description, test := range testCases { | ||
if test.event.Fields == nil { | ||
test.event.Fields = common.MapStr{} | ||
} | ||
config, err := connectorConfigFromString(test.configStr) | ||
if err != nil { | ||
t.Errorf("[%s] %v", description, err) | ||
continue | ||
} | ||
processors, err := processorsForConfig(test.beatInfo, config) | ||
if err != nil { | ||
t.Errorf("[%s] %v", description, err) | ||
continue | ||
} | ||
processedEvent, err := processors.Run(&test.event) | ||
// We don't check if err != nil, because we are testing the final outcome | ||
// of running the processors, including when some of them fail. | ||
if processedEvent == nil { | ||
t.Errorf("[%s] Unexpected fatal error running processors: %v\n", | ||
description, err) | ||
} | ||
for key, value := range test.expectedFields { | ||
field, err := processedEvent.GetValue(key) | ||
if err != nil { | ||
t.Errorf("[%s] Couldn't get field %s from event: %v", description, key, err) | ||
continue | ||
} | ||
assert.Equal(t, field, value) | ||
fieldStr, ok := field.(string) | ||
if !ok { | ||
// Note that requiring a string here is just to simplify the test setup, | ||
// not a requirement of the underlying api. | ||
t.Errorf("[%s] Field [%s] should be a string", description, key) | ||
continue | ||
} | ||
if fieldStr != value { | ||
t.Errorf("[%s] Event field [%s]: expected [%s], got [%s]", description, key, value, fieldStr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Helper function to convert from YML input string to an unpacked | ||
// connectorConfig | ||
func connectorConfigFromString(s string) (connectorConfig, error) { | ||
config := connectorConfig{} | ||
cfg, err := common.NewConfigFrom(s) | ||
if err != nil { | ||
return config, err | ||
} | ||
if err := cfg.Unpack(&config); err != nil { | ||
return config, err | ||
} | ||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters