-
Notifications
You must be signed in to change notification settings - Fork 9
/
evaluator_test.go
214 lines (210 loc) · 5.5 KB
/
evaluator_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"testing"
clientv3 "go.etcd.io/etcd/client/v3"
)
func TestValidateRule(t *testing.T) {
testCases := []struct {
name string
rule string
expectError bool
}{
{
name: "valid rule with comparison",
rule: "dbSize > dbQuota*80/100",
expectError: false,
},
{
name: "valid rule with arithmetic operator",
rule: "dbSize - dbSizeInUse > 200*1024*1024",
expectError: false,
},
{
name: "valid rule with logic OR",
rule: "dbSize > dbQuota*80/100 || dbSize - dbSizeInUse > 200*1024*1024",
expectError: false,
},
{
name: "valid rule with logic AND",
rule: "dbSize > dbQuota*80/100 && dbSize - dbSizeInUse > 200*1024*1024",
expectError: false,
},
{
name: "valid rule with parenthesis",
rule: "(dbSize > dbQuota*80/100) || (dbSize - dbSizeInUse > 200*1024*1024)",
expectError: false,
},
{
name: "valid rule with dbQuotaUsage",
rule: "dbQuotaUsage > 0.3",
expectError: false,
},
{
name: "valid rule with dbSizeFree",
rule: "dbSizeFree > 100",
expectError: false,
},
{
name: "not a boolean expression",
rule: "dbSize - dbSizeInUse",
expectError: true,
},
{
name: "invalid variable",
rule: "dbSizE > 100",
expectError: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := validateRule(tc.rule)
if tc.expectError != (err != nil) {
t.Errorf("Unexpected result, expected error: %t, got %v", tc.expectError, err)
}
})
}
}
func TestEvaluate(t *testing.T) {
testCases := []struct {
name string
rule string
expectError bool
dbQuota int
dbSize int
dbSizeInUse int
evaluationResult bool
}{
{
name: "not a boolean expression",
rule: "dbSize - dbSizeInUse",
expectError: true,
},
{
name: "invalid variable",
rule: "dbSizE > 100",
expectError: true,
},
{
name: "dbSize is greater than dbQuota*80/100",
rule: "dbSize > dbQuota*80/100",
dbQuota: 100,
dbSize: 81,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8",
rule: "dbQuotaUsage > 0.8",
dbQuota: 100,
dbSize: 81,
evaluationResult: true,
},
{
name: "dbSize is less than dbQuota*80/100",
rule: "dbSize > dbQuota*80/100",
dbQuota: 100,
dbSize: 79,
},
{
name: "dbQuotaUsage is less than 0.8",
rule: "dbQuotaUsage > 0.8",
dbQuota: 100,
dbSize: 79,
},
{
name: "free space is greater than 200",
rule: "dbSize - dbSizeInUse > 200",
dbSize: 301,
dbSizeInUse: 100,
evaluationResult: true,
},
{
name: "dbSizeFree is greater than 200",
rule: "dbSizeFree > 200",
dbSize: 301,
dbSizeInUse: 100,
evaluationResult: true,
},
{
name: "free space is less than 200",
rule: "dbSize - dbSizeInUse > 200",
dbSize: 299,
dbSizeInUse: 100,
},
{
name: "dbSizeFree is less than 200",
rule: "dbSizeFree > 200",
dbSize: 299,
dbSizeInUse: 100,
},
{
name: "dbSize is greater than dbQuota*80/100 AND free space is greater than 200",
rule: "dbSize > dbQuota*80/100 && dbSize - dbSizeInUse > 200",
dbQuota: 1000,
dbSize: 801,
dbSizeInUse: 600,
evaluationResult: true,
},
{
name: "dbSize is greater than dbQuota*80/100 AND free space is less than 200",
rule: "dbSize > dbQuota*80/100 && dbSize - dbSizeInUse > 200",
dbQuota: 100,
dbSize: 81,
dbSizeInUse: 60,
},
{
name: "dbSize is greater than dbQuota*80/100 OR free space is greater than 200",
rule: "dbSize > dbQuota*80/100 || dbSize - dbSizeInUse > 200",
dbQuota: 1000,
dbSize: 801,
dbSizeInUse: 600,
evaluationResult: true,
},
{
name: "dbSize is greater than dbQuota*80/100 AND free space is less than 200",
rule: "dbSize > dbQuota*80/100 || dbSize - dbSizeInUse > 200",
dbQuota: 100,
dbSize: 81,
dbSizeInUse: 60,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8 AND free space is greater than 200",
rule: "dbQuotaUsage > 0.8 && dbSizeFree > 200",
dbQuota: 1000,
dbSize: 801,
dbSizeInUse: 600,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8 OR free space is less than 200",
rule: "dbQuotaUsage > 0.8 || dbSizeFree > 200",
dbQuota: 100,
dbSize: 81,
dbSizeInUse: 60,
evaluationResult: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
gcfg := globalConfig{
defragRule: tc.rule,
dbQuotaBytes: tc.dbQuota,
}
es := epStatus{
Resp: &clientv3.StatusResponse{
DbSize: int64(tc.dbSize),
DbSizeInUse: int64(tc.dbSizeInUse),
},
}
ret, err := evaluate(gcfg, es)
if tc.expectError != (err != nil) {
t.Fatalf("Unexpected result, expected error: %t, got %v", tc.expectError, err)
}
if ret != tc.evaluationResult {
t.Fatalf("Unexpected evaluation result, expected %t, got %t", tc.evaluationResult, ret)
}
})
}
}