sigs.k8s.io/controller-runtime@v0.18.2/pkg/metrics/filters/filter_suite_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 filters 18 19 import ( 20 "fmt" 21 "net/http" 22 "testing" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 "k8s.io/client-go/kubernetes" 27 "k8s.io/client-go/rest" 28 "sigs.k8s.io/controller-runtime/pkg/envtest" 29 logf "sigs.k8s.io/controller-runtime/pkg/log" 30 "sigs.k8s.io/controller-runtime/pkg/log/zap" 31 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" 32 ) 33 34 func TestSource(t *testing.T) { 35 RegisterFailHandler(Fail) 36 RunSpecs(t, "Filters Suite") 37 } 38 39 var testenv *envtest.Environment 40 var cfg *rest.Config 41 var clientset *kubernetes.Clientset 42 43 // clientTransport is used to force-close keep-alives in tests that check for leaks. 44 var clientTransport *http.Transport 45 46 var _ = BeforeSuite(func() { 47 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 48 49 testenv = &envtest.Environment{} 50 51 var err error 52 cfg, err = testenv.Start() 53 Expect(err).NotTo(HaveOccurred()) 54 55 cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper { 56 // NB(directxman12): we can't set Transport *and* use TLS options, 57 // so we grab the transport right after it gets created so that we can 58 // type-assert on it (hopefully)? 59 // hopefully this doesn't break 🤞 60 transport, isTransport := rt.(*http.Transport) 61 if !isTransport { 62 panic(fmt.Sprintf("wasn't able to grab underlying transport from REST client's RoundTripper, can't figure out how to close keep-alives: expected an *http.Transport, got %#v", rt)) 63 } 64 clientTransport = transport 65 return rt 66 } 67 68 clientset, err = kubernetes.NewForConfig(cfg) 69 Expect(err).NotTo(HaveOccurred()) 70 71 // Prevent the metrics listener being created 72 metricsserver.DefaultBindAddress = "0" 73 }) 74 75 var _ = AfterSuite(func() { 76 Expect(testenv.Stop()).To(Succeed()) 77 78 // Put the DefaultBindAddress back 79 metricsserver.DefaultBindAddress = ":8080" 80 })