2014-05-23 09:31:47 +08:00
|
|
|
package gopsutil
|
2014-05-11 23:12:43 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-12 10:51:08 +08:00
|
|
|
"testing"
|
2014-05-11 23:12:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddrString(t *testing.T) {
|
2014-05-23 09:31:47 +08:00
|
|
|
v := Addr{IP: "192.168.0.1", Port: 8000}
|
2014-05-11 23:12:43 +08:00
|
|
|
|
|
|
|
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) {
|
2014-05-23 09:31:47 +08:00
|
|
|
v := NetIOCountersStat{
|
2014-05-12 10:51:08 +08:00
|
|
|
Name: "test",
|
2014-05-11 23:12:43 +08:00
|
|
|
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) {
|
2014-05-23 09:31:47 +08:00
|
|
|
v := NetConnectionStat{
|
2014-05-12 10:51:08 +08:00
|
|
|
Fd: 10,
|
2014-05-11 23:12:43 +08:00
|
|
|
Family: 10,
|
2014-05-12 10:51:08 +08:00
|
|
|
Type: 10,
|
2014-05-11 23:12:43 +08:00
|
|
|
}
|
2014-05-16 13:21:19 +08:00
|
|
|
e := `{"fd":10,"family":10,"type":10,"localaddr":{"ip":"","port":0},"remoteaddr":{"ip":"","port":0},"status":"","pid":0}`
|
2014-05-11 23:12:43 +08:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("NetConnectionStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetIOCounters(t *testing.T) {
|
2014-05-23 09:31:47 +08:00
|
|
|
v, err := NetIOCounters(true)
|
2014-05-12 10:51:08 +08:00
|
|
|
if err != nil {
|
2014-05-11 23:12:43 +08:00
|
|
|
t.Errorf("Could not get NetIOCounters: %v", err)
|
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
if len(v) == 0 {
|
2014-05-11 23:12:43 +08:00
|
|
|
t.Errorf("Could not get NetIOCounters: %v", v)
|
|
|
|
}
|
2014-05-12 10:51:08 +08:00
|
|
|
for _, vv := range v {
|
|
|
|
if vv.Name == "" {
|
2014-05-11 23:12:43 +08:00
|
|
|
t.Errorf("Invalid NetIOCounters: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-02 23:33:55 +08:00
|
|
|
|
2014-06-02 23:39:47 +08:00
|
|
|
func TestNetInterfaces(t *testing.T) {
|
2014-06-02 23:33:55 +08:00
|
|
|
v, err := NetInterfaces()
|
2014-06-02 23:39:47 +08:00
|
|
|
if err != nil {
|
2014-06-02 23:33:55 +08:00
|
|
|
t.Errorf("Could not get NetInterfaceStat: %v", err)
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
if len(v) == 0 {
|
2014-06-02 23:33:55 +08:00
|
|
|
t.Errorf("Could not get NetInterfaceStat: %v", err)
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
for _, vv := range v {
|
|
|
|
if vv.Name == "" {
|
2014-06-02 23:33:55 +08:00
|
|
|
t.Errorf("Invalid NetInterface: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|