2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
func TestFilter_ApplyEmpty(t *testing.T) {
|
|
|
|
f := Filter{}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
assert.False(t, f.IsActive())
|
|
|
|
|
|
|
|
assert.True(t, f.Apply("m", map[string]interface{}{"value": int64(1)}, map[string]string{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyTagsDontPass(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filters,
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
assert.True(t, f.IsActive())
|
|
|
|
|
|
|
|
assert.False(t, f.Apply("m",
|
|
|
|
map[string]interface{}{"value": int64(1)},
|
|
|
|
map[string]string{"cpu": "cpu-total"}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyDeleteFields(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"value"},
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
assert.True(t, f.IsActive())
|
|
|
|
|
|
|
|
fields := map[string]interface{}{"value": int64(1), "value2": int64(2)}
|
|
|
|
assert.True(t, f.Apply("m", fields, nil))
|
|
|
|
assert.Equal(t, map[string]interface{}{"value2": int64(2)}, fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyDeleteAllFields(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"value*"},
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
assert.True(t, f.IsActive())
|
|
|
|
|
|
|
|
fields := map[string]interface{}{"value": int64(1), "value2": int64(2)}
|
|
|
|
assert.False(t, f.Apply("m", fields, nil))
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
func TestFilter_Empty(t *testing.T) {
|
|
|
|
f := Filter{}
|
|
|
|
|
|
|
|
measurements := []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"supercalifradjulisticexpialidocious",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range measurements {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
func TestFilter_NamePass(t *testing.T) {
|
2016-01-22 18:54:12 +00:00
|
|
|
f := Filter{
|
2016-02-20 05:35:12 +00:00
|
|
|
NamePass: []string{"foo*", "cpu_usage_idle"},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
func TestFilter_NameDrop(t *testing.T) {
|
2016-01-22 18:54:12 +00:00
|
|
|
f := Filter{
|
2016-02-20 05:35:12 +00:00
|
|
|
NameDrop: []string{"foo*", "cpu_usage_idle"},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldNamePass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FieldPass(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldPass: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FieldDrop(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldFieldPass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagPass(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
TagFilter{
|
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagPass: filters,
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
passes := []map[string]string{
|
|
|
|
{"cpu": "cpu-total"},
|
|
|
|
{"cpu": "cpu-0"},
|
|
|
|
{"cpu": "cpu-1"},
|
|
|
|
{"cpu": "cpu-2"},
|
|
|
|
{"mem": "mem_free"},
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []map[string]string{
|
|
|
|
{"cpu": "cputotal"},
|
|
|
|
{"cpu": "cpu0"},
|
|
|
|
{"cpu": "cpu1"},
|
|
|
|
{"cpu": "cpu2"},
|
|
|
|
{"mem": "mem_used"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagDrop(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
TagFilter{
|
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filters,
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
drops := []map[string]string{
|
|
|
|
{"cpu": "cpu-total"},
|
|
|
|
{"cpu": "cpu-0"},
|
|
|
|
{"cpu": "cpu-1"},
|
|
|
|
{"cpu": "cpu-2"},
|
|
|
|
{"mem": "mem_free"},
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []map[string]string{
|
|
|
|
{"cpu": "cputotal"},
|
|
|
|
{"cpu": "cpu0"},
|
|
|
|
{"cpu": "cpu1"},
|
|
|
|
{"cpu": "cpu2"},
|
|
|
|
{"mem": "mem_used"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
func TestFilter_FilterTagsNoMatches(t *testing.T) {
|
|
|
|
pretags := map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
}
|
|
|
|
f := Filter{
|
|
|
|
TagExclude: []string{"nomatch"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.filterTags(pretags)
|
2016-04-12 23:06:27 +00:00
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
}, pretags)
|
|
|
|
|
|
|
|
f = Filter{
|
|
|
|
TagInclude: []string{"nomatch"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.filterTags(pretags)
|
2016-04-12 23:06:27 +00:00
|
|
|
assert.Equal(t, map[string]string{}, pretags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FilterTagsMatches(t *testing.T) {
|
|
|
|
pretags := map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
}
|
|
|
|
f := Filter{
|
|
|
|
TagExclude: []string{"ho*"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.filterTags(pretags)
|
2016-04-12 23:06:27 +00:00
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"mytag": "foobar",
|
|
|
|
}, pretags)
|
|
|
|
|
|
|
|
pretags = map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
}
|
|
|
|
f = Filter{
|
|
|
|
TagInclude: []string{"my*"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.filterTags(pretags)
|
2016-04-12 23:06:27 +00:00
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"mytag": "foobar",
|
|
|
|
}, pretags)
|
|
|
|
}
|