| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import json |
| 5 |
import os |
| 6 |
import typing as t |
| 7 |
|
| 8 |
import pytest |
| 9 |
import pytest_mock |
| 10 |
|
| 11 |
if t.TYPE_CHECKING: |
| 12 |
from ansible_test._internal.ci.azp import AzurePipelinesChanges |
| 13 |
|
| 14 |
|
| 15 |
def create_azure_pipelines_changes(mocker: pytest_mock.MockerFixture) -> AzurePipelinesChanges: |
| 16 |
"""Prepare an AzurePipelinesChanges instance for testing.""" |
| 17 |
from ansible_test._internal.ci.azp import AzurePipelinesChanges |
| 18 |
from ansible_test._internal.config import CommonConfig |
| 19 |
|
| 20 |
namespace = argparse.Namespace() |
| 21 |
namespace.color = False |
| 22 |
namespace.explain = False |
| 23 |
namespace.verbosity = False |
| 24 |
namespace.debug = False |
| 25 |
namespace.truncate = False |
| 26 |
namespace.redact = False |
| 27 |
namespace.display_traceback = False |
| 28 |
|
| 29 |
config = CommonConfig(namespace, 'sanity') |
| 30 |
|
| 31 |
env = dict( |
| 32 |
HOME=os.environ['HOME'], |
| 33 |
SYSTEM_COLLECTIONURI='https://dev.azure.com/ansible/', |
| 34 |
SYSTEM_TEAMPROJECT='ansible', |
| 35 |
BUILD_REPOSITORY_PROVIDER='GitHub', |
| 36 |
BUILD_SOURCEBRANCH='devel', |
| 37 |
BUILD_SOURCEBRANCHNAME='devel', |
| 38 |
) |
| 39 |
|
| 40 |
mocker.patch.dict(os.environ, env, clear=True) |
| 41 |
|
| 42 |
return AzurePipelinesChanges(config) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.parametrize("status_code,response,expected_commits,expected_warning", ( |
| 46 |
# valid 200 responses |
| 47 |
(200, dict(value=[]), None, None), |
| 48 |
(200, dict(value=[dict(sourceVersion='abc')]), {'abc'}, None), |
| 49 |
# invalid 200 responses |
| 50 |
(200, 'not-json', None, "Unable to find project due to HTTP 200 Non-JSON result."), |
| 51 |
(200, '"not-a-dict"', None, "Unexpected response format from HTTP 200 JSON result: string indices must be integers, not 'str'"), |
| 52 |
(200, dict(value='not-a-list'), None, "Unexpected response format from HTTP 200 JSON result: string indices must be integers, not 'str'"), |
| 53 |
(200, dict(value=['not-a-dict']), None, "Unexpected response format from HTTP 200 JSON result: string indices must be integers, not 'str'"), |
| 54 |
(200, dict(), None, "Missing 'value' key in response from HTTP 200 JSON result."), |
| 55 |
(200, dict(value=[{}]), None, "Missing 'sourceVersion' key in response from HTTP 200 JSON result."), |
| 56 |
# non-200 responses |
| 57 |
(404, '', None, "Unable to find project due to HTTP 404 Non-JSON result."), |
| 58 |
(404, '""', None, "Unable to find project due to HTTP 404 JSON result."), |
| 59 |
(404, dict(value=[]), None, "Unable to find project due to HTTP 404 JSON result."), |
| 60 |
)) |
| 61 |
def test_get_successful_merge_run_commits( |
| 62 |
status_code: int, |
| 63 |
response: object, |
| 64 |
expected_commits: set[str] | None, |
| 65 |
expected_warning: str | None, |
| 66 |
mocker: pytest_mock.MockerFixture, |
| 67 |
) -> None: |
| 68 |
"""Verify AZP commit retrieval handles invalid responses gracefully.""" |
| 69 |
from ansible_test._internal.ci.azp import AzurePipelinesChanges |
| 70 |
from ansible_test._internal.git import Git |
| 71 |
from ansible_test._internal.http import HttpClient, HttpResponse |
| 72 |
from ansible_test._internal.util import display |
| 73 |
|
| 74 |
if not isinstance(response, str): |
| 75 |
response = json.dumps(response) |
| 76 |
|
| 77 |
if expected_warning: |
| 78 |
expected_warning = f'Cannot determine changes. All tests will be executed. Reason: {expected_warning}' |
| 79 |
|
| 80 |
patched_get = mocker.patch.object(HttpClient, 'get', return_value=HttpResponse('GET', 'URL', status_code, response)) |
| 81 |
patched_warning = mocker.patch.object(display, 'warning') |
| 82 |
|
| 83 |
mocker.patch.object(Git, 'run_git', return_value='') # avoid git |
| 84 |
|
| 85 |
spy_get_successful_merge_run_commits = mocker.spy(AzurePipelinesChanges, 'get_successful_merge_run_commits') |
| 86 |
|
| 87 |
create_azure_pipelines_changes(mocker) |
| 88 |
|
| 89 |
assert patched_get.call_count == 1 |
| 90 |
|
| 91 |
if expected_warning: |
| 92 |
patched_warning.assert_called_once_with(expected_warning) |
| 93 |
else: |
| 94 |
patched_warning.assert_not_called() |
| 95 |
|
| 96 |
assert spy_get_successful_merge_run_commits.spy_return == (expected_commits or set()) |