...

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

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

     1  /*
     2  Copyright 2020 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 defaultbinder
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	v1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/client-go/kubernetes/fake"
    29  	clienttesting "k8s.io/client-go/testing"
    30  	"k8s.io/klog/v2/ktesting"
    31  	frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
    32  	st "k8s.io/kubernetes/pkg/scheduler/testing"
    33  )
    34  
    35  func TestDefaultBinder(t *testing.T) {
    36  	testPod := st.MakePod().Name("foo").Namespace("ns").Obj()
    37  	testNode := "foohost.kubernetes.mydomain.com"
    38  	tests := []struct {
    39  		name        string
    40  		injectErr   error
    41  		wantBinding *v1.Binding
    42  	}{
    43  		{
    44  			name: "successful",
    45  			wantBinding: &v1.Binding{
    46  				ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "foo"},
    47  				Target:     v1.ObjectReference{Kind: "Node", Name: testNode},
    48  			},
    49  		}, {
    50  			name:      "binding error",
    51  			injectErr: errors.New("binding error"),
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			_, ctx := ktesting.NewTestContext(t)
    57  			ctx, cancel := context.WithCancel(ctx)
    58  			defer cancel()
    59  
    60  			var gotBinding *v1.Binding
    61  			client := fake.NewSimpleClientset(testPod)
    62  			client.PrependReactor("create", "pods", func(action clienttesting.Action) (bool, runtime.Object, error) {
    63  				if action.GetSubresource() != "binding" {
    64  					return false, nil, nil
    65  				}
    66  				if tt.injectErr != nil {
    67  					return true, nil, tt.injectErr
    68  				}
    69  				gotBinding = action.(clienttesting.CreateAction).GetObject().(*v1.Binding)
    70  				return true, gotBinding, nil
    71  			})
    72  
    73  			fh, err := frameworkruntime.NewFramework(ctx, nil, nil, frameworkruntime.WithClientSet(client))
    74  			if err != nil {
    75  				t.Fatal(err)
    76  			}
    77  			binder := &DefaultBinder{handle: fh}
    78  			status := binder.Bind(ctx, nil, testPod, testNode)
    79  			if got := status.AsError(); (tt.injectErr != nil) != (got != nil) {
    80  				t.Errorf("got error %q, want %q", got, tt.injectErr)
    81  			}
    82  			if diff := cmp.Diff(tt.wantBinding, gotBinding); diff != "" {
    83  				t.Errorf("got different binding (-want, +got): %s", diff)
    84  			}
    85  		})
    86  	}
    87  }
    88  

View as plain text