...

Source file src/github.com/shirou/gopsutil/host/host_test.go

Documentation: github.com/shirou/gopsutil/host

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

View as plain text