| 1 |
/* |
| 2 |
Copyright 2019 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 resourcequota |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"reflect" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
|
| 25 |
corev1 "k8s.io/api/core/v1" |
| 26 |
resourcequotaapi "k8s.io/apiserver/pkg/admission/plugin/resourcequota/apis/resourcequota" |
| 27 |
) |
| 28 |
|
| 29 |
func TestLoadConfiguration(t *testing.T) { |
| 30 |
testcases := []struct { |
| 31 |
name string |
| 32 |
input string |
| 33 |
expectErr string |
| 34 |
expectConfig *resourcequotaapi.Configuration |
| 35 |
}{ |
| 36 |
{ |
| 37 |
name: "empty", |
| 38 |
input: ``, |
| 39 |
expectErr: `'Kind' is missing`, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "unknown type", |
| 43 |
input: `{"kind":"Unknown","apiVersion":"v1"}`, |
| 44 |
expectErr: `no kind "Unknown" is registered`, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "duplicate field error; strict validation", |
| 48 |
input: ` |
| 49 |
kind: ResourceQuotaConfiguration |
| 50 |
apiVersion: apiserver.config.k8s.io/v1 |
| 51 |
limitedResources: |
| 52 |
- apiGroup: "" |
| 53 |
resource: persistentvolumeclaims |
| 54 |
resource: persistentvolumeclaims |
| 55 |
matchContains: |
| 56 |
- .storageclass.storage.k8s.io/requests.storage |
| 57 |
`, |
| 58 |
expectErr: `strict decoding error`, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "unknown field error; strict validation", |
| 62 |
input: ` |
| 63 |
kind: ResourceQuotaConfiguration |
| 64 |
apiVersion: apiserver.config.k8s.io/v1 |
| 65 |
limitedResources: |
| 66 |
- apiGroup: "" |
| 67 |
foo: bar |
| 68 |
resource: persistentvolumeclaims |
| 69 |
matchContains: |
| 70 |
- .storageclass.storage.k8s.io/requests.storage |
| 71 |
`, |
| 72 |
expectErr: `strict decoding error`, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "valid v1alpha1 config", |
| 76 |
input: ` |
| 77 |
kind: Configuration |
| 78 |
apiVersion: resourcequota.admission.k8s.io/v1alpha1 |
| 79 |
limitedResources: |
| 80 |
- apiGroup: "" |
| 81 |
resource: persistentvolumeclaims |
| 82 |
matchContains: |
| 83 |
- .storageclass.storage.k8s.io/requests.storage |
| 84 |
- apiGroup: "" |
| 85 |
resource: pods |
| 86 |
matchScopes: |
| 87 |
- scopeName: PriorityClass |
| 88 |
operator: In |
| 89 |
values: |
| 90 |
- cluster-services |
| 91 |
`, |
| 92 |
expectConfig: &resourcequotaapi.Configuration{ |
| 93 |
LimitedResources: []resourcequotaapi.LimitedResource{ |
| 94 |
{APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}}, |
| 95 |
{APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{ |
| 96 |
{ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}}, |
| 97 |
}, |
| 98 |
}, |
| 99 |
}}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "valid v1beta1 config", |
| 103 |
input: ` |
| 104 |
kind: Configuration |
| 105 |
apiVersion: resourcequota.admission.k8s.io/v1beta1 |
| 106 |
limitedResources: |
| 107 |
- apiGroup: "" |
| 108 |
resource: persistentvolumeclaims |
| 109 |
matchContains: |
| 110 |
- .storageclass.storage.k8s.io/requests.storage |
| 111 |
- apiGroup: "" |
| 112 |
resource: pods |
| 113 |
matchScopes: |
| 114 |
- scopeName: PriorityClass |
| 115 |
operator: In |
| 116 |
values: |
| 117 |
- cluster-services |
| 118 |
`, |
| 119 |
expectConfig: &resourcequotaapi.Configuration{ |
| 120 |
LimitedResources: []resourcequotaapi.LimitedResource{ |
| 121 |
{APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}}, |
| 122 |
{APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{ |
| 123 |
{ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}}, |
| 124 |
}, |
| 125 |
}, |
| 126 |
}}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "valid v1 config", |
| 130 |
input: ` |
| 131 |
kind: ResourceQuotaConfiguration |
| 132 |
apiVersion: apiserver.config.k8s.io/v1 |
| 133 |
limitedResources: |
| 134 |
- apiGroup: "" |
| 135 |
resource: persistentvolumeclaims |
| 136 |
matchContains: |
| 137 |
- .storageclass.storage.k8s.io/requests.storage |
| 138 |
- apiGroup: "" |
| 139 |
resource: pods |
| 140 |
matchScopes: |
| 141 |
- scopeName: PriorityClass |
| 142 |
operator: In |
| 143 |
values: |
| 144 |
- cluster-services |
| 145 |
`, |
| 146 |
expectConfig: &resourcequotaapi.Configuration{ |
| 147 |
LimitedResources: []resourcequotaapi.LimitedResource{ |
| 148 |
{APIGroup: "", Resource: "persistentvolumeclaims", MatchContains: []string{".storageclass.storage.k8s.io/requests.storage"}}, |
| 149 |
{APIGroup: "", Resource: "pods", MatchScopes: []corev1.ScopedResourceSelectorRequirement{ |
| 150 |
{ScopeName: "PriorityClass", Operator: "In", Values: []string{"cluster-services"}}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
}}, |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
for _, tc := range testcases { |
| 158 |
t.Run(tc.name, func(t *testing.T) { |
| 159 |
config, err := LoadConfiguration(bytes.NewBuffer([]byte(tc.input))) |
| 160 |
|
| 161 |
if len(tc.expectErr) > 0 { |
| 162 |
if err == nil { |
| 163 |
t.Fatal("expected err, got none") |
| 164 |
} |
| 165 |
if !strings.Contains(err.Error(), tc.expectErr) { |
| 166 |
t.Fatalf("expected err containing %q, got %v", tc.expectErr, err) |
| 167 |
} |
| 168 |
return |
| 169 |
} |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
if !reflect.DeepEqual(config, tc.expectConfig) { |
| 174 |
t.Fatalf("expected\n%#v\ngot\n%#v", tc.expectConfig, config) |
| 175 |
} |
| 176 |
}) |
| 177 |
} |
| 178 |
} |