From 7cd2f5b30fa2b738c52db3c76c256cb261b55f21 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 5 Jul 2022 13:25:49 +0300 Subject: [PATCH] [system] Improve tests --- system/info_fs_linux.go | 3 ++- system/info_linux_test.go | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/system/info_fs_linux.go b/system/info_fs_linux.go index 790918cc..226bca69 100644 --- a/system/info_fs_linux.go +++ b/system/info_fs_linux.go @@ -85,7 +85,7 @@ func CalculateIOUtil(io1, io2 map[string]*IOStats, duration time.Duration) map[s itv := uint64(duration / (time.Millisecond * 10)) for device := range io1 { - if io1[device] == nil || io2[device] == nil { + if io2[device] == nil { continue } @@ -118,6 +118,7 @@ func parseIOStats(s *bufio.Scanner) (map[string]*IOStats, error) { device := strutil.ReadField(text, 2, true) if len(device) > 3 { + // Skip devices with names like ram* and loop* if device[:3] == "ram" || device[:3] == "loo" { continue } diff --git a/system/info_linux_test.go b/system/info_linux_test.go index d34ca243..66ac4b63 100644 --- a/system/info_linux_test.go +++ b/system/info_linux_test.go @@ -8,6 +8,7 @@ package system // ////////////////////////////////////////////////////////////////////////////////// // import ( + "bufio" "io/ioutil" "os" "strconv" @@ -448,14 +449,22 @@ func (s *SystemSuite) TestFSUsage(c *C) { c.Assert(err, IsNil) c.Assert(fs, NotNil) + c.Assert(CalculateIOUtil(nil, nil, time.Minute), IsNil) + util = CalculateIOUtil( - map[string]*IOStats{"abc": {IOMs: 10}}, - map[string]*IOStats{"abc": {IOMs: 1840}}, + map[string]*IOStats{"1": {IOMs: 10}, "2": {IOMs: 1}, "3": {IOMs: 11}}, + map[string]*IOStats{"1": {IOMs: 1840}, "2": {IOMs: 10000000}}, time.Minute, ) c.Assert(util, NotNil) - c.Assert(util["abc"], Equals, 3.05) + c.Assert(util["1"], Equals, 3.05) + + bs := bufio.NewScanner(strings.NewReader(" 11 0 vda 0 0 0 0 0 0 0 0 0 0 0\n 1 0 ram0 0 0 0 0 0 0 0 0 0 0 0\n 7 0 loop0 0 0 0 0 0 0 0 0 0 0 0\n")) + st, err := parseIOStats(bs) + + c.Assert(err, IsNil) + c.Assert(st, HasLen, 1) procDiskStatsFile = origProcDiskStatsFile procStatFile = origProcStatFile