Skip to content

Commit 8441240

Browse files
nan-yuthockin
authored andcommitted
Enable the GitHub app e2e test
The GitHub app e2e test requires a GitHub app to be created and installed, and also requires a few environment variables to be set. This commit updates the GitHub action workflow by providing the environment variables which can be set via GitHub Secret. GitHub Secrests cannot start with `GITHUB_`. Hence, this commit prepends `TEST_` to the env variables. It also updates how GitHub app private key file is set. It can be set by either `TEST_GITHUB_APP_PRIVATE_KEY` or `TEST_GITHUB_APP_PRIVATE_KEY_FILE`.
1 parent 0e2e0f0 commit 8441240

File tree

3 files changed

+88
-35
lines changed

3 files changed

+88
-35
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ jobs:
4444

4545
- name: make test
4646
working-directory: git-sync
47+
env:
48+
SKIP_GITHUB_APP_TEST: false
49+
TEST_GITHUB_APP_APPLICATION_ID: ${{ secrets.TEST_GITHUB_APP_APPLICATION_ID }}
50+
TEST_GITHUB_APP_AUTH_TEST_REPO: ${{ secrets.TEST_GITHUB_APP_AUTH_TEST_REPO }}
51+
TEST_GITHUB_APP_CLIENT_ID: ${{ secrets.TEST_GITHUB_APP_CLIENT_ID }}
52+
TEST_GITHUB_APP_INSTALLATION_ID: ${{ secrets.TEST_GITHUB_APP_INSTALLATION_ID }}
53+
TEST_GITHUB_APP_PRIVATE_KEY: ${{ secrets.TEST_GITHUB_APP_PRIVATE_KEY }}
4754
run: |
4855
make test
4956

docs/dev/testing_github_app_auth.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@ Go to https://github.com/settings/apps/new
1515

1616
## Step 2: Export the necessary environment variables
1717

18-
The following environment variables are *required* to run the git-sync github app auth tests:
19-
- `GITHUB_APP_PRIVATE_KEY`
20-
- `GITHUB_APP_APPLICATION_ID`
21-
- `GITHUB_APP_CLIENT_ID`
22-
- `GITHUB_APP_INSTALLATION_ID`
23-
- `GITHUB_APP_AUTH_TEST_REPO`
18+
The following environment variables are *required* to run the git-sync GitHub app auth tests:
19+
- `TEST_GITHUB_APP_PRIVATE_KEY` or `TEST_GITHUB_APP_PRIVATE_KEY_FILE`. If both are set, `TEST_GITHUB_APP_PRIVATE_KEY` overwrites `TEST_GITHUB_APP_PRIVATE_KEY_FILE`.
20+
- `TEST_GITHUB_APP_APPLICATION_ID`
21+
- `TEST_GITHUB_APP_CLIENT_ID`
22+
- `TEST_GITHUB_APP_INSTALLATION_ID`
23+
- `TEST_GITHUB_APP_AUTH_TEST_REPO`
2424

25-
### GITHUB_APP_PRIVATE_KEY
26-
Should have been saved when creating the app
25+
### TEST_GITHUB_APP_PRIVATE_KEY
26+
The content of the GitHub app's private key file. It should have been saved when creating the app.
27+
If `TEST_GITHUB_APP_PRIVATE_KEY_FILE` is also set, it overwrites the file with the content.
28+
Otherwise, it saves the content to `/tmp/git-sync-e2e.random-id/github_app_private_key.pem`.
2729

28-
### GITHUB_APP_APPLICATION_ID
29-
The value after "App ID" in the app's settings page
30+
### TEST_GITHUB_APP_PRIVATE_KEY_FILE
31+
The absolute path to the file that stores the GitHub app's private key file. It should have been saved when creating the app.
3032

31-
### GITHUB_APP_CLIENT_ID
32-
The value after "Client ID" in the app's settings page
33+
### TEST_GITHUB_APP_APPLICATION_ID
34+
The value after "App ID" in the app's settings page.
3335

34-
### GITHUB_APP_INSTALLATION_ID
36+
### TEST_GITHUB_APP_CLIENT_ID
37+
The value after "Client ID" in the app's settings page.
38+
39+
### TEST_GITHUB_APP_INSTALLATION_ID
3540
Found in the URL of the app's installation page if you installed it to a repository: https://github.com/settings/installations/<installation_id>
3641

37-
### GITHUB_APP_AUTH_TEST_REPO
38-
Should be set to the repository that the github app is installed to.
42+
### TEST_GITHUB_APP_AUTH_TEST_REPO.
43+
Should be set to the repository that the GitHub app is installed to.

