disk_test.go 2.2 KB
Newer Older
S
Shirou WAKAYAMA 已提交
1
package disk
W
WAKAYAMA Shirou 已提交
2 3

import (
4
	"fmt"
5
	"runtime"
W
WAKAYAMA Shirou 已提交
6 7 8 9
	"testing"
)

func TestDisk_usage(t *testing.T) {
10 11 12 13
	path := "/"
	if runtime.GOOS == "windows" {
		path = "C:"
	}
14
	v, err := DiskUsage(path)
W
WAKAYAMA Shirou 已提交
15 16 17
	if err != nil {
		t.Errorf("error %v", err)
	}
S
Shirou WAKAYAMA 已提交
18 19 20
	if v.Path != path {
		t.Errorf("error %v", err)
	}
W
WAKAYAMA Shirou 已提交
21
}
22 23

func TestDisk_partitions(t *testing.T) {
24
	ret, err := DiskPartitions(false)
25
	if err != nil || len(ret) == 0 {
26 27
		t.Errorf("error %v", err)
	}
28
	empty := DiskPartitionStat{}
S
Shirou WAKAYAMA 已提交
29 30 31 32 33
	for _, disk := range ret {
		if disk == empty {
			t.Errorf("Could not get device info %v", disk)
		}
	}
34
}
35 36

func TestDisk_io_counters(t *testing.T) {
37
	ret, err := DiskIOCounters()
38
	if err != nil {
39 40
		t.Errorf("error %v", err)
	}
41
	if len(ret) == 0 {
42
		t.Errorf("ret is empty, %v", ret)
43
	}
44
	empty := DiskIOCountersStat{}
45
	for part, io := range ret {
46
		fmt.Println(io)
S
Shirou WAKAYAMA 已提交
47
		if io == empty {
48
			t.Errorf("io_counter error %v, %v", part, io)
49 50 51
		}
	}
}
S
Shirou WAKAYAMA 已提交
52 53

func TestDiskUsageStat_String(t *testing.T) {
54
	v := DiskUsageStat{
W
WAKAYAMA Shirou 已提交
55 56 57 58 59 60 61 62 63
		Path:              "/",
		Total:             1000,
		Free:              2000,
		Used:              3000,
		UsedPercent:       50.1,
		InodesTotal:       4000,
		InodesUsed:        5000,
		InodesFree:        6000,
		InodesUsedPercent: 49.1,
S
Shirou WAKAYAMA 已提交
64
	}
65
	e := `{"path":"/","total":1000,"free":2000,"used":3000,"used_percent":50.1,"inodes_total":4000,"inodes_used":5000,"inodes_free":6000,"inodes_used_percent":49.1}`
S
Shirou WAKAYAMA 已提交
66 67 68 69 70 71
	if e != fmt.Sprintf("%v", v) {
		t.Errorf("DiskUsageStat string is invalid: %v", v)
	}
}

func TestDiskPartitionStat_String(t *testing.T) {
72
	v := DiskPartitionStat{
S
Shirou WAKAYAMA 已提交
73 74 75 76 77 78 79 80 81 82 83 84
		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) {
85
	v := DiskIOCountersStat{
86 87 88 89 90 91
		Name:         "sd01",
		ReadCount:    100,
		WriteCount:   200,
		ReadBytes:    300,
		WriteBytes:   400,
		SerialNumber: "SERIAL",
S
Shirou WAKAYAMA 已提交
92
	}
93
	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"}`
S
Shirou WAKAYAMA 已提交
94 95 96 97
	if e != fmt.Sprintf("%v", v) {
		t.Errorf("DiskUsageStat string is invalid: %v", v)
	}
}