shirou_gopsutil/disk/disk_test.go

104 lines
2.3 KiB
Go
Raw Normal View History

2014-12-30 21:09:05 +08:00
package disk
2014-04-18 15:34:47 +08:00
import (
"fmt"
2014-04-19 23:03:47 +08:00
"runtime"
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:"
}
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) {
ret, err := Partitions(false)
if err != nil || len(ret) == 0 {
2014-04-20 00:53:34 +08:00
t.Errorf("error %v", err)
}
t.Log(ret)
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) {
ret, err := IOCounters()
if err != nil {
2014-04-29 13:59:22 +08:00
t.Errorf("error %v", err)
}
if len(ret) == 0 {
2016-04-23 22:43:00 +08:00
t.Errorf("ret is empty")
}
empty := IOCountersStat{}
for part, io := range ret {
t.Log(part, io)
2014-05-12 10:51:08 +08:00
if io == empty {
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
func TestDiskUsageStat_String(t *testing.T) {
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,
Fstype: "ext4",
2014-05-12 10:51:08 +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) {
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) {
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)
}
}