github.com/cilium/cilium@v1.16.2/test/controlplane/services/helpers/lbmap_asserts.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package helpers 5 6 import ( 7 "fmt" 8 9 lb "github.com/cilium/cilium/pkg/loadbalancer" 10 "github.com/cilium/cilium/pkg/testutils/mockmaps" 11 ) 12 13 // 14 // LBMap assertions 15 // 16 17 type SVCL3Type string 18 19 const ( 20 SVCIPv4 = SVCL3Type("IPv4") 21 SVCIPv6 = SVCL3Type("IPV6") 22 ) 23 24 type LBMapAssert struct { 25 lbmap *mockmaps.LBMockMap 26 } 27 28 func NewLBMapAssert(lbmap *mockmaps.LBMockMap) LBMapAssert { 29 return LBMapAssert{lbmap} 30 } 31 32 // findService finds a service matching the parameters and that it has a 33 // backend with specified l4 type and port. 34 // 35 // Linear in time, but should be OK for tests with few 10s of services 36 // and backends. 37 func (a LBMapAssert) FindServiceWithBackend(name string, svcType lb.SVCType, l3 SVCL3Type, l4 lb.L4Type, port uint16) *lb.SVC { 38 for _, svc := range a.lbmap.ServiceByID { 39 svcL3Type := SVCIPv4 40 if svc.Frontend.IsIPv6() { 41 svcL3Type = SVCIPv6 42 } 43 match := svc.Type == svcType 44 match = match && l3 == svcL3Type 45 l4match := false 46 for _, be := range svc.Backends { 47 if l4 == be.L4Addr.Protocol && be.L4Addr.Port == port { 48 l4match = true 49 break 50 } 51 } 52 match = match && l4match 53 if match { 54 return svc 55 } 56 } 57 return nil 58 } 59 60 // servicesExist asserts that the service with given name (<namespace>/<name>) exists for 61 // the listed service types and has frontends with the given L3 protocols, and that 62 // it has backend with the given L4 type and port. 63 func (a LBMapAssert) ServicesExist(name string, svcTypes []lb.SVCType, l3s []SVCL3Type, l4 lb.L4Type, port uint16) error { 64 for _, svcType := range svcTypes { 65 for _, l3 := range l3s { 66 if svc := a.FindServiceWithBackend(name, svcType, l3, l4, port); svc == nil { 67 return fmt.Errorf("Service for name=%q, type=%q, l3=%q, l4=%q, port=%d not found", name, svcType, l3, l4, port) 68 } 69 } 70 } 71 return nil 72 }