Skip to content

Commit

Permalink
Merge pull request #40 from bats-core/#39
Browse files Browse the repository at this point in the history
#39 Support spaces and dashes in regular expressions
  • Loading branch information
vincent-zurczak committed Oct 12, 2023
2 parents 0d71702 + 904de37 commit 12e9013
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 41 deletions.
42 changes: 21 additions & 21 deletions examples/bats/test_helm_package.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ setup() {
}

verify_helm() {
helm template ../drupal | kubectl apply --dry-run -f -
helm template ../drupal | kubectl apply --dry-run -f -
}


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

run verify_helm
[ "$status" -eq 0 ]
[ "$status" -eq 0 ]
}


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

sleep 60
run verify "there is 0 pod named 'my-test'"
[ "$status" -eq 0 ]
run verify "there is 0 pod named 'my-test'"
[ "$status" -eq 0 ]
}


Expand Down
8 changes: 4 additions & 4 deletions examples/bats/test_kubectl_and_oc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ try "at most 2 times every 30s to get svc named 'nginx' and verify that '.spec.p

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

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

7 changes: 4 additions & 3 deletions lib/detik.bash
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ verify_value() {
for line in $result; do

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

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

reg=$(echo "$value" | grep -E "$expected_value")
reg=$(echo "$value" | grep -E -- "$expected_value")
if [[ "$?" -ne 0 ]]; then
echo "Current value for $element is $value..."
invalid=$((invalid + 1))
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ detik_debug() {

if [[ "$DEBUG_DETIK" == "true" ]]; then
debug "$1"
fi
fi
}


Expand Down
12 changes: 6 additions & 6 deletions tests/resources/without_lint_errors.no.run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ try "at most 2 times every 30s to find 1 svc named 'nginx' with '.spec.ports[*].

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

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

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

try "at most 2 times every 30s "\
"to find 1 svc named 'nginx' "\
Expand Down
12 changes: 6 additions & 6 deletions tests/resources/without_lint_errors.run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ run try "at most 2 times every 30s to find 1 svc named 'nginx' with '.spec.ports

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

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

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

run try "at most 2 times every 30s "\
"to find 1 svc named 'nginx' "\
Expand Down
19 changes: 19 additions & 0 deletions tests/test.detik.try.to.find.being.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ mytest_with_namespace() {
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
}

mytest_with_spaces() {
# The namespace should not appear (it is set in 1st position)
[[ "$1" != "--namespace=test_ns" ]] || return 1

# Return the result
echo -e "NAME PROP\ncert1 ----OOPS----\ncert2 ----BEGIN CERTIFICATE----"
}


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


@test "trying to find 1 certificate as a single-line value with spaces" {
DETIK_CLIENT_NAME="mytest_with_spaces"
run try "at most 1 times every 1s to find 1 certificate named 'cert' with 'value' being '----BEGIN CERTIFICATE----'"
[ "$status" -eq 0 ]
[ ${#lines[@]} -eq 3 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "Current value for cert1 is ----oops----..." ]
[ "${lines[2]}" = "cert2 has the right value (----begin certificate----)." ]
}


@test "trying to find 2 PODs with the lower-case syntax (and a different K8s namespace)" {
DETIK_CLIENT_NAME="mytest_with_namespace"
DETIK_CLIENT_NAMESPACE="test_ns"
Expand Down
19 changes: 19 additions & 0 deletions tests/test.detik.try.to.find.matching.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ mytest_with_namespace() {
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
}

mytest_with_spaces() {
# The namespace should not appear (it is set in 1st position)
[[ "$1" != "--namespace=test_ns" ]] || return 1

# Return the result
echo -e "NAME PROP\ncert1 ----OOPS----\ncert2 ----BEGIN CERTIFICATE----"
}


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


@test "trying to find 1 certificate as a single-line value with spaces" {
DETIK_CLIENT_NAME="mytest_with_spaces"
run try "at most 1 times every 1s to find 1 certificate named 'cert' with 'value' matching '----BEGIN .*'"
[ "$status" -eq 0 ]
[ ${#lines[@]} -eq 3 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "Current value for cert1 is ----OOPS----..." ]
[ "${lines[2]}" = "cert2 matches the regular expression (found ----BEGIN CERTIFICATE----)." ]
}


@test "trying to find 2 PODs with the lower-case syntax (with a partial match and a different K8s namespace)" {
DETIK_CLIENT_NAME="mytest_with_namespace"
DETIK_CLIENT_NAMESPACE="test_ns"
Expand Down
14 changes: 14 additions & 0 deletions tests/test.detik.try.to.verify.is.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mytest() {
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
}

mytest_with_spaces() {
echo -e "NAME PROP\ncert1 ----BEGIN CERTIFICATE----"
}


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


@test "trying to verify the content of a single-line value with spaces" {
DETIK_CLIENT_NAME="mytest_with_spaces"
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' is '----BEGIN CERTIFICATE----'"
[ "$status" -eq 0 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "cert1 has the right value (----begin certificate----)." ]
}


@test "trying to verify the syntax check (invalid wording)" {
run try "at most 5 times VERY 5hours to GET pods named 'nginx' and verify that 'status' is 'RUNNING'"
[ "$status" -eq 2 ]
Expand Down
24 changes: 24 additions & 0 deletions tests/test.detik.try.to.verify.matches.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mytest() {
echo -e "NAME PROP\nnginx-deployment-75675f5897-6dg9r Running\nnginx-deployment-75675f5897-gstkw Running"
}

mytest_with_spaces() {
echo -e "NAME PROP\ncert1 ----BEGIN CERTIFICATE----"
}


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


@test "trying to verify the content of a single-line value with spaces (success)" {
DETIK_CLIENT_NAME="mytest_with_spaces"
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' matches '----BEGIN CERT.*'"
[ "$status" -eq 0 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "cert1 matches the regular expression (found ----BEGIN CERTIFICATE----)." ]
}


@test "trying to verify the content of a single-line value with spaces (failure)" {
DETIK_CLIENT_NAME="mytest_with_spaces"
run try "at most 1 times every 1s to get something named 'cert1' and verify that 'value' matches 'BEGIN CATE'"
[ "$status" -eq 3 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "Current value for cert1 is ----BEGIN CERTIFICATE----..." ]
}


@test "trying to verify the syntax check (invalid wording, with a pattern match)" {
run try "at most 5 times VERY 5hours to GET pods named 'nginx' and verify that 'status' matches 'RUNNING'"
[ "$status" -eq 2 ]
Expand Down

0 comments on commit 12e9013

Please sign in to comment.