2018-07-11 23:43:49 +00:00
|
|
|
package disk
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-18 18:42:58 +00:00
|
|
|
"os"
|
2015-12-11 20:07:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-07-11 23:43:49 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-12-11 20:07:32 +00:00
|
|
|
"github.com/shirou/gopsutil/disk"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-04-18 18:42:58 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2015-12-11 20:07:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-04-18 18:42:58 +00:00
|
|
|
type MockFileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiskUsage(t *testing.T) {
|
|
|
|
mck := &mock.Mock{}
|
2018-07-11 23:43:49 +00:00
|
|
|
mps := system.MockPSDisk{SystemPS: &system.SystemPS{PSDiskDeps: &system.MockDiskUsage{Mock: mck}}, Mock: mck}
|
2017-04-18 18:42:58 +00:00
|
|
|
defer mps.AssertExpectations(t)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
psAll := []disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
2017-09-06 21:28:11 +00:00
|
|
|
Opts: "ro,noatime,nodiratime",
|
2017-04-18 18:42:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Device: "/dev/sdb",
|
|
|
|
Mountpoint: "/home",
|
|
|
|
Fstype: "ext4",
|
2017-09-06 21:28:11 +00:00
|
|
|
Opts: "rw,noatime,nodiratime,errors=remount-ro",
|
2017-04-18 18:42:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
duAll := []disk.UsageStat{
|
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Total: 128,
|
|
|
|
Free: 23,
|
|
|
|
Used: 100,
|
|
|
|
InodesTotal: 1234,
|
|
|
|
InodesFree: 234,
|
|
|
|
InodesUsed: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/home",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Total: 256,
|
|
|
|
Free: 46,
|
|
|
|
Used: 200,
|
|
|
|
InodesTotal: 2468,
|
|
|
|
InodesFree: 468,
|
|
|
|
InodesUsed: 2000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mps.On("Partitions", true).Return(psAll, nil)
|
|
|
|
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return("")
|
|
|
|
mps.On("PSDiskUsage", "/").Return(&duAll[0], nil)
|
|
|
|
mps.On("PSDiskUsage", "/home").Return(&duAll[1], nil)
|
|
|
|
|
|
|
|
err = (&DiskStats{ps: mps}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
numDiskMetrics := acc.NFields()
|
|
|
|
expectedAllDiskMetrics := 14
|
|
|
|
assert.Equal(t, expectedAllDiskMetrics, numDiskMetrics)
|
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"path": "/",
|
|
|
|
"fstype": "ext4",
|
|
|
|
"device": "sda",
|
2017-09-06 21:28:11 +00:00
|
|
|
"mode": "ro",
|
2017-04-18 18:42:58 +00:00
|
|
|
}
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"path": "/home",
|
|
|
|
"fstype": "ext4",
|
|
|
|
"device": "sdb",
|
2017-09-06 21:28:11 +00:00
|
|
|
"mode": "rw",
|
2017-04-18 18:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fields1 := map[string]interface{}{
|
|
|
|
"total": uint64(128),
|
|
|
|
"used": uint64(100),
|
|
|
|
"free": uint64(23),
|
|
|
|
"inodes_total": uint64(1234),
|
|
|
|
"inodes_free": uint64(234),
|
|
|
|
"inodes_used": uint64(1000),
|
|
|
|
"used_percent": float64(81.30081300813008),
|
|
|
|
}
|
|
|
|
fields2 := 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),
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "disk", fields1, tags1)
|
|
|
|
acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)
|
|
|
|
|
|
|
|
// We expect 6 more DiskMetrics to show up with an explicit match on "/"
|
|
|
|
// and /home not matching the /dev in MountPoints
|
|
|
|
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc)
|
|
|
|
assert.Equal(t, expectedAllDiskMetrics+7, acc.NFields())
|
|
|
|
|
|
|
|
// We should see all the diskpoints as MountPoints includes both
|
|
|
|
// / and /home
|
|
|
|
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home"}}).Gather(&acc)
|
|
|
|
assert.Equal(t, 2*expectedAllDiskMetrics+7, acc.NFields())
|
|
|
|
}
|
|
|
|
|
2018-01-31 02:06:53 +00:00
|
|
|
func TestDiskUsageHostMountPrefix(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
partitionStats []disk.PartitionStat
|
|
|
|
usageStats []*disk.UsageStat
|
|
|
|
hostMountPrefix string
|
|
|
|
expectedTags map[string]string
|
|
|
|
expectedFields map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no host mount prefix",
|
|
|
|
partitionStats: []disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Opts: "ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
usageStats: []*disk.UsageStat{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2018-01-31 02:06:53 +00:00
|
|
|
Path: "/",
|
|
|
|
Total: 42,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedTags: map[string]string{
|
|
|
|
"path": "/",
|
|
|
|
"device": "sda",
|
|
|
|
"fstype": "ext4",
|
|
|
|
"mode": "ro",
|
|
|
|
},
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"total": uint64(42),
|
|
|
|
"used": uint64(0),
|
|
|
|
"free": uint64(0),
|
|
|
|
"inodes_total": uint64(0),
|
|
|
|
"inodes_free": uint64(0),
|
|
|
|
"inodes_used": uint64(0),
|
|
|
|
"used_percent": float64(0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "host mount prefix",
|
|
|
|
partitionStats: []disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/hostfs/var",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Opts: "ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
usageStats: []*disk.UsageStat{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2018-01-31 02:06:53 +00:00
|
|
|
Path: "/hostfs/var",
|
|
|
|
Total: 42,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hostMountPrefix: "/hostfs",
|
|
|
|
expectedTags: map[string]string{
|
|
|
|
"path": "/var",
|
|
|
|
"device": "sda",
|
|
|
|
"fstype": "ext4",
|
|
|
|
"mode": "ro",
|
|
|
|
},
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"total": uint64(42),
|
|
|
|
"used": uint64(0),
|
|
|
|
"free": uint64(0),
|
|
|
|
"inodes_total": uint64(0),
|
|
|
|
"inodes_free": uint64(0),
|
|
|
|
"inodes_used": uint64(0),
|
|
|
|
"used_percent": float64(0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "host mount prefix exact match",
|
|
|
|
partitionStats: []disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/hostfs",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Opts: "ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
usageStats: []*disk.UsageStat{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2018-01-31 02:06:53 +00:00
|
|
|
Path: "/hostfs",
|
|
|
|
Total: 42,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hostMountPrefix: "/hostfs",
|
|
|
|
expectedTags: map[string]string{
|
|
|
|
"path": "/",
|
|
|
|
"device": "sda",
|
|
|
|
"fstype": "ext4",
|
|
|
|
"mode": "ro",
|
|
|
|
},
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"total": uint64(42),
|
|
|
|
"used": uint64(0),
|
|
|
|
"free": uint64(0),
|
|
|
|
"inodes_total": uint64(0),
|
|
|
|
"inodes_free": uint64(0),
|
|
|
|
"inodes_used": uint64(0),
|
|
|
|
"used_percent": float64(0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
mck := &mock.Mock{}
|
2018-07-11 23:43:49 +00:00
|
|
|
mps := system.MockPSDisk{SystemPS: &system.SystemPS{PSDiskDeps: &system.MockDiskUsage{Mock: mck}}, Mock: mck}
|
2018-01-31 02:06:53 +00:00
|
|
|
defer mps.AssertExpectations(t)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
mps.On("Partitions", true).Return(tt.partitionStats, nil)
|
|
|
|
|
|
|
|
for _, v := range tt.usageStats {
|
|
|
|
mps.On("PSDiskUsage", v.Path).Return(v, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
mps.On("OSGetenv", "HOST_MOUNT_PREFIX").Return(tt.hostMountPrefix)
|
|
|
|
|
|
|
|
err = (&DiskStats{ps: mps}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "disk", tt.expectedFields, tt.expectedTags)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
func TestDiskStats(t *testing.T) {
|
2018-07-11 23:43:49 +00:00
|
|
|
var mps system.MockPS
|
2015-12-11 20:07:32 +00:00
|
|
|
defer mps.AssertExpectations(t)
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
var err error
|
|
|
|
|
2016-05-19 14:05:08 +00:00
|
|
|
duAll := []*disk.UsageStat{
|
2015-12-11 20:07:32 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Total: 128,
|
|
|
|
Free: 23,
|
2016-01-26 00:34:09 +00:00
|
|
|
Used: 100,
|
2015-12-11 20:07:32 +00:00
|
|
|
InodesTotal: 1234,
|
|
|
|
InodesFree: 234,
|
2016-01-26 00:34:09 +00:00
|
|
|
InodesUsed: 1000,
|
2015-12-11 20:07:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/home",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Total: 256,
|
|
|
|
Free: 46,
|
2016-01-26 00:34:09 +00:00
|
|
|
Used: 200,
|
2015-12-11 20:07:32 +00:00
|
|
|
InodesTotal: 2468,
|
|
|
|
InodesFree: 468,
|
2016-01-26 00:34:09 +00:00
|
|
|
InodesUsed: 2000,
|
2015-12-11 20:07:32 +00:00
|
|
|
},
|
|
|
|
}
|
2016-05-19 14:05:08 +00:00
|
|
|
duFiltered := []*disk.UsageStat{
|
2016-01-20 17:42:55 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Total: 128,
|
|
|
|
Free: 23,
|
2016-01-26 00:34:09 +00:00
|
|
|
Used: 100,
|
2016-01-20 17:42:55 +00:00
|
|
|
InodesTotal: 1234,
|
|
|
|
InodesFree: 234,
|
2016-01-26 00:34:09 +00:00
|
|
|
InodesUsed: 1000,
|
2016-01-20 17:42:55 +00:00
|
|
|
},
|
|
|
|
}
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-12-05 17:42:36 +00:00
|
|
|
psAll := []*disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
2017-09-06 21:28:11 +00:00
|
|
|
Opts: "ro,noatime,nodiratime",
|
2016-12-05 17:42:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Device: "/dev/sdb",
|
|
|
|
Mountpoint: "/home",
|
|
|
|
Fstype: "ext4",
|
2017-09-06 21:28:11 +00:00
|
|
|
Opts: "rw,noatime,nodiratime,errors=remount-ro",
|
2016-12-05 17:42:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
psFiltered := []*disk.PartitionStat{
|
|
|
|
{
|
|
|
|
Device: "/dev/sda",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
2017-09-06 21:28:11 +00:00
|
|
|
Opts: "ro,noatime,nodiratime",
|
2016-12-05 17:42:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mps.On("DiskUsage", []string(nil), []string(nil)).Return(duAll, psAll, nil)
|
|
|
|
mps.On("DiskUsage", []string{"/", "/dev"}, []string(nil)).Return(duFiltered, psFiltered, nil)
|
|
|
|
mps.On("DiskUsage", []string{"/", "/home"}, []string(nil)).Return(duAll, psAll, nil)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
err = (&DiskStats{ps: &mps}).Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
numDiskMetrics := acc.NFields()
|
|
|
|
expectedAllDiskMetrics := 14
|
|
|
|
assert.Equal(t, expectedAllDiskMetrics, numDiskMetrics)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"path": "/",
|
|
|
|
"fstype": "ext4",
|
2016-12-05 17:42:36 +00:00
|
|
|
"device": "sda",
|
2017-09-06 21:28:11 +00:00
|
|
|
"mode": "ro",
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"path": "/home",
|
|
|
|
"fstype": "ext4",
|
2016-12-05 17:42:36 +00:00
|
|
|
"device": "sdb",
|
2017-09-06 21:28:11 +00:00
|
|
|
"mode": "rw",
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:58:35 +00:00
|
|
|
fields1 := map[string]interface{}{
|
2016-01-20 17:42:55 +00:00
|
|
|
"total": uint64(128),
|
2016-01-26 00:34:09 +00:00
|
|
|
"used": uint64(100),
|
2016-01-20 17:42:55 +00:00
|
|
|
"free": uint64(23),
|
|
|
|
"inodes_total": uint64(1234),
|
|
|
|
"inodes_free": uint64(234),
|
|
|
|
"inodes_used": uint64(1000),
|
2016-01-26 00:34:09 +00:00
|
|
|
"used_percent": float64(81.30081300813008),
|
2016-01-05 23:58:35 +00:00
|
|
|
}
|
|
|
|
fields2 := map[string]interface{}{
|
2016-01-20 17:42:55 +00:00
|
|
|
"total": uint64(256),
|
2016-01-26 00:34:09 +00:00
|
|
|
"used": uint64(200),
|
2016-01-20 17:42:55 +00:00
|
|
|
"free": uint64(46),
|
|
|
|
"inodes_total": uint64(2468),
|
|
|
|
"inodes_free": uint64(468),
|
|
|
|
"inodes_used": uint64(2000),
|
2016-01-26 00:34:09 +00:00
|
|
|
"used_percent": float64(81.30081300813008),
|
2016-01-05 23:58:35 +00:00
|
|
|
}
|
2016-01-06 00:28:15 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "disk", fields1, tags1)
|
|
|
|
acc.AssertContainsTaggedFields(t, "disk", fields2, tags2)
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
// We expect 6 more DiskMetrics to show up with an explicit match on "/"
|
2016-01-20 17:42:55 +00:00
|
|
|
// and /home not matching the /dev in MountPoints
|
|
|
|
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/dev"}}).Gather(&acc)
|
2016-01-27 23:15:14 +00:00
|
|
|
assert.Equal(t, expectedAllDiskMetrics+7, acc.NFields())
|
2015-12-11 20:07:32 +00:00
|
|
|
|
2016-01-20 17:42:55 +00:00
|
|
|
// We should see all the diskpoints as MountPoints includes both
|
2015-12-11 20:07:32 +00:00
|
|
|
// / and /home
|
2016-01-20 17:42:55 +00:00
|
|
|
err = (&DiskStats{ps: &mps, MountPoints: []string{"/", "/home"}}).Gather(&acc)
|
2016-01-27 23:15:14 +00:00
|
|
|
assert.Equal(t, 2*expectedAllDiskMetrics+7, acc.NFields())
|
2015-12-11 20:07:32 +00:00
|
|
|
}
|