github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/policymap/policymap_test.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package policymap 18 19 import ( 20 "github.com/cilium/cilium/pkg/policy/trafficdirection" 21 22 "testing" 23 24 . "gopkg.in/check.v1" 25 ) 26 27 func Test(t *testing.T) { 28 TestingT(t) 29 } 30 31 type PolicyMapTestSuite struct{} 32 33 var _ = Suite(&PolicyMapTestSuite{}) 34 35 func (pm *PolicyMapTestSuite) TestPolicyEntriesDump_Less(c *C) { 36 type args struct { 37 i int 38 j int 39 } 40 tests := []struct { 41 name string 42 p PolicyEntriesDump 43 args args 44 want bool 45 }{ 46 { 47 name: "Same element", 48 p: PolicyEntriesDump{ 49 { 50 Key: PolicyKey{ 51 Identity: uint32(0), 52 DestPort: 0, 53 Nexthdr: 0, 54 TrafficDirection: uint8(trafficdirection.Ingress), 55 }, 56 }, 57 }, 58 args: args{ 59 i: 0, 60 j: 0, 61 }, 62 want: false, 63 }, 64 { 65 name: "Element #0 is less than #1 because identity is smaller", 66 p: PolicyEntriesDump{ 67 { 68 Key: PolicyKey{ 69 Identity: uint32(0), 70 DestPort: 0, 71 Nexthdr: 0, 72 TrafficDirection: uint8(trafficdirection.Ingress), 73 }, 74 }, 75 { 76 Key: PolicyKey{ 77 Identity: uint32(1), 78 DestPort: 0, 79 Nexthdr: 0, 80 TrafficDirection: uint8(trafficdirection.Ingress), 81 }, 82 }, 83 }, 84 args: args{ 85 i: 0, 86 j: 1, 87 }, 88 want: true, 89 }, 90 { 91 name: "Element #0 is less than #1 because TrafficDirection is smaller", 92 p: PolicyEntriesDump{ 93 { 94 Key: PolicyKey{ 95 Identity: uint32(0), 96 DestPort: 0, 97 Nexthdr: 0, 98 TrafficDirection: uint8(trafficdirection.Ingress), 99 }, 100 }, 101 { 102 Key: PolicyKey{ 103 Identity: uint32(1), 104 DestPort: 0, 105 Nexthdr: 0, 106 TrafficDirection: uint8(trafficdirection.Egress), 107 }, 108 }, 109 }, 110 args: args{ 111 i: 0, 112 j: 1, 113 }, 114 want: true, 115 }, 116 { 117 name: "Element #0 is not less than #1 because Identity is bigger", 118 p: PolicyEntriesDump{ 119 { 120 Key: PolicyKey{ 121 Identity: uint32(1), 122 DestPort: 0, 123 Nexthdr: 0, 124 TrafficDirection: uint8(trafficdirection.Egress), 125 }, 126 }, 127 { 128 Key: PolicyKey{ 129 Identity: uint32(0), 130 DestPort: 0, 131 Nexthdr: 0, 132 TrafficDirection: uint8(trafficdirection.Egress), 133 }, 134 }, 135 }, 136 args: args{ 137 i: 0, 138 j: 1, 139 }, 140 want: false, 141 }, 142 } 143 for _, tt := range tests { 144 got := tt.p.Less(tt.args.i, tt.args.j) 145 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 146 } 147 }