|
| 1 | +#!/usr/bin/env bats |
| 2 | +## |
| 3 | +# Unit tests for GitHub notifications (notify.sh). |
| 4 | +# |
| 5 | +#shellcheck disable=SC2030,SC2031,SC2034 |
| 6 | + |
| 7 | +load ../_helper.bash |
| 8 | + |
| 9 | +@test "Notify: github, pre_deployment" { |
| 10 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 11 | + |
| 12 | + app_id="123456789" |
| 13 | + |
| 14 | + declare -a STEPS=( |
| 15 | + "Started dispatching notifications." |
| 16 | + "Started GitHub notification for pre_deployment event." |
| 17 | + "@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\":\"existingbranch\", \"environment\": \"PR\", \"auto_merge\": false, \"required_contexts\": []} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}" |
| 18 | + "Marked deployment as started." |
| 19 | + "Finished GitHub notification for pre_deployment event." |
| 20 | + "Finished dispatching notifications." |
| 21 | + ) |
| 22 | + |
| 23 | + mocks="$(run_steps "setup")" |
| 24 | + |
| 25 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 26 | + export VORTEX_NOTIFY_EVENT="pre_deployment" |
| 27 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 28 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 29 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 30 | + run ./scripts/vortex/notify.sh |
| 31 | + assert_success |
| 32 | + |
| 33 | + run_steps "assert" "${mocks[@]}" |
| 34 | + |
| 35 | + popd >/dev/null || exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +@test "Notify: github, pre_deployment, PR" { |
| 39 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 40 | + |
| 41 | + app_id="123456789" |
| 42 | + |
| 43 | + declare -a STEPS=( |
| 44 | + "Started dispatching notifications." |
| 45 | + "Started GitHub notification for pre_deployment event." |
| 46 | + "@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\":\"existingbranch\", \"environment\": \"PR-123\", \"auto_merge\": false, \"required_contexts\": []} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}" |
| 47 | + "Marked deployment as started." |
| 48 | + "Finished GitHub notification for pre_deployment event." |
| 49 | + "Finished dispatching notifications." |
| 50 | + ) |
| 51 | + |
| 52 | + mocks="$(run_steps "setup")" |
| 53 | + |
| 54 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 55 | + export VORTEX_NOTIFY_EVENT="pre_deployment" |
| 56 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 57 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 58 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 59 | + export VORTEX_NOTIFY_PR_NUMBER="123" |
| 60 | + run ./scripts/vortex/notify.sh |
| 61 | + assert_success |
| 62 | + |
| 63 | + run_steps "assert" "${mocks[@]}" |
| 64 | + |
| 65 | + popd >/dev/null || exit 1 |
| 66 | +} |
| 67 | + |
| 68 | +@test "Notify: github, pre_deployment, longer ID" { |
| 69 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 70 | + |
| 71 | + app_id="12345678987" |
| 72 | + |
| 73 | + declare -a STEPS=( |
| 74 | + "Started dispatching notifications." |
| 75 | + "Started GitHub notification for pre_deployment event." |
| 76 | + "@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\":\"existingbranch\", \"environment\": \"PR\", \"auto_merge\": false, \"required_contexts\": []} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}" |
| 77 | + "Marked deployment as started." |
| 78 | + "Finished GitHub notification for pre_deployment event." |
| 79 | + "Finished dispatching notifications." |
| 80 | + ) |
| 81 | + |
| 82 | + mocks="$(run_steps "setup")" |
| 83 | + |
| 84 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 85 | + export VORTEX_NOTIFY_EVENT="pre_deployment" |
| 86 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 87 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 88 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 89 | + run ./scripts/vortex/notify.sh |
| 90 | + assert_success |
| 91 | + |
| 92 | + run_steps "assert" "${mocks[@]}" |
| 93 | + |
| 94 | + popd >/dev/null || exit 1 |
| 95 | +} |
| 96 | + |
| 97 | +@test "Notify: github, pre_deployment, failure" { |
| 98 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 99 | + |
| 100 | + declare -a STEPS=( |
| 101 | + "Started dispatching notifications." |
| 102 | + "Started GitHub notification for pre_deployment event." |
| 103 | + '@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":"nonexistingbranch", "environment": "PR", "auto_merge": false, "required_contexts": []} # {"message": "No ref found for: nonexistingbranch","documentation_url": "https://docs.github.com/rest/deployments/deployments#create-a-deployment","status": "422"}' |
| 104 | + "Failed to get a deployment ID for a pre_deployment operation. Payload:" |
| 105 | + "Wait for GitHub checks to finish and try again." |
| 106 | + "-Marked deployment as finished." |
| 107 | + ) |
| 108 | + |
| 109 | + mocks="$(run_steps "setup")" |
| 110 | + |
| 111 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 112 | + export VORTEX_NOTIFY_EVENT="pre_deployment" |
| 113 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 114 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 115 | + export VORTEX_NOTIFY_BRANCH="nonexistingbranch" |
| 116 | + run ./scripts/vortex/notify.sh |
| 117 | + assert_failure |
| 118 | + |
| 119 | + run_steps "assert" "${mocks[@]}" |
| 120 | + |
| 121 | + popd >/dev/null || exit 1 |
| 122 | +} |
| 123 | + |
| 124 | +@test "Notify: github, post_deployment" { |
| 125 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 126 | + |
| 127 | + app_id="123456789" |
| 128 | + mock_curl=$(mock_command "curl") |
| 129 | + |
| 130 | + declare -a STEPS=( |
| 131 | + "Started dispatching notifications." |
| 132 | + "Started GitHub notification for post_deployment event." |
| 133 | + "@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]" |
| 134 | + "@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}" |
| 135 | + "Marked deployment as finished." |
| 136 | + "Finished GitHub notification for post_deployment event." |
| 137 | + "Finished dispatching notifications." |
| 138 | + ) |
| 139 | + mocks="$(run_steps "setup")" |
| 140 | + |
| 141 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 142 | + export VORTEX_NOTIFY_EVENT="post_deployment" |
| 143 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 144 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 145 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 146 | + export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com" |
| 147 | + run ./scripts/vortex/notify.sh |
| 148 | + assert_success |
| 149 | + |
| 150 | + run_steps "assert" "${mocks[@]}" |
| 151 | + |
| 152 | + popd >/dev/null || exit 1 |
| 153 | +} |
| 154 | + |
| 155 | +@test "Notify: github, post_deployment, longer ID" { |
| 156 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 157 | + |
| 158 | + app_id="12345678987" |
| 159 | + mock_curl=$(mock_command "curl") |
| 160 | + |
| 161 | + declare -a STEPS=( |
| 162 | + "Started dispatching notifications." |
| 163 | + "Started GitHub notification for post_deployment event." |
| 164 | + "@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]" |
| 165 | + "@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}" |
| 166 | + "Marked deployment as finished." |
| 167 | + "Finished GitHub notification for post_deployment event." |
| 168 | + "Finished dispatching notifications." |
| 169 | + ) |
| 170 | + mocks="$(run_steps "setup")" |
| 171 | + |
| 172 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 173 | + export VORTEX_NOTIFY_EVENT="post_deployment" |
| 174 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 175 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 176 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 177 | + export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com" |
| 178 | + run ./scripts/vortex/notify.sh |
| 179 | + assert_success |
| 180 | + |
| 181 | + run_steps "assert" "${mocks[@]}" |
| 182 | + |
| 183 | + popd >/dev/null || exit 1 |
| 184 | +} |
| 185 | + |
| 186 | +@test "Notify: github, post_deployment, failure to get id" { |
| 187 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 188 | + |
| 189 | + mock_curl=$(mock_command "curl") |
| 190 | + |
| 191 | + declare -a STEPS=( |
| 192 | + "Started dispatching notifications." |
| 193 | + "Started GitHub notification for post_deployment event." |
| 194 | + "@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=nonexistingbranch # []" |
| 195 | + "Failed to get a deployment ID for a post_deployment operation. Payload:" |
| 196 | + "Check that a pre_deployment notification was dispatched." |
| 197 | + "-Marked deployment as finished." |
| 198 | + ) |
| 199 | + mocks="$(run_steps "setup")" |
| 200 | + |
| 201 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 202 | + export VORTEX_NOTIFY_EVENT="post_deployment" |
| 203 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 204 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 205 | + export VORTEX_NOTIFY_BRANCH="nonexistingbranch" |
| 206 | + export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com" |
| 207 | + run ./scripts/vortex/notify.sh |
| 208 | + assert_failure |
| 209 | + |
| 210 | + run_steps "assert" "${mocks[@]}" |
| 211 | + |
| 212 | + popd >/dev/null || exit 1 |
| 213 | +} |
| 214 | + |
| 215 | +@test "Notify: github, post_deployment, failure to set status" { |
| 216 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 217 | + |
| 218 | + app_id="12345678987" |
| 219 | + mock_curl=$(mock_command "curl") |
| 220 | + |
| 221 | + declare -a STEPS=( |
| 222 | + "Started dispatching notifications." |
| 223 | + "Started GitHub notification for post_deployment event." |
| 224 | + "@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]" |
| 225 | + "@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"notsuccess\", \"othervar\": \"54321\"}" |
| 226 | + "Previous deployment was found, but was unable to update the deployment status. Payload:" |
| 227 | + "-Marked deployment as finished." |
| 228 | + ) |
| 229 | + mocks="$(run_steps "setup")" |
| 230 | + |
| 231 | + export VORTEX_NOTIFY_CHANNELS="github" |
| 232 | + export VORTEX_NOTIFY_EVENT="post_deployment" |
| 233 | + export VORTEX_NOTIFY_GITHUB_TOKEN="token12345" |
| 234 | + export VORTEX_NOTIFY_GITHUB_REPOSITORY="myorg/myrepo" |
| 235 | + export VORTEX_NOTIFY_BRANCH="existingbranch" |
| 236 | + export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com" |
| 237 | + run ./scripts/vortex/notify.sh |
| 238 | + assert_failure |
| 239 | + |
| 240 | + run_steps "assert" "${mocks[@]}" |
| 241 | + |
| 242 | + popd >/dev/null || exit 1 |
| 243 | +} |
0 commit comments