Skip to content

Commit

Permalink
fix: simplify check module
Browse files Browse the repository at this point in the history
The `check` module now accepts `command` and `args`, which is more
familiar than `program`. It also returns `exitcode`, `error` and
`output`, which are useful for debugging.
  • Loading branch information
jta committed Oct 18, 2023
1 parent 0128454 commit b574a46
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 37 deletions.
3 changes: 2 additions & 1 deletion integration/scripts/check_bucket_not_empty
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -euo pipefail

DIE() { echo "$*" 1>&2; exit 1; }

[[ ! -z "${SOURCE}" ]] || DIE "source not set"
[[ ! -z "${SOURCE:-}" ]] || DIE "source not set"

echo "listing ${SOURCE}"
[[ ! -z $(aws s3 ls ${SOURCE}) ]] || DIE "bucket is empty"
27 changes: 6 additions & 21 deletions integration/scripts/check_object_copy
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,9 @@ TMPNAME=$(basename ${TMPFILE})

echo "{\"hello\": \"world\"}" > "$TMPFILE"

aws s3 cp ${TMPFILE} s3://${SOURCE} --content-type application/json 1>&2
if [ $? -ne 0 ]; then
DIE "failed to copy file to source"
fi

ORIGINAL=$(aws s3api head-object --bucket ${SOURCE} --key ${TMPNAME} | jq 'del(.LastModified)')
if [ $? -ne 0 ]; then
DIE "failed to read file from source"
fi


COPY=$(aws s3api head-object --bucket ${DESTINATION} --key ${TMPNAME} | jq 'del(.LastModified)')
if [ $? -ne 0 ]; then
DIE "failed to read file from destination"
fi

if [ "$ORIGINAL" = "$COPY" ]; then
echo "ok"
else
DIE "object differs"
fi
aws s3 cp ${TMPFILE} s3://${SOURCE} --content-type application/json 1>&2 || DIE "failed to copy file to source"

ORIGINAL=$(aws s3api head-object --bucket ${SOURCE} --key ${TMPNAME} | jq 'del(.LastModified)' || DIE "failed to read file from source")
COPY=$(aws s3api head-object --bucket ${DESTINATION} --key ${TMPNAME} | jq 'del(.LastModified)' || DIE "failed to read file from destination")

[ "$ORIGINAL" = "$COPY" ] || DIE "object differs"
2 changes: 1 addition & 1 deletion integration/tests/check/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
data "external" "check" {
program = concat(["${path.module}/run"], var.program)
program = concat(["${path.module}/run"], [var.command], var.args)
query = var.env_vars
}
12 changes: 10 additions & 2 deletions integration/tests/check/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
output "result" {
value = data.external.check.result
output "error" {
value = data.external.check.result.error
}

output "exitcode" {
value = tonumber(data.external.check.result.exitcode)
}

output "output" {
value = data.external.check.result.output
}
3 changes: 2 additions & 1 deletion integration/tests/check/run
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ if [ $RESULT -ne 0 ]; then
ERROR=$(echo -n "$OUTPUT" | tail -n1 | jq -Rsa .)
fi

# terraform external data source expects all arguments to be strings
echo "{\"error\": ${ERROR}, \"exitcode\": \"${RESULT}\", \"output\": $(echo -n "$OUTPUT" | jq -Rsa .)}"
# always return 0, let caller decide if error message warrants any action
echo "{\"error\": ${ERROR}}"
18 changes: 11 additions & 7 deletions integration/tests/check/variables.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
variable "program" {
variable "command" {
description = <<-EOF
A list of strings, whose first element is the program to run and whose
subsequent elements are optional command line arguments to the program.
Terraform does not execute the program through a shell, so it is not
necessary to escape shell metacharacters nor add quotes around arguments
containing spaces.
Command to execute
EOF
type = list(string)
type = string
nullable = false
}

variable "args" {
description = <<-EOF
Command line arguments
EOF
type = list(string)
nullable = false
default = []
}
variable "env_vars" {
description = <<-EOF
Environment variables
Expand Down
23 changes: 21 additions & 2 deletions integration/tests/forwarder.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ run "install_forwarder" {
}
}

run "check_file_not_copied" {
module {
source = "./tests/check"
}

variables {
command = "./scripts/check_object_copy"
env_vars = {
SOURCE = run.setup.source.bucket
DESTINATION = run.setup.destination.bucket
}
}

assert {
condition = output.result.error == "failed to read file from destination"
error_message = "Unexpected error"
}
}

run "subscribe_bucket_notifications_to_sqs" {
module {
source = "./tests/bucket_subscription"
Expand All @@ -31,13 +50,13 @@ run "subscribe_bucket_notifications_to_sqs" {
}
}

run "check" {
run "check_copy_succeeds" {
module {
source = "./tests/check"
}

variables {
program = ["./scripts/check_object_copy"]
command = "./scripts/check_object_copy"
env_vars = {
SOURCE = run.setup.source.bucket
DESTINATION = run.setup.destination.bucket
Expand Down
4 changes: 2 additions & 2 deletions integration/tests/simple.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ run "check" {
}

variables {
program = ["./scripts/check_bucket_not_empty"]
command = "./scripts/check_bucket_not_empty"
env_vars = {
SOURCE = run.setup.source.bucket
}
}

assert {
condition = output.result.error == ""
condition = output.exitcode == 0
error_message = "Bucket not empty check failed"
}
}

0 comments on commit b574a46

Please sign in to comment.