2019-12-03 08:05:50 +08:00
|
|
|
package systemd_units
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"reflect"
|
2024-01-17 21:56:12 +08:00
|
|
|
"sort"
|
|
|
|
|
"strings"
|
2019-12-03 08:05:50 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-17 21:56:12 +08:00
|
|
|
// Global test definitions and structure.
|
|
|
|
|
// Tests are located within `subcommand_list_test.go` and
|
|
|
|
|
// `subcommand_show_test.go`.
|
|
|
|
|
|
|
|
|
|
type TestDef struct {
|
|
|
|
|
Name string
|
|
|
|
|
Line string
|
|
|
|
|
Lines []string
|
|
|
|
|
Tags map[string]string
|
|
|
|
|
Fields map[string]interface{}
|
|
|
|
|
Status int
|
|
|
|
|
Err error
|
|
|
|
|
}
|
2019-12-03 08:05:50 +08:00
|
|
|
|
2024-01-17 21:56:12 +08:00
|
|
|
func runParserTests(t *testing.T, tests []TestDef, dut *subCommandInfo) {
|
2019-12-03 08:05:50 +08:00
|
|
|
for _, tt := range tests {
|
2024-01-17 21:56:12 +08:00
|
|
|
t.Run(tt.Name, func(t *testing.T) {
|
2019-12-03 08:05:50 +08:00
|
|
|
acc := new(testutil.Accumulator)
|
2024-01-17 21:56:12 +08:00
|
|
|
|
|
|
|
|
var line string
|
|
|
|
|
if len(tt.Lines) > 0 && len(tt.Line) == 0 {
|
|
|
|
|
line = strings.Join(tt.Lines, "\n")
|
|
|
|
|
} else if len(tt.Lines) == 0 && len(tt.Line) > 0 {
|
|
|
|
|
line = tt.Line
|
|
|
|
|
} else {
|
|
|
|
|
t.Error("property Line and Lines set in test definition")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dut.parseResult(acc, bytes.NewBufferString(line))
|
|
|
|
|
err := acc.FirstError()
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.Err, err) {
|
|
|
|
|
t.Errorf("%s: expected error '%#v' got '%#v'", tt.Name, tt.Err, err)
|
2019-12-03 08:05:50 +08:00
|
|
|
}
|
|
|
|
|
if len(acc.Metrics) > 0 {
|
|
|
|
|
m := acc.Metrics[0]
|
|
|
|
|
if !reflect.DeepEqual(m.Measurement, measurement) {
|
2024-01-17 21:56:12 +08:00
|
|
|
t.Errorf("%s: expected measurement '%#v' got '%#v'\n", tt.Name, measurement, m.Measurement)
|
2019-12-03 08:05:50 +08:00
|
|
|
}
|
2024-01-17 21:56:12 +08:00
|
|
|
if !reflect.DeepEqual(m.Tags, tt.Tags) {
|
|
|
|
|
t.Errorf("%s: expected tags\n%#v got\n%#v\n", tt.Name, tt.Tags, m.Tags)
|
2019-12-03 08:05:50 +08:00
|
|
|
}
|
2024-01-17 21:56:12 +08:00
|
|
|
if !reflect.DeepEqual(m.Fields, tt.Fields) {
|
|
|
|
|
t.Errorf("%s: expected fields\n%#v got\n%#v\n", tt.Name, tt.Fields, m.Fields)
|
2019-12-03 08:05:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-17 21:56:12 +08:00
|
|
|
|
|
|
|
|
func runCommandLineTest(t *testing.T, paramsTemplate []string, dut *subCommandInfo, systemdUnits *SystemdUnits) {
|
|
|
|
|
params := *dut.getParameters(systemdUnits)
|
|
|
|
|
|
|
|
|
|
// Because we sort the params and the template array before comparison
|
|
|
|
|
// we have to compare the positional parameters first.
|
|
|
|
|
for i, v := range paramsTemplate {
|
|
|
|
|
if strings.HasPrefix(v, "--") {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v != params[i] {
|
|
|
|
|
t.Errorf("Positional parameter %d is '%s'. Expected '%s'.", i, params[i], v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Because the maps do not lead to a stable order of the "--property"
|
|
|
|
|
// arguments sort all the command line arguments and compare them.
|
|
|
|
|
sort.Strings(params)
|
|
|
|
|
sort.Strings(paramsTemplate)
|
|
|
|
|
if !reflect.DeepEqual(params, paramsTemplate) {
|
|
|
|
|
t.Errorf("Generated list of command line arguments '%#v' do not match expected list command line arguments '%#v'", params, paramsTemplate)
|
|
|
|
|
}
|
|
|
|
|
}
|