1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/mem/mem_plan9_test.go
Eng Zer Jun 37894e9b28
test: use T.Setenv to set env vars in tests
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-12-20 13:13:01 +08:00

74 lines
1.4 KiB
Go

//go:build plan9
// +build plan9
package mem
import (
"reflect"
"testing"
)
var virtualMemoryTests = []struct {
mockedRootFS string
stat *VirtualMemoryStat
}{
{
"swap", &VirtualMemoryStat{
Total: 1071185920,
Available: 808370176,
Used: 11436032,
UsedPercent: 1.3949677238843257,
Free: 808370176,
SwapTotal: 655360000,
SwapFree: 655360000,
},
},
}
func TestVirtualMemoryPlan9(t *testing.T) {
for _, tt := range virtualMemoryTests {
t.Run(tt.mockedRootFS, func(t *testing.T) {
t.Setenv("HOST_ROOT", "testdata/plan9/virtualmemory/")
stat, err := VirtualMemory()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if !reflect.DeepEqual(stat, tt.stat) {
t.Errorf("got: %+v\nwant: %+v", stat, tt.stat)
}
})
}
}
var swapMemoryTests = []struct {
mockedRootFS string
swap *SwapMemoryStat
}{
{
"swap", &SwapMemoryStat{
Total: 655360000,
Used: 0,
Free: 655360000,
},
},
}
func TestSwapMemoryPlan9(t *testing.T) {
for _, tt := range swapMemoryTests {
t.Run(tt.mockedRootFS, func(t *testing.T) {
t.Setenv("HOST_ROOT", "testdata/plan9/virtualmemory/")
swap, err := SwapMemory()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if !reflect.DeepEqual(swap, tt.swap) {
t.Errorf("got: %+v\nwant: %+v", swap, tt.swap)
}
})
}
}