From 45710b5c9dbe868a21fe875c1e0fcd9fb919e2c1 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Fri, 23 May 2014 10:31:47 +0900 Subject: [PATCH] could not get coverage from test subdirectory. move back to top. But it becomes messy again. --- cpu_test.go | 58 ++++++++++++++++++++++ disk_test.go | 88 +++++++++++++++++++++++++++++++++ host_test.go | 67 +++++++++++++++++++++++++ load_test.go | 30 ++++++++++++ mem_test.go | 56 +++++++++++++++++++++ net_test.go | 54 ++++++++++++++++++++ process_posix_test.go | 19 +++++++ process_test.go | 118 ++++++++++++++++++++++++++++++++++++++++++++ test/cpu_test.go | 59 ---------------------- test/disk_test.go | 90 ---------------------------------- test/host_test.go | 69 -------------------------- test/load_test.go | 32 ------------ test/mem_test.go | 58 ---------------------- test/net_test.go | 56 --------------------- test/process_posix_test.go | 21 -------- test/process_test.go | 120 --------------------------------------------- 16 files changed, 490 insertions(+), 505 deletions(-) create mode 100644 cpu_test.go create mode 100644 disk_test.go create mode 100644 host_test.go create mode 100644 load_test.go create mode 100644 mem_test.go create mode 100644 net_test.go create mode 100644 process_posix_test.go create mode 100644 process_test.go delete mode 100644 test/cpu_test.go delete mode 100644 test/disk_test.go delete mode 100644 test/host_test.go delete mode 100644 test/load_test.go delete mode 100644 test/mem_test.go delete mode 100644 test/net_test.go delete mode 100644 test/process_posix_test.go delete mode 100644 test/process_test.go diff --git a/cpu_test.go b/cpu_test.go new file mode 100644 index 0000000..963e3e8 --- /dev/null +++ b/cpu_test.go @@ -0,0 +1,58 @@ +package gopsutil + +import ( + "fmt" + "testing" + +) + +func TestCpu_times(t *testing.T) { + v, err := CPUTimes(false) + if err != nil { + t.Errorf("error %v", err) + } + if len(v) == 0 { + t.Errorf("could not get CPUs ", err) + } + empty := CPUTimesStat{} + for _, vv := range v { + if vv == empty { + t.Errorf("could not get CPU User: %v", vv) + } + } +} + +func TestCpu_counts(t *testing.T) { + v, err := CPUCounts(true) + if err != nil { + t.Errorf("error %v", err) + } + if v == 0 { + 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) + } +} + +func TestCpuInfo(t *testing.T) { + v, err := CPUInfo() + if err != nil { + t.Errorf("error %v", err) + } + for _, vv := range v { + if vv.ModelName == "" { + t.Errorf("could not get CPU Info: %v", vv) + } + } +} diff --git a/disk_test.go b/disk_test.go new file mode 100644 index 0000000..06d7d50 --- /dev/null +++ b/disk_test.go @@ -0,0 +1,88 @@ +package gopsutil + +import ( + "fmt" + "runtime" + "testing" +) + +func TestDisk_usage(t *testing.T) { + path := "/" + if runtime.GOOS == "windows" { + path = "C:" + } + v, err := DiskUsage(path) + if err != nil { + t.Errorf("error %v", err) + } + if v.Path != path { + t.Errorf("error %v", err) + } +} + +func TestDisk_partitions(t *testing.T) { + ret, err := DiskPartitions(false) + if err != nil { + 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) { + ret, err := DiskIOCounters() + if err != nil { + t.Errorf("error %v", err) + } + empty := DiskIOCountersStat{} + for _, io := range ret { + if io == empty { + 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) + } +} diff --git a/host_test.go b/host_test.go new file mode 100644 index 0000000..285ae13 --- /dev/null +++ b/host_test.go @@ -0,0 +1,67 @@ +package gopsutil + +import ( + "fmt" + "testing" +) + +func TestHostInfo(t *testing.T) { + v, err := HostInfo() + if err != nil { + t.Errorf("error %v", err) + } + empty := &HostInfoStat{} + if v == empty { + t.Errorf("Could not get hostinfo %v", v) + } +} + +func TestBoot_time(t *testing.T) { + v, err := BootTime() + if err != nil { + t.Errorf("error %v", err) + } + if v == 0 { + t.Errorf("Could not boot time %v", v) + } +} + +func TestUsers(t *testing.T) { + v, err := Users() + if err != nil { + t.Errorf("error %v", err) + } + empty := UserStat{} + for _, u := range v { + if u == empty { + t.Errorf("Could not Users %v", v) + } + } +} + +func TestHostInfoStat_String(t *testing.T) { + v := HostInfoStat{ + Hostname: "test", + Uptime: 3000, + Procs: 100, + OS: "linux", + Platform: "ubuntu", + } + e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","virtualizationSystem":"","virtualizationRole":""}` + 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) + } +} diff --git a/load_test.go b/load_test.go new file mode 100644 index 0000000..660e940 --- /dev/null +++ b/load_test.go @@ -0,0 +1,30 @@ +package gopsutil + +import ( + "fmt" + "testing" +) + +func TestLoad(t *testing.T) { + v, err := LoadAvg() + if err != nil { + t.Errorf("error %v", err) + } + + empty := &LoadAvgStat{} + if v == empty { + 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) + } +} diff --git a/mem_test.go b/mem_test.go new file mode 100644 index 0000000..7d66550 --- /dev/null +++ b/mem_test.go @@ -0,0 +1,56 @@ +package gopsutil + +import ( + "fmt" + "testing" +) + +func TestVirtual_memory(t *testing.T) { + v, err := VirtualMemory() + if err != nil { + t.Errorf("error %v", err) + } + + empty := &VirtualMemoryStat{} + if v == empty { + t.Errorf("error %v", v) + } +} + +func TestSwap_memory(t *testing.T) { + v, err := SwapMemory() + if err != nil { + t.Errorf("error %v", err) + } + 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) + } +} diff --git a/net_test.go b/net_test.go new file mode 100644 index 0000000..6d7fde2 --- /dev/null +++ b/net_test.go @@ -0,0 +1,54 @@ +package gopsutil + +import ( + "fmt" + "testing" +) + +func TestAddrString(t *testing.T) { + v := Addr{IP: "192.168.0.1", Port: 8000} + + s := fmt.Sprintf("%v", v) + if s != "{\"ip\":\"192.168.0.1\",\"port\":8000}" { + t.Errorf("Addr string is invalid: %v", v) + } +} + +func TestNetIOCountersStatString(t *testing.T) { + v := NetIOCountersStat{ + Name: "test", + 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}` + if e != fmt.Sprintf("%v", v) { + t.Errorf("NetIOCountersStat string is invalid: %v", v) + } +} + +func TestNetConnectionStatString(t *testing.T) { + v := NetConnectionStat{ + Fd: 10, + Family: 10, + Type: 10, + } + e := `{"fd":10,"family":10,"type":10,"localaddr":{"ip":"","port":0},"remoteaddr":{"ip":"","port":0},"status":"","pid":0}` + if e != fmt.Sprintf("%v", v) { + t.Errorf("NetConnectionStat string is invalid: %v", v) + } + +} + +func TestNetIOCounters(t *testing.T) { + v, err := NetIOCounters(true) + if err != nil { + t.Errorf("Could not get NetIOCounters: %v", err) + } + if len(v) == 0 { + t.Errorf("Could not get NetIOCounters: %v", v) + } + for _, vv := range v { + if vv.Name == "" { + t.Errorf("Invalid NetIOCounters: %v", vv) + } + } +} diff --git a/process_posix_test.go b/process_posix_test.go new file mode 100644 index 0000000..b45418a --- /dev/null +++ b/process_posix_test.go @@ -0,0 +1,19 @@ +// +build linux freebsd + +package gopsutil + +import ( + "os" + "syscall" + "testing" +) + +func Test_SendSignal(t *testing.T) { + checkPid := os.Getpid() + + p, _ := NewProcess(int32(checkPid)) + err := p.SendSignal(syscall.SIGCONT) + if err != nil { + t.Errorf("send signal %v", err) + } +} diff --git a/process_test.go b/process_test.go new file mode 100644 index 0000000..6598e28 --- /dev/null +++ b/process_test.go @@ -0,0 +1,118 @@ +package gopsutil + +import ( + "os" + "runtime" + "testing" +) + +func test_getProcess() Process { + checkPid := os.Getpid() + if runtime.GOOS == "windows" { + checkPid = 7960 + } + ret, _ := NewProcess(int32(checkPid)) + return *ret +} + +func Test_Pids(t *testing.T) { + ret, err := Pids() + if err != nil { + t.Errorf("error %v", err) + } + if len(ret) == 0 { + t.Errorf("could not get pids %v", ret) + } +} + +func Test_Pid_exists(t *testing.T) { + checkPid := 1 + if runtime.GOOS == "windows" { + checkPid = 0 + } + + ret, err := PidExists(int32(checkPid)) + if err != nil { + t.Errorf("error %v", err) + } + + if ret == false { + t.Errorf("could not get init process %v", ret) + } +} + +func Test_NewProcess(t *testing.T) { + checkPid := 1 + if runtime.GOOS == "windows" { + checkPid = 0 + } + + ret, err := NewProcess(int32(checkPid)) + if err != nil { + t.Errorf("error %v", err) + } + empty := &Process{} + if runtime.GOOS != "windows" { // Windows pid is 0 + if empty == ret { + t.Errorf("error %v", ret) + } + } + +} + +func Test_Process_memory_maps(t *testing.T) { + checkPid := os.Getpid() + if runtime.GOOS == "windows" { + checkPid = 0 + } + ret, err := NewProcess(int32(checkPid)) + + mmaps, err := ret.MemoryMaps(false) + if err != nil { + t.Errorf("memory map get error %v", err) + } + empty := MemoryMapsStat{} + for _, m := range *mmaps { + if m == empty { + t.Errorf("memory map get error %v", m) + } + } + +} + +func Test_Process_Ppid(t *testing.T) { + p := test_getProcess() + + v, err := p.Ppid() + if err != nil { + t.Errorf("geting ppid error %v", err) + } + if v == 0 { + t.Errorf("return value is 0 %v", v) + } + +} + +func Test_Process_IOCounters(t *testing.T) { + p := test_getProcess() + + v, err := p.IOCounters() + if err != nil { + t.Errorf("geting ppid error %v", err) + return + } + empty := &IOCountersStat{} + if v == empty { + t.Errorf("error %v", v) + } +} + +func Test_Process_NumCtx(t *testing.T) { + p := test_getProcess() + + _, err := p.NumCtxSwitches() + if err != nil { + t.Errorf("geting numctx error %v", err) + return + } +} diff --git a/test/cpu_test.go b/test/cpu_test.go deleted file mode 100644 index 2eeaea7..0000000 --- a/test/cpu_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package test - -import ( - "fmt" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestCpu_times(t *testing.T) { - v, err := gopsutil.CPUTimes(false) - if err != nil { - t.Errorf("error %v", err) - } - if len(v) == 0 { - t.Errorf("could not get CPUs ", err) - } - empty := gopsutil.CPUTimesStat{} - for _, vv := range v { - if vv == empty { - t.Errorf("could not get CPU User: %v", vv) - } - } -} - -func TestCpu_counts(t *testing.T) { - v, err := gopsutil.CPUCounts(true) - if err != nil { - t.Errorf("error %v", err) - } - if v == 0 { - t.Errorf("could not get CPU counts: %v", v) - } -} - -func TestCPUTimeStat_String(t *testing.T) { - v := gopsutil.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) - } -} - -func TestCpuInfo(t *testing.T) { - v, err := gopsutil.CPUInfo() - if err != nil { - t.Errorf("error %v", err) - } - for _, vv := range v { - if vv.ModelName == "" { - t.Errorf("could not get CPU Info: %v", vv) - } - } -} diff --git a/test/disk_test.go b/test/disk_test.go deleted file mode 100644 index dd9a836..0000000 --- a/test/disk_test.go +++ /dev/null @@ -1,90 +0,0 @@ -package test - -import ( - "fmt" - "runtime" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestDisk_usage(t *testing.T) { - path := "/" - if runtime.GOOS == "windows" { - path = "C:" - } - v, err := gopsutil.DiskUsage(path) - if err != nil { - t.Errorf("error %v", err) - } - if v.Path != path { - t.Errorf("error %v", err) - } -} - -func TestDisk_partitions(t *testing.T) { - ret, err := gopsutil.DiskPartitions(false) - if err != nil { - t.Errorf("error %v", err) - } - empty := gopsutil.DiskPartitionStat{} - for _, disk := range ret { - if disk == empty { - t.Errorf("Could not get device info %v", disk) - } - } -} - -func TestDisk_io_counters(t *testing.T) { - ret, err := gopsutil.DiskIOCounters() - if err != nil { - t.Errorf("error %v", err) - } - empty := gopsutil.DiskIOCountersStat{} - for _, io := range ret { - if io == empty { - t.Errorf("io_counter error %v", io) - } - } -} - -func TestDiskUsageStat_String(t *testing.T) { - v := gopsutil.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 := gopsutil.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 := gopsutil.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) - } -} diff --git a/test/host_test.go b/test/host_test.go deleted file mode 100644 index 5724b15..0000000 --- a/test/host_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package test - -import ( - "fmt" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestHostInfo(t *testing.T) { - v, err := gopsutil.HostInfo() - if err != nil { - t.Errorf("error %v", err) - } - empty := &gopsutil.HostInfoStat{} - if v == empty { - t.Errorf("Could not get hostinfo %v", v) - } -} - -func TestBoot_time(t *testing.T) { - v, err := gopsutil.BootTime() - if err != nil { - t.Errorf("error %v", err) - } - if v == 0 { - t.Errorf("Could not boot time %v", v) - } -} - -func TestUsers(t *testing.T) { - v, err := gopsutil.Users() - if err != nil { - t.Errorf("error %v", err) - } - empty := gopsutil.UserStat{} - for _, u := range v { - if u == empty { - t.Errorf("Could not Users %v", v) - } - } -} - -func TestHostInfoStat_String(t *testing.T) { - v := gopsutil.HostInfoStat{ - Hostname: "test", - Uptime: 3000, - Procs: 100, - OS: "linux", - Platform: "ubuntu", - } - e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","virtualizationSystem":"","virtualizationRole":""}` - if e != fmt.Sprintf("%v", v) { - t.Errorf("HostInfoStat string is invalid: %v", v) - } -} - -func TestUserStat_String(t *testing.T) { - v := gopsutil.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) - } -} diff --git a/test/load_test.go b/test/load_test.go deleted file mode 100644 index a3feb61..0000000 --- a/test/load_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package test - -import ( - "fmt" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestLoad(t *testing.T) { - v, err := gopsutil.LoadAvg() - if err != nil { - t.Errorf("error %v", err) - } - - empty := &gopsutil.LoadAvgStat{} - if v == empty { - t.Errorf("error load: %v", v) - } -} - -func TestLoadAvgStat_String(t *testing.T) { - v := gopsutil.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) - } -} diff --git a/test/mem_test.go b/test/mem_test.go deleted file mode 100644 index 163eff8..0000000 --- a/test/mem_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package test - -import ( - "fmt" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestVirtual_memory(t *testing.T) { - v, err := gopsutil.VirtualMemory() - if err != nil { - t.Errorf("error %v", err) - } - - empty := &gopsutil.VirtualMemoryStat{} - if v == empty { - t.Errorf("error %v", v) - } -} - -func TestSwap_memory(t *testing.T) { - v, err := gopsutil.SwapMemory() - if err != nil { - t.Errorf("error %v", err) - } - empty := &gopsutil.SwapMemoryStat{} - if v == empty { - t.Errorf("error %v", v) - } -} - -func TestVirtualMemoryStat_String(t *testing.T) { - v := gopsutil.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 := gopsutil.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) - } -} diff --git a/test/net_test.go b/test/net_test.go deleted file mode 100644 index 3a25486..0000000 --- a/test/net_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package test - -import ( - "fmt" - "testing" - - "github.com/shirou/gopsutil" -) - -func TestAddrString(t *testing.T) { - v := gopsutil.Addr{IP: "192.168.0.1", Port: 8000} - - s := fmt.Sprintf("%v", v) - if s != "{\"ip\":\"192.168.0.1\",\"port\":8000}" { - t.Errorf("Addr string is invalid: %v", v) - } -} - -func TestNetIOCountersStatString(t *testing.T) { - v := gopsutil.NetIOCountersStat{ - Name: "test", - 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}` - if e != fmt.Sprintf("%v", v) { - t.Errorf("NetIOCountersStat string is invalid: %v", v) - } -} - -func TestNetConnectionStatString(t *testing.T) { - v := gopsutil.NetConnectionStat{ - Fd: 10, - Family: 10, - Type: 10, - } - e := `{"fd":10,"family":10,"type":10,"localaddr":{"ip":"","port":0},"remoteaddr":{"ip":"","port":0},"status":"","pid":0}` - if e != fmt.Sprintf("%v", v) { - t.Errorf("NetConnectionStat string is invalid: %v", v) - } - -} - -func TestNetIOCounters(t *testing.T) { - v, err := gopsutil.NetIOCounters(true) - if err != nil { - t.Errorf("Could not get NetIOCounters: %v", err) - } - if len(v) == 0 { - t.Errorf("Could not get NetIOCounters: %v", v) - } - for _, vv := range v { - if vv.Name == "" { - t.Errorf("Invalid NetIOCounters: %v", vv) - } - } -} diff --git a/test/process_posix_test.go b/test/process_posix_test.go deleted file mode 100644 index 94df7f1..0000000 --- a/test/process_posix_test.go +++ /dev/null @@ -1,21 +0,0 @@ -// +build linux freebsd - -package test - -import ( - "os" - "syscall" - "testing" - - "github.com/shirou/gopsutil" -) - -func Test_SendSignal(t *testing.T) { - checkPid := os.Getpid() - - p, _ := gopsutil.NewProcess(int32(checkPid)) - err := p.SendSignal(syscall.SIGCONT) - if err != nil { - t.Errorf("send signal %v", err) - } -} diff --git a/test/process_test.go b/test/process_test.go deleted file mode 100644 index ce4dfab..0000000 --- a/test/process_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package test - -import ( - "os" - "runtime" - "testing" - - "github.com/shirou/gopsutil" -) - -func test_getProcess() gopsutil.Process { - checkPid := os.Getpid() - if runtime.GOOS == "windows" { - checkPid = 7960 - } - ret, _ := gopsutil.NewProcess(int32(checkPid)) - return *ret -} - -func Test_Pids(t *testing.T) { - ret, err := gopsutil.Pids() - if err != nil { - t.Errorf("error %v", err) - } - if len(ret) == 0 { - t.Errorf("could not get pids %v", ret) - } -} - -func Test_Pid_exists(t *testing.T) { - checkPid := 1 - if runtime.GOOS == "windows" { - checkPid = 0 - } - - ret, err := gopsutil.PidExists(int32(checkPid)) - if err != nil { - t.Errorf("error %v", err) - } - - if ret == false { - t.Errorf("could not get init process %v", ret) - } -} - -func Test_NewProcess(t *testing.T) { - checkPid := 1 - if runtime.GOOS == "windows" { - checkPid = 0 - } - - ret, err := gopsutil.NewProcess(int32(checkPid)) - if err != nil { - t.Errorf("error %v", err) - } - empty := &gopsutil.Process{} - if runtime.GOOS != "windows" { // Windows pid is 0 - if empty == ret { - t.Errorf("error %v", ret) - } - } - -} - -func Test_Process_memory_maps(t *testing.T) { - checkPid := os.Getpid() - if runtime.GOOS == "windows" { - checkPid = 0 - } - ret, err := gopsutil.NewProcess(int32(checkPid)) - - mmaps, err := ret.MemoryMaps(false) - if err != nil { - t.Errorf("memory map get error %v", err) - } - empty := gopsutil.MemoryMapsStat{} - for _, m := range *mmaps { - if m == empty { - t.Errorf("memory map get error %v", m) - } - } - -} - -func Test_Process_Ppid(t *testing.T) { - p := test_getProcess() - - v, err := p.Ppid() - if err != nil { - t.Errorf("geting ppid error %v", err) - } - if v == 0 { - t.Errorf("return value is 0 %v", v) - } - -} - -func Test_Process_IOCounters(t *testing.T) { - p := test_getProcess() - - v, err := p.IOCounters() - if err != nil { - t.Errorf("geting ppid error %v", err) - return - } - empty := &gopsutil.IOCountersStat{} - if v == empty { - t.Errorf("error %v", v) - } -} - -func Test_Process_NumCtx(t *testing.T) { - p := test_getProcess() - - _, err := p.NumCtxSwitches() - if err != nil { - t.Errorf("geting numctx error %v", err) - return - } -}