diff options
Diffstat (limited to 'tests/unittests')
-rw-r--r-- | tests/unittests/sources/azure/test_errors.py | 7 | ||||
-rw-r--r-- | tests/unittests/sources/azure/test_kvp.py | 88 | ||||
-rw-r--r-- | tests/unittests/sources/test_azure.py | 80 | ||||
-rw-r--r-- | tests/unittests/sources/test_azure_helper.py | 2 |
4 files changed, 146 insertions, 31 deletions
diff --git a/tests/unittests/sources/azure/test_errors.py b/tests/unittests/sources/azure/test_errors.py index 9211d472..d2213613 100644 --- a/tests/unittests/sources/azure/test_errors.py +++ b/tests/unittests/sources/azure/test_errors.py @@ -105,7 +105,8 @@ def test_reportable_errors( ) data = [ - "PROVISIONING_ERROR: " + quote_csv_value(f"reason={reason}"), + "result=error", + quote_csv_value(f"reason={reason}"), f"agent=Cloud-Init/{version.version_string()}", ] data += [quote_csv_value(f"{k}={v}") for k, v in supporting_data.items()] @@ -115,7 +116,7 @@ def test_reportable_errors( "documentation_url=https://aka.ms/linuxprovisioningerror", ] - assert error.as_description() == "|".join(data) + assert error.as_encoded_report() == "|".join(data) def test_unhandled_exception(): @@ -136,4 +137,4 @@ def test_unhandled_exception(): assert trace.endswith("ValueError: my value error\n") quoted_value = quote_csv_value(f"exception={source_error!r}") - assert f"|{quoted_value}|" in error.as_description() + assert f"|{quoted_value}|" in error.as_encoded_report() diff --git a/tests/unittests/sources/azure/test_kvp.py b/tests/unittests/sources/azure/test_kvp.py new file mode 100644 index 00000000..f0f4a999 --- /dev/null +++ b/tests/unittests/sources/azure/test_kvp.py @@ -0,0 +1,88 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +from datetime import datetime +from unittest import mock + +import pytest + +from cloudinit import version +from cloudinit.sources.azure import errors, kvp + + +@pytest.fixture() +def fake_utcnow(): + timestamp = datetime.utcnow() + with mock.patch.object(kvp, "datetime", autospec=True) as m: + m.utcnow.return_value = timestamp + yield timestamp + + +@pytest.fixture() +def fake_vm_id(): + vm_id = "fake-vm-id" + with mock.patch.object(kvp.identity, "query_vm_id", autospec=True) as m: + m.return_value = vm_id + yield vm_id + + +@pytest.fixture +def telemetry_reporter(tmp_path): + kvp_file_path = tmp_path / "kvp_pool_file" + kvp_file_path.write_bytes(b"") + reporter = kvp.handlers.HyperVKvpReportingHandler( + kvp_file_path=str(kvp_file_path) + ) + + kvp.instantiated_handler_registry.register_item("telemetry", reporter) + yield reporter + kvp.instantiated_handler_registry.unregister_item("telemetry") + + +class TestReportFailureToHost: + def test_report_failure_to_host(self, caplog, telemetry_reporter): + error = errors.ReportableError(reason="test") + assert kvp.report_failure_to_host(error) is True + assert ( + "KVP handler not enabled, skipping host report." not in caplog.text + ) + + report = { + "key": "PROVISIONING_REPORT", + "value": error.as_encoded_report(), + } + assert report in list(telemetry_reporter._iterate_kvps(0)) + + def test_report_skipped_without_telemetry(self, caplog): + error = errors.ReportableError(reason="test") + + assert kvp.report_failure_to_host(error) is False + assert "KVP handler not enabled, skipping host report." in caplog.text + + +class TestReportSuccessToHost: + def test_report_success_to_host( + self, caplog, fake_utcnow, fake_vm_id, telemetry_reporter + ): + assert kvp.report_success_to_host() is True + assert ( + "KVP handler not enabled, skipping host report." not in caplog.text + ) + + report_value = errors.encode_report( + [ + "result=success", + f"agent=Cloud-Init/{version.version_string()}", + f"timestamp={fake_utcnow.isoformat()}", + f"vm_id={fake_vm_id}", + ] + ) + + report = { + "key": "PROVISIONING_REPORT", + "value": report_value, + } + assert report in list(telemetry_reporter._iterate_kvps(0)) + + def test_report_skipped_without_telemetry(self, caplog): + assert kvp.report_success_to_host() is False + assert "KVP handler not enabled, skipping host report." in caplog.text diff --git a/tests/unittests/sources/test_azure.py b/tests/unittests/sources/test_azure.py index 6251b7ed..47e65658 100644 --- a/tests/unittests/sources/test_azure.py +++ b/tests/unittests/sources/test_azure.py @@ -136,6 +136,26 @@ def mock_ephemeral_dhcp_v4(): @pytest.fixture +def mock_kvp_report_failure_to_host(): + with mock.patch( + MOCKPATH + "kvp.report_failure_to_host", + return_value=True, + autospec=True, + ) as m: + yield m + + +@pytest.fixture +def mock_kvp_report_success_to_host(): + with mock.patch( + MOCKPATH + "kvp.report_success_to_host", + return_value=True, + autospec=True, + ) as m: + yield m + + +@pytest.fixture def mock_net_dhcp_maybe_perform_dhcp_discovery(): with mock.patch( "cloudinit.net.ephemeral.maybe_perform_dhcp_discovery", @@ -308,9 +328,9 @@ def telemetry_reporter(tmp_path): kvp_file_path.write_bytes(b"") reporter = HyperVKvpReportingHandler(kvp_file_path=str(kvp_file_path)) - dsaz.instantiated_handler_registry.register_item("telemetry", reporter) + dsaz.kvp.instantiated_handler_registry.register_item("telemetry", reporter) yield reporter - dsaz.instantiated_handler_registry.unregister_item("telemetry") + dsaz.kvp.instantiated_handler_registry.unregister_item("telemetry") def fake_http_error_for_code(status_code: int): @@ -3482,6 +3502,8 @@ class TestProvisioning: mock_dmi_read_dmi_data, mock_get_interfaces, mock_get_interface_mac, + mock_kvp_report_failure_to_host, + mock_kvp_report_success_to_host, mock_netlink, mock_readurl, mock_subp_subp, @@ -3510,6 +3532,8 @@ class TestProvisioning: self.mock_dmi_read_dmi_data = mock_dmi_read_dmi_data self.mock_get_interfaces = mock_get_interfaces self.mock_get_interface_mac = mock_get_interface_mac + self.mock_kvp_report_failure_to_host = mock_kvp_report_failure_to_host + self.mock_kvp_report_success_to_host = mock_kvp_report_success_to_host self.mock_netlink = mock_netlink self.mock_readurl = mock_readurl self.mock_subp_subp = mock_subp_subp @@ -3612,6 +3636,10 @@ class TestProvisioning: assert self.wrapped_util_write_file.mock_calls == [] assert self.patched_reported_ready_marker_path.exists() is False + # Verify reports via KVP. + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 1 + def test_running_pps(self): self.imds_md["extended"]["compute"]["ppsType"] = "Running" @@ -3716,6 +3744,10 @@ class TestProvisioning: ) assert self.patched_reported_ready_marker_path.exists() is False + # Verify reports via KVP. + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 2 + def test_savable_pps(self): self.imds_md["extended"]["compute"]["ppsType"] = "Savable" @@ -3835,6 +3867,10 @@ class TestProvisioning: ) assert self.patched_reported_ready_marker_path.exists() is False + # Verify reports via KVP. + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 2 + @pytest.mark.parametrize( "fabric_side_effect", [ @@ -4072,6 +4108,10 @@ class TestProvisioning: ] assert self.patched_reported_ready_marker_path.exists() is False + # Verify reports via KVP. + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 1 + @pytest.mark.parametrize("pps_type", ["Savable", "Running", "Unknown"]) def test_source_pps_fails_initial_dhcp(self, pps_type): self.imds_md["extended"]["compute"]["ppsType"] = pps_type @@ -4089,18 +4129,21 @@ class TestProvisioning: dhcp.NoDHCPLeaseError() ] - with mock.patch.object(self.azure_ds, "_report_failure") as m_report: - self.azure_ds._get_data() - - assert m_report.mock_calls == [mock.call(mock.ANY)] + assert self.azure_ds._get_data() is False assert self.mock_wrapping_setup_ephemeral_networking.mock_calls == [ mock.call(timeout_minutes=20), + # Second round for _report_failure(). + mock.call(timeout_minutes=20), ] assert self.mock_readurl.mock_calls == [] assert self.mock_azure_get_metadata_from_fabric.mock_calls == [] assert self.mock_netlink.mock_calls == [] + # Verify reports via KVP. + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 1 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 0 + @pytest.mark.parametrize( "subp_side_effect", [ @@ -4169,6 +4212,10 @@ class TestProvisioning: assert self.patched_reported_ready_marker_path.exists() is False assert self.wrapped_util_write_file.mock_calls == [] + # Verify reports via KVP. Ignore failure reported after sleep(). + assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 1 + assert len(self.mock_kvp_report_success_to_host.mock_calls) == 1 + class TestValidateIMDSMetadata: @pytest.mark.parametrize( @@ -4390,24 +4437,3 @@ class TestValidateIMDSMetadata: } assert azure_ds.validate_imds_network_metadata(imds_md) is False - - -class TestReportFailureToHost: - def test_report(self, azure_ds, caplog, telemetry_reporter): - error = errors.ReportableError(reason="test") - assert azure_ds._report_failure_to_host(error) is True - assert ( - "KVP handler not enabled, skipping host report." not in caplog.text - ) - - report = { - "key": "PROVISIONING_REPORT", - "value": error.as_description(), - } - assert report in list(telemetry_reporter._iterate_kvps(0)) - - def test_report_skipped_without_telemtry(self, azure_ds, caplog): - error = errors.ReportableError(reason="test") - - assert azure_ds._report_failure_to_host(error) is False - assert "KVP handler not enabled, skipping host report." in caplog.text diff --git a/tests/unittests/sources/test_azure_helper.py b/tests/unittests/sources/test_azure_helper.py index a08011a6..971da9af 100644 --- a/tests/unittests/sources/test_azure_helper.py +++ b/tests/unittests/sources/test_azure_helper.py @@ -1382,7 +1382,7 @@ class TestGetMetadataGoalStateXMLAndReportFailureToFabric(CiTestCase): # default err message description should be shown to the user # if an empty description is passed in self.m_shim.return_value.register_with_azure_and_report_failure.assert_called_once_with( # noqa: E501 - description=error.as_description(), + description=error.as_encoded_report(), ) def test_instantiates_shim_with_kwargs(self): |