...

Source file src/k8s.io/kubernetes/pkg/apis/scheduling/validation/validation_test.go

Documentation: k8s.io/kubernetes/pkg/apis/scheduling/validation

     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 validation
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  	"k8s.io/kubernetes/pkg/apis/core"
    25  	"k8s.io/kubernetes/pkg/apis/scheduling"
    26  	schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
    27  )
    28  
    29  func TestValidatePriorityClass(t *testing.T) {
    30  	spcs := schedulingapiv1.SystemPriorityClasses()
    31  	successCases := map[string]scheduling.PriorityClass{
    32  		"no description": {
    33  			ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},
    34  			Value:      100,
    35  		},
    36  		"with description": {
    37  			ObjectMeta:    metav1.ObjectMeta{Name: "tier1", Namespace: ""},
    38  			Value:         200,
    39  			GlobalDefault: false,
    40  			Description:   "Used for the highest priority pods.",
    41  		},
    42  		"system node critical": {
    43  			ObjectMeta:    metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
    44  			Value:         spcs[0].Value,
    45  			GlobalDefault: spcs[0].GlobalDefault,
    46  			Description:   "system priority class 0",
    47  		},
    48  	}
    49  
    50  	for k, v := range successCases {
    51  		if errs := ValidatePriorityClass(&v); len(errs) != 0 {
    52  			t.Errorf("Expected success for %s, got %v", k, errs)
    53  		}
    54  	}
    55  
    56  	errorCases := map[string]scheduling.PriorityClass{
    57  		"with namespace": {
    58  			ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "foo"},
    59  			Value:      100,
    60  		},
    61  		"invalid name": {
    62  			ObjectMeta: metav1.ObjectMeta{Name: "tier&1", Namespace: ""},
    63  			Value:      100,
    64  		},
    65  		"incorrect system class name": {
    66  			ObjectMeta:    metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
    67  			Value:         0,
    68  			GlobalDefault: spcs[0].GlobalDefault,
    69  		},
    70  		"incorrect system class value": {
    71  			ObjectMeta:    metav1.ObjectMeta{Name: "system-something", Namespace: ""},
    72  			Value:         spcs[0].Value,
    73  			GlobalDefault: spcs[0].GlobalDefault,
    74  		},
    75  	}
    76  
    77  	for k, v := range errorCases {
    78  		if errs := ValidatePriorityClass(&v); len(errs) == 0 {
    79  			t.Errorf("Expected error for %s, but it succeeded", k)
    80  		}
    81  	}
    82  }
    83  
    84  func TestValidatePriorityClassUpdate(t *testing.T) {
    85  	preemptLowerPriority := core.PreemptLowerPriority
    86  	preemptNever := core.PreemptNever
    87  
    88  	old := scheduling.PriorityClass{
    89  		ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "1"},
    90  		Value:            100,
    91  		PreemptionPolicy: &preemptLowerPriority,
    92  	}
    93  	successCases := map[string]scheduling.PriorityClass{
    94  		"no change": {
    95  			ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
    96  			Value:            100,
    97  			PreemptionPolicy: &preemptLowerPriority,
    98  			Description:      "Used for the highest priority pods.",
    99  		},
   100  		"change description": {
   101  			ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   102  			Value:            100,
   103  			PreemptionPolicy: &preemptLowerPriority,
   104  			Description:      "A different description.",
   105  		},
   106  		"remove description": {
   107  			ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   108  			Value:            100,
   109  			PreemptionPolicy: &preemptLowerPriority,
   110  		},
   111  		"change globalDefault": {
   112  			ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   113  			Value:            100,
   114  			PreemptionPolicy: &preemptLowerPriority,
   115  			GlobalDefault:    true,
   116  		},
   117  	}
   118  
   119  	for k, v := range successCases {
   120  		if errs := ValidatePriorityClassUpdate(&v, &old); len(errs) != 0 {
   121  			t.Errorf("Expected success for %s, got %v", k, errs)
   122  		}
   123  	}
   124  
   125  	errorCases := map[string]struct {
   126  		P scheduling.PriorityClass
   127  		T field.ErrorType
   128  	}{
   129  		"add namespace": {
   130  			P: scheduling.PriorityClass{
   131  				ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "foo", ResourceVersion: "2"},
   132  				Value:            100,
   133  				PreemptionPolicy: &preemptLowerPriority,
   134  			},
   135  			T: field.ErrorTypeInvalid,
   136  		},
   137  		"change name": {
   138  			P: scheduling.PriorityClass{
   139  				ObjectMeta:       metav1.ObjectMeta{Name: "tier2", Namespace: "", ResourceVersion: "2"},
   140  				Value:            100,
   141  				PreemptionPolicy: &preemptLowerPriority,
   142  			},
   143  			T: field.ErrorTypeInvalid,
   144  		},
   145  		"remove value": {
   146  			P: scheduling.PriorityClass{
   147  				ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   148  				PreemptionPolicy: &preemptLowerPriority,
   149  			},
   150  			T: field.ErrorTypeForbidden,
   151  		},
   152  		"change value": {
   153  			P: scheduling.PriorityClass{
   154  				ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   155  				Value:            101,
   156  				PreemptionPolicy: &preemptLowerPriority,
   157  			},
   158  			T: field.ErrorTypeForbidden,
   159  		},
   160  		"change preemptionPolicy": {
   161  			P: scheduling.PriorityClass{
   162  				ObjectMeta:       metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
   163  				Value:            100,
   164  				PreemptionPolicy: &preemptNever,
   165  			},
   166  			T: field.ErrorTypeInvalid,
   167  		},
   168  	}
   169  
   170  	for k, v := range errorCases {
   171  		errs := ValidatePriorityClassUpdate(&v.P, &old)
   172  		if len(errs) == 0 {
   173  			t.Errorf("Expected error for %s, but it succeeded", k)
   174  			continue
   175  		}
   176  		for i := range errs {
   177  			if errs[i].Type != v.T {
   178  				t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
   179  			}
   180  		}
   181  	}
   182  }
   183  

View as plain text