1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-24 13:48:56 +08:00
shirou_gopsutil/disk/disk_test.go

134 lines
3.0 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2014-12-30 22:09:05 +09:00
package disk
2014-04-18 16:34:47 +09:00
import (
2021-12-04 22:29:38 +01:00
"errors"
"fmt"
2014-04-20 00:03:47 +09:00
"runtime"
"sync"
2014-04-18 16:34:47 +09:00
"testing"
"github.com/shirou/gopsutil/v4/internal/common"
2014-04-18 16:34:47 +09:00
)
func skipIfNotImplementedErr(t *testing.T, err error) {
2021-12-04 22:29:38 +01:00
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}
func TestUsage(t *testing.T) {
2014-04-20 00:03:47 +09:00
path := "/"
if runtime.GOOS == "windows" {
path = "C:"
}
v, err := Usage(path)
skipIfNotImplementedErr(t, err)
2014-04-18 16:34:47 +09:00
if err != nil {
t.Errorf("error %v", err)
}
2014-05-12 11:51:08 +09:00
if v.Path != path {
t.Errorf("error %v", err)
}
2014-04-18 16:34:47 +09:00
}
2014-04-20 01:53:34 +09:00
func TestPartitions(t *testing.T) {
ret, err := Partitions(false)
skipIfNotImplementedErr(t, err)
if err != nil || len(ret) == 0 {
2014-04-20 01:53:34 +09:00
t.Errorf("error %v", err)
}
t.Log(ret)
2016-04-23 23:43:00 +09:00
if len(ret) == 0 {
t.Errorf("ret is empty")
}
2014-05-12 11:51:08 +09:00
for _, disk := range ret {
if disk.Device == "" {
2014-05-12 11:51:08 +09:00
t.Errorf("Could not get device info %v", disk)
}
}
2014-04-20 01:53:34 +09:00
}
2014-04-29 14:59:22 +09:00
func TestIOCounters(t *testing.T) {
ret, err := IOCounters()
skipIfNotImplementedErr(t, err)
if err != nil {
2014-04-29 14:59:22 +09:00
t.Errorf("error %v", err)
}
if len(ret) == 0 {
2016-04-23 23:43:00 +09:00
t.Errorf("ret is empty")
}
empty := IOCountersStat{}
for part, io := range ret {
t.Log(part, io)
2014-05-12 11:51:08 +09:00
if io == empty {
t.Errorf("io_counter error %v, %v", part, io)
2014-04-29 14:59:22 +09:00
}
}
}
2014-05-12 11:51:08 +09:00
// https://github.com/shirou/gopsutil/issues/560 regression test
func TestIOCounters_concurrency_on_darwin_cgo(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("darwin only")
}
var wg sync.WaitGroup
2024-08-17 22:41:29 +09:00
const maxCount = 1000
for i := 1; i < maxCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
IOCounters()
}()
}
wg.Wait()
}
func TestUsageStat_String(t *testing.T) {
v := UsageStat{
2014-08-26 22:17:35 +09: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 11:51:08 +09:00
}
e := `{"path":"/","fstype":"ext4","total":1000,"free":2000,"used":3000,"usedPercent":50.1,"inodesTotal":4000,"inodesUsed":5000,"inodesFree":6000,"inodesUsedPercent":49.1}`
2014-05-12 11:51:08 +09:00
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}
func TestPartitionStat_String(t *testing.T) {
v := PartitionStat{
2014-05-12 11:51:08 +09:00
Device: "sd01",
Mountpoint: "/",
Fstype: "ext4",
Opts: []string{"ro"},
2014-05-12 11:51:08 +09:00
}
e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":["ro"]}`
2014-05-12 11:51:08 +09:00
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}
func TestIOCountersStat_String(t *testing.T) {
v := IOCountersStat{
2014-09-22 16:35:47 +09:00
Name: "sd01",
ReadCount: 100,
WriteCount: 200,
ReadBytes: 300,
WriteBytes: 400,
SerialNumber: "SERIAL",
2014-05-12 11:51:08 +09:00
}
2018-05-02 14:58:36 +09:00
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
2014-05-12 11:51:08 +09:00
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}