github.com/cilium/cilium@v1.16.2/pkg/hubble/parser/debug/parser_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package debug
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/binary"
     9  	"fmt"
    10  	"io"
    11  	"testing"
    12  
    13  	"github.com/sirupsen/logrus"
    14  	"github.com/stretchr/testify/assert"
    15  	"google.golang.org/protobuf/types/known/wrapperspb"
    16  
    17  	flowpb "github.com/cilium/cilium/api/v1/flow"
    18  	"github.com/cilium/cilium/pkg/byteorder"
    19  	"github.com/cilium/cilium/pkg/hubble/parser/getters"
    20  	"github.com/cilium/cilium/pkg/hubble/testutils"
    21  	"github.com/cilium/cilium/pkg/monitor"
    22  	monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
    23  )
    24  
    25  var log *logrus.Logger
    26  
    27  func init() {
    28  	log = logrus.New()
    29  	log.SetOutput(io.Discard)
    30  }
    31  
    32  func encodeDebugEvent(msg *monitor.DebugMsg) []byte {
    33  	buf := &bytes.Buffer{}
    34  	if err := binary.Write(buf, byteorder.Native, msg); err != nil {
    35  		panic(fmt.Sprintf("failed to encode debug event: %s", err))
    36  	}
    37  	return buf.Bytes()
    38  }
    39  
    40  func TestDecodeDebugEvent(t *testing.T) {
    41  	endpointGetter := &testutils.FakeEndpointGetter{
    42  		OnGetEndpointInfoByID: func(id uint16) (endpoint getters.EndpointInfo, ok bool) {
    43  			if id == 1234 {
    44  				return &testutils.FakeEndpointInfo{
    45  					ID:           1234,
    46  					Identity:     5678,
    47  					PodName:      "somepod",
    48  					PodNamespace: "default",
    49  				}, true
    50  			}
    51  			return nil, false
    52  		},
    53  	}
    54  
    55  	p, err := New(log, endpointGetter)
    56  	assert.NoError(t, err)
    57  
    58  	tt := []struct {
    59  		name    string
    60  		data    []byte
    61  		cpu     int
    62  		ev      *flowpb.DebugEvent
    63  		wantErr bool
    64  	}{
    65  		{
    66  			name: "Generic event",
    67  			data: encodeDebugEvent(&monitor.DebugMsg{
    68  				Type:    monitorAPI.MessageTypeDebug,
    69  				SubType: monitor.DbgGeneric,
    70  				Source:  0,
    71  				Arg1:    1,
    72  				Arg2:    2,
    73  			}),
    74  			cpu: 0,
    75  			ev: &flowpb.DebugEvent{
    76  				Type:    flowpb.DebugEventType_DBG_GENERIC,
    77  				Hash:    wrapperspb.UInt32(0),
    78  				Arg1:    wrapperspb.UInt32(1),
    79  				Arg2:    wrapperspb.UInt32(2),
    80  				Arg3:    wrapperspb.UInt32(0),
    81  				Message: "No message, arg1=1 (0x1) arg2=2 (0x2)",
    82  				Cpu:     wrapperspb.Int32(0),
    83  			},
    84  		},
    85  		{
    86  			name: "IPv4 Mapping",
    87  			data: encodeDebugEvent(&monitor.DebugMsg{
    88  				Type:    monitorAPI.MessageTypeDebug,
    89  				SubType: monitor.DbgIPIDMapSucceed4,
    90  				Source:  1234,
    91  				Hash:    705182630,
    92  				Arg1:    3909094154,
    93  				Arg2:    2,
    94  			}),
    95  			cpu: 2,
    96  			ev: &flowpb.DebugEvent{
    97  				Type: flowpb.DebugEventType_DBG_IP_ID_MAP_SUCCEED4,
    98  				Source: &flowpb.Endpoint{
    99  					ID:        1234,
   100  					Identity:  5678,
   101  					PodName:   "somepod",
   102  					Namespace: "default",
   103  				},
   104  				Hash:    wrapperspb.UInt32(705182630),
   105  				Arg1:    wrapperspb.UInt32(3909094154),
   106  				Arg2:    wrapperspb.UInt32(2),
   107  				Arg3:    wrapperspb.UInt32(0),
   108  				Message: "Successfully mapped addr=10.11.0.233 to identity=2",
   109  				Cpu:     wrapperspb.Int32(2),
   110  			},
   111  		},
   112  		{
   113  			name: "ICMP6 Handle",
   114  			data: encodeDebugEvent(&monitor.DebugMsg{
   115  				Type:    monitorAPI.MessageTypeDebug,
   116  				SubType: monitor.DbgIcmp6Handle,
   117  				Source:  1234,
   118  				Hash:    0x9dd55684,
   119  				Arg1:    129,
   120  			}),
   121  			cpu: 3,
   122  			ev: &flowpb.DebugEvent{
   123  				Type: flowpb.DebugEventType_DBG_ICMP6_HANDLE,
   124  				Source: &flowpb.Endpoint{
   125  					ID:        1234,
   126  					Identity:  5678,
   127  					PodName:   "somepod",
   128  					Namespace: "default",
   129  				},
   130  				Hash:    wrapperspb.UInt32(0x9dd55684),
   131  				Arg1:    wrapperspb.UInt32(129),
   132  				Arg2:    wrapperspb.UInt32(0),
   133  				Arg3:    wrapperspb.UInt32(0),
   134  				Message: "Handling ICMPv6 type=129",
   135  				Cpu:     wrapperspb.Int32(3),
   136  			},
   137  		},
   138  		{
   139  			name: "Unknown event",
   140  			data: encodeDebugEvent(&monitor.DebugMsg{
   141  				Type:    monitorAPI.MessageTypeDebug,
   142  				SubType: monitor.DbgUnspec,
   143  				Source:  10000,
   144  				Hash:    0x12345678,
   145  				Arg1:    10,
   146  				Arg2:    20,
   147  				Arg3:    30,
   148  			}),
   149  			cpu: 1,
   150  			ev: &flowpb.DebugEvent{
   151  				Type: flowpb.DebugEventType_DBG_EVENT_UNKNOWN,
   152  				Source: &flowpb.Endpoint{
   153  					ID: 10000,
   154  				},
   155  				Hash:    wrapperspb.UInt32(0x12345678),
   156  				Arg1:    wrapperspb.UInt32(10),
   157  				Arg2:    wrapperspb.UInt32(20),
   158  				Arg3:    wrapperspb.UInt32(30),
   159  				Message: "Unknown message type=0 arg1=10 arg2=20",
   160  				Cpu:     wrapperspb.Int32(1),
   161  			},
   162  		},
   163  		{
   164  			name:    "No data",
   165  			data:    nil,
   166  			wantErr: true,
   167  		},
   168  		{
   169  			name:    "Invalid data",
   170  			data:    []byte{0, 1, 2},
   171  			wantErr: true,
   172  		},
   173  	}
   174  
   175  	for _, tc := range tt {
   176  		t.Run(tc.name, func(t *testing.T) {
   177  			ev, err := p.Decode(tc.data, tc.cpu)
   178  			if tc.wantErr {
   179  				assert.NotNil(t, err)
   180  			} else {
   181  				assert.NoError(t, err)
   182  				assert.Equal(t, tc.ev, ev)
   183  			}
   184  		})
   185  	}
   186  }