Skip to content

Commit

Permalink
Improve non-integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Jan 25, 2024
1 parent 85f29c0 commit 310e6bd
Showing 1 changed file with 75 additions and 30 deletions.
105 changes: 75 additions & 30 deletions plugins/inputs/chrony/chrony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,93 @@ import (

func TestGather(t *testing.T) {
c := Chrony{
path: "chronyc",
path: "chronyc",
params: []string{"tracking"},
}
// overwriting exec commands with mock commands
execCommand = fakeExecCommand
defer func() { execCommand = exec.Command }()
var acc testutil.Accumulator

err := c.Gather(&acc)
if err != nil {
t.Fatal(err)
expected := []telegraf.Metric{
metric.New(
"chrony",
map[string]string{
"reference_id": "192.168.1.22",
"leap_status": "not synchronized",
"stratum": "3",
},
map[string]interface{}{
"system_time": 0.000020390,
"last_offset": 0.000012651,
"rms_offset": 0.000025577,
"frequency": -16.001,
"residual_freq": 0.0,
"skew": 0.006,
"root_delay": 0.001655,
"root_dispersion": 0.003307,
"update_interval": 507.2,
},
time.Unix(0, 0),
),
}

tags := map[string]string{
"reference_id": "192.168.1.22",
"leap_status": "not synchronized",
"stratum": "3",
}
fields := map[string]interface{}{
"system_time": 0.000020390,
"last_offset": 0.000012651,
"rms_offset": 0.000025577,
"frequency": -16.001,
"residual_freq": 0.0,
"skew": 0.006,
"root_delay": 0.001655,
"root_dispersion": 0.003307,
"update_interval": 507.2,
options := []cmp.Option{
// tests on linux with go1.20 will add a warning about code coverage, ignore that tag
testutil.IgnoreTags("warning"),
testutil.IgnoreTime(),
}

// tests on linux with go1.20 will add a warning about code coverage
// due to the code coverage dir not being set
delete(acc.Metrics[0].Tags, "warning")
var acc testutil.Accumulator
require.NoError(t, c.Gather(&acc))

acc.AssertContainsTaggedFields(t, "chrony", fields, tags)
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, options...)
}

// test with dns lookup
c.DNSLookup = true
err = c.Gather(&acc)
if err != nil {
t.Fatal(err)
func TestGatherDNS(t *testing.T) {
c := Chrony{
DNSLookup: true,
path: "chronyc",
params: []string{"tracking"},
}
acc.AssertContainsTaggedFields(t, "chrony", fields, tags)
// overwriting exec commands with mock commands
execCommand = fakeExecCommand
defer func() { execCommand = exec.Command }()

expected := []telegraf.Metric{
metric.New(
"chrony",
map[string]string{
"reference_id": "192.168.1.22",
"leap_status": "not synchronized",
"stratum": "3",
},
map[string]interface{}{
"system_time": 0.000020390,
"last_offset": 0.000012651,
"rms_offset": 0.000025577,
"frequency": -16.001,
"residual_freq": 0.0,
"skew": 0.006,
"root_delay": 0.001655,
"root_dispersion": 0.003307,
"update_interval": 507.2,
},
time.Unix(0, 0),
),
}

options := []cmp.Option{
// tests on linux with go1.20 will add a warning about code coverage, ignore that tag
testutil.IgnoreTags("warning"),
testutil.IgnoreTime(),
}

var acc testutil.Accumulator
require.NoError(t, c.Gather(&acc))

actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, options...)
}

// fakeExecCommand is a helper function that mock
Expand Down

0 comments on commit 310e6bd

Please sign in to comment.