diff --git a/metricbeat/module/system/socket/_meta/data.json b/metricbeat/module/system/socket/_meta/data.json index 81e634a8b70..0a02196601d 100644 --- a/metricbeat/module/system/socket/_meta/data.json +++ b/metricbeat/module/system/socket/_meta/data.json @@ -19,16 +19,16 @@ }, "process": { "args": [ - "/tmp/go-build122914279/b001/socket.test", + "/tmp/go-build701928973/b001/socket.test", "-data" ], - "executable": "/tmp/go-build122914279/b001/socket.test", + "executable": "/tmp/go-build701928973/b001/socket.test", "name": "socket.test", - "pid": 20595 + "pid": 9578 }, "server": { "ip": "::", - "port": 32915 + "port": 42573 }, "service": { "type": "system" @@ -37,10 +37,10 @@ "socket": { "local": { "ip": "::", - "port": 32915 + "port": 42573 }, "process": { - "cmdline": "/tmp/go-build122914279/b001/socket.test -data" + "cmdline": "/tmp/go-build701928973/b001/socket.test -data" } } }, diff --git a/metricbeat/module/system/socket/_meta/data_inbound.json b/metricbeat/module/system/socket/_meta/data_inbound.json new file mode 100644 index 00000000000..f473886e9fc --- /dev/null +++ b/metricbeat/module/system/socket/_meta/data_inbound.json @@ -0,0 +1,46 @@ +{ + "@timestamp": "2017-10-12T08:05:34.853Z", + "agent": { + "hostname": "host.example.com", + "name": "host.example.com" + }, + "event": { + "dataset": "system.socket", + "duration": 115000, + "module": "system" + }, + "metricset": { + "name": "socket" + }, + "network": { + "direction": "inbound", + "iana_number": "41", + "type": "ipv6" + }, + "process": { + "pid": -1 + }, + "server": { + "ip": "::1", + "port": 42573 + }, + "service": { + "type": "system" + }, + "system": { + "socket": { + "local": { + "ip": "::1", + "port": 42573 + }, + "remote": { + "ip": "::1", + "port": 37854 + } + } + }, + "user": { + "full_name": "root", + "id": "0" + } +} \ No newline at end of file diff --git a/metricbeat/module/system/socket/_meta/data_outbound.json b/metricbeat/module/system/socket/_meta/data_outbound.json new file mode 100644 index 00000000000..511c00a6668 --- /dev/null +++ b/metricbeat/module/system/socket/_meta/data_outbound.json @@ -0,0 +1,46 @@ +{ + "@timestamp": "2017-10-12T08:05:34.853Z", + "agent": { + "hostname": "host.example.com", + "name": "host.example.com" + }, + "event": { + "dataset": "system.socket", + "duration": 115000, + "module": "system" + }, + "metricset": { + "name": "socket" + }, + "network": { + "direction": "outbound", + "iana_number": "41", + "type": "ipv6" + }, + "process": { + "pid": -1 + }, + "server": { + "ip": "::1", + "port": 37858 + }, + "service": { + "type": "system" + }, + "system": { + "socket": { + "local": { + "ip": "::1", + "port": 37858 + }, + "remote": { + "ip": "::1", + "port": 42573 + } + } + }, + "user": { + "full_name": "root", + "id": "0" + } +} \ No newline at end of file diff --git a/metricbeat/module/system/socket/socket_test.go b/metricbeat/module/system/socket/socket_test.go index d939784ad15..45234816098 100644 --- a/metricbeat/module/system/socket/socket_test.go +++ b/metricbeat/module/system/socket/socket_test.go @@ -20,6 +20,7 @@ package socket import ( + "fmt" "net" "os" "strconv" @@ -31,20 +32,48 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/libbeat/common" + sock "github.com/elastic/beats/metricbeat/helper/socket" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { + directionIs := func(direction string) func(e common.MapStr) bool { + return func(e common.MapStr) bool { + v, err := e.GetValue("network.direction") + return err == nil && v == direction + } + } + + dataFiles := []struct { + direction string + path string + }{ + {sock.ListeningName, "."}, + {sock.InboundName, "./_meta/data_inbound.json"}, + {sock.OutboundName, "./_meta/data_outbound.json"}, + } + + f := mbtest.NewReportingMetricSetV2(t, getConfig()) + ln, err := net.Listen("tcp", ":0") if err != nil { t.Fatal(err) } defer ln.Close() - f := mbtest.NewReportingMetricSetV2(t, getConfig()) - err = mbtest.WriteEventsReporterV2(f, t, ".") - if err != nil { - t.Fatal("write", err) + for _, df := range dataFiles { + c, err := net.Dial("tcp", ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + c.Close() + + t.Run(fmt.Sprintf("direction:%s", df.direction), func(t *testing.T) { + err = mbtest.WriteEventsReporterV2Cond(f, t, df.path, directionIs(df.direction)) + if err != nil { + t.Fatal("write", err) + } + }) } }