diff --git a/examples/bats/test_helm_package.bats b/examples/bats/test_helm_package.bats index dc3fad6..c16501e 100755 --- a/examples/bats/test_helm_package.bats +++ b/examples/bats/test_helm_package.bats @@ -15,7 +15,7 @@ setup() { } verify_helm() { - helm template ../drupal | kubectl apply --dry-run -f - + helm template ../drupal | kubectl apply --dry-run -f - } @@ -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 ] } @@ -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 ] @@ -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 @@ -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 ] } diff --git a/examples/bats/test_kubectl_and_oc.sh b/examples/bats/test_kubectl_and_oc.sh index 4fa9b9d..b740e79 100644 --- a/examples/bats/test_kubectl_and_oc.sh +++ b/examples/bats/test_kubectl_and_oc.sh @@ -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'" diff --git a/lib/detik.bash b/lib/detik.bash index 8e26f47..01d1df2 100644 --- a/lib/detik.bash +++ b/lib/detik.bash @@ -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 @@ -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)) diff --git a/lib/utils.bash b/lib/utils.bash index 7af2738..ff6f5b1 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -63,7 +63,7 @@ detik_debug() { if [[ "$DEBUG_DETIK" == "true" ]]; then debug "$1" - fi + fi } diff --git a/tests/resources/without_lint_errors.no.run.txt b/tests/resources/without_lint_errors.no.run.txt index bed0d46..6e2a970 100644 --- a/tests/resources/without_lint_errors.no.run.txt +++ b/tests/resources/without_lint_errors.no.run.txt @@ -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' "\ diff --git a/tests/resources/without_lint_errors.run.txt b/tests/resources/without_lint_errors.run.txt index 6e45b63..13e9ced 100644 --- a/tests/resources/without_lint_errors.run.txt +++ b/tests/resources/without_lint_errors.run.txt @@ -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' "\ diff --git a/tests/test.detik.try.to.find.being.bats b/tests/test.detik.try.to.find.being.bats index fef8b1f..3124815 100755 --- a/tests/test.detik.try.to.find.being.bats +++ b/tests/test.detik.try.to.find.being.bats @@ -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'" @@ -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" diff --git a/tests/test.detik.try.to.find.matching.bats b/tests/test.detik.try.to.find.matching.bats index c73e1ae..edfef63 100755 --- a/tests/test.detik.try.to.find.matching.bats +++ b/tests/test.detik.try.to.find.matching.bats @@ -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'" @@ -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" diff --git a/tests/test.detik.try.to.verify.is.bats b/tests/test.detik.try.to.verify.is.bats index 1531883..ebc876c 100755 --- a/tests/test.detik.try.to.verify.is.bats +++ b/tests/test.detik.try.to.verify.is.bats @@ -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'" @@ -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 ] diff --git a/tests/test.detik.try.to.verify.matches.bats b/tests/test.detik.try.to.verify.matches.bats index cfbfa54..ea267c7 100755 --- a/tests/test.detik.try.to.verify.matches.bats +++ b/tests/test.detik.try.to.verify.matches.bats @@ -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'" @@ -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 ]