@@ -32,6 +32,7 @@ import (
32
32
"os"
33
33
"path/filepath"
34
34
"runtime"
35
+ "testing"
35
36
"time"
36
37
37
38
"github.com/spf13/pflag"
@@ -56,10 +57,11 @@ import (
56
57
"k8s.io/klog/v2"
57
58
"k8s.io/kube-aggregator/pkg/apiserver"
58
59
"k8s.io/kubernetes/pkg/features"
60
+ testutil "k8s.io/kubernetes/test/utils"
61
+ "k8s.io/kubernetes/test/utils/ktesting"
59
62
60
63
"k8s.io/kubernetes/cmd/kube-apiserver/app"
61
64
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
62
- testutil "k8s.io/kubernetes/test/utils"
63
65
)
64
66
65
67
func init () {
@@ -139,7 +141,9 @@ func NewDefaultTestServerOptions() *TestServerInstanceOptions {
139
141
// Note: we return a tear-down func instead of a stop channel because the later will leak temporary
140
142
// files that because Golang testing's call to os.Exit will not give a stop channel go routine
141
143
// enough time to remove temporary files.
142
- func StartTestServer (t Logger , instanceOptions * TestServerInstanceOptions , customFlags []string , storageConfig * storagebackend.Config ) (result TestServer , err error ) {
144
+ func StartTestServer (t ktesting.TB , instanceOptions * TestServerInstanceOptions , customFlags []string , storageConfig * storagebackend.Config ) (result TestServer , err error ) {
145
+ tCtx := ktesting .Init (t )
146
+
143
147
if instanceOptions == nil {
144
148
instanceOptions = NewDefaultTestServerOptions ()
145
149
}
@@ -149,12 +153,11 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
149
153
return result , fmt .Errorf ("failed to create temp dir: %v" , err )
150
154
}
151
155
152
- stopCh := make (chan struct {})
153
156
var errCh chan error
154
157
tearDown := func () {
155
- // Closing stopCh is stopping apiserver and cleaning up
158
+ // Cancel is stopping apiserver and cleaning up
156
159
// after itself, including shutting down its storage layer.
157
- close ( stopCh )
160
+ tCtx . Cancel ( "tearing down" )
158
161
159
162
// If the apiserver was started, let's wait for it to
160
163
// shutdown clearly.
@@ -359,15 +362,15 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
359
362
}
360
363
361
364
errCh = make (chan error )
362
- go func (stopCh <- chan struct {} ) {
365
+ go func () {
363
366
defer close (errCh )
364
367
prepared , err := server .PrepareRun ()
365
368
if err != nil {
366
369
errCh <- err
367
- } else if err := prepared .Run (stopCh ); err != nil {
370
+ } else if err := prepared .Run (tCtx ); err != nil {
368
371
errCh <- err
369
372
}
370
- }(stopCh )
373
+ }()
371
374
372
375
client , err := kubernetes .NewForConfig (server .GenericAPIServer .LoopbackClientConfig )
373
376
if err != nil {
@@ -465,7 +468,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
465
468
}
466
469
467
470
// StartTestServerOrDie calls StartTestServer t.Fatal if it does not succeed.
468
- func StartTestServerOrDie (t Logger , instanceOptions * TestServerInstanceOptions , flags []string , storageConfig * storagebackend.Config ) * TestServer {
471
+ func StartTestServerOrDie (t testing. TB , instanceOptions * TestServerInstanceOptions , flags []string , storageConfig * storagebackend.Config ) * TestServer {
469
472
result , err := StartTestServer (t , instanceOptions , flags , storageConfig )
470
473
if err == nil {
471
474
return & result
0 commit comments