github.com/cilium/cilium@v1.16.2/pkg/endpoint/id/id_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package id 5 6 import ( 7 "net/netip" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestSplitID(t *testing.T) { 15 type want struct { 16 prefixType PrefixType 17 id string 18 } 19 tests := []struct { 20 name string 21 id string 22 want want 23 }{ 24 { 25 name: "ID without a prefix", 26 id: "123456", 27 want: want{ 28 prefixType: CiliumLocalIdPrefix, 29 id: "123456", 30 }, 31 }, 32 { 33 name: "ID CiliumLocalIdPrefix prefix", 34 id: string(CiliumLocalIdPrefix) + ":123456", 35 want: want{ 36 prefixType: CiliumLocalIdPrefix, 37 id: "123456", 38 }, 39 }, 40 { 41 name: "ID with PodNamePrefix prefix", 42 id: string(PodNamePrefix) + ":default:foobar", 43 want: want{ 44 prefixType: PodNamePrefix, 45 id: "default:foobar", 46 }, 47 }, 48 { 49 name: "ID with CEPNamePrefix prefix", 50 id: string(CEPNamePrefix) + ":default:baz-net1", 51 want: want{ 52 prefixType: CEPNamePrefix, 53 id: "default:baz-net1", 54 }, 55 }, 56 { 57 name: "ID with ':'", 58 id: ":", 59 want: want{ 60 prefixType: "", 61 id: "", 62 }, 63 }, 64 { 65 name: "Empty ID", 66 id: "", 67 want: want{ 68 prefixType: CiliumLocalIdPrefix, 69 id: "", 70 }, 71 }, 72 } 73 for _, tt := range tests { 74 prefixType, id := splitID(tt.id) 75 require.Equal(t, tt.want.prefixType, prefixType, "Test Name: %s", tt.name) 76 require.Equal(t, tt.want.id, id, "Test Name: %s", tt.name) 77 } 78 } 79 80 func BenchmarkSplitID(b *testing.B) { 81 tests := []struct { 82 str string 83 prefixType PrefixType 84 id string 85 }{ 86 {"123456", CiliumLocalIdPrefix, "123456"}, 87 {string(CiliumLocalIdPrefix + ":123456"), CiliumLocalIdPrefix, "123456"}, 88 {string(PodNamePrefix + ":default:foobar"), PodNamePrefix, "default:foobar"}, 89 } 90 count := 0 91 b.ResetTimer() 92 for i := 0; i < b.N; i++ { 93 for _, test := range tests { 94 pt, str := splitID(test.str) 95 if pt == test.prefixType && str == test.id { 96 count++ 97 } 98 } 99 } 100 b.StopTimer() 101 if count != len(tests)*b.N { 102 b.Errorf("splitID didn't produce correct results") 103 } 104 b.ReportAllocs() 105 } 106 107 func TestParse(t *testing.T) { 108 type test struct { 109 input PrefixType 110 wantPrefix PrefixType 111 wantID string 112 expectFail bool 113 } 114 115 tests := []test{ 116 {DockerEndpointPrefix + ":foo", DockerEndpointPrefix, "foo", false}, 117 {DockerEndpointPrefix + ":foo:foo", DockerEndpointPrefix, "foo:foo", false}, 118 {"unknown:unknown", "", "", true}, 119 {"unknown", CiliumLocalIdPrefix, "unknown", false}, 120 } 121 122 for _, tt := range tests { 123 prefix, id, err := Parse(string(tt.input)) 124 require.Equal(t, tt.wantPrefix, prefix) 125 require.Equal(t, tt.wantID, id) 126 if tt.expectFail { 127 require.NotNil(t, err) 128 } else { 129 require.Nil(t, err) 130 } 131 } 132 } 133 134 func TestNewIPPrefix(t *testing.T) { 135 require.Equal(t, true, strings.HasPrefix(NewIPPrefixID(netip.MustParseAddr("1.1.1.1")), string(IPv4Prefix))) 136 require.Equal(t, true, strings.HasPrefix(NewIPPrefixID(netip.MustParseAddr("f00d::1")), string(IPv6Prefix))) 137 }