...

Source file src/k8s.io/kubernetes/pkg/scheduler/framework/plugins/testing/testing.go

Documentation: k8s.io/kubernetes/pkg/scheduler/framework/plugins/testing

     1  /*
     2  Copyright 2021 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 testing
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/client-go/informers"
    27  	"k8s.io/client-go/kubernetes/fake"
    28  	"k8s.io/kubernetes/pkg/scheduler/framework"
    29  	frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
    30  )
    31  
    32  // SetupPluginWithInformers creates a plugin using a framework handle that includes
    33  // the provided sharedLister and a SharedInformerFactory with the provided objects.
    34  // The function also creates an empty namespace (since most tests creates pods with
    35  // empty namespace), and start informer factory.
    36  func SetupPluginWithInformers(
    37  	ctx context.Context,
    38  	tb testing.TB,
    39  	pf frameworkruntime.PluginFactory,
    40  	config runtime.Object,
    41  	sharedLister framework.SharedLister,
    42  	objs []runtime.Object,
    43  ) framework.Plugin {
    44  	objs = append([]runtime.Object{&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ""}}}, objs...)
    45  	informerFactory := informers.NewSharedInformerFactory(fake.NewSimpleClientset(objs...), 0)
    46  	fh, err := frameworkruntime.NewFramework(ctx, nil, nil,
    47  		frameworkruntime.WithSnapshotSharedLister(sharedLister),
    48  		frameworkruntime.WithInformerFactory(informerFactory))
    49  	if err != nil {
    50  		tb.Fatalf("Failed creating framework runtime: %v", err)
    51  	}
    52  	p, err := pf(ctx, config, fh)
    53  	if err != nil {
    54  		tb.Fatal(err)
    55  	}
    56  	informerFactory.Start(ctx.Done())
    57  	informerFactory.WaitForCacheSync(ctx.Done())
    58  	return p
    59  }
    60  
    61  // SetupPlugin creates a plugin using a framework handle that includes
    62  // the provided sharedLister.
    63  func SetupPlugin(
    64  	ctx context.Context,
    65  	tb testing.TB,
    66  	pf frameworkruntime.PluginFactory,
    67  	config runtime.Object,
    68  	sharedLister framework.SharedLister,
    69  ) framework.Plugin {
    70  	fh, err := frameworkruntime.NewFramework(ctx, nil, nil,
    71  		frameworkruntime.WithSnapshotSharedLister(sharedLister))
    72  	if err != nil {
    73  		tb.Fatalf("Failed creating framework runtime: %v", err)
    74  	}
    75  	p, err := pf(ctx, config, fh)
    76  	if err != nil {
    77  		tb.Fatal(err)
    78  	}
    79  	return p
    80  }
    81  

View as plain text