Skip to content

Commit cc98de0

Browse files
Merge pull request #47 from bats-core/#46
#46 Fix regexp support for resource names
2 parents 5dca7f5 + fde4db8 commit cc98de0

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ The syntax is quite simple and may be easily adapted for other solutions, such a
279279

280280
## Syntax Reference
281281

282+
For all the available sentences, both a resource name and a resource type are expected.
283+
The resource name can be a simple string (e.g. `nginx`) or a regular expression
284+
(e.g. `^nginx-[a-z0-9]{9,10}-[a-z0-9]{5}\$`), while the resource type is one of
285+
the K8s ones (e.g. `pods`, `po`, `services`, `svc`...) or a CRD. See
286+
[https://kubernetes.io/docs/reference/kubectl/overview/#resource-types](https://kubernetes.io/docs/reference/kubectl/overview/#resource-types) for a complete reference of the official resources. The available custom resources (e.g.
287+
`settings.management.cattle.io`) will depend on your cluster setup.
288+
289+
282290
### Counting Resources
283291

284292
Verify there are N resources of this type with this name pattern.
@@ -297,15 +305,11 @@ verify "there are more than <number> <resource-type> named '<regular-expression>
297305
verify "there are less than <number> <resource-type> named '<regular-expression>'"
298306
```
299307

300-
*resource-type* is one of the K8s ones (e.g. `pods`, `po`, `services`, `svc`...).
301-
See [https://kubernetes.io/docs/reference/kubectl/overview/#resource-types](https://kubernetes.io/docs/reference/kubectl/overview/#resource-types) for a complete reference.
302-
303-
304308
> :warning: This simple assertion may fail sometimes.
305309
>
306310
> As an example, if you count the number of PODs, run your test and then kill the POD, they will still be listed, with the `TERMINATING` state.
307311
>
308-
> So, most of the time, you will want to verify the number of instances with a given property value. Example: count the number of PODs with a given name pattern and having the `started` status.
312+
> So, most of the time, you will want to verify the number of instances with a given property value. Example: count the number of PODs with a given name pattern and having the `running` status.
309313
310314
Hence this additional syntax (using [next section](#verifying-property-values) documentation to verify additionnal properties):
311315

lib/detik.bash

+28-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ verify() {
153153
echo "Valid expression. Verification in progress..."
154154
query=$(build_k8s_request "")
155155
client_with_options=$(build_k8s_client_with_options)
156-
result=$(eval $client_with_options get $resource $query | grep $name | tail -n +1 | wc -l | tr -d '[:space:]')
156+
result=$(eval $client_with_options get $resource $query \
157+
| tail -n +2 \
158+
| filter_by_resource_name "$name" \
159+
| wc -l \
160+
| tr -d '[:space:]')
157161

158162
# Debug?
159163
detik_debug "-----DETIK:begin-----"
@@ -173,7 +177,7 @@ verify() {
173177
echo "Found $result $resource named $name (less than $card as expected)."
174178
else
175179
echo "Found $result $resource named $name (instead of less than $card expected)."
176-
return 3
180+
return 3
177181
fi
178182
elif [[ "$exp" =~ "more than" ]]; then
179183
if [[ "$result" -gt "$card" ]]; then
@@ -243,10 +247,12 @@ verify_value() {
243247
expected_count="$6"
244248
exp="$7"
245249

246-
# List the items and remove the first line (the one that contains the column names)
250+
# 1. Query / list the items
251+
# 2. Remove the first line (the one that contains the column names)
252+
# 3. Filter by resource name
247253
query=$(build_k8s_request "$property")
248254
client_with_options=$(build_k8s_client_with_options)
249-
result=$(eval $client_with_options get $resource $query | grep $name | tail -n +1)
255+
result=$(eval $client_with_options get $resource $query | tail -n +2 | filter_by_resource_name "$name")
250256

251257
# Debug?
252258
detik_debug "-----DETIK:begin-----"
@@ -380,3 +386,21 @@ build_k8s_client_with_options() {
380386

381387
echo "$client_with_options"
382388
}
389+
390+
391+
# Filters results by resource name (or name pattern).
392+
# The results are directly read, they are not passed as variables.
393+
#
394+
# @param $1 the resource name or name pattern
395+
# @return 0
396+
filter_by_resource_name() {
397+
398+
# For all the output lines...
399+
while IFS= read -r line; do
400+
# ... extract the resource name (first column)
401+
# and only keep the lines where the resource name matches
402+
if echo "$line" | cut -d ' ' -f1 | grep -qE "$1"; then
403+
echo "$line"
404+
fi
405+
done
406+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ mytest_deployment(){
145145
}
146146

147147

148+
@test "trying to find of a POD with an extended pattern name" {
149+
run try "at most 1 times every 5s to get pod named '^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{5}\$' and verify that 'status' is 'running'"
150+
[ "$status" -eq 0 ]
151+
[ ${#lines[@]} -eq 3 ]
152+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
153+
[ "${lines[1]}" = "nginx-deployment-75675f5897-6dg9r has the right value (running)." ]
154+
[ "${lines[2]}" = "nginx-deployment-75675f5897-gstkw has the right value (running)." ]
155+
}
156+
157+
148158
@test "trying to verify the status of a POD with an invalid pattern name" {
149159
run try "at most 1 times every 1s to get pods named 'ngin.+x' and verify that 'status' is 'running'"
150160
[ "$status" -eq 3 ]
@@ -154,6 +164,16 @@ mytest_deployment(){
154164
}
155165

156166

167+
@test "trying to find of a POD with an invalid extended pattern name" {
168+
run try "at most 1 times every 1s to find 2 pods named '^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{4}\$' with 'status' being 'running'"
169+
[ "$status" -eq 3 ]
170+
[ ${#lines[@]} -eq 3 ]
171+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
172+
[ "${lines[1]}" = "No resource of type 'pods' was found with the name '^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{4}\$'." ]
173+
[ "${lines[2]}" = "Expected 2 pods named ^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{4}\$ to have this value (running). Found 0." ]
174+
}
175+
176+
157177
@test "trying to verify the status of a POD with the lower-case syntax (multi-lines)" {
158178
run try " at most 5 times every 5s to get pods " \
159179
" named 'nginx' and " \

tests/test.detik.verify.bats

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ mytest_with_namespace() {
7676
}
7777

7878

79+
@test "verifying the number of PODs with an extended pattern syntax (exact number, 0 as singular)" {
80+
run verify "There are 2 PODS named '^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{5}\$'"
81+
[ "$status" -eq 0 ]
82+
[ ${#lines[@]} -eq 2 ]
83+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
84+
[ "${lines[1]}" = "Found 2 pods named ^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{5}\$ (as expected)." ]
85+
}
86+
87+
7988
@test "verifying the number of resources with their type including dots" {
8089
# The value is not important. We want to make sure resource
8190
# types with dots is supported.

tests/test.linter.bats

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ setup() {
7272

7373
run verify_against_pattern "at most 5 times eVery 5s to GET pods named nginx' and verify that 'status' matches '^RUNNING$'" "$try_regex_verify_matches"
7474
[ "$status" -eq 1 ]
75+
76+
run verify_against_pattern "at most 5 times eVery 5s to GET pods named '^nginx-deployment-[a-z0-9]{9,10}-[a-z0-9]{4}\$' and verify that 'status' matches '^RUNNING$'" "$try_regex_verify_matches"
77+
[ "$status" -eq 0 ]
7578
}
7679

7780

0 commit comments

Comments
 (0)