Newer
Older
Mohamed S. Mahmoud
committed
package decode
import (
"testing"
"time"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow"
Mohamed S. Mahmoud
committed
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
// NOTE: more tests in convert_flp_test.go
Mohamed S. Mahmoud
committed
func TestPBFlowToMap(t *testing.T) {
someTime := time.Now()
var someDuration time.Duration = 10000000 // 10ms
flow := &pbflow.Record{
Interface: "eth0",
DupList: []*pbflow.DupMapEntry{
{
Interface: "5e6e92caa1d51cf",
Direction: pbflow.Direction_INGRESS,
},
{
Interface: "eth0",
Direction: pbflow.Direction_EGRESS,
},
},
Mohamed S. Mahmoud
committed
EthProtocol: 2048,
Bytes: 456,
Packets: 123,
Direction: pbflow.Direction_EGRESS,
TimeFlowStart: timestamppb.New(someTime),
TimeFlowEnd: timestamppb.New(someTime),
Duplicate: true,
Network: &pbflow.Network{
SrcAddr: &pbflow.IP{
IpFamily: &pbflow.IP_Ipv4{Ipv4: 0x01020304},
},
DstAddr: &pbflow.IP{
IpFamily: &pbflow.IP_Ipv4{Ipv4: 0x05060708},
},
Dscp: 64,
Mohamed S. Mahmoud
committed
},
DataLink: &pbflow.DataLink{
DstMac: 0x112233445566,
SrcMac: 0x010203040506,
},
Transport: &pbflow.Transport{
Protocol: 6,
SrcPort: 23000,
DstPort: 443,
},
AgentIp: &pbflow.IP{
IpFamily: &pbflow.IP_Ipv4{Ipv4: 0x0a090807},
},
Flags: 0x100,
PktDropBytes: 200,
PktDropPackets: 20,
PktDropLatestFlags: 0x100,
PktDropLatestState: 1,
PktDropLatestDropCause: 4,
DnsLatency: durationpb.New(someDuration),
DnsId: 1,
DnsFlags: 0x80,
Mohamed S. Mahmoud
committed
TimeFlowRtt: durationpb.New(someDuration),
}
out := PBFlowToMap(flow)
assert.NotZero(t, out["TimeReceived"])
delete(out, "TimeReceived")
assert.Equal(t, config.GenericMap{
"IfDirections": []int{0, 1},
Mohamed S. Mahmoud
committed
"Bytes": uint64(456),
"SrcAddr": "1.2.3.4",
"DstAddr": "5.6.7.8",
Mohamed S. Mahmoud
committed
"DstMac": "11:22:33:44:55:66",
"SrcMac": "01:02:03:04:05:06",
"SrcPort": uint16(23000),
"DstPort": uint16(443),
Mohamed S. Mahmoud
committed
"Duplicate": true,
"Etype": uint16(2048),
"Packets": uint32(123),
"Proto": uint8(6),
Mohamed S. Mahmoud
committed
"TimeFlowStartMs": someTime.UnixMilli(),
"TimeFlowEndMs": someTime.UnixMilli(),
"Interfaces": []string{"5e6e92caa1d51cf", "eth0"},
Mohamed S. Mahmoud
committed
"AgentIP": "10.9.8.7",
"Flags": uint16(0x100),
Mohamed S. Mahmoud
committed
"PktDropBytes": uint64(200),
"PktDropPackets": uint32(20),
"PktDropLatestFlags": uint16(0x100),
Mohamed S. Mahmoud
committed
"PktDropLatestState": "TCP_ESTABLISHED",
"PktDropLatestDropCause": "SKB_DROP_REASON_PKT_TOO_SMALL",
"DnsLatencyMs": someDuration.Milliseconds(),
"DnsId": uint16(1),
"DnsFlags": uint16(0x80),
Mohamed S. Mahmoud
committed
"DnsFlagsResponseCode": "NoError",
Mohamed S. Mahmoud
committed
"DnsErrno": uint8(0),
Mohamed S. Mahmoud
committed
"TimeFlowRttNs": someDuration.Nanoseconds(),
}, out)
}