@@ -4,14 +4,15 @@ import (
4
4
"k8s.io/kubernetes/pkg/api"
5
5
"k8s.io/kubernetes/pkg/api/resource"
6
6
"k8s.io/kubernetes/pkg/api/unversioned"
7
+ "k8s.io/kubernetes/pkg/api/validation"
7
8
"k8s.io/kubernetes/pkg/util/validation/field"
8
9
"testing"
9
10
10
11
quotaapi "github.com/openshift/origin/pkg/quota/api"
11
12
)
12
13
13
- func TestValidationClusterQuota ( t * testing. T ) {
14
- spec := api.ResourceQuotaSpec {
14
+ func spec ( scopes ... api. ResourceQuotaScope ) api. ResourceQuotaSpec {
15
+ return api.ResourceQuotaSpec {
15
16
Hard : api.ResourceList {
16
17
api .ResourceCPU : resource .MustParse ("100" ),
17
18
api .ResourceMemory : resource .MustParse ("10000" ),
@@ -26,8 +27,25 @@ func TestValidationClusterQuota(t *testing.T) {
26
27
api .ResourceConfigMaps : resource .MustParse ("10" ),
27
28
api .ResourceSecrets : resource .MustParse ("10" ),
28
29
},
30
+ Scopes : scopes ,
29
31
}
32
+ }
30
33
34
+ func scopeableSpec (scopes ... api.ResourceQuotaScope ) api.ResourceQuotaSpec {
35
+ return api.ResourceQuotaSpec {
36
+ Hard : api.ResourceList {
37
+ api .ResourceCPU : resource .MustParse ("100" ),
38
+ api .ResourceMemory : resource .MustParse ("10000" ),
39
+ api .ResourceRequestsCPU : resource .MustParse ("100" ),
40
+ api .ResourceRequestsMemory : resource .MustParse ("10000" ),
41
+ api .ResourceLimitsCPU : resource .MustParse ("100" ),
42
+ api .ResourceLimitsMemory : resource .MustParse ("10000" ),
43
+ },
44
+ Scopes : scopes ,
45
+ }
46
+ }
47
+
48
+ func TestValidationClusterQuota (t * testing.T ) {
31
49
// storage is not yet supported as a quota tracked resource
32
50
invalidQuotaResourceSpec := api.ResourceQuotaSpec {
33
51
Hard : api.ResourceList {
@@ -41,7 +59,7 @@ func TestValidationClusterQuota(t *testing.T) {
41
59
ObjectMeta : api.ObjectMeta {Name : "good" },
42
60
Spec : quotaapi.ClusterResourceQuotaSpec {
43
61
Selector : quotaapi.ClusterResourceQuotaSelector {LabelSelector : & unversioned.LabelSelector {MatchLabels : validLabels }},
44
- Quota : spec ,
62
+ Quota : spec () ,
45
63
},
46
64
},
47
65
)
@@ -59,7 +77,7 @@ func TestValidationClusterQuota(t *testing.T) {
59
77
ObjectMeta : api.ObjectMeta {Namespace : "bad" , Name : "good" },
60
78
Spec : quotaapi.ClusterResourceQuotaSpec {
61
79
Selector : quotaapi.ClusterResourceQuotaSelector {LabelSelector : & unversioned.LabelSelector {MatchLabels : validLabels }},
62
- Quota : spec ,
80
+ Quota : spec () ,
63
81
},
64
82
},
65
83
T : field .ErrorTypeForbidden ,
@@ -69,12 +87,33 @@ func TestValidationClusterQuota(t *testing.T) {
69
87
A : quotaapi.ClusterResourceQuota {
70
88
ObjectMeta : api.ObjectMeta {Name : "good" },
71
89
Spec : quotaapi.ClusterResourceQuotaSpec {
72
- Quota : spec ,
90
+ Quota : spec (),
91
+ },
92
+ },
93
+ T : field .ErrorTypeRequired ,
94
+ F : "spec.selector" ,
95
+ },
96
+ "ok scope" : {
97
+ A : quotaapi.ClusterResourceQuota {
98
+ ObjectMeta : api.ObjectMeta {Name : "good" },
99
+ Spec : quotaapi.ClusterResourceQuotaSpec {
100
+ Quota : scopeableSpec (api .ResourceQuotaScopeNotTerminating ),
73
101
},
74
102
},
75
103
T : field .ErrorTypeRequired ,
76
104
F : "spec.selector" ,
77
105
},
106
+ "bad scope" : {
107
+ A : quotaapi.ClusterResourceQuota {
108
+ ObjectMeta : api.ObjectMeta {Name : "good" },
109
+ Spec : quotaapi.ClusterResourceQuotaSpec {
110
+ Selector : quotaapi.ClusterResourceQuotaSelector {LabelSelector : & unversioned.LabelSelector {MatchLabels : validLabels }},
111
+ Quota : spec (api .ResourceQuotaScopeNotTerminating ),
112
+ },
113
+ },
114
+ T : field .ErrorTypeInvalid ,
115
+ F : "spec.quota.scopes" ,
116
+ },
78
117
"bad quota spec" : {
79
118
A : quotaapi.ClusterResourceQuota {
80
119
ObjectMeta : api.ObjectMeta {Name : "good" },
@@ -103,3 +142,25 @@ func TestValidationClusterQuota(t *testing.T) {
103
142
}
104
143
}
105
144
}
145
+
146
+ func TestValidationQuota (t * testing.T ) {
147
+ tests := map [string ]struct {
148
+ A api.ResourceQuota
149
+ T field.ErrorType
150
+ F string
151
+ }{
152
+ "scope" : {
153
+ A : api.ResourceQuota {
154
+ ObjectMeta : api.ObjectMeta {Namespace : "ns" , Name : "good" },
155
+ Spec : scopeableSpec (api .ResourceQuotaScopeNotTerminating ),
156
+ },
157
+ },
158
+ }
159
+ for k , v := range tests {
160
+ errs := validation .ValidateResourceQuota (& v .A )
161
+ if len (errs ) != 0 {
162
+ t .Errorf ("%s: %v" , k , errs )
163
+ continue
164
+ }
165
+ }
166
+ }
0 commit comments