sigs.k8s.io/gateway-api@v1.0.0/conformance/experimental_conformance_test.go (about) 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 conformance_test 18 19 import ( 20 "os" 21 "testing" 22 23 "k8s.io/apimachinery/pkg/util/sets" 24 "k8s.io/client-go/kubernetes" 25 "k8s.io/client-go/rest" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 "sigs.k8s.io/controller-runtime/pkg/client/config" 28 "sigs.k8s.io/yaml" 29 30 v1 "sigs.k8s.io/gateway-api/apis/v1" 31 "sigs.k8s.io/gateway-api/apis/v1alpha2" 32 "sigs.k8s.io/gateway-api/apis/v1beta1" 33 confv1a1 "sigs.k8s.io/gateway-api/conformance/apis/v1alpha1" 34 "sigs.k8s.io/gateway-api/conformance/tests" 35 "sigs.k8s.io/gateway-api/conformance/utils/flags" 36 "sigs.k8s.io/gateway-api/conformance/utils/suite" 37 ) 38 39 var ( 40 cfg *rest.Config 41 k8sClientset *kubernetes.Clientset 42 mgrClient client.Client 43 supportedFeatures sets.Set[suite.SupportedFeature] 44 exemptFeatures sets.Set[suite.SupportedFeature] 45 namespaceLabels map[string]string 46 namespaceAnnotations map[string]string 47 implementation *confv1a1.Implementation 48 conformanceProfiles sets.Set[suite.ConformanceProfileName] 49 skipTests []string 50 ) 51 52 func TestExperimentalConformance(t *testing.T) { 53 var err error 54 cfg, err = config.GetConfig() 55 if err != nil { 56 t.Fatalf("Error loading Kubernetes config: %v", err) 57 } 58 mgrClient, err = client.New(cfg, client.Options{}) 59 if err != nil { 60 t.Fatalf("Error initializing Kubernetes client: %v", err) 61 } 62 k8sClientset, err = kubernetes.NewForConfig(cfg) 63 if err != nil { 64 t.Fatalf("Error initializing Kubernetes REST client: %v", err) 65 } 66 67 v1alpha2.AddToScheme(mgrClient.Scheme()) 68 v1beta1.AddToScheme(mgrClient.Scheme()) 69 v1.AddToScheme(mgrClient.Scheme()) 70 71 // standard conformance flags 72 supportedFeatures = suite.ParseSupportedFeatures(*flags.SupportedFeatures) 73 exemptFeatures = suite.ParseSupportedFeatures(*flags.ExemptFeatures) 74 skipTests = suite.ParseSkipTests(*flags.SkipTests) 75 namespaceLabels = suite.ParseKeyValuePairs(*flags.NamespaceLabels) 76 namespaceAnnotations = suite.ParseKeyValuePairs(*flags.NamespaceAnnotations) 77 78 // experimental conformance flags 79 conformanceProfiles = suite.ParseConformanceProfiles(*flags.ConformanceProfiles) 80 81 if conformanceProfiles.Len() > 0 { 82 // if some conformance profiles have been set, run the experimental conformance suite... 83 implementation, err = suite.ParseImplementation( 84 *flags.ImplementationOrganization, 85 *flags.ImplementationProject, 86 *flags.ImplementationURL, 87 *flags.ImplementationVersion, 88 *flags.ImplementationContact, 89 ) 90 if err != nil { 91 t.Fatalf("Error parsing implementation's details: %v", err) 92 } 93 testExperimentalConformance(t) 94 } else { 95 // ...otherwise run the standard conformance suite. 96 t.Run("standard conformance tests", TestConformance) 97 } 98 } 99 100 func testExperimentalConformance(t *testing.T) { 101 t.Logf("Running experimental conformance tests with %s GatewayClass\n cleanup: %t\n debug: %t\n enable all features: %t \n supported features: [%v]\n exempt features: [%v]", 102 *flags.GatewayClassName, *flags.CleanupBaseResources, *flags.ShowDebug, *flags.EnableAllSupportedFeatures, *flags.SupportedFeatures, *flags.ExemptFeatures) 103 104 cSuite, err := suite.NewExperimentalConformanceTestSuite( 105 suite.ExperimentalConformanceOptions{ 106 Options: suite.Options{ 107 Client: mgrClient, 108 RestConfig: cfg, 109 // This clientset is needed in addition to the client only because 110 // controller-runtime client doesn't support non CRUD sub-resources yet (https://github.com/kubernetes-sigs/controller-runtime/issues/452). 111 Clientset: k8sClientset, 112 GatewayClassName: *flags.GatewayClassName, 113 Debug: *flags.ShowDebug, 114 CleanupBaseResources: *flags.CleanupBaseResources, 115 SupportedFeatures: supportedFeatures, 116 ExemptFeatures: exemptFeatures, 117 EnableAllSupportedFeatures: *flags.EnableAllSupportedFeatures, 118 NamespaceLabels: namespaceLabels, 119 NamespaceAnnotations: namespaceAnnotations, 120 SkipTests: skipTests, 121 }, 122 Implementation: *implementation, 123 ConformanceProfiles: conformanceProfiles, 124 }) 125 if err != nil { 126 t.Fatalf("error creating experimental conformance test suite: %v", err) 127 } 128 129 cSuite.Setup(t) 130 cSuite.Run(t, tests.ConformanceTests) 131 report, err := cSuite.Report() 132 if err != nil { 133 t.Fatalf("error generating conformance profile report: %v", err) 134 } 135 writeReport(t.Logf, *report, *flags.ReportOutput) 136 } 137 138 func writeReport(logf func(string, ...any), report confv1a1.ConformanceReport, output string) error { 139 rawReport, err := yaml.Marshal(report) 140 if err != nil { 141 return err 142 } 143 144 if output != "" { 145 if err = os.WriteFile(output, rawReport, 0o600); err != nil { 146 return err 147 } 148 } 149 logf("Conformance report:\n%s", string(rawReport)) 150 151 return nil 152 }