From f377a07e5a2bce9b13c7aa97bd09c732b4ba6215 Mon Sep 17 00:00:00 2001 From: shirou Date: Sun, 25 Oct 2020 19:01:38 +0900 Subject: [PATCH] fix test and openbsd errors. --- cpu/cpu_test.go | 22 +++++++++++++++++----- host/host_test.go | 20 ++++++++++---------- net/net_openbsd.go | 5 ++--- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 8ae9a82..4d0d456 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -40,7 +40,7 @@ func TestCpu_times(t *testing.T) { t.Errorf("error %v", err) } if len(cpuTotal) == 0 { - t.Error("could not get CPUs ", err) + t.Error("could not get CPUs", err) } perCPU, err := Times(true) skipIfNotImplementedErr(t, err) @@ -48,7 +48,7 @@ func TestCpu_times(t *testing.T) { t.Errorf("error %v", err) } if len(perCPU) == 0 { - t.Error("could not get CPUs ", err) + t.Error("could not get CPUs", err) } var perCPUUserTimeSum float64 var perCPUSystemTimeSum float64 @@ -59,9 +59,21 @@ func TestCpu_times(t *testing.T) { perCPUIdleTimeSum += pc.Idle } margin := 2.0 - assert.InEpsilon(t, cpuTotal[0].User, perCPUUserTimeSum, margin) - assert.InEpsilon(t, cpuTotal[0].System, perCPUSystemTimeSum, margin) - assert.InEpsilon(t, cpuTotal[0].Idle, perCPUIdleTimeSum, margin) + t.Log(cpuTotal[0]) + + if cpuTotal[0].User == 0 && cpuTotal[0].System == 0 && cpuTotal[0].Idle == 0 { + t.Error("could not get cpu values") + } + if cpuTotal[0].User != 0 { + assert.InEpsilon(t, cpuTotal[0].User, perCPUUserTimeSum, margin) + } + if cpuTotal[0].System != 0 { + assert.InEpsilon(t, cpuTotal[0].System, perCPUSystemTimeSum, margin) + } + if cpuTotal[0].Idle != 0 { + assert.InEpsilon(t, cpuTotal[0].Idle, perCPUIdleTimeSum, margin) + } + } func TestCpu_counts(t *testing.T) { diff --git a/host/host_test.go b/host/host_test.go index e1749f3..c8c4107 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -92,13 +92,13 @@ func TestUsers(t *testing.T) { func TestHostInfoStat_String(t *testing.T) { v := InfoStat{ - Hostname: "test", - Uptime: 3000, - Procs: 100, - OS: "linux", - Platform: "ubuntu", - BootTime: 1447040000, - HostID: "edfd25ff-3c9c-b1a4-e660-bd826495ad35", + 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"}` @@ -121,15 +121,15 @@ func TestUserStat_String(t *testing.T) { } func TestHostGuid(t *testing.T) { - hi, err := Info() + id, err := HostID() skipIfNotImplementedErr(t, err) if err != nil { t.Error(err) } - if hi.HostID == "" { + if id == "" { t.Error("Host id is empty") } else { - t.Logf("Host id value: %v", hi.HostID) + t.Logf("Host id value: %v", id) } } diff --git a/net/net_openbsd.go b/net/net_openbsd.go index 3cf0a89..cfed2be 100644 --- a/net/net_openbsd.go +++ b/net/net_openbsd.go @@ -4,7 +4,6 @@ package net import ( "context" - "errors" "fmt" "os/exec" "regexp" @@ -153,7 +152,7 @@ func FilterCounters() ([]FilterStat, error) { } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, errors.New("NetFilterCounters not implemented for openbsd") + return nil, common.ErrNotImplementedError } func ConntrackStats(percpu bool) ([]ConntrackStat, error) { @@ -173,7 +172,7 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { } func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, errors.New("NetProtoCounters not implemented for openbsd") + return nil, common.ErrNotImplementedError } func parseNetstatLine(line string) (ConnectionStat, error) {