Skip to content

Commit

Permalink
Merge pull request #30 from bats-core/vz
Browse files Browse the repository at this point in the history
Review assertions without an expected count
  • Loading branch information
vincent-zurczak authored Jul 21, 2021
2 parents 0925f14 + 48b29b4 commit e3ea96e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,15 @@ with a given property value. Example: count the number of PODs with a given name
Hence this additional syntax.

```bash
# Expecting 0 or 1 instance
try "at most <number> times every <number>s \
to find <0 or 1> <resource-type> named '<regular-expression>' \
with '<property-name>' being '<expected-value>'"

# Expecting more than 1 instance
# Expecting a given number of instances
try "at most <number> times every <number>s \
to find <number> <resource-type> named '<regular-expression>' \
with '<property-name>' being '<expected-value>'"
```

This is a checking loop.
It breaks the loop if as soon as the assertion is verified. If it reaches the end of the loop
with having been verified, an error is thrown. Please, refer to [this section](#property-names) for details
without having been verified, an error is thrown. Please, refer to [this section](#property-names) for details
about the property names.

This assertion is useful for PODs, whose life cycle changes take time.
Expand All @@ -318,11 +313,11 @@ try "at most <number> times every <number>s \

This is a checking loop.
It breaks the loop if as soon as the assertion is verified. If it reaches the end of the loop
with having been verified, an error is thrown. Please, refer to [this section](#property-names) for details
without having been verified, an error is thrown. Please, refer to [this section](#property-names) for details
about the property names.

This assertion verifies all the instances have this property value.
But unlike the assertion type to count resources, you do not verify how many instances have this value.
But unlike the assertion type to count resources, you do not verify how many instances have this value. Notice however that **if it finds 0 item verifying the property, the assertion fails**.


### Property Names
Expand Down
3 changes: 3 additions & 0 deletions lib/detik.bash
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ verify_value() {
else
invalid=0
fi
# If no count expected, raise an error when 0 found items
elif [[ "$valid" == "0" ]]; then
invalid=102
fi

return $invalid
Expand Down
4 changes: 2 additions & 2 deletions tests/test.detik.try.to.verify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mytest() {

@test "trying to verify the status of a POD with an invalid name" {
run try "at most 1 times every 1s to get pods named 'nginx-something' and verify that 'status' is 'running'"
[ "$status" -eq 0 ]
[ "$status" -eq 3 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "No resource of type 'pods' was found with the name 'nginx-something'." ]
Expand All @@ -129,7 +129,7 @@ mytest() {

@test "trying to verify the status of a POD with an invalid pattern name" {
run try "at most 1 times every 1s to get pods named 'ngin.+x' and verify that 'status' is 'running'"
[ "$status" -eq 0 ]
[ "$status" -eq 3 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "No resource of type 'pods' was found with the name 'ngin.+x'." ]
Expand Down
4 changes: 2 additions & 2 deletions tests/test.detik.verify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mytest_with_namespace() {

@test "verifying the status of a POD with an invalid name" {
run verify "'status' is 'running' for pods named 'nginx-something'"
[ "$status" -eq 0 ]
[ "$status" -eq 3 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "No resource of type 'pods' was found with the name 'nginx-something'." ]
Expand All @@ -343,7 +343,7 @@ mytest_with_namespace() {

@test "verifying the status of a POD with an invalid pattern name" {
run verify "'status' is 'running' for pods named 'ngin.+x'"
[ "$status" -eq 0 ]
[ "$status" -eq 3 ]
[ ${#lines[@]} -eq 2 ]
[ "${lines[0]}" = "Valid expression. Verification in progress..." ]
[ "${lines[1]}" = "No resource of type 'pods' was found with the name 'ngin.+x'." ]
Expand Down

0 comments on commit e3ea96e

Please sign in to comment.