...

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

Documentation: github.com/shirou/gopsutil/mem

     1  // +build linux
     2  
     3  package mem
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestVirtualMemoryEx(t *testing.T) {
    16  	v, err := VirtualMemoryEx()
    17  	if err != nil {
    18  		t.Error(err)
    19  	}
    20  
    21  	t.Log(v)
    22  }
    23  
    24  var virtualMemoryTests = []struct {
    25  	mockedRootFS string
    26  	stat         *VirtualMemoryStat
    27  }{
    28  	{"intelcorei5", &VirtualMemoryStat{
    29  		Total:          16502300672,
    30  		Available:      11495358464,
    31  		Used:           3437277184,
    32  		UsedPercent:    20.82907863769651,
    33  		Free:           8783491072,
    34  		Active:         4347392000,
    35  		Inactive:       2938834944,
    36  		Wired:          0,
    37  		Laundry:        0,
    38  		Buffers:        212496384,
    39  		Cached:         4069036032,
    40  		Writeback:      0,
    41  		Dirty:          176128,
    42  		WritebackTmp:   0,
    43  		Shared:         1222402048,
    44  		Slab:           253771776,
    45  		SReclaimable:   186470400,
    46  		SUnreclaim:     67301376,
    47  		PageTables:     65241088,
    48  		SwapCached:     0,
    49  		CommitLimit:    16509730816,
    50  		CommittedAS:    12360818688,
    51  		HighTotal:      0,
    52  		HighFree:       0,
    53  		LowTotal:       0,
    54  		LowFree:        0,
    55  		SwapTotal:      8258580480,
    56  		SwapFree:       8258580480,
    57  		Mapped:         1172627456,
    58  		VMallocTotal:   35184372087808,
    59  		VMallocUsed:    0,
    60  		VMallocChunk:   0,
    61  		HugePagesTotal: 0,
    62  		HugePagesFree:  0,
    63  		HugePageSize:   2097152},
    64  	},
    65  	{"issue1002", &VirtualMemoryStat{
    66  		Total:          260579328,
    67  		Available:      215199744,
    68  		Used:           34328576,
    69  		UsedPercent:    13.173944481121694,
    70  		Free:           124506112,
    71  		Active:         108785664,
    72  		Inactive:       8581120,
    73  		Wired:          0,
    74  		Laundry:        0,
    75  		Buffers:        4915200,
    76  		Cached:         96829440,
    77  		Writeback:      0,
    78  		Dirty:          0,
    79  		WritebackTmp:   0,
    80  		Shared:         0,
    81  		Slab:           9293824,
    82  		SReclaimable:   2764800,
    83  		SUnreclaim:     6529024,
    84  		PageTables:     405504,
    85  		SwapCached:     0,
    86  		CommitLimit:    130289664,
    87  		CommittedAS:    25567232,
    88  		HighTotal:      134217728,
    89  		HighFree:       67784704,
    90  		LowTotal:       126361600,
    91  		LowFree:        56721408,
    92  		SwapTotal:      0,
    93  		SwapFree:       0,
    94  		Mapped:         38793216,
    95  		VMallocTotal:   1996488704,
    96  		VMallocUsed:    0,
    97  		VMallocChunk:   0,
    98  		HugePagesTotal: 0,
    99  		HugePagesFree:  0,
   100  		HugePageSize:   0},
   101  	},
   102  }
   103  
   104  func TestVirtualMemoryLinux(t *testing.T) {
   105  	origProc := os.Getenv("HOST_PROC")
   106  	defer os.Setenv("HOST_PROC", origProc)
   107  
   108  	for _, tt := range virtualMemoryTests {
   109  		t.Run(tt.mockedRootFS, func(t *testing.T) {
   110  			os.Setenv("HOST_PROC", filepath.Join("testdata/linux/virtualmemory/", tt.mockedRootFS, "proc"))
   111  
   112  			stat, err := VirtualMemory()
   113  			skipIfNotImplementedErr(t, err)
   114  			if err != nil {
   115  				t.Errorf("error %v", err)
   116  			}
   117  			if !reflect.DeepEqual(stat, tt.stat) {
   118  				t.Errorf("got: %+v\nwant: %+v", stat, tt.stat)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  const validFile = `Filename				Type		Size		Used		Priority
   125  /dev/dm-2                               partition	67022844	490788		-2
   126  /swapfile                               file		2		1		-3
   127  `
   128  
   129  const invalidFile = `INVALID				Type		Size		Used		Priority
   130  /dev/dm-2                               partition	67022844	490788		-2
   131  /swapfile                               file		1048572		0		-3
   132  `
   133  
   134  func TestParseSwapsFile_ValidFile(t *testing.T) {
   135  	assert := assert.New(t)
   136  	stats, err := parseSwapsFile(strings.NewReader(validFile))
   137  	assert.NoError(err)
   138  
   139  	assert.Equal(*stats[0], SwapDevice{
   140  		Name:      "/dev/dm-2",
   141  		UsedBytes: 502566912,
   142  		FreeBytes: 68128825344,
   143  	})
   144  
   145  	assert.Equal(*stats[1], SwapDevice{
   146  		Name:      "/swapfile",
   147  		UsedBytes: 1024,
   148  		FreeBytes: 1024,
   149  	})
   150  }
   151  
   152  func TestParseSwapsFile_InvalidFile(t *testing.T) {
   153  	_, err := parseSwapsFile(strings.NewReader(invalidFile))
   154  	assert.Error(t, err)
   155  }
   156  
   157  func TestParseSwapsFile_EmptyFile(t *testing.T) {
   158  	_, err := parseSwapsFile(strings.NewReader(""))
   159  	assert.Error(t, err)
   160  }
   161  

View as plain text