|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import importlib.util |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | +from unittest.mock import patch |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +MODULE_PATH = Path(__file__).resolve().parents[3] / "scripts" / "ci" / "slack_notification_state.py" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def slack_state_module(): |
| 32 | + module_name = "test_slack_notification_state_module" |
| 33 | + sys.modules.pop(module_name, None) |
| 34 | + spec = importlib.util.spec_from_file_location(module_name, MODULE_PATH) |
| 35 | + assert spec is not None |
| 36 | + assert spec.loader is not None |
| 37 | + module = importlib.util.module_from_spec(spec) |
| 38 | + spec.loader.exec_module(module) |
| 39 | + return module |
| 40 | + |
| 41 | + |
| 42 | +class TestDownloadPreviousState: |
| 43 | + def test_uses_explicit_get_method(self, slack_state_module): |
| 44 | + """`gh api` defaults to POST when -f is passed; we must force GET to avoid 404.""" |
| 45 | + completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="null", stderr="") |
| 46 | + with patch.object(subprocess, "run", return_value=completed) as mock_run: |
| 47 | + slack_state_module.download_previous_state("artifact", "apache/airflow") |
| 48 | + args = mock_run.call_args[0][0] |
| 49 | + assert "--method" in args |
| 50 | + method_index = args.index("--method") |
| 51 | + assert args[method_index + 1] == "GET" |
| 52 | + |
| 53 | + |
| 54 | +class TestDetermineAction: |
| 55 | + def test_no_failures_no_prev_state(self, slack_state_module): |
| 56 | + assert slack_state_module.determine_action([], None) == "skip" |
| 57 | + |
| 58 | + def test_no_failures_no_prev_failures(self, slack_state_module): |
| 59 | + prev = {"failures": [], "last_notified": "2025-01-01T00:00:00+00:00"} |
| 60 | + assert slack_state_module.determine_action([], prev) == "skip" |
| 61 | + |
| 62 | + def test_recovery_when_clear_after_failures(self, slack_state_module): |
| 63 | + prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"} |
| 64 | + assert slack_state_module.determine_action([], prev) == "notify_recovery" |
| 65 | + |
| 66 | + def test_notify_new_on_first_failure(self, slack_state_module): |
| 67 | + assert slack_state_module.determine_action(["job-a"], None) == "notify_new" |
| 68 | + |
| 69 | + def test_notify_new_on_changed_failures(self, slack_state_module): |
| 70 | + prev = {"failures": ["job-a"], "last_notified": "2025-01-01T00:00:00+00:00"} |
| 71 | + assert slack_state_module.determine_action(["job-b"], prev) == "notify_new" |
0 commit comments