Skip to content

Commit

Permalink
add test cases for validation process
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Oct 31, 2024
1 parent bc0367b commit 7a0d890
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "status": "OK", "version": "v2.999.0", "sha": "1234567890ABCDEF" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "status": "OK", "version": "undefined", "sha": "undefined"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Invalid Response Format"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "status": "OK", "version": "v2.999.0", "sha": "BAD_SHA" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "status": "BAD_STATUS", "version": "v2.999.0", "sha": "1234567890ABCDEF" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "status": "OK", "version": "v1.0.0-BAD", "sha": "1234567890ABCDEF" }
9 changes: 9 additions & 0 deletions tests/scripts/app-workflows/test-fetch-deploy-info.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bats

load 'test_helpers'

@test "Test bogus URL" {
run ./scripts/app-workflows/fetch-deploy-info.sh "http://localhost/bogus-url"
test_value 1 "$status"
test_value "Failed to fetch the verification URL: http://localhost/bogus-url" "${lines[0]}"
}
53 changes: 53 additions & 0 deletions tests/scripts/app-workflows/test-validate-deploy.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bats

load 'test_helpers'

@test "Test bad status" {
fixture=$(load_json_fixture "validate-status-error.json")
run ./scripts/app-workflows/validate-deploy.sh $fixture "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Validation failed: status is not OK" "${lines[0]}"
test_value "Expected: OK, Actual: BAD_STATUS" "${lines[1]}"
}

@test "Test incorrect version" {
fixture=$(load_json_fixture "validate-version-error.json")
run ./scripts/app-workflows/validate-deploy.sh $fixture "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Validation failed: version mismatch" "${lines[0]}"
test_value "Expected: v2.999.0, Actual: v1.0.0-BAD" "${lines[1]}"
}

@test "Test incorrect sha" {
fixture=$(load_json_fixture "validate-sha-error.json")
run ./scripts/app-workflows/validate-deploy.sh $fixture "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Validation failed: sha mismatch" "${lines[0]}"
test_value "Expected: 1234567890ABCDEF, Actual: BAD_SHA" "${lines[1]}"
}

@test "Test valid response" {
fixture=$(load_json_fixture "validate-good-response.json")
run ./scripts/app-workflows/validate-deploy.sh $fixture "v2.999.0" "1234567890ABCDEF"
test_value 0 "$status"
}

@test "Test non-JSON response" {
fixture=$(load_json_fixture "validate-non-json-response.json")
run ./scripts/app-workflows/validate-deploy.sh '$fixture' "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Error: Invalid JSON string" "${lines[0]}"
}

@test "Test invalid JSON response" {
fixture=$(load_json_fixture "validate-invalid-json-response.json")
run ./scripts/app-workflows/validate-deploy.sh '$fixture' "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Error: Invalid JSON string" "${lines[0]}"
}

@test "Test empty response" {
run ./scripts/app-workflows/validate-deploy.sh "" "v2.999.0" "1234567890ABCDEF"
test_value 1 "$status"
test_value "Error: Invalid JSON string" "${lines[0]}"
}
31 changes: 30 additions & 1 deletion tests/scripts/app-workflows/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,34 @@
test_value() {
local expected="$1"
local actual="$2"
diff <(echo "$actual") <(echo "$expected") || { echo -e "expected: $expected\nactual: '$actual'"; return 1; }
diff <(echo "$actual") <(echo "$expected") || { echo -e "expected: '$expected'\nactual: '$actual'"; return 1; }
}

load_json_fixture() {
local fixture_filename="$1"
fixtures_dir="./tests/scripts/app-workflows/fixtures"
fixture_fullpath="$fixtures_dir/$fixture_filename"

run cat "$fixture_fullpath"
fixture=$output

# Validate and reformat the fixture as a JSON string
if echo "$fixture" | jq empty > /dev/null 2>&1; then
# If valid, reformat the JSON string
fixture_json=$(echo "$fixture" | jq -c .)
else
# Let bad data through for testing
fixture_json=$fixture
# exit 1
fi

echo "$fixture_json"
}

script_fullpath() {
local script_filename="$1"
scripts_dir="./scripts/app-workflows"
fullpath="$scripts_dir/$script_filename"

echo "$fullpath"
}

0 comments on commit 7a0d890

Please sign in to comment.