2021-08-24 04:37:44 +08:00
|
|
|
//go:build linux
|
2016-03-30 12:39:50 +08:00
|
|
|
|
|
|
|
|
package conntrack
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2018-06-04 10:43:06 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2021-04-23 05:08:03 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-30 12:39:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func restoreDflts(savedFiles, savedDirs []string) {
|
|
|
|
|
dfltFiles = savedFiles
|
|
|
|
|
dfltDirs = savedDirs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNoFilesFound(t *testing.T) {
|
|
|
|
|
defer restoreDflts(dfltFiles, dfltDirs)
|
|
|
|
|
|
|
|
|
|
dfltFiles = []string{"baz.txt"}
|
|
|
|
|
dfltDirs = []string{"./foo/bar"}
|
|
|
|
|
c := &Conntrack{}
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
err := c.Gather(acc)
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.EqualError(t, err, "Conntrack input failed to collect metrics. "+
|
2016-03-30 12:39:50 +08:00
|
|
|
"Is the conntrack kernel module loaded?")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDefaultsUsed(t *testing.T) {
|
|
|
|
|
defer restoreDflts(dfltFiles, dfltDirs)
|
2022-04-19 03:27:59 +08:00
|
|
|
tmpdir := t.TempDir()
|
2016-03-30 12:39:50 +08:00
|
|
|
|
2021-09-29 05:16:32 +08:00
|
|
|
tmpFile, err := os.CreateTemp(tmpdir, "ip_conntrack_count")
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, err)
|
2022-04-19 03:27:59 +08:00
|
|
|
t.Cleanup(func() { require.NoError(t, tmpFile.Close()) })
|
2016-03-30 12:39:50 +08:00
|
|
|
|
|
|
|
|
dfltDirs = []string{tmpdir}
|
|
|
|
|
fname := path.Base(tmpFile.Name())
|
|
|
|
|
dfltFiles = []string{fname}
|
|
|
|
|
|
|
|
|
|
count := 1234321
|
2021-09-29 05:16:32 +08:00
|
|
|
require.NoError(t, os.WriteFile(tmpFile.Name(), []byte(strconv.Itoa(count)), 0660))
|
2016-03-30 12:39:50 +08:00
|
|
|
c := &Conntrack{}
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, c.Gather(acc))
|
2016-03-30 12:39:50 +08:00
|
|
|
acc.AssertContainsFields(t, inputName, map[string]interface{}{
|
|
|
|
|
fname: float64(count)})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestConfigsUsed(t *testing.T) {
|
|
|
|
|
defer restoreDflts(dfltFiles, dfltDirs)
|
2022-04-19 03:27:59 +08:00
|
|
|
tmpdir := t.TempDir()
|
2016-03-30 12:39:50 +08:00
|
|
|
|
2021-09-29 05:16:32 +08:00
|
|
|
cntFile, err := os.CreateTemp(tmpdir, "nf_conntrack_count")
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, err)
|
2022-04-19 03:27:59 +08:00
|
|
|
t.Cleanup(func() { require.NoError(t, cntFile.Close()) })
|
|
|
|
|
|
2021-09-29 05:16:32 +08:00
|
|
|
maxFile, err := os.CreateTemp(tmpdir, "nf_conntrack_max")
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, err)
|
2022-04-19 03:27:59 +08:00
|
|
|
t.Cleanup(func() { require.NoError(t, maxFile.Close()) })
|
2016-03-30 12:39:50 +08:00
|
|
|
|
|
|
|
|
dfltDirs = []string{tmpdir}
|
|
|
|
|
cntFname := path.Base(cntFile.Name())
|
|
|
|
|
maxFname := path.Base(maxFile.Name())
|
|
|
|
|
dfltFiles = []string{cntFname, maxFname}
|
|
|
|
|
|
|
|
|
|
count := 1234321
|
|
|
|
|
max := 9999999
|
2021-09-29 05:16:32 +08:00
|
|
|
require.NoError(t, os.WriteFile(cntFile.Name(), []byte(strconv.Itoa(count)), 0660))
|
|
|
|
|
require.NoError(t, os.WriteFile(maxFile.Name(), []byte(strconv.Itoa(max)), 0660))
|
2016-03-30 12:39:50 +08:00
|
|
|
c := &Conntrack{}
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, c.Gather(acc))
|
2016-03-30 12:39:50 +08:00
|
|
|
|
|
|
|
|
fix := func(s string) string {
|
|
|
|
|
return strings.Replace(s, "nf_", "ip_", 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc.AssertContainsFields(t, inputName,
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
fix(cntFname): float64(count),
|
|
|
|
|
fix(maxFname): float64(max),
|
|
|
|
|
})
|
|
|
|
|
}
|