Skip to content

Commit

Permalink
[#1213] Updated tests for notify scripts to use the step runner format.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgaunt authored Mar 14, 2024
1 parent 018bc61 commit ad5531c
Showing 1 changed file with 45 additions and 62 deletions.
107 changes: 45 additions & 62 deletions .drevops/tests/bats/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ load _helper.bash
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

app_id="123456789"
mock_curl=$(mock_command "curl")

mock_set_output "${mock_curl}" "{\"id\": \"${app_id}\", \"othervar\": \"54321\"}" 1
declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for pre_deployment event."
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"mybranch\", \"environment\": \"PR\", \"auto_merge\": false} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}"
"Marked deployment as started."
"Finished GitHub notification for pre_deployment event."
"Finished dispatching notifications."
)

mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="pre_deployment"
Expand All @@ -126,16 +134,7 @@ load _helper.bash
run ./scripts/drevops/notify.sh
assert_success

assert_output_contains "Started dispatching notifications."

assert_output_contains "Started GitHub notification for pre_deployment event."

assert_equal '-X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {"ref":"mybranch", "environment": "PR", "auto_merge": false}' "$(mock_get_call_args "${mock_curl}" 1)"

assert_output_contains "Marked deployment as started."
assert_output_contains "Finished GitHub notification for pre_deployment event."

assert_output_contains "Finished dispatching notifications."
run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}
Expand All @@ -146,8 +145,16 @@ load _helper.bash
app_id="123456789"
mock_curl=$(mock_command "curl")

mock_set_output "${mock_curl}" "[{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"987654321\", \"othervar\": \"12345\"}]" 1
mock_set_output "${mock_curl}" '{"state": "success", "othervar": "54321"}' 2
declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for post_deployment event."
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=mybranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"987654321\", \"othervar\": \"12345\"}]"
"@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/123456789/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}"
"Marked deployment as finished."
"Finished GitHub notification for post_deployment event."
"Finished dispatching notifications."
)
mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="post_deployment"
Expand All @@ -158,17 +165,7 @@ load _helper.bash
run ./scripts/drevops/notify.sh
assert_success

assert_output_contains "Started dispatching notifications."

assert_output_contains "Started GitHub notification for post_deployment event."

assert_equal "-X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=mybranch" "$(mock_get_call_args "${mock_curl}" 1)"
assert_equal '-X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/123456789/statuses -s -d {"state":"success", "environment_url": "https://develop.testproject.com"}' "$(mock_get_call_args "${mock_curl}" 2)"

assert_output_contains "Marked deployment as finished."
assert_output_contains "Finished GitHub notification for post_deployment event."

assert_output_contains "Finished dispatching notifications."
run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}
Expand All @@ -180,14 +177,28 @@ load _helper.bash
assignee_account_id="987654321c20165700ede21g"
comment_id="1234"

mock_curl=$(mock_command "curl")

mock_set_output "${mock_curl}" "{\"accountId\": \"${account_id}\", \"othervar\": \"54321\"}" 1
mock_set_output "${mock_curl}" "{\"id\": \"${comment_id}\", \"othervar\": \"54321\"}" 2
mock_set_output "${mock_curl}" '{"expand":"transitions","transitions":[{"id":"123","name":"QA"},{"id":"456","name":"Closed"}]}' 3
mock_set_output "${mock_curl}" "" 4
mock_set_output "${mock_curl}" "[{\"accountId\": \"${assignee_account_id}\", \"othervar\": \"54321\"}, {\"accountId\": \"01987654321c20165700edeg\", \"othervar\": \"54321\"}]" 5
mock_set_output "${mock_curl}" "" 6
declare -a STEPS=(
"Started dispatching notifications."
"Started JIRA notification."
"Found issue proj-1234."
"- Branch feature/proj-1234-some-description does not contain issue number."
"Checking API access."
"@curl -s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json https://jira.atlassian.com/rest/api/3/myself # {\"accountId\": \"${account_id}\", \"othervar\": \"54321\"}"
"Posting a comment."
"@curl -s -X POST -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/comment --data {\"body\": {\"type\": \"doc\", \"version\": 1, \"content\": [{\"type\": \"paragraph\", \"content\": [{\"type\": \"text\",\"text\": \"Deployed to \"},{\"type\": \"inlineCard\",\"attrs\": {\"url\": \"https://develop.testproject.com\"}}]}]}} # {\"id\": \"${comment_id}\", \"othervar\": \"54321\"}"
"Posted comment with ID ${comment_id}."
"Transitioning issue to QA"
"Discovering transition ID for QA"
"@curl -s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/transitions # {\"expand\":\"transitions\",\"transitions\":[{\"id\":\"123\",\"name\":\"QA\"},{\"id\":\"456\",\"name\":\"Closed\"}]}"
"@curl -s -X POST -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/transitions --data { \"transition\": {\"id\": \"123\"}} # "
"Transitioned issue to QA"
"Assigning issue to [email protected]"
"Discovering user ID for [email protected]"
"@curl -s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/user/assignable/[email protected]&issueKey=proj-1234 # [{\"accountId\": \"${assignee_account_id}\", \"othervar\": \"54321\"}, {\"accountId\": \"01987654321c20165700edeg\", \"othervar\": \"54321\"}]"
"@curl -s -X PUT -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/assignee --data { \"accountId\": \"987654321c20165700ede21g\"} # "
)

mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="jira"
export DREVOPS_NOTIFY_JIRA_USER="[email protected]"
Expand All @@ -199,35 +210,7 @@ load _helper.bash
run ./scripts/drevops/notify.sh
assert_success

assert_output_contains "Started dispatching notifications."

assert_output_contains "Started JIRA notification."
assert_output_contains "Found issue proj-1234."
assert_output_not_contains "Branch feature/proj-1234-some-description does not contain issue number."

# Authenticate and get an account id.
assert_output_contains "Checking API access."
assert_equal "-s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json https://jira.atlassian.com/rest/api/3/myself" "$(mock_get_call_args "${mock_curl}" 1)"

assert_output_contains "Posting a comment."
assert_equal '-s -X POST -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/comment --data {"body": {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text","text": "Deployed to "},{"type": "inlineCard","attrs": {"url": "https://develop.testproject.com"}}]}]}}' "$(mock_get_call_args "${mock_curl}" 2)"
assert_output_contains "Posted comment with ID ${comment_id}."

assert_output_contains "Transitioning issue to QA"
assert_output_contains "Discovering transition ID for QA"
assert_equal "-s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/transitions" "$(mock_get_call_args "${mock_curl}" 3)"
assert_equal '-s -X POST -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/transitions --data { "transition": {"id": "123"}}' "$(mock_get_call_args "${mock_curl}" 4)"
assert_output_contains "Transitioned issue to QA"

assert_output_contains "Assigning issue to [email protected]"
assert_output_contains "Discovering user ID for [email protected]"
assert_equal "-s -X GET -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/user/assignable/[email protected]&issueKey=proj-1234" "$(mock_get_call_args "${mock_curl}" 5)"
assert_equal '-s -X PUT -H Authorization: Basic am9obi5kb2VAZXhhbXBsZS5jb206dG9rZW4xMjM0NQ== -H Content-Type: application/json --url https://jira.atlassian.com/rest/api/3/issue/proj-1234/assignee --data { "accountId": "987654321c20165700ede21g"}' "$(mock_get_call_args "${mock_curl}" 6)"
assert_output_contains "Assigned issue to [email protected]"

assert_output_contains "Finished JIRA notification."

assert_output_contains "Finished dispatching notifications."
run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

0 comments on commit ad5531c

Please sign in to comment.