...

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

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

     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 helper
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    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/client-go/informers"
    28  	"k8s.io/client-go/kubernetes/fake"
    29  	st "k8s.io/kubernetes/pkg/scheduler/testing"
    30  )
    31  
    32  func TestGetPodServices(t *testing.T) {
    33  	fakeInformerFactory := informers.NewSharedInformerFactory(&fake.Clientset{}, 0*time.Second)
    34  	var services []*v1.Service
    35  	for i := 0; i < 3; i++ {
    36  		service := &v1.Service{
    37  			ObjectMeta: metav1.ObjectMeta{
    38  				Name:      fmt.Sprintf("service-%d", i),
    39  				Namespace: "test",
    40  			},
    41  			Spec: v1.ServiceSpec{
    42  				Selector: map[string]string{
    43  					"app": fmt.Sprintf("test-%d", i),
    44  				},
    45  			},
    46  		}
    47  		services = append(services, service)
    48  		fakeInformerFactory.Core().V1().Services().Informer().GetStore().Add(service)
    49  	}
    50  	var pods []*v1.Pod
    51  	for i := 0; i < 5; i++ {
    52  		pod := st.MakePod().Name(fmt.Sprintf("test-pod-%d", i)).
    53  			Namespace("test").
    54  			Label("app", fmt.Sprintf("test-%d", i)).
    55  			Label("label", fmt.Sprintf("label-%d", i)).
    56  			Obj()
    57  		pods = append(pods, pod)
    58  	}
    59  
    60  	tests := []struct {
    61  		name   string
    62  		pod    *v1.Pod
    63  		expect []*v1.Service
    64  	}{
    65  		{
    66  			name:   "GetPodServices for pod-0",
    67  			pod:    pods[0],
    68  			expect: []*v1.Service{services[0]},
    69  		},
    70  		{
    71  			name:   "GetPodServices for pod-1",
    72  			pod:    pods[1],
    73  			expect: []*v1.Service{services[1]},
    74  		},
    75  		{
    76  			name:   "GetPodServices for pod-2",
    77  			pod:    pods[2],
    78  			expect: []*v1.Service{services[2]},
    79  		},
    80  		{
    81  			name:   "GetPodServices for pod-3",
    82  			pod:    pods[3],
    83  			expect: nil,
    84  		},
    85  		{
    86  			name:   "GetPodServices for pod-4",
    87  			pod:    pods[4],
    88  			expect: nil,
    89  		},
    90  	}
    91  	for _, test := range tests {
    92  		t.Run(test.name, func(t *testing.T) {
    93  			get, err := GetPodServices(fakeInformerFactory.Core().V1().Services().Lister(), test.pod)
    94  			if err != nil {
    95  				t.Errorf("Error from GetPodServices: %v", err)
    96  			} else if diff := cmp.Diff(test.expect, get); diff != "" {
    97  				t.Errorf("Unexpected services (-want, +got):\n%s", diff)
    98  			}
    99  		})
   100  	}
   101  }
   102  

View as plain text