k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/utils/ktesting/tcontext_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 ktesting_test 18 19 import ( 20 "sync" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" 26 "k8s.io/client-go/dynamic" 27 clientset "k8s.io/client-go/kubernetes" 28 "k8s.io/client-go/rest" 29 "k8s.io/client-go/restmapper" 30 "k8s.io/klog/v2" 31 "k8s.io/kubernetes/test/utils/ktesting" 32 ) 33 34 func TestCancelManual(t *testing.T) { 35 tCtx := ktesting.Init(t) 36 var wg sync.WaitGroup 37 wg.Add(1) 38 go func() { 39 defer wg.Done() 40 // Blocks until tCtx.Cancel is called below. 41 <-tCtx.Done() 42 }() 43 tCtx.Cancel("manually canceled") 44 wg.Wait() 45 } 46 47 func TestCancelAutomatic(t *testing.T) { 48 var wg sync.WaitGroup 49 // This callback gets registered first and thus 50 // gets invoked last. 51 t.Cleanup(wg.Wait) 52 tCtx := ktesting.Init(t) 53 wg.Add(1) 54 go func() { 55 defer wg.Done() 56 // Blocks until the context gets canceled automatically. 57 <-tCtx.Done() 58 }() 59 } 60 61 func TestCancelCtx(t *testing.T) { 62 tCtx := ktesting.Init(t) 63 var discardLogger klog.Logger 64 tCtx = ktesting.WithLogger(tCtx, discardLogger) 65 tCtx = ktesting.WithRESTConfig(tCtx, new(rest.Config)) 66 baseCtx := tCtx 67 68 tCtx.Cleanup(func() { 69 if tCtx.Err() == nil { 70 t.Error("context should be canceled but isn't") 71 } 72 }) 73 tCtx.CleanupCtx(func(tCtx ktesting.TContext) { 74 if tCtx.Err() != nil { 75 t.Errorf("context should not be canceled but is: %v", tCtx.Err()) 76 } 77 assert.Equal(t, baseCtx.Logger(), tCtx.Logger(), "Logger()") 78 assert.Equal(t, baseCtx.RESTConfig(), tCtx.RESTConfig(), "RESTConfig()") 79 assert.Equal(t, baseCtx.RESTMapper(), tCtx.RESTMapper(), "RESTMapper()") 80 assert.Equal(t, baseCtx.Client(), tCtx.Client(), "Client()") 81 assert.Equal(t, baseCtx.Dynamic(), tCtx.Dynamic(), "Dynamic()") 82 assert.Equal(t, baseCtx.APIExtensions(), tCtx.APIExtensions(), "APIExtensions()") 83 }) 84 85 // Cancel, then let testing.T invoke test cleanup. 86 tCtx.Cancel("test is complete") 87 } 88 89 func TestWithTB(t *testing.T) { 90 tCtx := ktesting.Init(t) 91 92 cfg := new(rest.Config) 93 mapper := new(restmapper.DeferredDiscoveryRESTMapper) 94 client := clientset.New(nil) 95 dynamic := dynamic.New(nil) 96 apiextensions := apiextensions.New(nil) 97 tCtx = ktesting.WithClients(tCtx, cfg, mapper, client, dynamic, apiextensions) 98 99 t.Run("sub", func(t *testing.T) { 100 tCtx := ktesting.WithTB(tCtx, t) 101 102 assert.Equal(t, cfg, tCtx.RESTConfig(), "RESTConfig") 103 assert.Equal(t, mapper, tCtx.RESTMapper(), "RESTMapper") 104 assert.Equal(t, client, tCtx.Client(), "Client") 105 assert.Equal(t, dynamic, tCtx.Dynamic(), "Dynamic") 106 assert.Equal(t, apiextensions, tCtx.APIExtensions(), "APIExtensions") 107 108 tCtx.Cancel("test is complete") 109 }) 110 111 if err := tCtx.Err(); err != nil { 112 t.Errorf("parent TContext should not have been cancelled: %v", err) 113 } 114 }