...

Source file src/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator/priority_test.go

Documentation: k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator

     1  /*
     2  Copyright 2017 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 aggregator
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
    24  )
    25  
    26  func newAPIServiceForTest(name, group string, minGroupPriority, versionPriority int32, svc *apiregistrationv1.ServiceReference) *apiregistrationv1.APIService {
    27  	r := apiregistrationv1.APIService{}
    28  	r.Spec.Group = group
    29  	r.Spec.GroupPriorityMinimum = minGroupPriority
    30  	r.Spec.VersionPriority = versionPriority
    31  	r.Spec.Service = svc
    32  	r.Name = name
    33  	return &r
    34  }
    35  
    36  func assertSortedServices(t *testing.T, actual []*apiregistrationv1.APIService, expectedNames []string) {
    37  	actualNames := []string{}
    38  	for _, a := range actual {
    39  		actualNames = append(actualNames, a.Name)
    40  	}
    41  	if !reflect.DeepEqual(actualNames, expectedNames) {
    42  		t.Errorf("Expected %s got %s.", expectedNames, actualNames)
    43  	}
    44  }
    45  
    46  func TestAPIServiceSort(t *testing.T) {
    47  	list := []*apiregistrationv1.APIService{
    48  		newAPIServiceForTest("FirstService", "Group1", 10, 5, &apiregistrationv1.ServiceReference{}),
    49  		newAPIServiceForTest("SecondService", "Group2", 15, 3, &apiregistrationv1.ServiceReference{}),
    50  		newAPIServiceForTest("FirstServiceInternal", "Group1", 16, 3, &apiregistrationv1.ServiceReference{}),
    51  		newAPIServiceForTest("ThirdService", "Group3", 15, 3, &apiregistrationv1.ServiceReference{}),
    52  		newAPIServiceForTest("local_service_1", "Group4", 15, 1, nil),
    53  		newAPIServiceForTest("local_service_3", "Group5", 15, 2, nil),
    54  		newAPIServiceForTest("local_service_2", "Group6", 15, 3, nil),
    55  	}
    56  	sortByPriority(list)
    57  	assertSortedServices(t, list, []string{"local_service_1", "local_service_2", "local_service_3", "FirstService", "FirstServiceInternal", "SecondService", "ThirdService"})
    58  }
    59  

View as plain text