github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/tracing_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package filters
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	flowpb "github.com/cilium/cilium/api/v1/flow"
    13  	v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
    14  )
    15  
    16  func TestTraceIDFilter(t *testing.T) {
    17  	type args struct {
    18  		f  []*flowpb.FlowFilter
    19  		ev *v1.Event
    20  	}
    21  	tests := []struct {
    22  		name string
    23  		args args
    24  		want bool
    25  	}{
    26  		{
    27  			name: "match example trace ID",
    28  			args: args{
    29  				f: []*flowpb.FlowFilter{
    30  					{
    31  						TraceId: []string{"4bf92f3577b34da6a3ce929d0e0e4736"},
    32  					},
    33  				},
    34  				ev: &v1.Event{Event: &flowpb.Flow{
    35  					TraceContext: &flowpb.TraceContext{
    36  						Parent: &flowpb.TraceParent{
    37  							TraceId: "4bf92f3577b34da6a3ce929d0e0e4736",
    38  						},
    39  					},
    40  				}},
    41  			},
    42  			want: true,
    43  		}, {
    44  			name: "match example trace ID with multiple input filters",
    45  			args: args{
    46  				f: []*flowpb.FlowFilter{
    47  					{
    48  						TraceId: []string{
    49  							"deadbeefcafe",
    50  							"4bf92f3577b34da6a3ce929d0e0e4736",
    51  						},
    52  					},
    53  				},
    54  				ev: &v1.Event{Event: &flowpb.Flow{
    55  					TraceContext: &flowpb.TraceContext{
    56  						Parent: &flowpb.TraceParent{
    57  							TraceId: "4bf92f3577b34da6a3ce929d0e0e4736",
    58  						},
    59  					},
    60  				}},
    61  			},
    62  			want: true,
    63  		}, {
    64  			name: "empty trace ID filter on flow without trace ID",
    65  			args: args{
    66  				f: []*flowpb.FlowFilter{
    67  					{
    68  						TraceId: []string{""},
    69  					},
    70  				},
    71  				ev: &v1.Event{Event: &flowpb.Flow{}},
    72  			},
    73  			want: true,
    74  		}, {
    75  			name: "empty trace ID filter on flow with trace ID",
    76  			args: args{
    77  				f: []*flowpb.FlowFilter{
    78  					{
    79  						TraceId: []string{""},
    80  					},
    81  				},
    82  				ev: &v1.Event{Event: &flowpb.Flow{
    83  					TraceContext: &flowpb.TraceContext{
    84  						Parent: &flowpb.TraceParent{
    85  							TraceId: "4bf92f3577b34da6a3ce929d0e0e4736",
    86  						},
    87  					},
    88  				}},
    89  			},
    90  			want: false,
    91  		}, {
    92  			name: "don't match example trace ID",
    93  			args: args{
    94  				f: []*flowpb.FlowFilter{
    95  					{
    96  						TraceId: []string{"deadbeefcafe"},
    97  					},
    98  				},
    99  				ev: &v1.Event{Event: &flowpb.Flow{
   100  					TraceContext: &flowpb.TraceContext{
   101  						Parent: &flowpb.TraceParent{
   102  							TraceId: "4bf92f3577b34da6a3ce929d0e0e4736",
   103  						},
   104  					},
   105  				}},
   106  			},
   107  			want: false,
   108  		}, {
   109  			name: "no trace ID in flow",
   110  			args: args{
   111  				f: []*flowpb.FlowFilter{
   112  					{
   113  						TraceId: []string{"4bf92f3577b34da6a3ce929d0e0e4736"},
   114  					},
   115  				},
   116  				ev: &v1.Event{Event: &flowpb.Flow{}},
   117  			},
   118  			want: false,
   119  		},
   120  	}
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			fl, err := BuildFilterList(context.Background(), tt.args.f, []OnBuildFilter{&TraceIDFilter{}})
   124  			assert.NoError(t, err)
   125  			assert.Equal(t, tt.want, fl.MatchOne(tt.args.ev))
   126  		})
   127  	}
   128  }