github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/host/host_test.go (about)

     1  package host
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/system/common"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func skipIfNotImplementedErr(t *testing.T, err error) {
    11  	if err == common.ErrNotImplementedError {
    12  		t.Skip("not implemented")
    13  	}
    14  }
    15  
    16  func TestHostInfo(t *testing.T) {
    17  	v, err := Info()
    18  	skipIfNotImplementedErr(t, err)
    19  	if err != nil {
    20  		t.Errorf("error %v", err)
    21  	}
    22  	empty := &InfoStat{}
    23  	if v == empty {
    24  		t.Errorf("Could not get hostinfo %v", v)
    25  	}
    26  	if v.Procs == 0 {
    27  		t.Errorf("Could not determine the number of host processes")
    28  	}
    29  }
    30  
    31  func TestUptime(t *testing.T) {
    32  	if os.Getenv("CIRCLECI") == "true" {
    33  		t.Skip("Skip CI")
    34  	}
    35  
    36  	v, err := Uptime()
    37  	skipIfNotImplementedErr(t, err)
    38  	if err != nil {
    39  		t.Errorf("error %v", err)
    40  	}
    41  	if v == 0 {
    42  		t.Errorf("Could not get up time %v", v)
    43  	}
    44  }
    45  
    46  func TestBoot_time(t *testing.T) {
    47  	if os.Getenv("CIRCLECI") == "true" {
    48  		t.Skip("Skip CI")
    49  	}
    50  	v, err := BootTime()
    51  	skipIfNotImplementedErr(t, err)
    52  	if err != nil {
    53  		t.Errorf("error %v", err)
    54  	}
    55  	if v == 0 {
    56  		t.Errorf("Could not get boot time %v", v)
    57  	}
    58  	if v < 946652400 {
    59  		t.Errorf("Invalid Boottime, older than 2000-01-01")
    60  	}
    61  	t.Logf("first boot time: %d", v)
    62  
    63  	v2, err := BootTime()
    64  	skipIfNotImplementedErr(t, err)
    65  	if err != nil {
    66  		t.Errorf("error %v", err)
    67  	}
    68  	if v != v2 {
    69  		t.Errorf("cached boot time is different")
    70  	}
    71  	t.Logf("second boot time: %d", v2)
    72  }
    73  
    74  func TestHostInfoStat_String(t *testing.T) {
    75  	v := InfoStat{
    76  		Hostname:   "test",
    77  		Uptime:     3000,
    78  		Procs:      100,
    79  		OS:         "linux",
    80  		Platform:   "ubuntu",
    81  		BootTime:   1447040000,
    82  		HostID:     "edfd25ff-3c9c-b1a4-e660-bd826495ad35",
    83  		KernelArch: "x86_64",
    84  	}
    85  	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"}`
    86  	if e != fmt.Sprintf("%v", v) {
    87  		t.Errorf("HostInfoStat string is invalid:\ngot  %v\nwant %v", v, e)
    88  	}
    89  }
    90  
    91  func TestUserStat_String(t *testing.T) {
    92  	v := UserStat{
    93  		User:     "user",
    94  		Terminal: "term",
    95  		Host:     "host",
    96  		Started:  100,
    97  	}
    98  	e := `{"user":"user","terminal":"term","host":"host","started":100}`
    99  	if e != fmt.Sprintf("%v", v) {
   100  		t.Errorf("UserStat string is invalid: %v", v)
   101  	}
   102  }
   103  
   104  func TestHostGuid(t *testing.T) {
   105  	id, err := HostID()
   106  	skipIfNotImplementedErr(t, err)
   107  	if err != nil {
   108  		t.Error(err)
   109  	}
   110  	if id == "" {
   111  		t.Error("Host id is empty")
   112  	} else {
   113  		t.Logf("Host id value: %v", id)
   114  	}
   115  }
   116  
   117  func TestTemperatureStat_String(t *testing.T) {
   118  	v := TemperatureStat{
   119  		SensorKey:   "CPU",
   120  		Temperature: 1.1,
   121  	}
   122  	s := `{"sensorKey":"CPU","sensorTemperature":1.1}`
   123  	if s != fmt.Sprintf("%v", v) {
   124  		t.Errorf("TemperatureStat string is invalid")
   125  	}
   126  }
   127  
   128  func TestKernelVersion(t *testing.T) {
   129  	version, err := KernelVersion()
   130  	skipIfNotImplementedErr(t, err)
   131  	if err != nil {
   132  		t.Errorf("KernelVersion() failed, %v", err)
   133  	}
   134  	if version == "" {
   135  		t.Errorf("KernelVersion() returns empty: %s", version)
   136  	}
   137  
   138  	t.Logf("KernelVersion(): %s", version)
   139  }
   140  
   141  func TestPlatformInformation(t *testing.T) {
   142  	platform, family, version, err := PlatformInformation()
   143  	skipIfNotImplementedErr(t, err)
   144  	if err != nil {
   145  		t.Errorf("PlatformInformation() failed, %v", err)
   146  	}
   147  	if platform == "" {
   148  		t.Errorf("PlatformInformation() returns empty: %v", platform)
   149  	}
   150  
   151  	t.Logf("PlatformInformation(): %v, %v, %v", platform, family, version)
   152  }