2024-02-17 03:48:29 +00:00
|
|
|
// 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"
|
2014-05-01 18:43:11 +09:00
|
|
|
"fmt"
|
2014-04-20 00:03:47 +09:00
|
|
|
"runtime"
|
2019-08-15 19:30:28 +02:00
|
|
|
"sync"
|
2014-04-18 16:34:47 +09:00
|
|
|
"testing"
|
2020-08-29 18:29:36 +02:00
|
|
|
|
2024-02-17 03:48:29 +00:00
|
|
|
"github.com/shirou/gopsutil/v4/internal/common"
|
2014-04-18 16:34:47 +09:00
|
|
|
)
|
|
|
|
|
2020-08-29 18:29:36 +02:00
|
|
|
func skipIfNotImplementedErr(t *testing.T, err error) {
|
2021-12-04 22:29:38 +01:00
|
|
|
if errors.Is(err, common.ErrNotImplementedError) {
|
2020-08-29 18:29:36 +02:00
|
|
|
t.Skip("not implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestUsage(t *testing.T) {
|
2014-04-20 00:03:47 +09:00
|
|
|
path := "/"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "C:"
|
|
|
|
}
|
2016-03-22 23:09:12 +09:00
|
|
|
v, err := Usage(path)
|
2020-08-29 18:29:36 +02:00
|
|
|
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
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestPartitions(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
ret, err := Partitions(false)
|
2020-08-29 18:29:36 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2014-05-25 00:48:01 +09:00
|
|
|
if err != nil || len(ret) == 0 {
|
2014-04-20 01:53:34 +09:00
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2017-12-31 17:15:45 +09:00
|
|
|
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 {
|
2021-11-06 09:53:56 +00:00
|
|
|
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
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestIOCounters(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
ret, err := IOCounters()
|
2020-08-29 18:29:36 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2015-02-22 01:24:08 +09:00
|
|
|
if err != nil {
|
2014-04-29 14:59:22 +09:00
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2015-02-22 01:24:08 +09:00
|
|
|
if len(ret) == 0 {
|
2016-04-23 23:43:00 +09:00
|
|
|
t.Errorf("ret is empty")
|
2015-02-22 01:24:08 +09:00
|
|
|
}
|
2016-03-22 23:09:12 +09:00
|
|
|
empty := IOCountersStat{}
|
2014-07-03 18:20:34 +09:00
|
|
|
for part, io := range ret {
|
2017-12-31 17:15:45 +09:00
|
|
|
t.Log(part, io)
|
2014-05-12 11:51:08 +09:00
|
|
|
if io == empty {
|
2014-07-03 18:20:34 +09:00
|
|
|
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
|
|
|
|
2019-08-15 19:30:28 +02:00
|
|
|
// https://github.com/shirou/gopsutil/issues/560 regression test
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestIOCounters_concurrency_on_darwin_cgo(t *testing.T) {
|
2019-08-15 19:30:28 +02:00
|
|
|
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++ {
|
2019-08-15 19:30:28 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
IOCounters()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestUsageStat_String(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
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,
|
2015-08-14 18:08:43 +09:00
|
|
|
Fstype: "ext4",
|
2014-05-12 11:51:08 +09:00
|
|
|
}
|
2016-03-23 10:52:46 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestPartitionStat_String(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
v := PartitionStat{
|
2014-05-12 11:51:08 +09:00
|
|
|
Device: "sd01",
|
|
|
|
Mountpoint: "/",
|
|
|
|
Fstype: "ext4",
|
2021-11-06 09:53:56 +00:00
|
|
|
Opts: []string{"ro"},
|
2014-05-12 11:51:08 +09:00
|
|
|
}
|
2021-11-06 09:53:56 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:58:24 +00:00
|
|
|
func TestIOCountersStat_String(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|