disk_test.go 2.1 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 || len(ret) == 0 {
39 40
		t.Errorf("error %v", err)
	}
41
	empty := DiskIOCountersStat{}
42
	for part, io := range ret {
S
Shirou WAKAYAMA 已提交
43
		if io == empty {
44
			t.Errorf("io_counter error %v, %v", part, io)
45 46 47
		}
	}
}
S
Shirou WAKAYAMA 已提交
48 49

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

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