|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for github.py.""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | +from airflow.exceptions import AirflowFailException |
| 20 | +from xlml.utils import github |
| 21 | + |
| 22 | + |
| 23 | +class GithubTest(unittest.TestCase): |
| 24 | + |
| 25 | + @mock.patch("xlml.utils.github.get_current_context") |
| 26 | + def test_validate_git_trigger_success(self, mock_context): |
| 27 | + mock_dag_run = mock.MagicMock() |
| 28 | + mock_dag_run.external_trigger = True |
| 29 | + mock_context.return_value = { |
| 30 | + "dag_run": mock_dag_run, |
| 31 | + "params": { |
| 32 | + "github_run_id": "12345", |
| 33 | + "github_repo": "owner/repo", |
| 34 | + "github_token": "secret_token", |
| 35 | + }, |
| 36 | + } |
| 37 | + # Should not raise exception |
| 38 | + github.validate_git_trigger.function() |
| 39 | + |
| 40 | + @mock.patch("xlml.utils.github.get_current_context") |
| 41 | + def test_validate_git_trigger_manual_run(self, mock_context): |
| 42 | + mock_dag_run = mock.MagicMock() |
| 43 | + mock_dag_run.external_trigger = False |
| 44 | + mock_context.return_value = { |
| 45 | + "dag_run": mock_dag_run, |
| 46 | + "params": { |
| 47 | + "github_run_id": "12345", |
| 48 | + "github_repo": "owner/repo", |
| 49 | + "github_token": "secret_token", |
| 50 | + }, |
| 51 | + } |
| 52 | + with self.assertRaises(AirflowFailException): |
| 53 | + github.validate_git_trigger.function() |
| 54 | + |
| 55 | + @mock.patch("xlml.utils.github.get_current_context") |
| 56 | + def test_validate_git_trigger_missing_params(self, mock_context): |
| 57 | + mock_dag_run = mock.MagicMock() |
| 58 | + mock_dag_run.external_trigger = True |
| 59 | + mock_context.return_value = { |
| 60 | + "dag_run": mock_dag_run, |
| 61 | + "params": { |
| 62 | + "github_run_id": "12345", |
| 63 | + # missing repo and token |
| 64 | + }, |
| 65 | + } |
| 66 | + with self.assertRaises(AirflowFailException): |
| 67 | + github.validate_git_trigger.function() |
| 68 | + |
| 69 | + @mock.patch("requests.post") |
| 70 | + @mock.patch("xlml.utils.github.get_current_context") |
| 71 | + def test_fire_github_callback_success(self, mock_context, mock_post): |
| 72 | + mock_dag_run = mock.MagicMock() |
| 73 | + mock_dag_run.dag_id = "test_dag" |
| 74 | + mock_dag_run.run_id = "manual__1" |
| 75 | + |
| 76 | + mock_context.return_value = { |
| 77 | + "dag_run": mock_dag_run, |
| 78 | + "params": { |
| 79 | + "github_repo": "owner/repo", |
| 80 | + "github_run_id": "12345", |
| 81 | + "github_token": "secret_token", |
| 82 | + "commit_sha": "abc1234", |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + mock_response = mock.MagicMock() |
| 87 | + mock_post.return_value = mock_response |
| 88 | + |
| 89 | + github.fire_github_callback.function(test_type="pre_training") |
| 90 | + |
| 91 | + mock_post.assert_called_once_with( |
| 92 | + "https://api.github.com/repos/owner/repo/dispatches", |
| 93 | + headers={ |
| 94 | + "Authorization": "Bearer secret_token", |
| 95 | + "Accept": "application/vnd.github+json", |
| 96 | + "X-GitHub-Api-Version": "2022-11-28", |
| 97 | + }, |
| 98 | + json={ |
| 99 | + "event_type": "airflow-dag-complete", |
| 100 | + "client_payload": { |
| 101 | + "state": "success", |
| 102 | + "dag_id": "test_dag", |
| 103 | + "dag_run_id": "manual__1", |
| 104 | + "sha": "abc1234", |
| 105 | + "github_run_id": "12345", |
| 106 | + "test_type": "pre_training", |
| 107 | + }, |
| 108 | + }, |
| 109 | + timeout=30, |
| 110 | + ) |
| 111 | + mock_response.raise_for_status.assert_called_once() |
| 112 | + |
| 113 | + @mock.patch("requests.post") |
| 114 | + @mock.patch("xlml.utils.github.get_current_context") |
| 115 | + def test_trigger_github_repository_dispatch_explicit_args( |
| 116 | + self, mock_context, mock_post |
| 117 | + ): |
| 118 | + mock_context.return_value = {} |
| 119 | + mock_response = mock.MagicMock() |
| 120 | + mock_post.return_value = mock_response |
| 121 | + |
| 122 | + github.trigger_github_repository_dispatch.function( |
| 123 | + repo="custom/repo", |
| 124 | + token="custom_token", |
| 125 | + event_type="custom-event", |
| 126 | + client_payload={"foo": "bar"}, |
| 127 | + ) |
| 128 | + |
| 129 | + mock_post.assert_called_once_with( |
| 130 | + "https://api.github.com/repos/custom/repo/dispatches", |
| 131 | + headers={ |
| 132 | + "Authorization": "Bearer custom_token", |
| 133 | + "Accept": "application/vnd.github+json", |
| 134 | + "X-GitHub-Api-Version": "2022-11-28", |
| 135 | + }, |
| 136 | + json={ |
| 137 | + "event_type": "custom-event", |
| 138 | + "client_payload": {"foo": "bar"}, |
| 139 | + }, |
| 140 | + timeout=30, |
| 141 | + ) |
| 142 | + mock_response.raise_for_status.assert_called_once() |
| 143 | + |
| 144 | + @mock.patch("xlml.utils.github.get_current_context") |
| 145 | + def test_fire_github_callback_missing_token(self, mock_context): |
| 146 | + mock_context.return_value = { |
| 147 | + "params": { |
| 148 | + "github_repo": "owner/repo", |
| 149 | + } |
| 150 | + } |
| 151 | + with self.assertRaises(AirflowFailException): |
| 152 | + github.fire_github_callback.function() |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main() |
0 commit comments