Skip to content

Commit 457549d

Browse files
Merge pull request #41 from peterparser/master
Support 'more than' and 'less than' statements in verify and try to verify is
2 parents 12e9013 + 74dd2c9 commit 457549d

File tree

4 files changed

+118
-6
lines changed

4 files changed

+118
-6
lines changed

lib/detik.bash

+36-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ try() {
4040
expected_count=""
4141
verify_strict_equality="true"
4242

43-
if [[ "$exp" =~ $try_regex_verify_is ]]; then
43+
if [[ "$exp" =~ $try_regex_verify_is ]] || [[ "$exp" =~ $try_regex_verify_is_more_than ]] || [[ "$exp" =~ $try_regex_verify_is_less_than ]]; then
4444

4545
# Extract parameters
4646
times="${BASH_REMATCH[1]}"
@@ -97,7 +97,7 @@ try() {
9797
for ((i=1; i<=times; i++)); do
9898

9999
# Verify the value
100-
verify_value "$verify_strict_equality" "$property" "$expected_value" "$resource" "$name" "$expected_count" && code=$? || code=$?
100+
verify_value "$verify_strict_equality" "$property" "$expected_value" "$resource" "$name" "$expected_count" "$exp" && code=$? || code=$?
101101

102102
# Break the loop prematurely?
103103
if [[ "$code" == "0" ]]; then
@@ -145,7 +145,7 @@ verify() {
145145
echo "An empty expression was not expected."
146146
return 1
147147

148-
elif [[ "$exp" =~ $verify_regex_count_is ]] || [[ "$exp" =~ $verify_regex_count_are ]]; then
148+
elif [[ "$exp" =~ $verify_regex_count_is ]] || [[ "$exp" =~ $verify_regex_count_are ]] || [[ "$exp" =~ $verify_regex_count_is_less_than ]] || [[ "$exp" =~ $verify_regex_count_is_more_than ]]; then
149149
card="${BASH_REMATCH[1]}"
150150
resource=$(to_lower_case "${BASH_REMATCH[2]}")
151151
name="${BASH_REMATCH[3]}"
@@ -168,7 +168,21 @@ verify() {
168168
detik_debug "-----DETIK:end-----"
169169
detik_debug ""
170170

171-
if [[ "$result" == "$card" ]]; then
171+
if [[ "$exp" =~ "less than" ]]; then
172+
if [[ "$result" -lt "$card" ]]; then
173+
echo "Found $result $resource named $name (less than $card as expected)."
174+
else
175+
echo "Found $result $resource named $name (instead of less than $card expected)."
176+
return 3
177+
fi
178+
elif [[ "$exp" =~ "more than" ]]; then
179+
if [[ "$result" -gt "$card" ]]; then
180+
echo "Found $result $resource named $name (more than $card as expected)."
181+
else
182+
echo "Found $result $resource named $name (instead of more than $card expected)."
183+
return 3
184+
fi
185+
elif [[ "$result" == "$card" ]]; then
172186
echo "Found $result $resource named $name (as expected)."
173187
else
174188
echo "Found $result $resource named $name (instead of $card expected)."
@@ -227,6 +241,7 @@ verify_value() {
227241
resource="$4"
228242
name="$5"
229243
expected_count="$6"
244+
exp="$7"
230245

231246
# List the items and remove the first line (the one that contains the column names)
232247
query=$(build_k8s_request "$property")
@@ -268,7 +283,23 @@ verify_value() {
268283
element=$(cut -d ' ' -f 1 <<< "$line" | xargs)
269284

270285
# Compare with an exact value (case insensitive)
271-
if [[ "$verify_strict_equality" == "true" ]]; then
286+
if [[ "$exp" =~ "more than" ]]; then
287+
if [[ "$value" -gt "$expected_value" ]]; then
288+
echo "$element matches the regular expression (found $value)."
289+
valid=$((valid + 1))
290+
else
291+
echo "Current value for $element is not more than $expected_value..."
292+
invalid=$((invalid + 1))
293+
fi
294+
elif [[ "$exp" =~ "less than" ]]; then
295+
if [[ "$value" -lt "$expected_value" ]]; then
296+
echo "$element matches the regular expression (found $value)."
297+
valid=$((valid + 1))
298+
else
299+
echo "Current value for $element is not less than $expected_value..."
300+
invalid=$((invalid + 1))
301+
fi
302+
elif [[ "$verify_strict_equality" == "true" ]]; then
272303
value=$(to_lower_case "$value")
273304
expected_value=$(to_lower_case "$expected_value")
274305
if [[ "$value" != "$expected_value" ]]; then
@@ -278,7 +309,6 @@ verify_value() {
278309
echo "$element has the right value ($value)."
279310
valid=$((valid + 1))
280311
fi
281-
282312
# Verify a regex (we preserve the case)
283313
else
284314
# We do not want another syntax for case-insensitivity

lib/utils.bash

+4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
# The regex for the "try" key word
55
try_regex_verify_is="^at +most +([0-9]+) +times +every +([0-9]+)s +to +get +([a-z]+) +named +'([^']+)' +and +verify +that +'([^']+)' +is +'([^']+)'$"
6+
try_regex_verify_is_more_than="^at +most +([0-9]+) +times +every +([0-9]+)s +to +get +([a-z]+) +named +'([^']+)' +and +verify +that +'([^']+)' +is more than +'([^']+)'$"
7+
try_regex_verify_is_less_than="^at +most +([0-9]+) +times +every +([0-9]+)s +to +get +([a-z]+) +named +'([^']+)' +and +verify +that +'([^']+)' +is less than +'([^']+)'$"
68
try_regex_verify_matches="^at +most +([0-9]+) +times +every +([0-9]+)s +to +get +([a-z]+) +named +'([^']+)' +and +verify +that +'([^']+)' +matches +'([^']+)'$"
79
try_regex_find_being="^at +most +([0-9]+) +times +every +([0-9]+)s +to +find +([0-9]+) +([a-z]+) +named +'([^']+)' +with +'([^']+)' +being +'([^']+)'$"
810
try_regex_find_matching="^at +most +([0-9]+) +times +every +([0-9]+)s +to +find +([0-9]+) +([a-z]+) +named +'([^']+)' +with +'([^']+)' +matching +'([^']+)'$"
911

1012
# The regex for the "verify" key word
1113
verify_regex_count_is="^there +is +(0|1) +([a-z]+) +named +'([^']+)'$"
1214
verify_regex_count_are="^there +are +([0-9]+) +([a-z]+) +named +'([^']+)'$"
15+
verify_regex_count_is_less_than="^there are less than +([0-9]+) +([a-z]+) +named +'([^']+)'$"
16+
verify_regex_count_is_more_than="^there are more than +([0-9]+) +([a-z]+) +named +'([^']+)'$"
1317
verify_regex_property_is="^'([^']+)' +is +'([^']+)' +for +([a-z]+) +named +'([^']+)'$"
1418
verify_regex_property_matches="^'([^']+)' +matches +'([^']+)' +for +([a-z]+) +named +'([^']+)'$"
1519

tests/test.detik.try.to.verify.is.bats

+42
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ mytest_with_spaces() {
1212
echo -e "NAME PROP\ncert1 ----BEGIN CERTIFICATE----"
1313
}
1414

15+
mytest_deployment(){
16+
echo -e "NAME PROP\nnginx 2"
17+
}
18+
1519

1620
@test "trying to verify the status of a POD with the lower-case syntax" {
1721
run try "at most 5 times every 5s to get pods named 'nginx' and verify that 'status' is 'running'"
@@ -224,3 +228,41 @@ mytest_with_spaces() {
224228
[[ -f "$path.backup" ]] && mv "$path.backup" "$path"
225229
rm -rf "$path.cmp"
226230
}
231+
232+
233+
@test "trying to verify the replicas of a DEPLOYMENT is more than with the lower-case syntax" {
234+
DETIK_CLIENT_NAME=mytest_deployment
235+
run try "at most 1 times every 5s to get deployment named 'nginx' and verify that 'status.readyReplicas' is more than '1'"
236+
[ "$status" -eq 0 ]
237+
[ ${#lines[@]} -eq 2 ]
238+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
239+
[ "${lines[1]}" = "nginx matches the regular expression (found 2)." ]
240+
}
241+
242+
243+
@test "trying to verify the replicas of a DEPLOYMENT is less than with the lower-case syntax" {
244+
DETIK_CLIENT_NAME=mytest_deployment
245+
run try "at most 1 times every 5s to get deployment named 'nginx' and verify that 'status.readyReplicas' is less than '3'"
246+
[ "$status" -eq 0 ]
247+
[ ${#lines[@]} -eq 2 ]
248+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
249+
[ "${lines[1]}" = "nginx matches the regular expression (found 2)." ]
250+
}
251+
252+
@test "trying to verify the replicas of a DEPLOYMENT is more than with the lower-case syntax with higher value" {
253+
DETIK_CLIENT_NAME=mytest_deployment
254+
run try "at most 1 times every 5s to get deployment named 'nginx' and verify that 'status.readyReplicas' is more than '2'"
255+
[ "$status" -eq 3 ]
256+
[ ${#lines[@]} -eq 2 ]
257+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
258+
[ "${lines[1]}" = "Current value for nginx is not more than 2..." ]
259+
}
260+
261+
@test "trying to verify the replicas of a DEPLOYMENT is less than with the lower-case syntax with lower value" {
262+
DETIK_CLIENT_NAME=mytest_deployment
263+
run try "at most 1 times every 5s to get deployment named 'nginx' and verify that 'status.readyReplicas' is less than '1'"
264+
[ "$status" -eq 3 ]
265+
[ ${#lines[@]} -eq 2 ]
266+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
267+
[ "${lines[1]}" = "Current value for nginx is not less than 1..." ]
268+
}

tests/test.detik.verify.bats

+36
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,39 @@ mytest_with_namespace() {
621621
rm -rf "$path.cmp"
622622
[ -f "$path.backup" ] && mv "$path.backup" "$path"
623623
}
624+
625+
626+
@test "verifying the number of PODs with the lower-case syntax (more than, plural)" {
627+
run verify "there are more than 1 pods named 'nginx'"
628+
[ "$status" -eq 0 ]
629+
[ ${#lines[@]} -eq 2 ]
630+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
631+
[ "${lines[1]}" = "Found 2 pods named nginx (more than 1 as expected)." ]
632+
}
633+
634+
635+
@test "verifying the number of PODs with the lower-case syntax (less than, plural)" {
636+
run verify "there are less than 4 pods named 'nginx'"
637+
[ "$status" -eq 0 ]
638+
[ ${#lines[@]} -eq 2 ]
639+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
640+
[ "${lines[1]}" = "Found 2 pods named nginx (less than 4 as expected)." ]
641+
}
642+
643+
644+
@test "verifying the number of PODs with the lower-case syntax failure (more than, plural)" {
645+
run verify "there are more than 5 pods named 'nginx'"
646+
[ "$status" -eq 3 ]
647+
[ ${#lines[@]} -eq 2 ]
648+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
649+
[ "${lines[1]}" = "Found 2 pods named nginx (instead of more than 5 expected)." ]
650+
}
651+
652+
653+
@test "verifying the number of PODs with the lower-case syntax failure (less than, plural)" {
654+
run verify "there are less than 1 pods named 'nginx'"
655+
[ "$status" -eq 3 ]
656+
[ ${#lines[@]} -eq 2 ]
657+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
658+
[ "${lines[1]}" = "Found 2 pods named nginx (instead of less than 1 expected)." ]
659+
}

0 commit comments

Comments
 (0)