Skip to content

Commit 12e9013

Browse files
Merge pull request #40 from bats-core/#39
#39 Support spaces and dashes in regular expressions
2 parents 0d71702 + 904de37 commit 12e9013

10 files changed

+118
-41
lines changed

examples/bats/test_helm_package.bats

+21-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ setup() {
1515
}
1616

1717
verify_helm() {
18-
helm template ../drupal | kubectl apply --dry-run -f -
18+
helm template ../drupal | kubectl apply --dry-run -f -
1919
}
2020

2121

@@ -29,7 +29,7 @@ verify_helm() {
2929
@test "verify the deployment of the chart in dry-run mode" {
3030

3131
run verify_helm
32-
[ "$status" -eq 0 ]
32+
[ "$status" -eq 0 ]
3333
}
3434

3535

@@ -59,28 +59,28 @@ verify_helm() {
5959
run verify "there is 1 pod named 'my-test-drupal'"
6060
[ "$status" -eq 0 ]
6161

62-
run verify "there is 1 pod named 'my-test-varnish'"
63-
[ "$status" -eq 0 ]
62+
run verify "there is 1 pod named 'my-test-varnish'"
63+
[ "$status" -eq 0 ]
6464

6565
# Postgres specifics
6666
run verify "there is 1 service named 'my-test-postgres'"
67-
[ "$status" -eq 0 ]
67+
[ "$status" -eq 0 ]
6868

6969
run verify "there is 1 ep named 'my-test-postgres'"
70-
[ "$status" -eq 0 ]
70+
[ "$status" -eq 0 ]
7171

7272
run verify "'.subsets[*].ports[*].port' is '44320' for endpoints named 'my-test-postgres'"
73-
[ "$status" -eq 0 ]
73+
[ "$status" -eq 0 ]
7474

7575
run verify "'.subsets[*].addresses[*].ip' is '10.234.121.117' for endpoints named 'my-test-postgres'"
76-
[ "$status" -eq 0 ]
76+
[ "$status" -eq 0 ]
7777

7878
# Services
7979
run verify "there is 1 service named 'my-test-drupal'"
8080
[ "$status" -eq 0 ]
8181

8282
run verify "there is 1 service named 'my-test-varnish'"
83-
[ "$status" -eq 0 ]
83+
[ "$status" -eq 0 ]
8484

8585
run verify "'port' is '80' for services named 'my-test-drupal'"
8686
[ "$status" -eq 0 ]
@@ -93,24 +93,24 @@ verify_helm() {
9393
[ "$status" -eq 0 ]
9494

9595
run verify "there is 1 deployment named 'my-test-varnish'"
96-
[ "$status" -eq 0 ]
96+
[ "$status" -eq 0 ]
9797

9898
# Ingress
9999
run verify "there is 1 ingress named 'my-test-varnish'"
100-
[ "$status" -eq 0 ]
100+
[ "$status" -eq 0 ]
101101

102102
run verify "'.spec.rules[*].host' is 'varnish.test.local' for ingress named 'my-test-varnish'"
103-
[ "$status" -eq 0 ]
103+
[ "$status" -eq 0 ]
104104

105105
run verify "'.spec.rules[*].http.paths[*].backend.serviceName' is 'my-test-varnish' for ingress named 'my-test-varnish'"
106-
[ "$status" -eq 0 ]
106+
[ "$status" -eq 0 ]
107107

108108
# PODs should be started
109109
run try "at most 5 times every 30s to get pods named 'my-test-drupal' and verify that 'status' is 'running'"
110110
[ "$status" -eq 0 ]
111111

112-
run try "at most 5 times every 30s to get pods named 'my-test-varnish' and verify that 'status' is 'running'"
113-
[ "$status" -eq 0 ]
112+
run try "at most 5 times every 30s to get pods named 'my-test-varnish' and verify that 'status' is 'running'"
113+
[ "$status" -eq 0 ]
114114

115115
# Indicate to other tests the deployment succeeded
116116
echo "started" > tests.status.tmp
@@ -140,15 +140,15 @@ verify_helm() {
140140
[ "$status" -eq 0 ]
141141
[ "$output" == "release \"my-test\" deleted" ]
142142

143-
run verify "there is 0 service named 'my-test'"
144-
[ "$status" -eq 0 ]
143+
run verify "there is 0 service named 'my-test'"
144+
[ "$status" -eq 0 ]
145145

146-
run verify "there is 0 deployment named 'my-test'"
147-
[ "$status" -eq 0 ]
146+
run verify "there is 0 deployment named 'my-test'"
147+
[ "$status" -eq 0 ]
148148

149149
sleep 60
150-
run verify "there is 0 pod named 'my-test'"
151-
[ "$status" -eq 0 ]
150+
run verify "there is 0 pod named 'my-test'"
151+
[ "$status" -eq 0 ]
152152
}
153153

154154

examples/bats/test_kubectl_and_oc.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ try "at most 2 times every 30s to get svc named 'nginx' and verify that '.spec.p
8181

8282
# Splitting a request over several lines
8383
try "at most 2 times every 30s "\
84-
"to get svc named 'nginx' and "\
85-
"verify that '.spec.ports[*].targetPort' is '8484'"
84+
"to get svc named 'nginx' and "\
85+
"verify that '.spec.ports[*].targetPort' is '8484'"
8686

8787
# Using quotes differently
8888
# (make sure to surround single quotes by double ones)
8989
try at most 2 times every 30s \
90-
to get svc named "'nginx'" and \
91-
verify that "'.spec.ports[*].targetPort'" is "'8484'"
90+
to get svc named "'nginx'" and \
91+
verify that "'.spec.ports[*].targetPort'" is "'8484'"
9292

lib/detik.bash

+4-3
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ verify_value() {
263263
for line in $result; do
264264

265265
# Keep the second column (property to verify)
266-
value=$(echo "$line" | awk '{ print $2 }')
267-
element=$(echo "$line" | awk '{ print $1 }')
266+
# This column may contain spaces.
267+
value=$(cut -d ' ' -f 2- <<< "$line" | xargs)
268+
element=$(cut -d ' ' -f 1 <<< "$line" | xargs)
268269

269270
# Compare with an exact value (case insensitive)
270271
if [[ "$verify_strict_equality" == "true" ]]; then
@@ -285,7 +286,7 @@ verify_value() {
285286
value=$(to_lower_case "$value")
286287
fi
287288

288-
reg=$(echo "$value" | grep -E "$expected_value")
289+
reg=$(echo "$value" | grep -E -- "$expected_value")
289290
if [[ "$?" -ne 0 ]]; then
290291
echo "Current value for $element is $value..."
291292
invalid=$((invalid + 1))

lib/utils.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ detik_debug() {
6363

6464
if [[ "$DEBUG_DETIK" == "true" ]]; then
6565
debug "$1"
66-
fi
66+
fi
6767
}
6868

6969

tests/resources/without_lint_errors.no.run.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ try "at most 2 times every 30s to find 1 svc named 'nginx' with '.spec.ports[*].
178178

179179
# Splitting a request over several lines
180180
try "at most 2 times every 30s "\
181-
"to get svc named 'nginx' and "\
182-
"verify that '.spec.ports[*].targetPort' is '8484'"
181+
"to get svc named 'nginx' and "\
182+
"verify that '.spec.ports[*].targetPort' is '8484'"
183183

184184
# Using quotes differently
185185
# (make sure to surround single quotes by double ones)
186186
try at most 2 times every 30s \
187-
to get svc named "'nginx'" and \
188-
verify that "'.spec.ports[*].targetPort'" is "'8484'"
187+
to get svc named "'nginx'" and \
188+
verify that "'.spec.ports[*].targetPort'" is "'8484'"
189189

190190
try at most 2 times every 30s \
191-
to get svc named "'nginx'" and \
192-
verify that "'.spec.ports[*].targetPort'" matches "'[[:digit:]]+'"
191+
to get svc named "'nginx'" and \
192+
verify that "'.spec.ports[*].targetPort'" matches "'[[:digit:]]+'"
193193

194194
try "at most 2 times every 30s "\
195195
"to find 1 svc named 'nginx' "\

tests/resources/without_lint_errors.run.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ run try "at most 2 times every 30s to find 1 svc named 'nginx' with '.spec.ports
178178

179179
# Splitting a request over several lines
180180
run try "at most 2 times every 30s "\
181-
"to get svc named 'nginx' and "\
182-
"verify that '.spec.ports[*].targetPort' is '8484'"
181+
"to get svc named 'nginx' and "\
182+
"verify that '.spec.ports[*].targetPort' is '8484'"
183183

184184
# Using quotes differently
185185
# (make sure to surround single quotes by double ones)
186186
run try at most 2 times every 30s \
187-
to get svc named "'nginx'" and \
188-
verify that "'.spec.ports[*].targetPort'" is "'8484'"
187+
to get svc named "'nginx'" and \
188+
verify that "'.spec.ports[*].targetPort'" is "'8484'"
189189

190190
run try at most 2 times every 30s \
191-
to get svc named "'nginx'" and \
192-
verify that "'.spec.ports[*].targetPort'" matches "'[[:digit:]]+'"
191+
to get svc named "'nginx'" and \
192+
verify that "'.spec.ports[*].targetPort'" matches "'[[:digit:]]+'"
193193

194194
run try "at most 2 times every 30s "\
195195
"to find 1 svc named 'nginx' "\

tests/test.detik.try.to.find.being.bats

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ mytest_with_namespace() {
2121
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
2222
}
2323

24+
mytest_with_spaces() {
25+
# The namespace should not appear (it is set in 1st position)
26+
[[ "$1" != "--namespace=test_ns" ]] || return 1
27+
28+
# Return the result
29+
echo -e "NAME PROP\ncert1 ----OOPS----\ncert2 ----BEGIN CERTIFICATE----"
30+
}
31+
2432

2533
@test "trying to find 1 POD with the lower-case syntax" {
2634
run try "at most 1 times every 5s to find 1 pod named 'nginx' with 'status' being 'running'"
@@ -43,6 +51,17 @@ mytest_with_namespace() {
4351
}
4452

4553

54+
@test "trying to find 1 certificate as a single-line value with spaces" {
55+
DETIK_CLIENT_NAME="mytest_with_spaces"
56+
run try "at most 1 times every 1s to find 1 certificate named 'cert' with 'value' being '----BEGIN CERTIFICATE----'"
57+
[ "$status" -eq 0 ]
58+
[ ${#lines[@]} -eq 3 ]
59+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
60+
[ "${lines[1]}" = "Current value for cert1 is ----oops----..." ]
61+
[ "${lines[2]}" = "cert2 has the right value (----begin certificate----)." ]
62+
}
63+
64+
4665
@test "trying to find 2 PODs with the lower-case syntax (and a different K8s namespace)" {
4766
DETIK_CLIENT_NAME="mytest_with_namespace"
4867
DETIK_CLIENT_NAMESPACE="test_ns"

tests/test.detik.try.to.find.matching.bats

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ mytest_with_namespace() {
2121
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
2222
}
2323

24+
mytest_with_spaces() {
25+
# The namespace should not appear (it is set in 1st position)
26+
[[ "$1" != "--namespace=test_ns" ]] || return 1
27+
28+
# Return the result
29+
echo -e "NAME PROP\ncert1 ----OOPS----\ncert2 ----BEGIN CERTIFICATE----"
30+
}
31+
2432

2533
@test "trying to find 1 POD with the lower-case syntax and a simple match" {
2634
run try "at most 1 times every 5s to find 1 pod named 'nginx' with 'status' matching 'Running'"
@@ -99,6 +107,17 @@ mytest_with_namespace() {
99107
}
100108

101109

110+
@test "trying to find 1 certificate as a single-line value with spaces" {
111+
DETIK_CLIENT_NAME="mytest_with_spaces"
112+
run try "at most 1 times every 1s to find 1 certificate named 'cert' with 'value' matching '----BEGIN .*'"
113+
[ "$status" -eq 0 ]
114+
[ ${#lines[@]} -eq 3 ]
115+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
116+
[ "${lines[1]}" = "Current value for cert1 is ----OOPS----..." ]
117+
[ "${lines[2]}" = "cert2 matches the regular expression (found ----BEGIN CERTIFICATE----)." ]
118+
}
119+
120+
102121
@test "trying to find 2 PODs with the lower-case syntax (with a partial match and a different K8s namespace)" {
103122
DETIK_CLIENT_NAME="mytest_with_namespace"
104123
DETIK_CLIENT_NAMESPACE="test_ns"

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

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ mytest() {
88
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
99
}
1010

11+
mytest_with_spaces() {
12+
echo -e "NAME PROP\ncert1 ----BEGIN CERTIFICATE----"
13+
}
14+
1115

1216
@test "trying to verify the status of a POD with the lower-case syntax" {
1317
run try "at most 5 times every 5s to get pods named 'nginx' and verify that 'status' is 'running'"
@@ -48,6 +52,16 @@ mytest() {
4852
}
4953

5054

55+
@test "trying to verify the content of a single-line value with spaces" {
56+
DETIK_CLIENT_NAME="mytest_with_spaces"
57+
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' is '----BEGIN CERTIFICATE----'"
58+
[ "$status" -eq 0 ]
59+
[ ${#lines[@]} -eq 2 ]
60+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
61+
[ "${lines[1]}" = "cert1 has the right value (----begin certificate----)." ]
62+
}
63+
64+
5165
@test "trying to verify the syntax check (invalid wording)" {
5266
run try "at most 5 times VERY 5hours to GET pods named 'nginx' and verify that 'status' is 'RUNNING'"
5367
[ "$status" -eq 2 ]

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

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ mytest() {
88
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
99
}
1010

11+
mytest_with_spaces() {
12+
echo -e "NAME PROP\ncert1 ----BEGIN CERTIFICATE----"
13+
}
14+
1115

1216
@test "trying to verify the status of a POD with the lower-case syntax and a simple match" {
1317
run try "at most 1 times every 1s to get pods named 'nginx' and verify that 'status' matches 'Running'"
@@ -99,6 +103,26 @@ mytest() {
99103
}
100104

101105

106+
@test "trying to verify the content of a single-line value with spaces (success)" {
107+
DETIK_CLIENT_NAME="mytest_with_spaces"
108+
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' matches '----BEGIN CERT.*'"
109+
[ "$status" -eq 0 ]
110+
[ ${#lines[@]} -eq 2 ]
111+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
112+
[ "${lines[1]}" = "cert1 matches the regular expression (found ----BEGIN CERTIFICATE----)." ]
113+
}
114+
115+
116+
@test "trying to verify the content of a single-line value with spaces (failure)" {
117+
DETIK_CLIENT_NAME="mytest_with_spaces"
118+
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' matches 'BEGIN CATE'"
119+
[ "$status" -eq 3 ]
120+
[ ${#lines[@]} -eq 2 ]
121+
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
122+
[ "${lines[1]}" = "Current value for cert1 is ----BEGIN CERTIFICATE----..." ]
123+
}
124+
125+
102126
@test "trying to verify the syntax check (invalid wording, with a pattern match)" {
103127
run try "at most 5 times VERY 5hours to GET pods named 'nginx' and verify that 'status' matches 'RUNNING'"
104128
[ "$status" -eq 2 ]

0 commit comments

Comments
 (0)