github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/events_test.go (about) 1 /* 2 * Copyright 2023 Gravitational, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 // TestWatchKindContains tests that the WatchKind.Contains method correctly detects whether its receiver contains its 26 // argument. 27 func TestWatchKindContains(t *testing.T) { 28 allCAFilter := make(CertAuthorityFilter) 29 for _, caType := range CertAuthTypes { 30 allCAFilter[caType] = Wildcard 31 } 32 testCases := []struct { 33 name string 34 kind WatchKind 35 other WatchKind 36 assertion require.BoolAssertionFunc 37 }{ 38 { 39 name: "yes: kind and subkind match", 40 kind: WatchKind{ 41 Kind: "a", 42 SubKind: "b", 43 }, 44 other: WatchKind{ 45 Kind: "a", 46 SubKind: "b", 47 }, 48 assertion: require.True, 49 }, 50 { 51 name: "no: kind and subkind don't match", 52 kind: WatchKind{ 53 Kind: "a", 54 SubKind: "b", 55 }, 56 other: WatchKind{ 57 Kind: "a", 58 SubKind: "c", 59 }, 60 assertion: require.False, 61 }, 62 { 63 name: "yes: only subset specifies name", 64 kind: WatchKind{ 65 Kind: "a", 66 SubKind: "b", 67 }, 68 other: WatchKind{ 69 Kind: "a", 70 SubKind: "b", 71 Name: "c", 72 }, 73 assertion: require.True, 74 }, 75 { 76 name: "no: subset is missing name when superset has one", 77 kind: WatchKind{ 78 Kind: "a", 79 SubKind: "b", 80 Name: "c", 81 }, 82 other: WatchKind{ 83 Kind: "a", 84 SubKind: "b", 85 }, 86 assertion: require.False, 87 }, 88 { 89 name: "no: different names", 90 kind: WatchKind{ 91 Kind: "a", 92 SubKind: "b", 93 Name: "c", 94 }, 95 other: WatchKind{ 96 Kind: "a", 97 SubKind: "b", 98 Name: "d", 99 }, 100 assertion: require.False, 101 }, 102 { 103 name: "yes: subset has narrower filter", 104 kind: WatchKind{ 105 Kind: "a", 106 SubKind: "b", 107 Filter: map[string]string{ 108 "c": "d", 109 }, 110 }, 111 other: WatchKind{ 112 Kind: "a", 113 SubKind: "b", 114 Filter: map[string]string{ 115 "c": "d", 116 "e": "f", 117 }, 118 }, 119 assertion: require.True, 120 }, 121 { 122 name: "no: subset has no filter", 123 kind: WatchKind{ 124 Kind: "a", 125 SubKind: "b", 126 Filter: map[string]string{ 127 "c": "d", 128 }, 129 }, 130 other: WatchKind{ 131 Kind: "a", 132 SubKind: "b", 133 }, 134 assertion: require.False, 135 }, 136 { 137 name: "no: subset has wider filter", 138 kind: WatchKind{ 139 Kind: "a", 140 SubKind: "b", 141 Filter: map[string]string{ 142 "c": "d", 143 "e": "f", 144 }, 145 }, 146 other: WatchKind{ 147 Kind: "a", 148 SubKind: "b", 149 Filter: map[string]string{ 150 "e": "f", 151 }, 152 }, 153 assertion: require.False, 154 }, 155 { 156 name: "yes: superset and subset have no CA filter", 157 kind: WatchKind{ 158 Kind: "cert_authority", 159 }, 160 other: WatchKind{ 161 Kind: "cert_authority", 162 }, 163 assertion: require.True, 164 }, 165 { 166 name: "yes: superset has no CA filter", 167 kind: WatchKind{ 168 Kind: "cert_authority", 169 }, 170 other: WatchKind{ 171 Kind: "cert_authority", 172 Filter: map[string]string{ 173 "a": "b", 174 "c": "d", 175 }, 176 }, 177 assertion: require.True, 178 }, 179 { 180 name: "yes: superset filter matches all, subset has no CA filter", 181 kind: WatchKind{ 182 Kind: "cert_authority", 183 Filter: allCAFilter.IntoMap(), 184 }, 185 other: WatchKind{ 186 Kind: "cert_authority", 187 }, 188 assertion: require.True, 189 }, 190 { 191 name: "yes: subset has narrower CA filter", 192 kind: WatchKind{ 193 Kind: "cert_authority", 194 Filter: map[string]string{ 195 "a": "b", 196 "c": Wildcard, 197 "e": "f", 198 }, 199 }, 200 other: WatchKind{ 201 Kind: "cert_authority", 202 Filter: map[string]string{ 203 "a": "b", 204 "c": "d", 205 }, 206 }, 207 assertion: require.True, 208 }, 209 { 210 name: "no: superset filter does not match all, subset has no CA filter", 211 kind: WatchKind{ 212 Kind: "cert_authority", 213 Filter: map[string]string{ 214 "a": "b", 215 "c": "d", 216 }, 217 }, 218 other: WatchKind{ 219 Kind: "cert_authority", 220 }, 221 assertion: require.False, 222 }, 223 { 224 name: "no: subset has wider CA filter", 225 kind: WatchKind{ 226 Kind: "cert_authority", 227 Filter: map[string]string{ 228 "a": "b", 229 "c": "d", 230 }, 231 }, 232 other: WatchKind{ 233 Kind: "cert_authority", 234 Filter: map[string]string{ 235 "a": "b", 236 "c": "d", 237 "e": "", 238 }, 239 }, 240 assertion: require.False, 241 }, 242 { 243 name: "no: subset filter does not match", 244 kind: WatchKind{ 245 Kind: "cert_authority", 246 Filter: map[string]string{ 247 "a": "b", 248 }, 249 }, 250 other: WatchKind{ 251 Kind: "cert_authority", 252 Filter: map[string]string{ 253 "a": "", 254 }, 255 }, 256 assertion: require.False, 257 }, 258 } 259 260 for _, tc := range testCases { 261 t.Run(tc.name, func(t *testing.T) { 262 tc.assertion(t, tc.kind.Contains(tc.other)) 263 }) 264 } 265 }