github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/disk/disk_test.go (about)

     1  package disk
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/isyscore/isc-gobase/system/common"
     9  )
    10  
    11  func skipIfNotImplementedErr(t *testing.T, err error) {
    12  	if err == common.ErrNotImplementedError {
    13  		t.Skip("not implemented")
    14  	}
    15  }
    16  
    17  func TestDisk_usage(t *testing.T) {
    18  	path := "/"
    19  	if runtime.GOOS == "windows" {
    20  		path = "C:"
    21  	}
    22  	v, err := Usage(path)
    23  	skipIfNotImplementedErr(t, err)
    24  	if err != nil {
    25  		t.Errorf("error %v", err)
    26  	}
    27  	if v.Path != path {
    28  		t.Errorf("error %v", err)
    29  	}
    30  }
    31  
    32  func TestDisk_partitions(t *testing.T) {
    33  	ret, err := Partitions(false)
    34  	skipIfNotImplementedErr(t, err)
    35  	if err != nil || len(ret) == 0 {
    36  		t.Errorf("error %v", err)
    37  	}
    38  	t.Log(ret)
    39  
    40  	empty := PartitionStat{}
    41  	if len(ret) == 0 {
    42  		t.Errorf("ret is empty")
    43  	}
    44  	for _, disk := range ret {
    45  		if disk == empty {
    46  			t.Errorf("Could not get device info %v", disk)
    47  		}
    48  	}
    49  }
    50  
    51  func TestDisk_io_counters(t *testing.T) {
    52  	ret, err := IOCounters()
    53  	skipIfNotImplementedErr(t, err)
    54  	if err != nil {
    55  		t.Errorf("error %v", err)
    56  	}
    57  	if len(ret) == 0 {
    58  		t.Errorf("ret is empty")
    59  	}
    60  	empty := IOCountersStat{}
    61  	for part, io := range ret {
    62  		t.Log(part, io)
    63  		if io == empty {
    64  			t.Errorf("io_counter error %v, %v", part, io)
    65  		}
    66  	}
    67  }
    68  
    69  
    70  func TestDiskUsageStat_String(t *testing.T) {
    71  	v := UsageStat{
    72  		Path:              "/",
    73  		Total:             1000,
    74  		Free:              2000,
    75  		Used:              3000,
    76  		UsedPercent:       50.1,
    77  		InodesTotal:       4000,
    78  		InodesUsed:        5000,
    79  		InodesFree:        6000,
    80  		InodesUsedPercent: 49.1,
    81  		Fstype:            "ext4",
    82  	}
    83  	e := `{"path":"/","fstype":"ext4","total":1000,"free":2000,"used":3000,"usedPercent":50.1,"inodesTotal":4000,"inodesUsed":5000,"inodesFree":6000,"inodesUsedPercent":49.1}`
    84  	if e != fmt.Sprintf("%v", v) {
    85  		t.Errorf("DiskUsageStat string is invalid: %v", v)
    86  	}
    87  }
    88  
    89  func TestDiskPartitionStat_String(t *testing.T) {
    90  	v := PartitionStat{
    91  		Device:     "sd01",
    92  		Mountpoint: "/",
    93  		Fstype:     "ext4",
    94  		Opts:       "ro",
    95  	}
    96  	e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":"ro"}`
    97  	if e != fmt.Sprintf("%v", v) {
    98  		t.Errorf("DiskUsageStat string is invalid: %v", v)
    99  	}
   100  }
   101  
   102  func TestDiskIOCountersStat_String(t *testing.T) {
   103  	v := IOCountersStat{
   104  		Name:         "sd01",
   105  		ReadCount:    100,
   106  		WriteCount:   200,
   107  		ReadBytes:    300,
   108  		WriteBytes:   400,
   109  		SerialNumber: "SERIAL",
   110  	}
   111  	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":""}`
   112  	if e != fmt.Sprintf("%v", v) {
   113  		t.Errorf("DiskUsageStat string is invalid: %v", v)
   114  	}
   115  }