forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd_units_linux_test.go
100 lines (96 loc) · 3.12 KB
/
systemd_units_linux_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
package systemd_units
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
)
func TestSystemdUnits(t *testing.T) {
tests := []struct {
name string
line string
tags map[string]string
fields map[string]interface{}
status int
err error
}{
{
name: "example loaded active running",
line: "example.service loaded active running example service description",
tags: map[string]string{"name": "example.service", "load": "loaded", "active": "active", "sub": "running"},
fields: map[string]interface{}{
"load_code": 0,
"active_code": 0,
"sub_code": 0,
},
},
{
name: "example loaded active exited",
line: "example.service loaded active exited example service description",
tags: map[string]string{"name": "example.service", "load": "loaded", "active": "active", "sub": "exited"},
fields: map[string]interface{}{
"load_code": 0,
"active_code": 0,
"sub_code": 4,
},
},
{
name: "example loaded failed failed",
line: "example.service loaded failed failed example service description",
tags: map[string]string{"name": "example.service", "load": "loaded", "active": "failed", "sub": "failed"},
fields: map[string]interface{}{
"load_code": 0,
"active_code": 3,
"sub_code": 12,
},
},
{
name: "example not-found inactive dead",
line: "example.service not-found inactive dead example service description",
tags: map[string]string{"name": "example.service", "load": "not-found", "active": "inactive", "sub": "dead"},
fields: map[string]interface{}{
"load_code": 2,
"active_code": 2,
"sub_code": 1,
},
},
{
name: "example unknown unknown unknown",
line: "example.service unknown unknown unknown example service description",
err: fmt.Errorf("Error parsing field 'load', value not in map: %s", "unknown"),
},
{
name: "example too few fields",
line: "example.service loaded fai",
err: fmt.Errorf("Error parsing line (expected at least 4 fields): %s", "example.service loaded fai"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
systemd_units := &SystemdUnits{
systemctl: func(Timeout internal.Duration, UnitType string) (*bytes.Buffer, error) {
return bytes.NewBufferString(tt.line), nil
},
}
acc := new(testutil.Accumulator)
err := acc.GatherError(systemd_units.Gather)
if !reflect.DeepEqual(tt.err, err) {
t.Errorf("%s: expected error '%#v' got '%#v'", tt.name, tt.err, err)
}
if len(acc.Metrics) > 0 {
m := acc.Metrics[0]
if !reflect.DeepEqual(m.Measurement, measurement) {
t.Errorf("%s: expected measurement '%#v' got '%#v'\n", tt.name, measurement, m.Measurement)
}
if !reflect.DeepEqual(m.Tags, tt.tags) {
t.Errorf("%s: expected tags\n%#v got\n%#v\n", tt.name, tt.tags, m.Tags)
}
if !reflect.DeepEqual(m.Fields, tt.fields) {
t.Errorf("%s: expected fields\n%#v got\n%#v\n", tt.name, tt.fields, m.Fields)
}
}
})
}
}