generated from kubewarden/go-policy-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquota_checks_test.go
168 lines (157 loc) · 3.75 KB
/
quota_checks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"strings"
"testing"
)
func TestCheckMalformedQuantities(t *testing.T) {
cases := []struct {
desc string
nsLimit, prjLimit, prjUsed string
expectedString string
}{
{"Bad namespace", "boom", "1k", "2k", "namespace"},
{"Bad project limit", "1k", "boom", "2k", "project limit"},
{"Bad project used quota", "1k", "2k", "boom", "project used quota"},
}
for _, tc := range cases {
err := checkLimitVsAvailableQuota(tc.nsLimit, tc.prjLimit, tc.prjUsed)
switch err := err.(type) {
case nil:
t.Errorf("%s: should have raised an error", tc.desc)
case *QuantityParseError:
if !strings.Contains(err.Error(), tc.expectedString) {
t.Errorf("%s: the error was supposed to be about '%s': %v", tc.desc, tc.expectedString, err)
}
default:
t.Errorf("%s: didn't get the expected error: %v", tc.desc, err)
}
}
}
func TestNamespaceRequestExceedsAvailability(t *testing.T) {
cases := []struct {
desc string
nsLimit, prjLimit, prjUsed string
}{
{"Way above", "2k", "1k", "500"},
{"Above", "500", "1k", "1k"},
{"Already overcommited", "500", "1k", "2k"},
}
for _, tc := range cases {
err := checkLimitVsAvailableQuota(tc.nsLimit, tc.prjLimit, tc.prjUsed)
switch err := err.(type) {
case nil:
t.Errorf("%s: should have raised an error", tc.desc)
case *NamespaceRequestExceedsAvailabilityError:
default:
t.Errorf("%s: didn't get the expected error: %v", tc.desc, err)
}
}
}
func TestLimitIsReasonable(t *testing.T) {
cases := []struct {
desc string
nsLimit, prjLimit, prjUsed string
}{
{"Equal", "1k", "2k", "1k"},
{"Below", "1k", "1M", "2k"},
{"No usage", "1k", "1M", "0"},
{"Not interested", "0", "1M", "1M"},
}
for _, tc := range cases {
err := checkLimitVsAvailableQuota(tc.nsLimit, tc.prjLimit, tc.prjUsed)
switch err := err.(type) {
case nil:
default:
t.Errorf("%s: should not have raised an error: %v", tc.desc, err)
}
}
}
func TestValidateQuotas(t *testing.T) {
cases := []struct {
desc string
project *Project
nsLimits *ResourceQuotaLimit
expectError bool
}{
{
"Project.Spec.ResourceQuota is nil",
&Project{Spec: &ProjectSpec{ResourceQuota: nil}},
nil,
false,
},
{
"Project.Spec.ResourceQuota has nothing set",
&Project{Spec: &ProjectSpec{ResourceQuota: &ProjectResourceQuota{}}},
nil,
false,
},
{
"nsLimits is nil",
&Project{
Spec: &ProjectSpec{
ResourceQuota: &ProjectResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "100",
},
UsedLimit: ResourceQuotaLimit{
Pods: "2",
},
},
},
},
nil,
false,
},
{
"a legitimate request",
&Project{
Spec: &ProjectSpec{
ResourceQuota: &ProjectResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "100",
Services: "100",
},
UsedLimit: ResourceQuotaLimit{
Pods: "50",
Services: "100",
},
},
},
},
&ResourceQuotaLimit{
Pods: "50",
},
false,
},
{
"requesting something that is not allowed",
&Project{
Spec: &ProjectSpec{
ResourceQuota: &ProjectResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "100",
ServicesLoadBalancers: "0",
},
UsedLimit: ResourceQuotaLimit{
Pods: "50",
ServicesLoadBalancers: "0",
},
},
},
},
&ResourceQuotaLimit{
ServicesLoadBalancers: "1",
},
true,
},
}
for _, tc := range cases {
err := validateQuotas(tc.project, tc.nsLimits)
if !tc.expectError && err != nil {
t.Errorf("%s: got an unexpected error: %v", tc.desc, err)
}
if tc.expectError && err == nil {
t.Errorf("%s: was expecting an error", tc.desc)
}
}
}