test_e2e.sh

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,48 @@ fi
194194
RUNID="${RANDOM}${RANDOM}"
195195
DIR="/tmp/git-sync-e2e.$RUNID"
196196
mkdir "$DIR"
197+
function final_cleanup() {
198+
if [[ "${CLEANUP:-}" == 0 ]]; then
199+
echo "leaving logs in $DIR"
200+
else
201+
rm -rf "$DIR"
202+
fi
203+
}
204+
# Set the trap to call the final_cleanup function on exit.
205+
trap final_cleanup EXIT
206+
207+
skip_github_app_test="${SKIP_GITHUB_APP_TEST:-false}"
208+
required_env_vars=()
209+
LOCAL_GITHUB_APP_PRIVATE_KEY_FILE="github_app_private_key.pem"
210+
GITHUB_APP_PRIVATE_KEY_MOUNT=""
211+
if [[ "${skip_github_app_test}" != "true" ]]; then
212+
required_env_vars=(
213+
"TEST_GITHUB_APP_AUTH_TEST_REPO"
214+
"TEST_GITHUB_APP_APPLICATION_ID"
215+
"TEST_GITHUB_APP_INSTALLATION_ID"
216+
"TEST_GITHUB_APP_CLIENT_ID"
217+
"TEST_GITHUB_APP_PRIVATE_KEY_FILE"
218+
)
219+
220+
# TEST_GITHUB_APP_PRIVATE_KEY, if set, overrides TEST_GITHUB_APP_PRIVATE_KEY_FILE
221+
if [[ -v TEST_GITHUB_APP_PRIVATE_KEY && -n "${TEST_GITHUB_APP_PRIVATE_KEY}" ]]; then
222+
if [[ ! -v TEST_GITHUB_APP_PRIVATE_KEY_FILE || -z "${TEST_GITHUB_APP_PRIVATE_KEY_FILE}" ]]; then
223+
TEST_GITHUB_APP_PRIVATE_KEY_FILE="${DIR}/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}"
224+
fi
225+
echo "${TEST_GITHUB_APP_PRIVATE_KEY}" > "${TEST_GITHUB_APP_PRIVATE_KEY_FILE}"
226+
fi
227+
228+
# Validate all required environment variables for the github-app-auth tests are provided.
229+
for var in "${required_env_vars[@]}"; do
230+
if [[ ! -v "${var}" ]]; then
231+
echo "Error: Required environment variable '${var}' is not set or empty. Either provide a value or skip the GitHub App test by setting SKIP_GITHUB_APP_TEST to 'true'."
232+
exit 1
233+
fi
234+
done
235+
236+
# Mount the GitHub App private key file to the git-sync container
237+
GITHUB_APP_PRIVATE_KEY_MOUNT=(-v "${TEST_GITHUB_APP_PRIVATE_KEY_FILE}":"/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}":ro)
238+
fi
197239

198240
# WORK is temp space and in reset for each testcase.
199241
WORK="$DIR/work"
@@ -295,7 +337,7 @@ function GIT_SYNC() {
295337
-v "$DOT_SSH/1/id_test":"/ssh/secret.1":ro \
296338
-v "$DOT_SSH/2/id_test":"/ssh/secret.2":ro \
297339
-v "$DOT_SSH/3/id_test":"/ssh/secret.3":ro \
298-
-v "$(pwd)/$GITHUB_APP_PRIVATE_KEY_FILE":"/github_app_private_key.pem":ro \
340+
"${GITHUB_APP_PRIVATE_KEY_MOUNT[@]}" \
299341
"${GIT_SYNC_E2E_IMAGE}" \
300342
-v=6 \
301343
--add-user \
@@ -2189,27 +2231,33 @@ function e2e::auth_askpass_url_slow_start() {
21892231
# Test github app auth
21902232
##############################################
21912233
function e2e::auth_github_app_application_id() {
2234+
if [[ "${skip_github_app_test}" == "true" ]]; then
2235+
return
2236+
fi
21922237
GIT_SYNC \
21932238
--one-time \
2194-
--repo="$GITHUB_APP_AUTH_TEST_REPO" \
2195-
--github-app-application-id "$GITHUB_APP_APPLICATION_ID" \
2196-
--github-app-installation-id "$GITHUB_APP_INSTALLATION_ID" \
2197-
--github-app-private-key-file "/github_app_private_key.pem" \
2198-
--root="$ROOT" \
2239+
--repo="${TEST_GITHUB_APP_AUTH_TEST_REPO}" \
2240+
--github-app-application-id "${TEST_GITHUB_APP_APPLICATION_ID}" \
2241+
--github-app-installation-id "${TEST_GITHUB_APP_INSTALLATION_ID}" \
2242+
--github-app-private-key-file "/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}" \
2243+
--root="${ROOT}" \
21992244
--link="link"
2200-
assert_file_exists "$ROOT/link/LICENSE"
2245+
assert_file_exists "${ROOT}/link/LICENSE"
22012246
}
22022247

22032248
function e2e::auth_github_app_client_id() {
2249+
if [[ "${skip_github_app_test}" == "true" ]]; then
2250+
return
2251+
fi
22042252
GIT_SYNC \
22052253
--one-time \
2206-
--repo="$GITHUB_APP_AUTH_TEST_REPO" \
2207-
--github-app-client-id "$GITHUB_APP_CLIENT_ID" \
2208-
--github-app-installation-id "$GITHUB_APP_INSTALLATION_ID" \
2209-
--github-app-private-key-file "/github_app_private_key.pem" \
2210-
--root="$ROOT" \
2254+
--repo="${TEST_GITHUB_APP_AUTH_TEST_REPO}" \
2255+
--github-app-client-id "${TEST_GITHUB_APP_CLIENT_ID}" \
2256+
--github-app-installation-id "${TEST_GITHUB_APP_INSTALLATION_ID}" \
2257+
--github-app-private-key-file "/${LOCAL_GITHUB_APP_PRIVATE_KEY_FILE}" \
2258+
--root="${ROOT}" \
22112259
--link="link"
2212-
assert_file_exists "$ROOT/link/LICENSE"
2260+
assert_file_exists "${ROOT}/link/LICENSE"
22132261
}
22142262

22152263
##############################################
@@ -3640,11 +3688,4 @@ if [[ "$FINAL_RET" != 0 ]]; then
36403688
exit 1
36413689
fi
36423690

3643-
# Finally...
3644-
echo
3645-
if [[ "${CLEANUP:-}" == 0 ]]; then
3646-
echo "leaving logs in $DIR"
3647-
else
3648-
rm -rf "$DIR"
3649-
fi
36503691

0 commit comments

Comments
 (0)