Skip to content

Commit d426997

Browse files
Merge pull request #57 from bats-core/feature/issues-50-and-53
Features #50 and #53
2 parents d11069b + 39ab295 commit d426997

6 files changed

Lines changed: 194 additions & 116 deletions

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ DETIK_CLIENT_NAME="kubectl"
145145
# If not set, queries will be run in the default namespace.
146146
DETIK_CLIENT_NAMESPACE="my-specific-namespace"
147147

148+
# If instead you want to target all the namespaces,
149+
# do not set DETIK_CLIENT_NAMESPACE. Instead, use:
150+
# DETIK_CLIENT_NAMESPACE_ALL="true"
151+
148152
# Verify the number of PODS and services
149153
verify "there are 2 pods named 'nginx'"
150154
verify "there is 1 service named 'nginx'"

lib/detik.bash

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ source "$directory/utils.bash"
1515
try() {
1616

1717
# Concatenate all the arguments into a single string
18-
IFS=' '
1918
exp="$*"
2019

2120
# Trim the expression
@@ -88,9 +87,6 @@ try() {
8887
# Do we have something?
8988
if [[ "$times" != "" ]]; then
9089

91-
# Prevent line breaks from being removed in command results
92-
IFS=""
93-
9490
# Start the loop
9591
echo "Valid expression. Verification in progress..."
9692
code=0
@@ -131,7 +127,6 @@ try() {
131127
verify() {
132128

133129
# Concatenate all the arguments into a single string
134-
IFS=' '
135130
exp="$*"
136131

137132
# Trim the expression
@@ -152,8 +147,9 @@ verify() {
152147

153148
echo "Valid expression. Verification in progress..."
154149
query=$(build_k8s_request "")
155-
client_with_options=$(build_k8s_client_with_options)
156-
result=$(eval $client_with_options get $resource $query \
150+
client_options=$(build_k8s_client_options)
151+
cmd=$(trim "$DETIK_CLIENT_NAME get $resource $query $client_options")
152+
result=$(eval $cmd \
157153
| tail -n +2 \
158154
| filter_by_resource_name "$name" \
159155
| wc -l \
@@ -165,7 +161,7 @@ verify() {
165161
detik_debug "$BATS_TEST_DESCRIPTION"
166162
detik_debug ""
167163
detik_debug "Client query:"
168-
detik_debug "$client_with_options get $resource $query"
164+
detik_debug "$cmd"
169165
detik_debug ""
170166
detik_debug "Result:"
171167
detik_debug "$result"
@@ -251,16 +247,17 @@ verify_value() {
251247
# 2. Remove the first line (the one that contains the column names)
252248
# 3. Filter by resource name
253249
query=$(build_k8s_request "$property")
254-
client_with_options=$(build_k8s_client_with_options)
255-
result=$(eval $client_with_options get $resource $query | tail -n +2 | filter_by_resource_name "$name")
250+
client_options=$(build_k8s_client_options)
251+
cmd=$(trim "$DETIK_CLIENT_NAME get $resource $query $client_options")
252+
result=$(eval $cmd | tail -n +2 | filter_by_resource_name "$name")
256253

257254
# Debug?
258255
detik_debug "-----DETIK:begin-----"
259256
detik_debug "$BATS_TEST_FILENAME"
260257
detik_debug "$BATS_TEST_DESCRIPTION"
261258
detik_debug ""
262259
detik_debug "Client query:"
263-
detik_debug "$client_with_options get $resource $query"
260+
detik_debug "$cmd"
264261
detik_debug ""
265262
detik_debug "Result:"
266263
detik_debug "$result"
@@ -272,66 +269,73 @@ verify_value() {
272269
detik_debug ""
273270

274271
# Is the result empty?
275-
empty=0
276-
if [[ "$result" == "" ]]; then
277-
echo "No resource of type '$resource' was found with the name '$name'."
278-
fi
279-
280-
# Verify the result
281-
IFS=$'\n'
282272
invalid=0
283273
valid=0
284-
for line in $result; do
285-
286-
# Keep the second column (property to verify)
287-
# This column may contain spaces.
288-
value=$(cut -d ' ' -f 2- <<< "$line" | xargs)
289-
element=$(cut -d ' ' -f 1 <<< "$line" | xargs)
290-
291-
# Compare with an exact value (case insensitive)
292-
if [[ "$exp" =~ "more than" ]]; then
293-
if [[ "$value" -gt "$expected_value" ]]; then
294-
echo "$element matches the regular expression (found $value)."
295-
valid=$((valid + 1))
296-
else
297-
echo "Current value for $element is not more than $expected_value..."
298-
invalid=$((invalid + 1))
299-
fi
300-
elif [[ "$exp" =~ "less than" ]]; then
301-
if [[ "$value" -lt "$expected_value" ]]; then
302-
echo "$element matches the regular expression (found $value)."
303-
valid=$((valid + 1))
304-
else
305-
echo "Current value for $element is not less than $expected_value..."
306-
invalid=$((invalid + 1))
307-
fi
308-
elif [[ "$verify_strict_equality" == "true" ]]; then
309-
value=$(to_lower_case "$value")
310-
expected_value=$(to_lower_case "$expected_value")
311-
if [[ "$value" != "$expected_value" ]]; then
312-
echo "Current value for $element is $value..."
313-
invalid=$((invalid + 1))
314-
else
315-
echo "$element has the right value ($value)."
316-
valid=$((valid + 1))
317-
fi
318-
# Verify a regex (we preserve the case)
319-
else
320-
# We do not want another syntax for case-insensitivity
321-
if [ "$DETIK_REGEX_CASE_INSENSITIVE_PROPERTIES" = "true" ]; then
322-
value=$(to_lower_case "$value")
323-
fi
274+
if [[ "$result" == "" ]] && [[ "$expected_count" != "0" ]]; then
275+
echo "No resource of type '$resource' was found with the name '$name'."
324276

325-
reg=$(echo "$value" | grep -E -- "$expected_value")
326-
if [[ "$?" -ne 0 ]]; then
327-
echo "Current value for $element is $value..."
328-
invalid=$((invalid + 1))
277+
# Otherwise, verify the result
278+
else
279+
# Read line by line and avoid overriding IFS globally.
280+
# Do not use mapfile (mapfile -t resultAsArray <<< "$result")
281+
# as it is not available in Bash 3 (used on MacOS)
282+
resultAsArray=()
283+
while IFS= read -r line; do resultAsArray+=("$line"); done <<< "$result"
284+
285+
# Now, deal with every line
286+
for line in "${resultAsArray[@]}"; do
287+
echo "$line" >> /tmp/toto3
288+
289+
# Keep the second column (property to verify)
290+
# This column may contain spaces.
291+
value=$(cut -d ' ' -f 2- <<< "$line" | xargs)
292+
element=$(cut -d ' ' -f 1 <<< "$line" | xargs)
293+
294+
# Compare with an exact value (case insensitive)
295+
if [[ "$exp" =~ "more than" ]]; then
296+
if [[ "$value" -gt "$expected_value" ]]; then
297+
echo "$element matches the regular expression (found $value)."
298+
valid=$((valid + 1))
299+
else
300+
echo "Current value for $element is not more than $expected_value..."
301+
invalid=$((invalid + 1))
302+
fi
303+
elif [[ "$exp" =~ "less than" ]]; then
304+
if [[ "$value" -lt "$expected_value" ]]; then
305+
echo "$element matches the regular expression (found $value)."
306+
valid=$((valid + 1))
307+
else
308+
echo "Current value for $element is not less than $expected_value..."
309+
invalid=$((invalid + 1))
310+
fi
311+
elif [[ "$verify_strict_equality" == "true" ]]; then
312+
value=$(to_lower_case "$value")
313+
expected_value=$(to_lower_case "$expected_value")
314+
if [[ "$value" != "$expected_value" ]]; then
315+
echo "Current value for $element is $value..."
316+
invalid=$((invalid + 1))
317+
else
318+
echo "$element has the right value ($value)."
319+
valid=$((valid + 1))
320+
fi
321+
# Verify a regex (we preserve the case)
329322
else
330-
echo "$element matches the regular expression (found $reg)."
331-
valid=$((valid + 1))
323+
# We do not want another syntax for case-insensitivity
324+
if [ "$DETIK_REGEX_CASE_INSENSITIVE_PROPERTIES" = "true" ]; then
325+
value=$(to_lower_case "$value")
326+
fi
327+
328+
reg=$(echo "$value" | grep -E -- "$expected_value")
329+
if [[ "$?" -ne 0 ]]; then
330+
echo "Current value for $element is $value..."
331+
invalid=$((invalid + 1))
332+
else
333+
echo "$element matches the regular expression (found $reg)."
334+
valid=$((valid + 1))
335+
fi
332336
fi
333-
fi
334-
done
337+
done
338+
fi
335339

336340
# Do we have the right number of elements?
337341
if [[ "$expected_count" != "" ]]; then
@@ -374,17 +378,20 @@ build_k8s_request() {
374378
}
375379

376380

377-
# Builds the client command, with the option for the K8s namespace, if any.
381+
# Builds the client options (e.g. the K8s namespace.
378382
# @return 0
379-
build_k8s_client_with_options() {
383+
build_k8s_client_options() {
380384

381-
client_with_options="$DETIK_CLIENT_NAME"
385+
client_options=""
382386
if [[ -n "$DETIK_CLIENT_NAMESPACE" ]]; then
383387
# eval does not "like" the '-n' syntax
384-
client_with_options="$DETIK_CLIENT_NAME --namespace=$DETIK_CLIENT_NAMESPACE"
388+
client_options="--namespace=$DETIK_CLIENT_NAMESPACE"
389+
elif [[ "$DETIK_CLIENT_NAMESPACE_ALL" == 'true' ]]; then
390+
# eval does not "like" the '-n' syntax
391+
client_options="--all-namespaces"
385392
fi
386393

387-
echo "$client_with_options"
394+
echo "$client_options"
388395
}
389396

390397

0 commit comments

Comments
 (0)