disk_test.go 2.3 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)
	}
S
Shirou WAKAYAMA 已提交
21
	fmt.Println(v)
W
WAKAYAMA Shirou 已提交
22
}
23 24

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

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

func TestDiskUsageStat_String(t *testing.T) {
55
	v := DiskUsageStat{
W
WAKAYAMA Shirou 已提交
56 57 58 59 60 61 62 63 64
		Path:              "/",
		Total:             1000,
		Free:              2000,
		Used:              3000,
		UsedPercent:       50.1,
		InodesTotal:       4000,
		InodesUsed:        5000,
		InodesFree:        6000,
		InodesUsedPercent: 49.1,
65
		Fstype:            "ext4",
S
Shirou WAKAYAMA 已提交
66
	}
67
	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}`
S
Shirou WAKAYAMA 已提交
68 69 70 71 72 73
	if e != fmt.Sprintf("%v", v) {
		t.Errorf("DiskUsageStat string is invalid: %v", v)
	}
}

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