...

Source file src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/helper/helpers_test.go

Documentation: k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/helper

     1  /*
     2  Copyright 2018 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  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
    24  )
    25  
    26  var (
    27  	a v1.APIServiceConditionType = "A"
    28  	b v1.APIServiceConditionType = "B"
    29  )
    30  
    31  func TestIsAPIServiceConditionTrue(t *testing.T) {
    32  	conditionATrue := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionTrue)
    33  	conditionAFalse := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionFalse)
    34  	tests := []*struct {
    35  		name          string
    36  		apiService    *v1.APIService
    37  		conditionType v1.APIServiceConditionType
    38  		expected      bool
    39  	}{
    40  		{
    41  			name:          "Should return false when condition of type is not present",
    42  			apiService:    makeNewAPIService("v1", 100),
    43  			conditionType: a,
    44  			expected:      false,
    45  		},
    46  		{
    47  			name:          "Should return false when condition of type is present but status is not ConditionTrue",
    48  			apiService:    makeNewAPIService("v1", 100, conditionAFalse),
    49  			conditionType: a,
    50  			expected:      false,
    51  		},
    52  		{
    53  			name:          "Should return false when condition of type is present but status is not ConditionTrue",
    54  			apiService:    makeNewAPIService("v1", 100, conditionATrue),
    55  			conditionType: a,
    56  			expected:      true,
    57  		},
    58  	}
    59  
    60  	for _, tc := range tests {
    61  		if isConditionTrue := IsAPIServiceConditionTrue(tc.apiService, tc.conditionType); isConditionTrue != tc.expected {
    62  			t.Errorf("expected condition of type %v to be %v, actually was %v",
    63  				tc.conditionType, isConditionTrue, tc.expected)
    64  
    65  		}
    66  	}
    67  }
    68  
    69  func TestSetAPIServiceCondition(t *testing.T) {
    70  	conditionA1 := makeNewAPIServiceCondition(a, "a1 reason", "a1 message", v1.ConditionTrue)
    71  	conditionA2 := makeNewAPIServiceCondition(a, "a2 reason", "a2 message", v1.ConditionTrue)
    72  	tests := []*struct {
    73  		name              string
    74  		apiService        *v1.APIService
    75  		conditionType     v1.APIServiceConditionType
    76  		initialCondition  *v1.APIServiceCondition
    77  		setCondition      v1.APIServiceCondition
    78  		expectedCondition *v1.APIServiceCondition
    79  	}{
    80  		{
    81  			name:              "Should set a new condition with type where previously there was no condition of that type",
    82  			apiService:        makeNewAPIService("v1", 100),
    83  			conditionType:     a,
    84  			initialCondition:  nil,
    85  			setCondition:      conditionA1,
    86  			expectedCondition: &conditionA1,
    87  		},
    88  		{
    89  			name:              "Should override a condition of type, when a condition of that type existed previously",
    90  			apiService:        makeNewAPIService("v1", 100, conditionA1),
    91  			conditionType:     a,
    92  			initialCondition:  &conditionA1,
    93  			setCondition:      conditionA2,
    94  			expectedCondition: &conditionA2,
    95  		},
    96  	}
    97  
    98  	for _, tc := range tests {
    99  		startingCondition := GetAPIServiceConditionByType(tc.apiService, tc.conditionType)
   100  		if !reflect.DeepEqual(startingCondition, tc.initialCondition) {
   101  			t.Errorf("expected to find condition %s initially, actual was %s", tc.initialCondition, startingCondition)
   102  
   103  		}
   104  		SetAPIServiceCondition(tc.apiService, tc.setCondition)
   105  		actual := GetAPIServiceConditionByType(tc.apiService, tc.setCondition.Type)
   106  		if !reflect.DeepEqual(actual, tc.expectedCondition) {
   107  			t.Errorf("expected %s, actual %s", tc.expectedCondition, actual)
   108  		}
   109  	}
   110  }
   111  
   112  func TestSortedAPIServicesByVersion(t *testing.T) {
   113  	tests := []*struct {
   114  		name     string
   115  		versions []string
   116  		expected []string
   117  	}{
   118  		{
   119  			name:     "case1",
   120  			versions: []string{"v1", "v2"},
   121  			expected: []string{"v2", "v1"},
   122  		},
   123  		{
   124  			name:     "case2",
   125  			versions: []string{"v2", "v10"},
   126  			expected: []string{"v10", "v2"},
   127  		},
   128  		{
   129  			name:     "case3",
   130  			versions: []string{"v2", "v2beta1", "v10beta2", "v10beta1", "v10alpha1", "v1"},
   131  			expected: []string{"v2", "v1", "v10beta2", "v10beta1", "v2beta1", "v10alpha1"},
   132  		},
   133  		{
   134  			name:     "case4",
   135  			versions: []string{"v1", "v2", "test", "foo10", "final", "foo2", "foo1"},
   136  			expected: []string{"v2", "v1", "final", "foo1", "foo10", "foo2", "test"},
   137  		},
   138  		{
   139  			name:     "case5_from_documentation",
   140  			versions: []string{"v12alpha1", "v10", "v11beta2", "v10beta3", "v3beta1", "v2", "v11alpha2", "foo1", "v1", "foo10"},
   141  			expected: []string{"v10", "v2", "v1", "v11beta2", "v10beta3", "v3beta1", "v12alpha1", "v11alpha2", "foo1", "foo10"},
   142  		},
   143  	}
   144  
   145  	for _, tc := range tests {
   146  		apiServices := []*v1.APIService{}
   147  		for _, v := range tc.versions {
   148  			apiServices = append(apiServices, makeNewAPIService(v, 100))
   149  		}
   150  		sortedServices := SortedByGroupAndVersion(apiServices)
   151  		actual := []string{}
   152  		for _, s := range sortedServices[0] {
   153  			actual = append(actual, s.Spec.Version)
   154  		}
   155  		if !reflect.DeepEqual(tc.expected, actual) {
   156  			t.Errorf("expected %s, actual %s", tc.expected, actual)
   157  		}
   158  	}
   159  }
   160  
   161  func TestGetAPIServiceConditionByType(t *testing.T) {
   162  	conditionA := makeNewAPIServiceCondition(a, "a reason", "a message", v1.ConditionTrue)
   163  	conditionB := makeNewAPIServiceCondition(b, "b reason", "b message", v1.ConditionTrue)
   164  	tests := []*struct {
   165  		name              string
   166  		apiService        *v1.APIService
   167  		conditionType     v1.APIServiceConditionType
   168  		expectedCondition *v1.APIServiceCondition
   169  	}{
   170  		{
   171  			name:              "Should find a matching condition from apiService",
   172  			apiService:        makeNewAPIService("v1", 100, conditionA, conditionB),
   173  			conditionType:     a,
   174  			expectedCondition: &conditionA,
   175  		},
   176  		{
   177  			name:              "Should not find a matching condition",
   178  			apiService:        makeNewAPIService("v1", 100, conditionA),
   179  			conditionType:     b,
   180  			expectedCondition: nil,
   181  		},
   182  	}
   183  
   184  	for _, tc := range tests {
   185  		actual := GetAPIServiceConditionByType(tc.apiService, tc.conditionType)
   186  		if !reflect.DeepEqual(tc.expectedCondition, actual) {
   187  			t.Errorf("expected %s, actual %s", tc.expectedCondition, actual)
   188  		}
   189  	}
   190  }
   191  
   192  func makeNewAPIService(version string, priority int32, conditions ...v1.APIServiceCondition) *v1.APIService {
   193  	status := v1.APIServiceStatus{Conditions: conditions}
   194  	return &v1.APIService{Spec: v1.APIServiceSpec{Version: version, VersionPriority: priority}, Status: status}
   195  }
   196  
   197  func makeNewAPIServiceCondition(conditionType v1.APIServiceConditionType, reason string, message string, status v1.ConditionStatus) v1.APIServiceCondition {
   198  	return v1.APIServiceCondition{Type: conditionType, Reason: reason, Message: message, Status: status}
   199  }
   200  

View as plain text