Skip to content

Commit

Permalink
Pimp the unit test to be able to use real 'mountinfo' files but faked…
Browse files Browse the repository at this point in the history
… disk-usage.
  • Loading branch information
Sven Rebhan committed Dec 22, 2021
1 parent 8af34c7 commit f239ac3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 59 deletions.
189 changes: 136 additions & 53 deletions plugins/inputs/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

diskUtil "github.com/shirou/gopsutil/v3/disk"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/influxdata/telegraf/testutil"
)
Expand Down Expand Up @@ -388,79 +391,159 @@ func TestDiskUsageIssues(t *testing.T) {
tests := []struct {
name string
prefix string
expected []map[string]string
du diskUtil.UsageStat
expected []telegraf.Metric
}{
{
name: "success",
prefix: "success",
expected: []map[string]string{
{
"device": "dev",
"fstype": "devtmpfs",
"mode": "rw",
"path": "/dev",
},
{
"device": "run",
"fstype": "tmpfs",
"mode": "rw",
"path": "/run",
},
{
"device": "tmpfs",
"fstype": "tmpfs",
"mode": "rw",
"path": "/tmp",
},
{
"device": "nvme0n1p4",
"fstype": "ext4",
"mode": "rw",
"path": "/",
},
prefix: "",
du: diskUtil.UsageStat{
Total: 256,
Free: 46,
Used: 200,
InodesTotal: 2468,
InodesFree: 468,
InodesUsed: 2000,
},
expected: []telegraf.Metric{
testutil.MustMetric(
"disk",
map[string]string{
"device": "tmpfs",
"fstype": "tmpfs",
"mode": "rw",
"path": "/tmp",
},
map[string]interface{}{
"total": uint64(256),
"used": uint64(200),
"free": uint64(46),
"inodes_total": uint64(2468),
"inodes_free": uint64(468),
"inodes_used": uint64(2000),
"used_percent": float64(81.30081300813008),
},
time.Unix(0, 0),
telegraf.Gauge,
),
testutil.MustMetric(
"disk",
map[string]string{
"device": "nvme0n1p4",
"fstype": "ext4",
"mode": "rw",
"path": "/",
},
map[string]interface{}{
"total": uint64(256),
"used": uint64(200),
"free": uint64(46),
"inodes_total": uint64(2468),
"inodes_free": uint64(468),
"inodes_used": uint64(2000),
"used_percent": float64(81.30081300813008),
},
time.Unix(0, 0),
telegraf.Gauge,
),
},
},
{
name: "issue #10297",
prefix: "issue_10297",
expected: []map[string]string{
{
"device": "sda1",
"fstype": "ext4",
"mode": "rw",
"path": "/",
},
{
"device": "sdb",
"fstype": "ext4",
"mode": "rw",
"path": "/tmp",
},
name: "issue 10297",
prefix: "/host",
du: diskUtil.UsageStat{
Total: 256,
Free: 46,
Used: 200,
InodesTotal: 2468,
InodesFree: 468,
InodesUsed: 2000,
},
expected: []telegraf.Metric{
testutil.MustMetric(
"disk",
map[string]string{
"device": "sda1",
"fstype": "ext4",
"mode": "rw",
"path": "/",
},
map[string]interface{}{
"total": uint64(256),
"used": uint64(200),
"free": uint64(46),
"inodes_total": uint64(2468),
"inodes_free": uint64(468),
"inodes_used": uint64(2000),
"used_percent": float64(81.30081300813008),
},
time.Unix(0, 0),
telegraf.Gauge,
),
testutil.MustMetric(
"disk",
map[string]string{
"device": "sdb",
"fstype": "ext4",
"mode": "rw",
"path": "/mnt/storage",
},
map[string]interface{}{
"total": uint64(256),
"used": uint64(200),
"free": uint64(46),
"inodes_total": uint64(2468),
"inodes_free": uint64(468),
"inodes_used": uint64(2000),
"used_percent": float64(81.30081300813008),
},
time.Unix(0, 0),
telegraf.Gauge,
),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup the environment
hostMountPrefix := tt.prefix
hostProcPrefix, err := filepath.Abs(filepath.Join("testdata", strings.ReplaceAll(tt.name, " ", "_")))
require.NoError(t, err)

// Get the partitions in the test-case
os.Clearenv()
prefix, err := filepath.Abs(filepath.Join("testdata", tt.prefix))
require.NoError(t, os.Setenv("HOST_PROC", hostProcPrefix))
partitions, err := diskUtil.Partitions(true)
require.NoError(t, err)
require.NoError(t, os.Setenv("HOST_PROC", prefix))

// Setup the plugin and test
plugin := &DiskStats{Log: testutil.Logger{}}
require.NoError(t, plugin.Init())
// Mock the disk usage
mck := &mock.Mock{}
mps := system.MockPSDisk{SystemPS: &system.SystemPS{PSDiskDeps: &system.MockDiskUsage{Mock: mck}}, Mock: mck}
defer mps.AssertExpectations(t)

mps.On("Partitions", true).Return(partitions, nil)

for _, partition := range partitions {
mountpoint := partition.Mountpoint
if hostMountPrefix != "" {
mountpoint = filepath.Join(hostMountPrefix, partition.Mountpoint)
}
diskUsage := tt.du
diskUsage.Path = mountpoint
diskUsage.Fstype = partition.Fstype
mps.On("PSDiskUsage", mountpoint).Return(&diskUsage, nil)
}
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return(hostMountPrefix)

// Setup the plugin and run the test
var acc testutil.Accumulator
plugin := &DiskStats{ps: &mps}
require.NoError(t, plugin.Gather(&acc))

metrics := acc.GetTelegrafMetrics()
actual := make([]map[string]string, 0, len(tt.expected))
for _, m := range metrics {
actual = append(actual, m.Tags())
}
require.ElementsMatch(t, tt.expected, actual)
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, tt.expected, actual, testutil.IgnoreTime(), testutil.SortMetrics())
})
}
os.Clearenv()
}
2 changes: 1 addition & 1 deletion plugins/inputs/disk/testdata/issue_10297/1/mountinfo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
31 1 8:1 / / rw,relatime shared:1 - ext4 /dev/sda1 rw,discard,errors=remount-ro
126 31 8:16 / /tmp rw,relatime shared:67 - ext4 /dev/sdb rw,discard
126 31 8:16 / /mnt/storage rw,relatime shared:67 - ext4 /dev/sdb rw,discard
4 changes: 0 additions & 4 deletions plugins/inputs/disk/testdata/success/1/mountinfo
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
22 26 0:21 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw
23 26 0:22 / /sys rw,nosuid,nodev,noexec,relatime shared:6 - sysfs sys rw
24 26 0:5 / /dev rw,nosuid,relatime shared:2 - devtmpfs dev rw,size=16386872k,nr_inodes=4096718,mode=755,inode64
25 26 0:23 / /run rw,nosuid,nodev,relatime shared:11 - tmpfs run rw,mode=755,inode64
26 1 259:4 / / rw,relatime shared:1 - ext4 /dev/nvme0n1p4 rw
39 26 0:32 / /tmp rw,nosuid,nodev shared:17 - tmpfs tmpfs rw,size=16427752k,nr_inodes=409600,inode64
2 changes: 1 addition & 1 deletion plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *SystemPS) DiskUsage(
s.Log.Debug("[SystemPS] => kept...")
}

du.Path = p.Mountpoint
du.Path = filepath.Join("/", strings.TrimPrefix(p.Mountpoint, hostMountPrefix))
du.Fstype = p.Fstype
usage = append(usage, du)
partitions = append(partitions, &p)
Expand Down

0 comments on commit f239ac3

Please sign in to comment.