github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/identity_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 TestIdentityFilter(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: "source-nil", 28 args: args{ 29 f: []*flowpb.FlowFilter{{ 30 SourceIdentity: []uint32{1}, 31 }}, 32 ev: nil, 33 }, 34 want: false, 35 }, 36 { 37 name: "destination-nil", 38 args: args{ 39 f: []*flowpb.FlowFilter{{ 40 DestinationIdentity: []uint32{1}, 41 }}, 42 ev: nil, 43 }, 44 want: false, 45 }, 46 { 47 name: "source-positive", 48 args: args{ 49 f: []*flowpb.FlowFilter{{ 50 SourceIdentity: []uint32{1, 2, 3}, 51 }}, 52 ev: &v1.Event{Event: &flowpb.Flow{ 53 Source: &flowpb.Endpoint{Identity: 3}, 54 }}, 55 }, 56 want: true, 57 }, 58 { 59 name: "source-negative", 60 args: args{ 61 f: []*flowpb.FlowFilter{{ 62 SourceIdentity: []uint32{1, 2, 3}, 63 }}, 64 ev: &v1.Event{Event: &flowpb.Flow{ 65 Source: &flowpb.Endpoint{Identity: 4}, 66 }}, 67 }, 68 want: false, 69 }, 70 { 71 name: "destination-negative", 72 args: args{ 73 f: []*flowpb.FlowFilter{{ 74 DestinationIdentity: []uint32{1, 2, 3}, 75 }}, 76 ev: &v1.Event{Event: &flowpb.Flow{ 77 Destination: &flowpb.Endpoint{Identity: 5}, 78 }}, 79 }, 80 want: false, 81 }, 82 } 83 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 fl, err := BuildFilterList(context.Background(), tt.args.f, []OnBuildFilter{&IdentityFilter{}}) 87 assert.NoError(t, err) 88 assert.Equal(t, tt.want, fl.MatchOne(tt.args.ev)) 89 }) 90 } 91 }