mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 19:28:16 +00:00
test: use cancelation from ktesting
The return type of ktesting.NewTestContext is now a TContext. Code which combined it WithCancel often didn't compile anymore (cannot overwrite ktesting.TContext with context.Context). This is a good thing because all of that code can be simplified to let ktesting handle the cancelation.
This commit is contained in:
@@ -75,11 +75,10 @@ func setup(t *testing.T, groupVersions ...schema.GroupVersion) (context.Context,
|
||||
return setupWithResources(t, groupVersions, nil)
|
||||
}
|
||||
|
||||
func setupWithResources(t *testing.T, groupVersions []schema.GroupVersion, resources []schema.GroupVersionResource) (context.Context, clientset.Interface, *restclient.Config, framework.TearDownFunc) {
|
||||
_, ctx := ktesting.NewTestContext(t)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
func setupWithResources(t *testing.T, groupVersions []schema.GroupVersion, resources []schema.GroupVersionResource) (context.Context, clientset.Interface /* TODO (pohly): return ktesting.TContext */, *restclient.Config, framework.TearDownFunc) {
|
||||
tCtx := ktesting.Init(t)
|
||||
|
||||
client, config, teardown := framework.StartTestServer(ctx, t, framework.TestServerSetup{
|
||||
client, config, teardown := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
|
||||
ModifyServerConfig: func(config *controlplane.Config) {
|
||||
if len(groupVersions) > 0 || len(resources) > 0 {
|
||||
resourceConfig := controlplane.DefaultAPIResourceConfigSource()
|
||||
@@ -91,11 +90,11 @@ func setupWithResources(t *testing.T, groupVersions []schema.GroupVersion, resou
|
||||
})
|
||||
|
||||
newTeardown := func() {
|
||||
cancel()
|
||||
tCtx.Cancel("tearing down apiserver")
|
||||
teardown()
|
||||
}
|
||||
|
||||
return ctx, client, config, newTeardown
|
||||
return tCtx, client, config, newTeardown
|
||||
}
|
||||
|
||||
func verifyStatusCode(t *testing.T, transport http.RoundTripper, verb, URL, body string, expectedStatusCode int) {
|
||||
@@ -375,12 +374,10 @@ func TestListOptions(t *testing.T) {
|
||||
|
||||
for _, watchCacheEnabled := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("watchCacheEnabled=%t", watchCacheEnabled), func(t *testing.T) {
|
||||
_, ctx := ktesting.NewTestContext(t)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
tCtx := ktesting.Init(t)
|
||||
|
||||
var storageTransport *storagebackend.TransportConfig
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(ctx, t, framework.TestServerSetup{
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
|
||||
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
|
||||
opts.Etcd.EnableWatchCache = watchCacheEnabled
|
||||
storageTransport = &opts.Etcd.StorageConfig.Transport
|
||||
@@ -397,7 +394,7 @@ func TestListOptions(t *testing.T) {
|
||||
for i := 0; i < 15; i++ {
|
||||
rs := newRS(ns.Name)
|
||||
rs.Name = fmt.Sprintf("test-%d", i)
|
||||
created, err := rsClient.Create(context.Background(), rs, metav1.CreateOptions{})
|
||||
created, err := rsClient.Create(tCtx, rs, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -407,7 +404,7 @@ func TestListOptions(t *testing.T) {
|
||||
// delete the first 5, and then compact them
|
||||
if i < 5 {
|
||||
var zero int64
|
||||
if err := rsClient.Delete(context.Background(), rs.Name, metav1.DeleteOptions{GracePeriodSeconds: &zero}); err != nil {
|
||||
if err := rsClient.Delete(tCtx, rs.Name, metav1.DeleteOptions{GracePeriodSeconds: &zero}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldestUncompactedRv = created.ResourceVersion
|
||||
@@ -427,12 +424,12 @@ func TestListOptions(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = kvClient.Compact(context.Background(), int64(revision))
|
||||
_, err = kvClient.Compact(tCtx, int64(revision))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
listObj, err := rsClient.List(context.Background(), metav1.ListOptions{
|
||||
listObj, err := rsClient.List(tCtx, metav1.ListOptions{
|
||||
Limit: 6,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -618,11 +615,9 @@ func TestListResourceVersion0(t *testing.T) {
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, ctx := ktesting.NewTestContext(t)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
tCtx := ktesting.Init(t)
|
||||
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(ctx, t, framework.TestServerSetup{
|
||||
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
|
||||
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
|
||||
opts.Etcd.EnableWatchCache = tc.watchCacheEnabled
|
||||
},
|
||||
@@ -637,7 +632,7 @@ func TestListResourceVersion0(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
rs := newRS(ns.Name)
|
||||
rs.Name = fmt.Sprintf("test-%d", i)
|
||||
if _, err := rsClient.Create(ctx, rs, metav1.CreateOptions{}); err != nil {
|
||||
if _, err := rsClient.Create(tCtx, rs, metav1.CreateOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -645,7 +640,7 @@ func TestListResourceVersion0(t *testing.T) {
|
||||
if tc.watchCacheEnabled {
|
||||
// poll until the watch cache has the full list in memory
|
||||
err := wait.PollImmediate(time.Second, wait.ForeverTestTimeout, func() (bool, error) {
|
||||
list, err := clientSet.AppsV1().ReplicaSets(ns.Name).List(ctx, metav1.ListOptions{ResourceVersion: "0"})
|
||||
list, err := clientSet.AppsV1().ReplicaSets(ns.Name).List(tCtx, metav1.ListOptions{ResourceVersion: "0"})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -657,12 +652,12 @@ func TestListResourceVersion0(t *testing.T) {
|
||||
}
|
||||
|
||||
pagerFn := func(opts metav1.ListOptions) (runtime.Object, error) {
|
||||
return rsClient.List(ctx, opts)
|
||||
return rsClient.List(tCtx, opts)
|
||||
}
|
||||
|
||||
p := pager.New(pager.SimplePageFunc(pagerFn))
|
||||
p.PageSize = 3
|
||||
listObj, _, err := p.List(ctx, metav1.ListOptions{ResourceVersion: "0"})
|
||||
listObj, _, err := p.List(tCtx, metav1.ListOptions{ResourceVersion: "0"})
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected list error: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user