1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package validation
18
19import (
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/scheduling"
25	schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
26)
27
28func TestValidatePriorityClass(t *testing.T) {
29	spcs := schedulingapiv1.SystemPriorityClasses()
30	successCases := map[string]scheduling.PriorityClass{
31		"no description": {
32			ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},
33			Value:      100,
34		},
35		"with description": {
36			ObjectMeta:    metav1.ObjectMeta{Name: "tier1", Namespace: ""},
37			Value:         200,
38			GlobalDefault: false,
39			Description:   "Used for the highest priority pods.",
40		},
41		"system node critical": {
42			ObjectMeta:    metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
43			Value:         spcs[0].Value,
44			GlobalDefault: spcs[0].GlobalDefault,
45			Description:   "system priority class 0",
46		},
47	}
48
49	for k, v := range successCases {
50		if errs := ValidatePriorityClass(&v); len(errs) != 0 {
51			t.Errorf("Expected success for %s, got %v", k, errs)
52		}
53	}
54
55	errorCases := map[string]scheduling.PriorityClass{
56		"with namespace": {
57			ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "foo"},
58			Value:      100,
59		},
60		"invalid name": {
61			ObjectMeta: metav1.ObjectMeta{Name: "tier&1", Namespace: ""},
62			Value:      100,
63		},
64		"incorrect system class name": {
65			ObjectMeta:    metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
66			Value:         0,
67			GlobalDefault: spcs[0].GlobalDefault,
68		},
69		"incorrect system class value": {
70			ObjectMeta:    metav1.ObjectMeta{Name: "system-something", Namespace: ""},
71			Value:         spcs[0].Value,
72			GlobalDefault: spcs[0].GlobalDefault,
73		},
74	}
75
76	for k, v := range errorCases {
77		if errs := ValidatePriorityClass(&v); len(errs) == 0 {
78			t.Errorf("Expected error for %s, but it succeeded", k)
79		}
80	}
81}
82
83func TestValidatePriorityClassUpdate(t *testing.T) {
84	old := scheduling.PriorityClass{
85		ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "1"},
86		Value:      100,
87	}
88	successCases := map[string]scheduling.PriorityClass{
89		"no change": {
90			ObjectMeta:  metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
91			Value:       100,
92			Description: "Used for the highest priority pods.",
93		},
94		"change description": {
95			ObjectMeta:  metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
96			Value:       100,
97			Description: "A different description.",
98		},
99		"remove description": {
100			ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
101			Value:      100,
102		},
103		"change globalDefault": {
104			ObjectMeta:    metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
105			Value:         100,
106			GlobalDefault: true,
107		},
108	}
109
110	for k, v := range successCases {
111		if errs := ValidatePriorityClassUpdate(&v, &old); len(errs) != 0 {
112			t.Errorf("Expected success for %s, got %v", k, errs)
113		}
114	}
115
116	errorCases := map[string]struct {
117		P scheduling.PriorityClass
118		T field.ErrorType
119	}{
120		"add namespace": {
121			P: scheduling.PriorityClass{
122				ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "foo", ResourceVersion: "2"},
123				Value:      100,
124			},
125			T: field.ErrorTypeInvalid,
126		},
127		"change name": {
128			P: scheduling.PriorityClass{
129				ObjectMeta: metav1.ObjectMeta{Name: "tier2", Namespace: "", ResourceVersion: "2"},
130				Value:      100,
131			},
132			T: field.ErrorTypeInvalid,
133		},
134		"remove value": {
135			P: scheduling.PriorityClass{
136				ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
137			},
138			T: field.ErrorTypeForbidden,
139		},
140		"change value": {
141			P: scheduling.PriorityClass{
142				ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
143				Value:      101,
144			},
145			T: field.ErrorTypeForbidden,
146		},
147	}
148
149	for k, v := range errorCases {
150		errs := ValidatePriorityClassUpdate(&v.P, &old)
151		if len(errs) == 0 {
152			t.Errorf("Expected error for %s, but it succeeded", k)
153			continue
154		}
155		for i := range errs {
156			if errs[i].Type != v.T {
157				t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
158			}
159		}
160	}
161}
162