github.com/cilium/cilium@v1.16.2/pkg/policy/policy_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package policy 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/cilium/cilium/api/v1/models" 12 "github.com/cilium/cilium/pkg/labels" 13 ) 14 15 func TestSearchContextString(t *testing.T) { 16 for expected, sc := range map[string]SearchContext{ 17 "From: [unspec:a, unspec:b, unspec:c] => To: [unspec:d, unspec:e, unspec:f] Ports: [HTTP/TCP, HTTPs/TCP]": { 18 Trace: 1, 19 Depth: 0, 20 From: labels.ParseLabelArray("a", "c", "b"), 21 To: labels.ParseLabelArray("d", "e", "f"), 22 DPorts: []*models.Port{ 23 { 24 Name: "HTTP", 25 Port: 80, 26 Protocol: "TCP", 27 }, 28 { 29 Name: "HTTPs", 30 Port: 442, 31 Protocol: "TCP", 32 }, 33 }, 34 rulesSelect: false, 35 }, 36 "From: [unspec:a, unspec:b, unspec:c] => To: [unspec:d, unspec:e, unspec:f] Ports: [80/TCP, 442/TCP]": { 37 Trace: 1, 38 Depth: 0, 39 From: labels.ParseLabelArray("a", "c", "b"), 40 To: labels.ParseLabelArray("d", "e", "f"), 41 DPorts: []*models.Port{ 42 { 43 Port: 80, 44 Protocol: "TCP", 45 }, 46 { 47 Port: 442, 48 Protocol: "TCP", 49 }, 50 }, 51 rulesSelect: false, 52 }, 53 "From: [k8s:a, local:b, unspec:c] => To: [unspec:d, unspec:e, unspec:f]": { 54 Trace: 1, 55 Depth: 0, 56 From: labels.ParseLabelArray("k8s:a", "unspec:c", "local:b"), 57 To: labels.ParseLabelArray("d", "e", "f"), 58 rulesSelect: false, 59 }, 60 } { 61 str := sc.String() 62 require.Equal(t, expected, str) 63 } 64 } 65 66 func BenchmarkSearchContextString(b *testing.B) { 67 b.ReportAllocs() 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 for _, sc := range []SearchContext{ 71 { 72 Trace: 1, 73 Depth: 0, 74 From: labels.ParseLabelArray("a", "t", "b"), 75 To: labels.ParseLabelArray("d", "e", "f"), 76 DPorts: []*models.Port{ 77 { 78 Name: "HTTP", 79 Port: 80, 80 Protocol: "TCP", 81 }, 82 { 83 Name: "HTTPs", 84 Port: 442, 85 Protocol: "TCP", 86 }, 87 }, 88 rulesSelect: false, 89 }, 90 { 91 Trace: 1, 92 Depth: 0, 93 From: labels.ParseLabelArray("a", "t", "b"), 94 To: labels.ParseLabelArray("d", "e", "f"), 95 DPorts: []*models.Port{ 96 { 97 Port: 80, 98 Protocol: "TCP", 99 }, 100 { 101 Port: 442, 102 Protocol: "TCP", 103 }, 104 }, 105 rulesSelect: false, 106 }, 107 } { 108 _ = sc.String() 109 } 110 } 111 }