2014-12-30 21:09:05 +08:00
|
|
|
package net
|
2014-05-11 23:12:43 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-16 15:33:07 +08:00
|
|
|
"os"
|
2016-03-15 21:47:56 +08:00
|
|
|
"runtime"
|
2014-05-12 10:51:08 +08:00
|
|
|
"testing"
|
2016-03-15 21:47:56 +08:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
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) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := IOCountersStat{
|
2014-05-12 10:51:08 +08:00
|
|
|
Name: "test",
|
2014-05-11 23:12:43 +08:00
|
|
|
BytesSent: 100,
|
|
|
|
}
|
2016-06-13 20:27:25 +08:00
|
|
|
e := `{"name":"test","bytesSent":100,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0}`
|
2014-05-11 23:12:43 +08:00
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("NetIOCountersStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 05:19:43 +08:00
|
|
|
func TestNetProtoCountersStatString(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := ProtoCountersStat{
|
2015-11-20 05:19:43 +08:00
|
|
|
Protocol: "tcp",
|
|
|
|
Stats: map[string]int64{
|
|
|
|
"MaxConn": -1,
|
|
|
|
"ActiveOpens": 4000,
|
|
|
|
"PassiveOpens": 3000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
e := `{"protocol":"tcp","stats":{"ActiveOpens":4000,"MaxConn":-1,"PassiveOpens":3000}}`
|
|
|
|
if e != fmt.Sprintf("%v", v) {
|
|
|
|
t.Errorf("NetProtoCountersStat string is invalid: %v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-11 23:12:43 +08:00
|
|
|
func TestNetConnectionStatString(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v := ConnectionStat{
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-01 20:29:25 +08:00
|
|
|
func TestNetIOCountersAll(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := IOCounters(false)
|
|
|
|
per, err := IOCounters(true)
|
2015-01-01 20:29:25 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get NetIOCounters: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) != 1 {
|
|
|
|
t.Errorf("Could not get NetIOCounters: %v", v)
|
|
|
|
}
|
|
|
|
if v[0].Name != "all" {
|
|
|
|
t.Errorf("Invalid NetIOCounters: %v", v)
|
|
|
|
}
|
|
|
|
var pr uint64
|
|
|
|
for _, p := range per {
|
|
|
|
pr += p.PacketsRecv
|
|
|
|
}
|
|
|
|
if v[0].PacketsRecv != pr {
|
|
|
|
t.Errorf("invalid sum value: %v, %v", v[0].PacketsRecv, pr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetIOCountersPerNic(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := IOCounters(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
|
|
|
|
2016-03-10 21:32:16 +08:00
|
|
|
func TestGetNetIOCountersAll(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
n := []IOCountersStat{
|
|
|
|
IOCountersStat{
|
2015-01-01 20:29:25 +08:00
|
|
|
Name: "a",
|
|
|
|
BytesRecv: 10,
|
|
|
|
PacketsRecv: 10,
|
|
|
|
},
|
2016-03-22 22:09:12 +08:00
|
|
|
IOCountersStat{
|
2015-01-01 20:29:25 +08:00
|
|
|
Name: "b",
|
|
|
|
BytesRecv: 10,
|
|
|
|
PacketsRecv: 10,
|
|
|
|
Errin: 10,
|
|
|
|
},
|
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
ret, err := getIOCountersAll(n)
|
2015-01-01 20:29:25 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(ret) != 1 {
|
|
|
|
t.Errorf("invalid return count")
|
|
|
|
}
|
|
|
|
if ret[0].Name != "all" {
|
|
|
|
t.Errorf("invalid return name")
|
|
|
|
}
|
|
|
|
if ret[0].BytesRecv != 20 {
|
|
|
|
t.Errorf("invalid count bytesrecv")
|
|
|
|
}
|
|
|
|
if ret[0].Errin != 10 {
|
|
|
|
t.Errorf("invalid count errin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 23:39:47 +08:00
|
|
|
func TestNetInterfaces(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := Interfaces()
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 15:12:41 +08:00
|
|
|
|
2015-11-20 05:19:43 +08:00
|
|
|
func TestNetProtoCountersStatsAll(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := ProtoCounters(nil)
|
2015-11-20 05:19:43 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get NetProtoCounters: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Fatalf("Could not get NetProtoCounters: %v", err)
|
|
|
|
}
|
|
|
|
for _, vv := range v {
|
|
|
|
if vv.Protocol == "" {
|
|
|
|
t.Errorf("Invalid NetProtoCountersStat: %v", vv)
|
|
|
|
}
|
|
|
|
if len(vv.Stats) == 0 {
|
|
|
|
t.Errorf("Invalid NetProtoCountersStat: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetProtoCountersStats(t *testing.T) {
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := ProtoCounters([]string{"tcp", "ip"})
|
2015-11-20 05:19:43 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get NetProtoCounters: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Fatalf("Could not get NetProtoCounters: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) != 2 {
|
|
|
|
t.Fatalf("Go incorrect number of NetProtoCounters: %v", err)
|
|
|
|
}
|
|
|
|
for _, vv := range v {
|
|
|
|
if vv.Protocol != "tcp" && vv.Protocol != "ip" {
|
|
|
|
t.Errorf("Invalid NetProtoCountersStat: %v", vv)
|
|
|
|
}
|
|
|
|
if len(vv.Stats) == 0 {
|
|
|
|
t.Errorf("Invalid NetProtoCountersStat: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:12:41 +08:00
|
|
|
func TestNetConnections(t *testing.T) {
|
2015-09-16 15:33:07 +08:00
|
|
|
if ci := os.Getenv("CI"); ci != "" { // skip if test on drone.io
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := Connections("inet")
|
2015-09-16 15:12:41 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not get NetConnections: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Errorf("could not get NetConnections: %v", v)
|
|
|
|
}
|
|
|
|
for _, vv := range v {
|
|
|
|
if vv.Family == 0 {
|
|
|
|
t.Errorf("invalid NetConnections: %v", vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-07 13:56:02 +08:00
|
|
|
|
|
|
|
func TestNetFilterCounters(t *testing.T) {
|
|
|
|
if ci := os.Getenv("CI"); ci != "" { // skip if test on drone.io
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:47:56 +08:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
// some test environment has not the path.
|
2016-03-23 09:52:46 +08:00
|
|
|
if !common.PathExists("/proc/sys/net/netfilter/nf_conntrackCount") {
|
2016-03-15 21:47:56 +08:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
v, err := FilterCounters()
|
2015-12-07 13:56:02 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not get NetConnections: %v", err)
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Errorf("could not get NetConnections: %v", v)
|
|
|
|
}
|
|
|
|
for _, vv := range v {
|
|
|
|
if vv.ConnTrackMax == 0 {
|
2016-03-23 09:52:46 +08:00
|
|
|
t.Errorf("nf_conntrackMax needs to be greater than zero: %v", vv)
|
2015-12-07 13:56:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|