...

Source file src/github.com/shirou/gopsutil/mem/mem_test.go

Documentation: github.com/shirou/gopsutil/mem

     1  package mem
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/shirou/gopsutil/internal/common"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func skipIfNotImplementedErr(t *testing.T, err error) {
    13  	if err == common.ErrNotImplementedError {
    14  		t.Skip("not implemented")
    15  	}
    16  }
    17  
    18  func TestVirtual_memory(t *testing.T) {
    19  	if runtime.GOOS == "solaris" {
    20  		t.Skip("Only .Total is supported on Solaris")
    21  	}
    22  
    23  	v, err := VirtualMemory()
    24  	skipIfNotImplementedErr(t, err)
    25  	if err != nil {
    26  		t.Errorf("error %v", err)
    27  	}
    28  	empty := &VirtualMemoryStat{}
    29  	if v == empty {
    30  		t.Errorf("error %v", v)
    31  	}
    32  	t.Log(v)
    33  
    34  	assert.True(t, v.Total > 0)
    35  	assert.True(t, v.Available > 0)
    36  	assert.True(t, v.Used > 0)
    37  
    38  	total := v.Used + v.Free + v.Buffers + v.Cached
    39  	totalStr := "used + free + buffers + cached"
    40  	switch runtime.GOOS {
    41  	case "windows":
    42  		total = v.Used + v.Available
    43  		totalStr = "used + available"
    44  	case "darwin", "openbsd":
    45  		total = v.Used + v.Free + v.Cached + v.Inactive
    46  		totalStr = "used + free + cached + inactive"
    47  	case "freebsd":
    48  		total = v.Used + v.Free + v.Cached + v.Inactive + v.Laundry
    49  		totalStr = "used + free + cached + inactive + laundry"
    50  	}
    51  	assert.Equal(t, v.Total, total,
    52  		"Total should be computable (%v): %v", totalStr, v)
    53  
    54  	assert.True(t, runtime.GOOS == "windows" || v.Free > 0)
    55  	assert.True(t, runtime.GOOS == "windows" || v.Available > v.Free,
    56  		"Free should be a subset of Available: %v", v)
    57  
    58  	inDelta := assert.InDelta
    59  	if runtime.GOOS == "windows" {
    60  		inDelta = assert.InEpsilon
    61  	}
    62  	inDelta(t, v.UsedPercent,
    63  		100*float64(v.Used)/float64(v.Total), 0.1,
    64  		"UsedPercent should be how many percent of Total is Used: %v", v)
    65  }
    66  
    67  func TestSwap_memory(t *testing.T) {
    68  	v, err := SwapMemory()
    69  	skipIfNotImplementedErr(t, err)
    70  	if err != nil {
    71  		t.Errorf("error %v", err)
    72  	}
    73  	empty := &SwapMemoryStat{}
    74  	if v == empty {
    75  		t.Errorf("error %v", v)
    76  	}
    77  
    78  	t.Log(v)
    79  }
    80  
    81  func TestVirtualMemoryStat_String(t *testing.T) {
    82  	v := VirtualMemoryStat{
    83  		Total:       10,
    84  		Available:   20,
    85  		Used:        30,
    86  		UsedPercent: 30.1,
    87  		Free:        40,
    88  	}
    89  	e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}`
    90  	if e != fmt.Sprintf("%v", v) {
    91  		t.Errorf("VirtualMemoryStat string is invalid: %v", v)
    92  	}
    93  }
    94  
    95  func TestSwapMemoryStat_String(t *testing.T) {
    96  	v := SwapMemoryStat{
    97  		Total:       10,
    98  		Used:        30,
    99  		Free:        40,
   100  		UsedPercent: 30.1,
   101  		Sin:         1,
   102  		Sout:        2,
   103  		PgIn:        3,
   104  		PgOut:       4,
   105  		PgFault:     5,
   106  		PgMajFault:  6,
   107  	}
   108  	e := `{"total":10,"used":30,"free":40,"usedPercent":30.1,"sin":1,"sout":2,"pgin":3,"pgout":4,"pgfault":5,"pgmajfault":6}`
   109  	if e != fmt.Sprintf("%v", v) {
   110  		t.Errorf("SwapMemoryStat string is invalid: %v", v)
   111  	}
   112  }
   113  
   114  func TestSwapDevices(t *testing.T) {
   115  	v, err := SwapDevices()
   116  	skipIfNotImplementedErr(t, err)
   117  	if err != nil {
   118  		t.Fatalf("error calling SwapDevices: %v", err)
   119  	}
   120  
   121  	t.Logf("SwapDevices() -> %+v", v)
   122  
   123  	if len(v) == 0 {
   124  		t.Fatalf("no swap devices found. [this is expected if the host has swap disabled]")
   125  	}
   126  
   127  	for _, device := range v {
   128  		if device.Name == "" {
   129  			t.Fatalf("deviceName not set in %+v", device)
   130  		}
   131  		if device.FreeBytes == 0 {
   132  			t.Logf("[WARNING] free-bytes is zero in %+v. This might be expected", device)
   133  		}
   134  		if device.UsedBytes == 0 {
   135  			t.Logf("[WARNING] used-bytes is zero in %+v. This might be expected", device)
   136  		}
   137  	}
   138  }
   139  

View as plain text