Skip to content
Snippets Groups Projects
decode_protobuf_test.go 3.13 KiB
Newer Older
  • Learn to ignore specific revisions
  • package decode
    
    import (
    	"testing"
    	"time"
    
    	"github.com/netobserv/flowlogs-pipeline/pkg/config"
    	"github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow"
    
    	"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
    
    
    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,
    			},
    		},
    
    		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},
    			},
    
    		},
    		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,
    
    		TimeFlowRtt:            durationpb.New(someDuration),
    	}
    
    	out := PBFlowToMap(flow)
    	assert.NotZero(t, out["TimeReceived"])
    	delete(out, "TimeReceived")
    	assert.Equal(t, config.GenericMap{
    
    		"Bytes":                  uint64(456),
    		"SrcAddr":                "1.2.3.4",
    		"DstAddr":                "5.6.7.8",
    
    		"DstMac":                 "11:22:33:44:55:66",
    		"SrcMac":                 "01:02:03:04:05:06",
    
    		"SrcPort":                uint16(23000),
    		"DstPort":                uint16(443),
    
    		"Etype":                  uint16(2048),
    		"Packets":                uint32(123),
    		"Proto":                  uint8(6),
    
    		"TimeFlowStartMs":        someTime.UnixMilli(),
    		"TimeFlowEndMs":          someTime.UnixMilli(),
    
    		"Interfaces":             []string{"5e6e92caa1d51cf", "eth0"},
    
    		"Flags":                  uint16(0x100),
    
    		"PktDropPackets":         uint32(20),
    		"PktDropLatestFlags":     uint16(0x100),
    
    		"PktDropLatestState":     "TCP_ESTABLISHED",
    		"PktDropLatestDropCause": "SKB_DROP_REASON_PKT_TOO_SMALL",
    		"DnsLatencyMs":           someDuration.Milliseconds(),
    
    		"DnsId":                  uint16(1),
    		"DnsFlags":               uint16(0x80),
    
    		"TimeFlowRttNs":          someDuration.Nanoseconds(),
    	}, out)
    
    }