提交 5f2e6bce 编写于 作者: S Shirou WAKAYAMA

add test about String().

上级 afd4d392
package gopsutil package gopsutil
import ( import (
"fmt"
"testing" "testing"
) )
...@@ -12,9 +13,9 @@ func TestCpu_times(t *testing.T) { ...@@ -12,9 +13,9 @@ func TestCpu_times(t *testing.T) {
if len(v) == 0 { if len(v) == 0 {
t.Errorf("could not get CPUs ", err) t.Errorf("could not get CPUs ", err)
} }
empty := CPUTimesStat{}
for _, vv := range v { for _, vv := range v {
if vv.User == 0 { if vv == empty {
t.Errorf("could not get CPU User: %v", vv) t.Errorf("could not get CPU User: %v", vv)
} }
} }
...@@ -29,3 +30,16 @@ func TestCpu_counts(t *testing.T) { ...@@ -29,3 +30,16 @@ func TestCpu_counts(t *testing.T) {
t.Errorf("could not get CPU counts: %v", v) t.Errorf("could not get CPU counts: %v", v)
} }
} }
func TestCPUTimeStat_String(t *testing.T) {
v := CPUTimesStat{
CPU: "cpu0",
User: 100.1,
System: 200.1,
Idle: 300.1,
}
e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0,"iowait":0,"irq":0,"softirq":0,"steal":0,"guest":0,"guest_nice":0,"stolen":0}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("CPUTimesStat string is invalid: %v", v)
}
}
...@@ -15,14 +15,22 @@ func TestDisk_usage(t *testing.T) { ...@@ -15,14 +15,22 @@ func TestDisk_usage(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
fmt.Println(v) if v.Path != path {
t.Errorf("error %v", err)
}
} }
func TestDisk_partitions(t *testing.T) { func TestDisk_partitions(t *testing.T) {
_, err := DiskPartitions(false) ret, err := DiskPartitions(false)
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := DiskPartitionStat{}
for _, disk := range ret {
if disk == empty {
t.Errorf("Could not get device info %v", disk)
}
}
} }
func TestDisk_io_counters(t *testing.T) { func TestDisk_io_counters(t *testing.T) {
...@@ -30,9 +38,51 @@ func TestDisk_io_counters(t *testing.T) { ...@@ -30,9 +38,51 @@ func TestDisk_io_counters(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := DiskIOCountersStat{}
for _, io := range ret { for _, io := range ret {
if io.Name == "" { if io == empty {
t.Errorf("io_counter error %v", io) t.Errorf("io_counter error %v", io)
} }
} }
} }
func TestDiskUsageStat_String(t *testing.T) {
v := DiskUsageStat{
Path: "/",
Total: 1000,
Free: 2000,
Used: 3000,
UsedPercent: 50.1,
}
e := `{"path":"/","total":1000,"free":2000,"used":3000,"usedPercent":50.1}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}
func TestDiskPartitionStat_String(t *testing.T) {
v := DiskPartitionStat{
Device: "sd01",
Mountpoint: "/",
Fstype: "ext4",
Opts: "ro",
}
e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":"ro"}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}
func TestDiskIOCountersStat_String(t *testing.T) {
v := DiskIOCountersStat{
Name: "sd01",
ReadCount: 100,
WriteCount: 200,
ReadBytes: 300,
WriteBytes: 400,
}
e := `{"readCount":100,"writeCount":200,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"name":"sd01"}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
}
package gopsutil package gopsutil
import ( import (
"fmt"
"testing" "testing"
) )
...@@ -9,7 +10,8 @@ func TestHostInfo(t *testing.T) { ...@@ -9,7 +10,8 @@ func TestHostInfo(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if v.Hostname == "" { empty := &HostInfoStat{}
if v == empty {
t.Errorf("Could not get hostinfo %v", v) t.Errorf("Could not get hostinfo %v", v)
} }
} }
...@@ -29,9 +31,35 @@ func TestUsers(t *testing.T) { ...@@ -29,9 +31,35 @@ func TestUsers(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := UserStat{}
for _, u := range v { for _, u := range v {
if u.User == "" { if u == empty {
t.Errorf("Could not Users %v", v) t.Errorf("Could not Users %v", v)
} }
} }
} }
func TestHostInfoStat_String(t *testing.T) {
v := HostInfoStat{
Hostname: "test",
Uptime: 3000,
Procs: 100,
}
e := `{"hostname":"test","uptime":3000,"procs":100}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("HostInfoStat string is invalid: %v", v)
}
}
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)
}
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
package gopsutil package gopsutil
import ( import (
"fmt"
"testing" "testing"
) )
...@@ -12,7 +13,20 @@ func TestLoad(t *testing.T) { ...@@ -12,7 +13,20 @@ func TestLoad(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if v.Load1 == 0 || v.Load5 == 0 || v.Load15 == 0 { empty := &LoadAvgStat{}
if v == empty {
t.Errorf("error load: %v", v) t.Errorf("error load: %v", v)
} }
} }
func TestLoadAvgStat_String(t *testing.T) {
v := LoadAvgStat{
Load1: 10.1,
Load5: 20.1,
Load15: 30.1,
}
e := `{"load1":10.1,"load5":20.1,"load15":30.1}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("LoadAvgStat string is invalid: %v", v)
}
}
...@@ -10,7 +10,12 @@ func TestVirtual_memory(t *testing.T) { ...@@ -10,7 +10,12 @@ func TestVirtual_memory(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
fmt.Println(v)
empty := &VirtualMemoryStat{}
if v == empty {
t.Errorf("error %v", v)
}
} }
func TestSwap_memory(t *testing.T) { func TestSwap_memory(t *testing.T) {
...@@ -18,5 +23,35 @@ func TestSwap_memory(t *testing.T) { ...@@ -18,5 +23,35 @@ func TestSwap_memory(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
fmt.Println(v) empty := &SwapMemoryStat{}
if v == empty {
t.Errorf("error %v", v)
}
}
func TestVirtualMemoryStat_String(t *testing.T) {
v := VirtualMemoryStat{
Total: 10,
Available: 20,
Used: 30,
UsedPercent: 30.1,
Free: 40,
}
e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"buffers":0,"cached":0,"wired":0,"shared":0}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("VirtualMemoryStat string is invalid: %v", v)
}
}
func TestSwapMemoryStat_String(t *testing.T) {
v := SwapMemoryStat{
Total: 10,
Used: 30,
Free: 40,
UsedPercent: 30.1,
}
e := `{"total":10,"used":30,"free":40,"usedPercent":30.1,"sin":0,"sout":0}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("SwapMemoryStat string is invalid: %v", v)
}
} }
package gopsutil package gopsutil
import ( import (
"testing"
"fmt" "fmt"
"testing"
) )
func TestAddrString(t *testing.T) { func TestAddrString(t *testing.T) {
...@@ -16,7 +16,7 @@ func TestAddrString(t *testing.T) { ...@@ -16,7 +16,7 @@ func TestAddrString(t *testing.T) {
func TestNetIOCountersStatString(t *testing.T) { func TestNetIOCountersStatString(t *testing.T) {
v := NetIOCountersStat{ v := NetIOCountersStat{
Name: "test", Name: "test",
BytesSent: 100, BytesSent: 100,
} }
e := `{"name":"test","bytes_sent":100,"bytes_recv":0,"packets_sent":0,"packets_recv":0,"errin":0,"errout":0,"dropin":0,"dropout":0}` e := `{"name":"test","bytes_sent":100,"bytes_recv":0,"packets_sent":0,"packets_recv":0,"errin":0,"errout":0,"dropin":0,"dropout":0}`
...@@ -27,9 +27,9 @@ func TestNetIOCountersStatString(t *testing.T) { ...@@ -27,9 +27,9 @@ func TestNetIOCountersStatString(t *testing.T) {
func TestNetConnectionStatString(t *testing.T) { func TestNetConnectionStatString(t *testing.T) {
v := NetConnectionStat{ v := NetConnectionStat{
Fd: 10, Fd: 10,
Family: 10, Family: 10,
Type: 10, Type: 10,
} }
e := `{"fd":10,"family":10,"type":10,"laddr":{"ip":"","port":0},"raddr":{"ip":"","port":0},"status":"","pid":0}` e := `{"fd":10,"family":10,"type":10,"laddr":{"ip":"","port":0},"raddr":{"ip":"","port":0},"status":"","pid":0}`
if e != fmt.Sprintf("%v", v) { if e != fmt.Sprintf("%v", v) {
...@@ -40,14 +40,14 @@ func TestNetConnectionStatString(t *testing.T) { ...@@ -40,14 +40,14 @@ func TestNetConnectionStatString(t *testing.T) {
func TestNetIOCounters(t *testing.T) { func TestNetIOCounters(t *testing.T) {
v, err := NetIOCounters(true) v, err := NetIOCounters(true)
if err != nil{ if err != nil {
t.Errorf("Could not get NetIOCounters: %v", err) t.Errorf("Could not get NetIOCounters: %v", err)
} }
if len(v) == 0{ if len(v) == 0 {
t.Errorf("Could not get NetIOCounters: %v", v) t.Errorf("Could not get NetIOCounters: %v", v)
} }
for _, vv := range(v){ for _, vv := range v {
if vv.Name == ""{ if vv.Name == "" {
t.Errorf("Invalid NetIOCounters: %v", vv) t.Errorf("Invalid NetIOCounters: %v", vv)
} }
} }
......
package gopsutil package gopsutil
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"testing" "testing"
...@@ -52,8 +51,13 @@ func Test_NewProcess(t *testing.T) { ...@@ -52,8 +51,13 @@ func Test_NewProcess(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := &Process{}
if runtime.GOOS != "windows" { // Windows pid is 0
if empty == ret {
t.Errorf("error %v", ret)
}
}
fmt.Println(ret)
} }
func Test_Process_memory_maps(t *testing.T) { func Test_Process_memory_maps(t *testing.T) {
...@@ -61,15 +65,17 @@ func Test_Process_memory_maps(t *testing.T) { ...@@ -61,15 +65,17 @@ func Test_Process_memory_maps(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
checkPid = 0 checkPid = 0
} }
return
ret, err := NewProcess(int32(checkPid)) ret, err := NewProcess(int32(checkPid))
mmaps, err := ret.MemoryMaps(false) mmaps, err := ret.MemoryMaps(false)
if err != nil { if err != nil {
t.Errorf("memory map get error %v", err) t.Errorf("memory map get error %v", err)
} }
empty := MemoryMapsStat{}
for _, m := range *mmaps { for _, m := range *mmaps {
fmt.Println(m) if m == empty {
t.Errorf("memory map get error %v", m)
}
} }
} }
...@@ -95,11 +101,10 @@ func Test_Process_IOCounters(t *testing.T) { ...@@ -95,11 +101,10 @@ func Test_Process_IOCounters(t *testing.T) {
t.Errorf("geting ppid error %v", err) t.Errorf("geting ppid error %v", err)
return return
} }
if v.ReadCount == 0 { empty := &IOCountersStat{}
t.Errorf("return value is 0 %v", v) if v == empty {
t.Errorf("error %v", v)
} }
fmt.Println(v)
} }
func Test_Process_NumCtx(t *testing.T) { func Test_Process_NumCtx(t *testing.T) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册