diff options
author | Chris Patterson <cpatterson@microsoft.com> | 2023-05-12 03:21:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-12 12:21:14 +0200 |
commit | 9f8450368c2ae713de0a2308f5d3cb73de5b39f2 (patch) | |
tree | f143224ac7459a05f2e14e7aab4d90b91981caf0 /tests/unittests/sources/test_azure.py | |
parent | 60248d88f2d9e963a0bd0510180ec7b4d41cebd9 (diff) | |
download | cloud-init-git-9f8450368c2ae713de0a2308f5d3cb73de5b39f2.tar.gz |
azure/errors: introduce reportable errors for imds (#3647)
Always report failure to host, but report failure to fabric only
outside of _check_if_nic_is_primary() which is expected to fail if
nic is not primary.
Add two types of reportable errors for IMDS metadata:
- add ReportableErrorImdsUrlError() for url errors.
- add ReportableErrorImdsMetadataParsingException() for parsing errors.
Tweak ReportableError repr to be a bit friendlier.
Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
Diffstat (limited to 'tests/unittests/sources/test_azure.py')
-rw-r--r-- | tests/unittests/sources/test_azure.py | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/tests/unittests/sources/test_azure.py b/tests/unittests/sources/test_azure.py index 59ac6459..ed605ee7 100644 --- a/tests/unittests/sources/test_azure.py +++ b/tests/unittests/sources/test_azure.py @@ -17,7 +17,7 @@ from cloudinit.reporting.handlers import HyperVKvpReportingHandler from cloudinit.sources import UNSET from cloudinit.sources import DataSourceAzure as dsaz from cloudinit.sources import InvalidMetaDataException -from cloudinit.sources.azure import errors, identity +from cloudinit.sources.azure import errors, identity, imds from cloudinit.sources.helpers import netlink from cloudinit.util import ( MountFailedError, @@ -136,6 +136,16 @@ def mock_ephemeral_dhcp_v4(): @pytest.fixture +def mock_imds_fetch_metadata_with_api_fallback(): + with mock.patch.object( + imds, + "fetch_metadata_with_api_fallback", + autospec=True, + ) as m: + yield m + + +@pytest.fixture def mock_kvp_report_failure_to_host(): with mock.patch( MOCKPATH + "kvp.report_failure_to_host", @@ -4244,6 +4254,59 @@ class TestProvisioning: assert len(self.mock_kvp_report_success_to_host.mock_calls) == 1 +class TestGetMetadataFromImds: + @pytest.mark.parametrize("report_failure", [False, True]) + @pytest.mark.parametrize( + "exception,reported_error_type", + [ + ( + url_helper.UrlError(requests.ConnectionError()), + errors.ReportableErrorImdsUrlError, + ), + ( + ValueError("bad data"), + errors.ReportableErrorImdsMetadataParsingException, + ), + ], + ) + def test_errors( + self, + azure_ds, + exception, + mock_azure_report_failure_to_fabric, + mock_imds_fetch_metadata_with_api_fallback, + mock_kvp_report_failure_to_host, + monkeypatch, + report_failure, + reported_error_type, + ): + monkeypatch.setattr( + azure_ds, "_is_ephemeral_networking_up", lambda: True + ) + mock_imds_fetch_metadata_with_api_fallback.side_effect = exception + + assert ( + azure_ds.get_metadata_from_imds(report_failure=report_failure) + == {} + ) + assert mock_imds_fetch_metadata_with_api_fallback.mock_calls == [ + mock.call(retry_deadline=mock.ANY) + ] + + reported_error = mock_kvp_report_failure_to_host.call_args[0][0] + assert isinstance(reported_error, reported_error_type) + assert reported_error.supporting_data["exception"] == repr(exception) + assert mock_kvp_report_failure_to_host.mock_calls == [ + mock.call(reported_error) + ] + if report_failure: + assert mock_azure_report_failure_to_fabric.mock_calls == [ + mock.call(endpoint=mock.ANY, error=reported_error) + ] + else: + assert mock_azure_report_failure_to_fabric.mock_calls == [] + + class TestReportFailure: @pytest.mark.parametrize("kvp_enabled", [False, True]) def report_host_only_kvp_enabled( |