forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_test.go
83 lines (61 loc) · 2.48 KB
/
clone_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package clone
import (
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/assert"
)
func createTestMetric() telegraf.Metric {
metric, _ := metric.New("m1",
map[string]string{"metric_tag": "from_metric"},
map[string]interface{}{"value": int64(1)},
time.Now(),
)
return metric
}
func calculateProcessedTags(processor Clone, metric telegraf.Metric) map[string]string {
processed := processor.Apply(metric)
return processed[0].Tags()
}
func TestRetainsTags(t *testing.T) {
processor := Clone{}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["metric_tag"]
assert.True(t, present, "Tag of metric was not present")
assert.Equal(t, "from_metric", value, "Value of Tag was changed")
}
func TestAddTags(t *testing.T) {
processor := Clone{Tags: map[string]string{"added_tag": "from_config", "another_tag": ""}}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["added_tag"]
assert.True(t, present, "Additional Tag of metric was not present")
assert.Equal(t, "from_config", value, "Value of Tag was changed")
assert.Equal(t, 3, len(tags), "Should have one previous and two added tags.")
}
func TestOverwritesPresentTagValues(t *testing.T) {
processor := Clone{Tags: map[string]string{"metric_tag": "from_config"}}
tags := calculateProcessedTags(processor, createTestMetric())
value, present := tags["metric_tag"]
assert.True(t, present, "Tag of metric was not present")
assert.Equal(t, 1, len(tags), "Should only have one tag.")
assert.Equal(t, "from_config", value, "Value of Tag was not changed")
}
func TestOverridesName(t *testing.T) {
processor := Clone{NameOverride: "overridden"}
processed := processor.Apply(createTestMetric())
assert.Equal(t, "overridden", processed[0].Name(), "Name was not overridden")
assert.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
}
func TestNamePrefix(t *testing.T) {
processor := Clone{NamePrefix: "Pre-"}
processed := processor.Apply(createTestMetric())
assert.Equal(t, "Pre-m1", processed[0].Name(), "Prefix was not applied")
assert.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
}
func TestNameSuffix(t *testing.T) {
processor := Clone{NameSuffix: "-suff"}
processed := processor.Apply(createTestMetric())
assert.Equal(t, "m1-suff", processed[0].Name(), "Suffix was not applied")
assert.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
}