1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// SPDX-License-Identifier: BSD-3-Clause
package host
import (
"errors"
"fmt"
"os"
"sync"
"testing"
"github.com/shirou/gopsutil/v4/internal/common"
)
func skipIfNotImplementedErr(t *testing.T, err error) {
if errors.Is(err, common.ErrNotImplementedError) {
t.Skip("not implemented")
}
}
func TestHostID(t *testing.T) {
v, err := HostID()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == "" {
t.Errorf("Could not get host id %v", v)
}
t.Log(v)
}
func TestInfo(t *testing.T) {
v, err := Info()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
empty := &InfoStat{}
if v == empty {
t.Errorf("Could not get hostinfo %v", v)
}
if v.Procs == 0 {
t.Errorf("Could not determine the number of host processes")
}
t.Log(v)
}
func TestUptime(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skip("Skip CI")
}
v, err := Uptime()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("Could not get up time %v", v)
}
}
func TestBootTime(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skip("Skip CI")
}
v, err := BootTime()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("Could not get boot time %v", v)
}
if v < 946652400 {
t.Errorf("Invalid Boottime, older than 2000-01-01")
}
t.Logf("first boot time: %d", v)
v2, err := BootTime()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v != v2 {
t.Errorf("cached boot time is different")
}
t.Logf("second boot time: %d", v2)
}
func TestUsers(t *testing.T) {
v, err := Users()
skipIfNotImplementedErr(t, err)
if errors.Is(err, os.ErrNotExist) {
t.Skip("Users file missing")
}
if err != nil {
t.Errorf("error %v", err)
}
empty := UserStat{}
if len(v) == 0 {
t.Skip("Users is empty")
}
for _, u := range v {
if u == empty {
t.Errorf("Could not Users %v", v)
}
t.Log(u)
}
}
func TestInfoStat_String(t *testing.T) {
v := InfoStat{
Hostname: "test",
Uptime: 3000,
Procs: 100,
OS: "linux",
Platform: "ubuntu",
BootTime: 1447040000,
HostID: "edfd25ff-3c9c-b1a4-e660-bd826495ad35",
KernelArch: "x86_64",
}
e := `{"hostname":"test","uptime":3000,"bootTime":1447040000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","kernelVersion":"","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostId":"edfd25ff-3c9c-b1a4-e660-bd826495ad35"}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
}
}
func TestUserStat_String(t *testing.T) {
v := UserStat{
User: "user",
Terminal: "term",
Host: "host",
Started: 100,
}
e := `{"user":"user","terminal":"term","host":"host","started":100}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("UserStat string is invalid: %v", v)
}
}
func TestGuid(t *testing.T) {
id, err := HostID()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Error(err)
}
if id == "" {
t.Error("Host id is empty")
} else {
t.Logf("Host id value: %v", id)
}
}
func TestVirtualization(t *testing.T) {
wg := sync.WaitGroup{}
testCount := 10
wg.Add(testCount)
for i := 0; i < testCount; i++ {
go func(j int) {
system, role, err := Virtualization()
wg.Done()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("Virtualization() failed, %v", err)
}
if j == 9 {
t.Logf("Virtualization(): %s, %s", system, role)
}
}(i)
}
wg.Wait()
}
func TestKernelVersion(t *testing.T) {
version, err := KernelVersion()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("KernelVersion() failed, %v", err)
}
if version == "" {
t.Errorf("KernelVersion() returns empty: %s", version)
}
t.Logf("KernelVersion(): %s", version)
}
func TestPlatformInformation(t *testing.T) {
platform, family, version, err := PlatformInformation()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("PlatformInformation() failed, %v", err)
}
if platform == "" {
t.Errorf("PlatformInformation() returns empty: %v", platform)
}
t.Logf("PlatformInformation(): %v, %v, %v", platform, family, version)
}
func BenchmarkBootTimeWithCache(b *testing.B) {
EnableBootTimeCache(true)
for i := 0; i < b.N; i++ {
BootTime()
}
}
func BenchmarkBootTimeWithoutCache(b *testing.B) {
EnableBootTimeCache(false)
for i := 0; i < b.N; i++ {
BootTime()
}
}
|