2014-12-30 22:09:05 +09:00
|
|
|
package host
|
2014-04-18 16:34:47 +09:00
|
|
|
|
|
|
|
import (
|
2014-05-12 11:51:08 +09:00
|
|
|
"fmt"
|
2018-01-09 11:30:38 +09:00
|
|
|
"os"
|
2020-10-06 17:03:49 +00:00
|
|
|
"sync"
|
2014-04-18 16:34:47 +09:00
|
|
|
"testing"
|
2020-08-29 18:29:48 +02:00
|
|
|
|
2021-11-06 09:53:56 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2014-04-18 16:34:47 +09:00
|
|
|
)
|
|
|
|
|
2020-08-29 18:29:48 +02:00
|
|
|
func skipIfNotImplementedErr(t *testing.T, err error) {
|
|
|
|
if err == common.ErrNotImplementedError {
|
|
|
|
t.Skip("not implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:34:47 +09:00
|
|
|
func TestHostInfo(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
v, err := Info()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2014-04-18 16:34:47 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2016-03-22 23:09:12 +09:00
|
|
|
empty := &InfoStat{}
|
2014-05-12 11:51:08 +09:00
|
|
|
if v == empty {
|
2014-04-22 10:39:55 +09:00
|
|
|
t.Errorf("Could not get hostinfo %v", v)
|
|
|
|
}
|
2016-07-10 23:47:29 -05:00
|
|
|
if v.Procs == 0 {
|
|
|
|
t.Errorf("Could not determine the number of host processes")
|
|
|
|
}
|
2014-04-22 10:39:55 +09:00
|
|
|
}
|
|
|
|
|
2016-06-12 23:20:51 +09:00
|
|
|
func TestUptime(t *testing.T) {
|
2018-01-09 11:30:38 +09:00
|
|
|
if os.Getenv("CIRCLECI") == "true" {
|
|
|
|
t.Skip("Skip CI")
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:20:51 +09:00
|
|
|
v, err := Uptime()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2016-06-12 23:20:51 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if v == 0 {
|
|
|
|
t.Errorf("Could not get up time %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 10:39:55 +09:00
|
|
|
func TestBoot_time(t *testing.T) {
|
2018-01-09 11:30:38 +09:00
|
|
|
if os.Getenv("CIRCLECI") == "true" {
|
|
|
|
t.Skip("Skip CI")
|
|
|
|
}
|
2014-05-23 10:31:47 +09:00
|
|
|
v, err := BootTime()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2014-04-22 10:39:55 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
|
|
|
if v == 0 {
|
2015-06-19 11:58:46 +09:00
|
|
|
t.Errorf("Could not get boot time %v", v)
|
2014-04-18 16:34:47 +09:00
|
|
|
}
|
2016-06-12 23:20:51 +09:00
|
|
|
if v < 946652400 {
|
|
|
|
t.Errorf("Invalid Boottime, older than 2000-01-01")
|
|
|
|
}
|
2017-10-22 16:39:36 +09:00
|
|
|
t.Logf("first boot time: %d", v)
|
2016-10-13 11:18:11 -04:00
|
|
|
|
|
|
|
v2, err := BootTime()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2018-06-21 16:58:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2016-10-13 11:18:11 -04:00
|
|
|
if v != v2 {
|
|
|
|
t.Errorf("cached boot time is different")
|
|
|
|
}
|
2017-10-22 16:39:36 +09:00
|
|
|
t.Logf("second boot time: %d", v2)
|
2014-04-18 16:34:47 +09:00
|
|
|
}
|
2014-04-22 17:38:47 +09:00
|
|
|
|
|
|
|
func TestUsers(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v, err := Users()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2014-04-22 17:38:47 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error %v", err)
|
|
|
|
}
|
2014-05-23 10:31:47 +09:00
|
|
|
empty := UserStat{}
|
2016-04-23 23:43:00 +09:00
|
|
|
if len(v) == 0 {
|
2020-08-30 01:31:13 +02:00
|
|
|
t.Skip("Users is empty")
|
2016-04-23 23:43:00 +09:00
|
|
|
}
|
2014-04-22 17:39:51 +09:00
|
|
|
for _, u := range v {
|
2014-05-12 11:51:08 +09:00
|
|
|
if u == empty {
|
2014-04-22 17:38:47 +09:00
|
|
|
t.Errorf("Could not Users %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 11:51:08 +09:00
|
|
|
|
|
|
|
func TestHostInfoStat_String(t *testing.T) {
|
2016-03-22 23:09:12 +09:00
|
|
|
v := InfoStat{
|
2020-10-25 19:01:38 +09:00
|
|
|
Hostname: "test",
|
|
|
|
Uptime: 3000,
|
|
|
|
Procs: 100,
|
|
|
|
OS: "linux",
|
|
|
|
Platform: "ubuntu",
|
|
|
|
BootTime: 1447040000,
|
|
|
|
HostID: "edfd25ff-3c9c-b1a4-e660-bd826495ad35",
|
2020-08-29 18:29:48 +02:00
|
|
|
KernelArch: "x86_64",
|
2014-05-12 11:51:08 +09:00
|
|
|
}
|
2021-11-06 09:53:56 +00:00
|
|
|
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"}`
|
2014-05-12 11:51:08 +09:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
2020-02-13 14:39:25 +01:00
|
|
|
t.Errorf("HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
|
2014-05-12 11:51:08 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserStat_String(t *testing.T) {
|
2014-05-23 10:31:47 +09:00
|
|
|
v := UserStat{
|
2014-05-12 11:51:08 +09:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 16:28:49 +04:00
|
|
|
|
|
|
|
func TestHostGuid(t *testing.T) {
|
2020-10-25 19:01:38 +09:00
|
|
|
id, err := HostID()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2017-03-03 16:28:49 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-10-25 19:01:38 +09:00
|
|
|
if id == "" {
|
2017-03-03 16:28:49 +04:00
|
|
|
t.Error("Host id is empty")
|
|
|
|
} else {
|
2020-10-25 19:01:38 +09:00
|
|
|
t.Logf("Host id value: %v", id)
|
2017-03-03 16:28:49 +04:00
|
|
|
}
|
|
|
|
}
|
2017-03-20 18:55:04 +01:00
|
|
|
|
|
|
|
func TestTemperatureStat_String(t *testing.T) {
|
|
|
|
v := TemperatureStat{
|
|
|
|
SensorKey: "CPU",
|
|
|
|
Temperature: 1.1,
|
2021-11-06 09:53:56 +00:00
|
|
|
High: 30.1,
|
|
|
|
Critical: 0.1,
|
2017-03-20 18:55:04 +01:00
|
|
|
}
|
2021-11-06 09:53:56 +00:00
|
|
|
s := `{"sensorKey":"CPU","temperature":1.1,"sensorHigh":30.1,"sensorCritical":0.1}`
|
2017-03-20 18:55:04 +01:00
|
|
|
if s != fmt.Sprintf("%v", v) {
|
2021-11-06 09:53:56 +00:00
|
|
|
t.Errorf("TemperatureStat string is invalid, %v", fmt.Sprintf("%v", v))
|
2017-03-20 18:55:04 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 10:14:20 +09:00
|
|
|
|
|
|
|
func TestVirtualization(t *testing.T) {
|
2020-10-06 17:03:49 +00:00
|
|
|
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()
|
2017-08-03 10:14:20 +09:00
|
|
|
}
|
2017-08-03 11:08:35 +09:00
|
|
|
|
|
|
|
func TestKernelVersion(t *testing.T) {
|
|
|
|
version, err := KernelVersion()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2017-08-03 11:08:35 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("KernelVersion() failed, %v", err)
|
|
|
|
}
|
|
|
|
if version == "" {
|
2019-03-18 02:52:26 +09:00
|
|
|
t.Errorf("KernelVersion() returns empty: %s", version)
|
2017-08-03 11:08:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("KernelVersion(): %s", version)
|
|
|
|
}
|
2018-03-11 23:08:04 +00:00
|
|
|
|
|
|
|
func TestPlatformInformation(t *testing.T) {
|
|
|
|
platform, family, version, err := PlatformInformation()
|
2020-08-29 18:29:48 +02:00
|
|
|
skipIfNotImplementedErr(t, err)
|
2018-03-11 23:08:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("PlatformInformation() failed, %v", err)
|
|
|
|
}
|
|
|
|
if platform == "" {
|
2019-03-18 02:52:26 +09:00
|
|
|
t.Errorf("PlatformInformation() returns empty: %v", platform)
|
2018-03-11 23:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("PlatformInformation(): %v, %v, %v", platform, family, version)
|
|
|
|
}
|