2014-12-30 21:09:05 +08:00
|
|
|
package disk
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-05-01 17:43:11 +08:00
|
|
|
"fmt"
|
2014-04-19 23:03:47 +08:00
|
|
|
"runtime"
|
2019-08-16 01:30:28 +08:00
|
|
|
"sync"
|
2014-04-18 15:34:47 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDisk_usage(t *testing.T) {
|
2014-04-19 23:03:47 +08:00
|
|
|
path := "/"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "C:"
|
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := Usage(path)
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
if v.Path != path {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2014-04-20 00:53:34 +08:00
|
|
|
|
|
|
|
func TestDisk_partitions(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
ret, err := Partitions(false)
|
2014-05-24 23:48:01 +08:00
|
|
|
if err != nil || len(ret) == 0 {
|
2014-04-20 00:53:34 +08:00
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2017-12-31 16:15:45 +08:00
|
|
|
t.Log(ret)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
empty := PartitionStat{}
|
2016-04-23 22:43:00 +08:00
|
|
|
if len(ret) == 0 {
|
|
|
|
t.Errorf("ret is empty")
|
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
for _, disk := range ret {
|
|
|
|
if disk == empty {
|
|
|
|
t.Errorf("Could not get device info %v", disk)
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 00:53:34 +08:00
|
|
|
}
|
2014-04-29 13:59:22 +08:00
|
|
|
|
|
|
|
func TestDisk_io_counters(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
ret, err := IOCounters()
|
2015-02-22 00:24:08 +08:00
|
|
|
if err != nil {
|
2014-04-29 13:59:22 +08:00
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2015-02-22 00:24:08 +08:00
|
|
|
if len(ret) == 0 {
|
2016-04-23 22:43:00 +08:00
|
|
|
t.Errorf("ret is empty")
|
2015-02-22 00:24:08 +08:00
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
empty := IOCountersStat{}
|
2014-07-03 17:20:34 +08:00
|
|
|
for part, io := range ret {
|
2017-12-31 16:15:45 +08:00
|
|
|
t.Log(part, io)
|
2014-05-12 10:51:08 +08:00
|
|
|
if io == empty {
|
2014-07-03 17:20:34 +08:00
|
|
|
t.Errorf("io_counter error %v, %v", part, io)
|
2014-04-29 13:59:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
|
2019-08-16 01:30:28 +08:00
|
|
|
// https://github.com/shirou/gopsutil/issues/560 regression test
|
|
|
|
func TestDisk_io_counters_concurrency_on_darwin_cgo(t *testing.T) {
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
t.Skip("darwin only")
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
const max = 1000
|
|
|
|
for i := 1; i < max; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
IOCounters()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2014-05-12 10:51:08 +08:00
|
|
|
func TestDiskUsageStat_String(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := UsageStat{
|
2014-08-26 21:17:35 +08:00
|
|
|
Path: "/",
|
|
|
|
Total: 1000,
|
|
|
|
Free: 2000,
|
|
|
|
Used: 3000,
|
|
|
|
UsedPercent: 50.1,
|
|
|
|
InodesTotal: 4000,
|
|
|
|
InodesUsed: 5000,
|
|
|
|
InodesFree: 6000,
|
|
|
|
InodesUsedPercent: 49.1,
|
2015-08-14 17:08:43 +08:00
|
|
|
Fstype: "ext4",
|
2014-05-12 10:51:08 +08:00
|
|
|
}
|
2016-03-23 09:52:46 +08:00
|
|
|
e := `{"path":"/","fstype":"ext4","total":1000,"free":2000,"used":3000,"usedPercent":50.1,"inodesTotal":4000,"inodesUsed":5000,"inodesFree":6000,"inodesUsedPercent":49.1}`
|
2014-05-12 10:51:08 +08:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("DiskUsageStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiskPartitionStat_String(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := PartitionStat{
|
2014-05-12 10:51:08 +08:00
|
|
|
Device: "sd01",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
|
|
|
Opts: "ro",
|
|
|
|
}
|
|
|
|
e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":"ro"}`
|
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("DiskUsageStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiskIOCountersStat_String(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := IOCountersStat{
|
2014-09-22 15:35:47 +08:00
|
|
|
Name: "sd01",
|
|
|
|
ReadCount: 100,
|
|
|
|
WriteCount: 200,
|
|
|
|
ReadBytes: 300,
|
|
|
|
WriteBytes: 400,
|
|
|
|
SerialNumber: "SERIAL",
|
2014-05-12 10:51:08 +08:00
|
|
|
}
|
2018-05-02 13:58:36 +08:00
|
|
|
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
|
2014-05-12 10:51:08 +08:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("DiskUsageStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
}
|