1 /* 2 Copyright 2023 The Kubernetes Authors. 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 suite 18 19 import ( 20 "fmt" 21 22 "k8s.io/apimachinery/pkg/util/sets" 23 ) 24 25 // ----------------------------------------------------------------------------- 26 // Conformance Profiles - Public Types 27 // ----------------------------------------------------------------------------- 28 29 // ConformanceProfile is a group of features that have a related purpose, e.g. 30 // to cover specific protocol support or a specific feature present in Gateway 31 // API. 32 // 33 // For more details see the relevant GEP: https://gateway-api.sigs.k8s.io/geps/gep-1709/ 34 type ConformanceProfile struct { 35 Name ConformanceProfileName 36 CoreFeatures sets.Set[SupportedFeature] 37 ExtendedFeatures sets.Set[SupportedFeature] 38 } 39 40 type ConformanceProfileName string 41 42 const ( 43 // HTTPConformanceProfileName indicates the name of the conformance profile 44 // which covers HTTP functionality, such as the HTTPRoute API. 45 HTTPConformanceProfileName ConformanceProfileName = "HTTP" 46 47 // TLSConformanceProfileName indicates the name of the conformance profile 48 // which covers TLS stream functionality, such as the TLSRoute API. 49 TLSConformanceProfileName ConformanceProfileName = "TLS" 50 51 // MeshConformanceProfileName indicates the name of the conformance profile 52 // which covers service mesh functionality. 53 MeshConformanceProfileName ConformanceProfileName = "MESH" 54 ) 55 56 // ----------------------------------------------------------------------------- 57 // Conformance Profiles - Public Vars 58 // ----------------------------------------------------------------------------- 59 60 var ( 61 // HTTPConformanceProfile is a ConformanceProfile that covers testing HTTP 62 // related functionality with Gateways. 63 HTTPConformanceProfile = ConformanceProfile{ 64 Name: HTTPConformanceProfileName, 65 CoreFeatures: sets.New( 66 SupportGateway, 67 SupportReferenceGrant, 68 SupportHTTPRoute, 69 ), 70 ExtendedFeatures: HTTPRouteExtendedFeatures, 71 } 72 73 // TLSConformanceProfile is a ConformanceProfile that covers testing TLS 74 // related functionality with Gateways. 75 TLSConformanceProfile = ConformanceProfile{ 76 Name: TLSConformanceProfileName, 77 CoreFeatures: sets.New( 78 SupportGateway, 79 SupportReferenceGrant, 80 SupportTLSRoute, 81 ), 82 } 83 84 // MeshConformanceProfile is a ConformanceProfile that covers testing 85 // service mesh related functionality. 86 MeshConformanceProfile = ConformanceProfile{ 87 Name: MeshConformanceProfileName, 88 CoreFeatures: sets.New( 89 SupportMesh, 90 ), 91 } 92 ) 93 94 // ----------------------------------------------------------------------------- 95 // Conformance Profiles - Private Profile Mapping Helpers 96 // ----------------------------------------------------------------------------- 97 98 // conformanceProfileMap maps short human-readable names to their respective 99 // ConformanceProfiles. 100 var conformanceProfileMap = map[ConformanceProfileName]ConformanceProfile{ 101 HTTPConformanceProfileName: HTTPConformanceProfile, 102 TLSConformanceProfileName: TLSConformanceProfile, 103 MeshConformanceProfileName: MeshConformanceProfile, 104 } 105 106 // getConformanceProfileForName retrieves a known ConformanceProfile by it's simple 107 // human readable ConformanceProfileName. 108 func getConformanceProfileForName(name ConformanceProfileName) (ConformanceProfile, error) { 109 profile, ok := conformanceProfileMap[name] 110 if !ok { 111 return profile, fmt.Errorf("%s is not a valid conformance profile", name) 112 } 113 114 return profile, nil 115 } 116 117 // getConformanceProfilesForTest retrieves the ConformanceProfiles a test belongs to. 118 func getConformanceProfilesForTest(test ConformanceTest, conformanceProfiles sets.Set[ConformanceProfileName]) sets.Set[*ConformanceProfile] { 119 matchingConformanceProfiles := sets.New[*ConformanceProfile]() 120 for _, conformanceProfileName := range conformanceProfiles.UnsortedList() { 121 cp := conformanceProfileMap[conformanceProfileName] 122 hasAllFeatures := true 123 for _, feature := range test.Features { 124 if !cp.CoreFeatures.Has(feature) && !cp.ExtendedFeatures.Has(feature) { 125 hasAllFeatures = false 126 break 127 } 128 } 129 if hasAllFeatures { 130 matchingConformanceProfiles.Insert(&cp) 131 } 132 } 133 134 return matchingConformanceProfiles 135 } 136