|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import lambda_function |
| 5 | +from lambda_function import download_log, installation_token |
| 6 | + |
| 7 | + |
| 8 | +def make_response(status_code, content=b"log data", headers=None): |
| 9 | + response = MagicMock() |
| 10 | + response.status_code = status_code |
| 11 | + response.ok = 200 <= status_code < 300 |
| 12 | + response.content = content |
| 13 | + response.headers = headers or {} |
| 14 | + return response |
| 15 | + |
| 16 | + |
| 17 | +class TestInstallationToken(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + lambda_function._token_cache.clear() |
| 20 | + |
| 21 | + @patch.object(lambda_function, "GITHUB_APP_ID", None) |
| 22 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", None) |
| 23 | + def test_returns_none_without_app_credentials(self): |
| 24 | + with patch.object(lambda_function, "fetch_installation_token") as fetch: |
| 25 | + self.assertIsNone(installation_token("pytorch/pytorch")) |
| 26 | + fetch.assert_not_called() |
| 27 | + |
| 28 | + @patch.object(lambda_function, "GITHUB_APP_ID", "4550824") |
| 29 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", "key") |
| 30 | + def test_token_is_cached_per_owner(self): |
| 31 | + with patch.object( |
| 32 | + lambda_function, |
| 33 | + "fetch_installation_token", |
| 34 | + return_value=("tok", 2**31), |
| 35 | + ) as fetch: |
| 36 | + self.assertEqual(installation_token("pytorch/pytorch"), "tok") |
| 37 | + # Same owner, different repo: served from cache, no second mint |
| 38 | + self.assertEqual(installation_token("pytorch/executorch"), "tok") |
| 39 | + fetch.assert_called_once() |
| 40 | + |
| 41 | + @patch.object(lambda_function, "GITHUB_APP_ID", "4550824") |
| 42 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", "key") |
| 43 | + def test_expired_cache_entry_is_refreshed(self): |
| 44 | + lambda_function._token_cache["pytorch"] = ("stale", 0) |
| 45 | + with patch.object( |
| 46 | + lambda_function, |
| 47 | + "fetch_installation_token", |
| 48 | + return_value=("fresh", 2**31), |
| 49 | + ): |
| 50 | + self.assertEqual(installation_token("pytorch/pytorch"), "fresh") |
| 51 | + |
| 52 | + @patch.object(lambda_function, "GITHUB_APP_ID", "4550824") |
| 53 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", "key") |
| 54 | + def test_uninstalled_owner_is_cached_as_none(self): |
| 55 | + with patch.object( |
| 56 | + lambda_function, |
| 57 | + "fetch_installation_token", |
| 58 | + return_value=(None, 2**31), |
| 59 | + ) as fetch: |
| 60 | + self.assertIsNone(installation_token("vllm-project/vllm")) |
| 61 | + self.assertIsNone(installation_token("vllm-project/vllm")) |
| 62 | + fetch.assert_called_once() |
| 63 | + |
| 64 | + @patch.object(lambda_function, "GITHUB_APP_ID", "4550824") |
| 65 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", "key") |
| 66 | + def test_mint_failure_is_not_cached(self): |
| 67 | + with patch.object( |
| 68 | + lambda_function, |
| 69 | + "fetch_installation_token", |
| 70 | + side_effect=lambda_function.requests.RequestException("boom"), |
| 71 | + ): |
| 72 | + self.assertIsNone(installation_token("pytorch/pytorch")) |
| 73 | + self.assertNotIn("pytorch", lambda_function._token_cache) |
| 74 | + |
| 75 | + @patch.object(lambda_function, "GITHUB_APP_ID", "4550824") |
| 76 | + @patch.object(lambda_function, "GITHUB_APP_PRIVATE_KEY", "bm90LWEta2V5") |
| 77 | + def test_unparseable_private_key_degrades_to_none(self): |
| 78 | + # A misconfigured key must not escape as an exception: the webhook still |
| 79 | + # has to archive its payload after the log download is skipped. |
| 80 | + self.assertIsNone(installation_token("pytorch/pytorch")) |
| 81 | + |
| 82 | + |
| 83 | +@patch.object(lambda_function, "GITHUB_TOKENS", "pat1,pat2") |
| 84 | +@patch.object(lambda_function, "s3") |
| 85 | +@patch.object(lambda_function, "urlopen") |
| 86 | +class TestDownloadLog(unittest.TestCase): |
| 87 | + def setUp(self): |
| 88 | + lambda_function._token_cache.clear() |
| 89 | + |
| 90 | + def test_uses_app_token_when_available(self, urlopen, s3): |
| 91 | + with patch.object( |
| 92 | + lambda_function, "installation_token", return_value="app-token" |
| 93 | + ), patch.object( |
| 94 | + lambda_function, "fetch_log", return_value=make_response(200) |
| 95 | + ) as fetch_log: |
| 96 | + download_log("pytorch/pytorch", "failure", 123) |
| 97 | + |
| 98 | + fetch_log.assert_called_once_with("pytorch/pytorch", 123, "app-token") |
| 99 | + s3.Object.assert_called_once_with("ossci-raw-job-status", "log/123") |
| 100 | + urlopen.assert_called_once() |
| 101 | + |
| 102 | + def test_falls_back_to_pat_when_rate_limited(self, urlopen, s3): |
| 103 | + responses = [ |
| 104 | + make_response(403, headers={"x-ratelimit-remaining": "0"}), |
| 105 | + make_response(200), |
| 106 | + ] |
| 107 | + with patch.object( |
| 108 | + lambda_function, "installation_token", return_value="app-token" |
| 109 | + ), patch.object( |
| 110 | + lambda_function, "fetch_log", side_effect=responses |
| 111 | + ) as fetch_log: |
| 112 | + download_log("pytorch/pytorch", "failure", 123) |
| 113 | + |
| 114 | + self.assertEqual(fetch_log.call_count, 2) |
| 115 | + self.assertIn(fetch_log.call_args_list[1][0][2], ("pat1", "pat2")) |
| 116 | + # The log still gets archived via the fallback credential |
| 117 | + s3.Object.assert_called_once_with("ossci-raw-job-status", "log/123") |
| 118 | + |
| 119 | + def test_rate_limited_app_is_skipped_until_reset(self, urlopen, s3): |
| 120 | + reset_at = 2**31 |
| 121 | + responses = [ |
| 122 | + make_response(429, headers={"x-ratelimit-reset": str(reset_at)}), |
| 123 | + make_response(200), |
| 124 | + make_response(200), |
| 125 | + ] |
| 126 | + with patch.object( |
| 127 | + lambda_function, "fetch_installation_token", return_value=("app", 2**31) |
| 128 | + ), patch.object(lambda_function, "GITHUB_APP_ID", "4550824"), patch.object( |
| 129 | + lambda_function, "GITHUB_APP_PRIVATE_KEY", "key" |
| 130 | + ), patch.object( |
| 131 | + lambda_function, "fetch_log", side_effect=responses |
| 132 | + ) as fetch_log: |
| 133 | + download_log("pytorch/pytorch", "failure", 123) |
| 134 | + download_log("pytorch/pytorch", "failure", 456) |
| 135 | + |
| 136 | + # 2 calls for the first job (app then PAT), 1 for the second (PAT only) |
| 137 | + self.assertEqual(fetch_log.call_count, 3) |
| 138 | + self.assertEqual( |
| 139 | + lambda_function._token_cache["pytorch"], (None, float(reset_at)) |
| 140 | + ) |
| 141 | + |
| 142 | + def test_error_response_is_not_archived(self, urlopen, s3): |
| 143 | + with patch.object( |
| 144 | + lambda_function, "installation_token", return_value=None |
| 145 | + ), patch.object( |
| 146 | + lambda_function, |
| 147 | + "fetch_log", |
| 148 | + return_value=make_response(404, content=b'{"message": "Not Found"}'), |
| 149 | + ): |
| 150 | + download_log("pytorch/pytorch", "skipped", 123) |
| 151 | + |
| 152 | + s3.Object.assert_not_called() |
| 153 | + urlopen.assert_not_called() |
| 154 | + |
| 155 | + def test_non_pytorch_repo_is_prefixed(self, urlopen, s3): |
| 156 | + with patch.object( |
| 157 | + lambda_function, "installation_token", return_value=None |
| 158 | + ), patch.object(lambda_function, "fetch_log", return_value=make_response(200)): |
| 159 | + download_log("pytorch/executorch", "success", 999) |
| 160 | + |
| 161 | + s3.Object.assert_called_once_with( |
| 162 | + "ossci-raw-job-status", "log/pytorch/executorch/999" |
| 163 | + ) |
| 164 | + |
| 165 | + def test_no_credentials_at_all_is_a_noop(self, urlopen, s3): |
| 166 | + with patch.object( |
| 167 | + lambda_function, "installation_token", return_value=None |
| 168 | + ), patch.object(lambda_function, "GITHUB_TOKENS", None), patch.object( |
| 169 | + lambda_function, "fetch_log" |
| 170 | + ) as fetch_log: |
| 171 | + download_log("pytorch/pytorch", "failure", 123) |
| 172 | + |
| 173 | + fetch_log.assert_not_called() |
| 174 | + s3.Object.assert_not_called() |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + unittest.main() |
0 commit comments