github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/reply_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 "google.golang.org/protobuf/types/known/wrapperspb" 11 12 flowpb "github.com/cilium/cilium/api/v1/flow" 13 v1 "github.com/cilium/cilium/pkg/hubble/api/v1" 14 monitorAPI "github.com/cilium/cilium/pkg/monitor/api" 15 ) 16 17 func Test_filterByReplyField(t *testing.T) { 18 type args struct { 19 f []*flowpb.FlowFilter 20 ev *v1.Event 21 } 22 tests := []struct { 23 name string 24 args args 25 wantErr bool 26 want bool 27 }{ 28 { 29 name: "nil flow", 30 args: args{ 31 f: []*flowpb.FlowFilter{{Reply: []bool{true}}}, 32 ev: &v1.Event{}, 33 }, 34 want: false, 35 }, 36 { 37 name: "empty-param", 38 args: args{ 39 f: []*flowpb.FlowFilter{{Reply: []bool{}}}, 40 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: true}}}, 41 }, 42 want: true, 43 }, 44 { 45 name: "empty-param-2", 46 args: args{ 47 f: []*flowpb.FlowFilter{{Reply: []bool{}}}, 48 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: false}}}, 49 }, 50 want: true, 51 }, 52 { 53 name: "no-reply", 54 args: args{ 55 f: []*flowpb.FlowFilter{{Reply: []bool{false}}}, 56 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: false}}}, 57 }, 58 want: true, 59 }, 60 { 61 name: "trace-event-from-endpoint", 62 args: args{ 63 f: []*flowpb.FlowFilter{{Reply: []bool{false}}}, 64 ev: &v1.Event{Event: &flowpb.Flow{ 65 EventType: &flowpb.CiliumEventType{ 66 Type: monitorAPI.MessageTypeTrace, 67 SubType: monitorAPI.TraceFromLxc, 68 }, 69 IsReply: nil, 70 }}, 71 }, 72 want: false, 73 }, 74 { 75 name: "trace-event-to-endpoint", 76 args: args{ 77 f: []*flowpb.FlowFilter{{Reply: []bool{false}}}, 78 ev: &v1.Event{Event: &flowpb.Flow{ 79 EventType: &flowpb.CiliumEventType{ 80 Type: monitorAPI.MessageTypeTrace, 81 SubType: monitorAPI.TraceToLxc, 82 }, 83 IsReply: &wrapperspb.BoolValue{Value: false}, 84 }}, 85 }, 86 want: true, 87 }, 88 { 89 name: "reply", 90 args: args{ 91 f: []*flowpb.FlowFilter{{Reply: []bool{true}}}, 92 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: true}}}, 93 }, 94 want: true, 95 }, 96 { 97 name: "drop implies reply=false", 98 args: args{ 99 f: []*flowpb.FlowFilter{{Reply: []bool{false}}}, 100 ev: &v1.Event{Event: &flowpb.Flow{Verdict: flowpb.Verdict_DROPPED, IsReply: nil}}, 101 }, 102 want: true, 103 }, 104 { 105 name: "no-match", 106 args: args{ 107 f: []*flowpb.FlowFilter{{Reply: []bool{true}}}, 108 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: false}}}, 109 }, 110 want: false, 111 }, 112 { 113 name: "no-match-2", 114 args: args{ 115 f: []*flowpb.FlowFilter{{Reply: []bool{false}}}, 116 ev: &v1.Event{Event: &flowpb.Flow{IsReply: &wrapperspb.BoolValue{Value: true}}}, 117 }, 118 want: false, 119 }, 120 } 121 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 fl, err := BuildFilterList(context.Background(), tt.args.f, []OnBuildFilter{&ReplyFilter{}}) 125 if (err != nil) != tt.wantErr { 126 t.Errorf("\"%s\" error = %v, wantErr %v", tt.name, err, tt.wantErr) 127 return 128 } 129 if err != nil { 130 return 131 } 132 if got := fl.MatchOne(tt.args.ev); got != tt.want { 133 t.Errorf("\"%s\" got %v, want %v", tt.name, got, tt.want) 134 } 135 }) 136 } 137 }