shirou_gopsutil/disk/disk_test.go

98 lines
2.2 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)
}
empty := PartitionStat{}
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 {
t.Errorf("ret is empty, %v", ret)
}
empty := IOCountersStat{}
for part, io := range ret {
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,"used_percent":50.1,"inodes_total":4000,"inodes_used":5000,"inodes_free":6000,"inodes_used_percent":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
}
e := `{"read_count":100,"write_count":200,"read_bytes":300,"write_bytes":400,"read_time":0,"write_time":0,"name":"sd01","io_time":0,"serial_number":"SERIAL"}`
2014-05-12 10:51:08 +08:00
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}