-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add geo fields to add_host_metadata
processor.
#9392
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,13 @@ | |
package add_host_metadata | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
|
@@ -112,3 +115,103 @@ func TestConfigNetInfoEnabled(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.NotNil(t, v) | ||
} | ||
|
||
func TestConfigGeoEnabled(t *testing.T) { | ||
event := &beat.Event{ | ||
Fields: common.MapStr{}, | ||
Timestamp: time.Now(), | ||
} | ||
|
||
config := map[string]interface{}{ | ||
"geo.name": "yerevan-am", | ||
"geo.location": "40.177200, 44.503490", | ||
"geo.continent_name": "Asia", | ||
"geo.country_iso_code": "AM", | ||
"geo.region_name": "Erevan", | ||
"geo.region_iso_code": "AM-ER", | ||
"geo.city_name": "Yerevan", | ||
} | ||
|
||
testConfig, err := common.NewConfigFrom(config) | ||
assert.NoError(t, err) | ||
|
||
p, err := newHostMetadataProcessor(testConfig) | ||
require.NoError(t, err) | ||
|
||
newEvent, err := p.Run(event) | ||
assert.NoError(t, err) | ||
|
||
for configKey, configValue := range config { | ||
t.Run(fmt.Sprintf("Check of %s", configKey), func(t *testing.T) { | ||
v, err := newEvent.GetValue(fmt.Sprintf("host.%s", configKey)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, configValue, v, "Could not find in %s", newEvent) | ||
}) | ||
} | ||
} | ||
|
||
func TestPartialGeo(t *testing.T) { | ||
event := &beat.Event{ | ||
Fields: common.MapStr{}, | ||
Timestamp: time.Now(), | ||
} | ||
|
||
config := map[string]interface{}{ | ||
"geo.name": "yerevan-am", | ||
"geo.city_name": " ", | ||
} | ||
|
||
testConfig, err := common.NewConfigFrom(config) | ||
assert.NoError(t, err) | ||
|
||
p, err := newHostMetadataProcessor(testConfig) | ||
require.NoError(t, err) | ||
|
||
newEvent, err := p.Run(event) | ||
assert.NoError(t, err) | ||
|
||
v, err := newEvent.Fields.GetValue("host.geo.name") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "yerevan-am", v) | ||
|
||
missing := []string{"continent_name", "country_name", "country_iso_code", "region_name", "region_iso_code", "city_name"} | ||
|
||
for _, k := range missing { | ||
path := "host.geo." + k | ||
v, err = newEvent.Fields.GetValue(path) | ||
|
||
assert.Equal(t, common.ErrKeyNotFound, err, "din expect to find %v", path) | ||
} | ||
} | ||
|
||
func TestGeoLocationValidation(t *testing.T) { | ||
locations := []struct { | ||
str string | ||
valid bool | ||
}{ | ||
{"40.177200, 44.503490", true}, | ||
{"-40.177200, -44.503490", true}, | ||
{"garbage", false}, | ||
{"9999999999", false}, | ||
} | ||
|
||
for _, location := range locations { | ||
t.Run(fmt.Sprintf("Location %s validation should be %t", location.str, location.valid), func(t *testing.T) { | ||
|
||
conf, err := common.NewConfigFrom(map[string]interface{}{ | ||
"geo": map[string]interface{}{ | ||
"location": location.str, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
_, err = newHostMetadataProcessor(conf) | ||
|
||
if location.valid { | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests. Perhaps one more to test "Delete any empty values"? Make sure your empty values include things like empty spaces too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @webmat I've added handling + tests for blank strings. Mind taking a look? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah good stuff. Seems like I noticed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thanks for all the details here.