forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifname_test.go
145 lines (121 loc) · 3.04 KB
/
ifname_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ifname
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/snmp"
si "github.com/influxdata/telegraf/plugins/inputs/snmp"
"github.com/influxdata/telegraf/testutil"
)
func TestTable(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
d := IfName{}
d.Init()
tab, err := d.makeTable("IF-MIB::ifTable")
require.NoError(t, err)
config := snmp.ClientConfig{
Version: 2,
Timeout: internal.Duration{Duration: 5 * time.Second}, // Doesn't work with 0 timeout
}
gs, err := snmp.NewWrapper(config)
require.NoError(t, err)
err = gs.SetAgent("127.0.0.1")
require.NoError(t, err)
err = gs.Connect()
require.NoError(t, err)
// Could use ifIndex but oid index is always the same
m, err := buildMap(gs, tab, "ifDescr")
require.NoError(t, err)
require.NotEmpty(t, m)
}
func TestIfName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
d := IfName{
SourceTag: "ifIndex",
DestTag: "ifName",
AgentTag: "agent",
CacheSize: 1000,
ClientConfig: snmp.ClientConfig{
Version: 2,
Timeout: internal.Duration{Duration: 5 * time.Second}, // Doesn't work with 0 timeout
},
}
err := d.Init()
require.NoError(t, err)
acc := testutil.Accumulator{}
err = d.Start(&acc)
require.NoError(t, err)
m := testutil.MustMetric(
"cpu",
map[string]string{
"ifIndex": "1",
"agent": "127.0.0.1",
},
map[string]interface{}{},
time.Unix(0, 0),
)
expected := testutil.MustMetric(
"cpu",
map[string]string{
"ifIndex": "1",
"agent": "127.0.0.1",
"ifName": "lo",
},
map[string]interface{}{},
time.Unix(0, 0),
)
err = d.addTag(m)
require.NoError(t, err)
testutil.RequireMetricEqual(t, expected, m)
}
func TestGetMap(t *testing.T) {
d := IfName{
CacheSize: 1000,
CacheTTL: config.Duration(10 * time.Second),
}
// Don't run net-snmp commands to look up table names.
d.makeTable = func(agent string) (*si.Table, error) {
return &si.Table{}, nil
}
err := d.Init()
require.NoError(t, err)
expected := nameMap{
1: "ifname1",
2: "ifname2",
}
var remoteCalls int32
// Mock the snmp transaction
d.getMapRemote = func(agent string) (nameMap, error) {
atomic.AddInt32(&remoteCalls, 1)
return expected, nil
}
m, age, err := d.getMap("agent")
require.NoError(t, err)
require.Zero(t, age) // Age is zero when map comes from getMapRemote
require.Equal(t, expected, m)
// Remote call should happen the first time getMap runs
require.Equal(t, int32(1), remoteCalls)
var wg sync.WaitGroup
const thMax = 3
for th := 0; th < thMax; th++ {
wg.Add(1)
go func() {
defer wg.Done()
m, age, err := d.getMap("agent")
require.NoError(t, err)
require.NotZero(t, age) // Age is nonzero when map comes from cache
require.Equal(t, expected, m)
}()
}
wg.Wait()
// Remote call should not happen subsequent times getMap runs
require.Equal(t, int32(1), remoteCalls)
}