github.com/cilium/cilium@v1.16.2/operator/pkg/model/helpers_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package model 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestAddSource(t *testing.T) { 13 14 testSource := FullyQualifiedResource{ 15 Name: "testSource", 16 Namespace: "testNamespace", 17 Group: "group", 18 Version: "v1", 19 Kind: "Test", 20 } 21 22 emptySlice := []FullyQualifiedResource{} 23 24 existsSlice := []FullyQualifiedResource{testSource} 25 26 nonexistSlice := []FullyQualifiedResource{ 27 { 28 Name: "SomeOtherResource", 29 }, 30 } 31 32 emptyOut := AddSource(emptySlice, testSource) 33 assert.Equal(t, existsSlice, emptyOut) 34 35 existsOut := AddSource(existsSlice, testSource) 36 assert.Equal(t, existsSlice, existsOut) 37 38 nonexistOut := AddSource(nonexistSlice, testSource) 39 assert.Equal(t, append(nonexistSlice, testSource), nonexistOut) 40 41 } 42 43 func TestComputeHosts(t *testing.T) { 44 type args struct { 45 routeHostnames []string 46 listenerHostname *string 47 excludeListenerHostnames []string 48 } 49 tests := []struct { 50 name string 51 args args 52 want []string 53 }{ 54 { 55 name: "no route hostnames, no listener hostname", 56 args: args{}, 57 want: []string{"*"}, 58 }, 59 { 60 name: "no route hostnames", 61 args: args{ 62 listenerHostname: strp("*.foo.com"), 63 }, 64 want: []string{"*.foo.com"}, 65 }, 66 { 67 name: "matching specific hostname exactly", 68 args: args{ 69 routeHostnames: []string{ 70 "non.matching.com", 71 "*.nonmatchingwildcard.io", 72 "very.specific.com", 73 }, 74 listenerHostname: strp("very.specific.com"), 75 }, 76 want: []string{"very.specific.com"}, 77 }, 78 { 79 name: "matching specific hostname on wildcard", 80 args: args{ 81 routeHostnames: []string{ 82 "non.matching.com", 83 "*.nonmatchingwildcard.io", 84 "*.specific.com", 85 }, 86 listenerHostname: strp("very.specific.com"), 87 }, 88 want: []string{"very.specific.com"}, 89 }, 90 { 91 name: "matching wildcard hostname", 92 args: args{ 93 routeHostnames: []string{ 94 "non.matching.com", 95 "*.nonmatchingwildcard.io", 96 "wildcard.io", 97 "foo.wildcard.io", 98 "bar.wildcard.io", 99 "foo.bar.wildcard.io", 100 "very.specific.com", 101 }, 102 listenerHostname: strp("*.wildcard.io"), 103 }, 104 want: []string{"bar.wildcard.io", "foo.bar.wildcard.io", "foo.wildcard.io"}, 105 }, 106 { 107 name: "matching wildcard hostname exactly", 108 args: args{ 109 routeHostnames: []string{ 110 "non.matching.com", 111 "*.nonmatchingwildcard.io", 112 "wildcard.io", 113 "*.wildcard.io", 114 "very.specific.com", 115 }, 116 listenerHostname: strp("*.wildcard.io"), 117 }, 118 want: []string{"*.wildcard.io"}, 119 }, 120 { 121 name: "with excluded listener hostname", 122 args: args{ 123 routeHostnames: []string{ 124 "non.matching.com", 125 "*.nonmatchingwildcard.io", 126 "wildcard.io", 127 "*.wildcard.io", 128 "very.specific.com", 129 }, 130 listenerHostname: strp("*"), 131 excludeListenerHostnames: []string{ 132 "non.matching.com", 133 "*.nonmatchingwildcard.io", 134 "*.wildcard.io", 135 "very.specific.com", 136 }, 137 }, 138 want: []string{"wildcard.io"}, 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 got := ComputeHosts(tt.args.routeHostnames, tt.args.listenerHostname, tt.args.excludeListenerHostnames) 144 assert.Equalf(t, tt.want, got, "ComputeHosts(%v, %v)", tt.args.routeHostnames, tt.args.listenerHostname) 145 }) 146 } 147 } 148 149 func strp(s string) *string { 150 return &s 151 }