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"
|
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)
|
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
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) {
|
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 {
|
2015-07-21 14:25:04 +08:00
|
|
|
t.Errorf("ret is empty, %v", ret)
|
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 {
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-08-14 17:08:43 +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) {
|
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
|
|
|
}
|
2014-12-27 22:43:18 +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)
|
|
|
|
}
|
|
|
|
}
|