Skip to content

Commit

Permalink
raise call to report_dmesg_to_kvp to _report_success/failure() and ad…
Browse files Browse the repository at this point in the history
…d tests
  • Loading branch information
cjp256 committed Jan 22, 2024
1 parent b1bcff6 commit 3a96d8b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
5 changes: 3 additions & 2 deletions cloudinit/sources/DataSourceAzure.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ def get_resource_disk_on_freebsd(port_id) -> Optional[str]:


class DataSourceAzure(sources.DataSource):

dsname = "Azure"
default_update_events = {
EventScope.NETWORK: {
Expand Down Expand Up @@ -1306,6 +1305,7 @@ def _report_failure(
f"Azure datasource failure occurred: {error.as_encoded_report()}",
logger_func=LOG.error,
)
report_dmesg_to_kvp()
reported = kvp.report_failure_to_host(error)
if host_only:
return reported
Expand Down Expand Up @@ -1365,6 +1365,7 @@ def _report_ready(
:returns: List of SSH keys, if requested.
"""
report_dmesg_to_kvp()
kvp.report_success_to_host()

try:
Expand Down Expand Up @@ -1822,7 +1823,7 @@ def _redact_password(cnt, fname):
if not files:
files = {}
util.ensure_dir(datadir, dirmode)
for (name, content) in files.items():
for name, content in files.items():
fname = os.path.join(datadir, name)
if "ovf-env.xml" in name:
content = _redact_password(content, fname)
Expand Down
36 changes: 24 additions & 12 deletions tests/unittests/sources/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ def mock_imds_fetch_metadata_with_api_fallback():
yield m


@pytest.fixture(autouse=True)
def mock_report_dmesg_to_kvp():
with mock.patch(
MOCKPATH + "report_dmesg_to_kvp",
return_value=True,
autospec=True,
) as m:
yield m


@pytest.fixture
def mock_kvp_report_failure_to_host():
with mock.patch(
Expand Down Expand Up @@ -355,17 +365,6 @@ def patched_reported_ready_marker_path(azure_ds, patched_markers_dir_path):
yield reported_ready_marker


@pytest.fixture
def telemetry_reporter(tmp_path):
kvp_file_path = tmp_path / "kvp_pool_file"
kvp_file_path.write_bytes(b"")
reporter = HyperVKvpReportingHandler(kvp_file_path=str(kvp_file_path))

dsaz.kvp.instantiated_handler_registry.register_item("telemetry", reporter)
yield reporter
dsaz.kvp.instantiated_handler_registry.unregister_item("telemetry")


def fake_http_error_for_code(status_code: int):
response_failure = requests.Response()
response_failure.status_code = status_code
Expand Down Expand Up @@ -3637,6 +3636,7 @@ def provisioning_setup(
mock_kvp_report_success_to_host,
mock_netlink,
mock_readurl,
mock_report_dmesg_to_kvp,
mock_subp_subp,
mock_timestamp,
mock_util_ensure_dir,
Expand Down Expand Up @@ -3666,6 +3666,7 @@ def provisioning_setup(
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_report_dmesg_to_kvp = mock_report_dmesg_to_kvp
self.mock_subp_subp = mock_subp_subp
self.mock_timestmp = mock_timestamp
self.mock_util_ensure_dir = mock_util_ensure_dir
Expand Down Expand Up @@ -3771,6 +3772,9 @@ def test_no_pps(self):
assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0
assert len(self.mock_kvp_report_success_to_host.mock_calls) == 1

# Verify dmesg reported via KVP.
assert len(self.mock_report_dmesg_to_kvp.mock_calls) == 1

@pytest.mark.parametrize("pps_type", ["Savable", "Running"])
def test_stale_pps(self, pps_type):
imds_md_source = copy.deepcopy(self.imds_md)
Expand Down Expand Up @@ -3944,6 +3948,9 @@ def test_running_pps(self):
assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0
assert len(self.mock_kvp_report_success_to_host.mock_calls) == 2

# Verify dmesg reported via KVP.
assert len(self.mock_report_dmesg_to_kvp.mock_calls) == 2

def test_savable_pps(self):
imds_md_source = copy.deepcopy(self.imds_md)
imds_md_source["extended"]["compute"]["ppsType"] = "Savable"
Expand Down Expand Up @@ -4062,6 +4069,9 @@ def test_savable_pps(self):
assert len(self.mock_kvp_report_failure_to_host.mock_calls) == 0
assert len(self.mock_kvp_report_success_to_host.mock_calls) == 2

# Verify dmesg reported via KVP.
assert len(self.mock_report_dmesg_to_kvp.mock_calls) == 2

@pytest.mark.parametrize(
"fabric_side_effect",
[
Expand Down Expand Up @@ -4522,13 +4532,14 @@ def test_errors(

class TestReportFailure:
@pytest.mark.parametrize("kvp_enabled", [False, True])
def report_host_only_kvp_enabled(
def test_report_host_only_kvp_enabled(
self,
azure_ds,
kvp_enabled,
mock_azure_report_failure_to_fabric,
mock_kvp_report_failure_to_host,
mock_kvp_report_success_to_host,
mock_report_dmesg_to_kvp,
):
mock_kvp_report_failure_to_host.return_value = kvp_enabled
error = errors.ReportableError(reason="foo")
Expand All @@ -4538,6 +4549,7 @@ def report_host_only_kvp_enabled(
assert mock_kvp_report_failure_to_host.mock_calls == [mock.call(error)]
assert mock_kvp_report_success_to_host.mock_calls == []
assert mock_azure_report_failure_to_fabric.mock_calls == []
assert mock_report_dmesg_to_kvp.mock_calls == [mock.call()]


class TestValidateIMDSMetadata:
Expand Down
35 changes: 32 additions & 3 deletions tests/unittests/sources/test_azure_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from textwrap import dedent
from xml.etree import ElementTree
from xml.sax.saxutils import escape, unescape
from cloudinit.reporting.handlers import HyperVKvpReportingHandler

import pytest
import requests

from cloudinit import url_helper
from cloudinit.reporting import instantiated_handler_registry
from cloudinit.sources.azure import errors
from cloudinit.sources.helpers import azure as azure_helper
from cloudinit.sources.helpers.azure import WALinuxAgentShim as wa_shim
Expand Down Expand Up @@ -146,7 +148,6 @@ def test_get_ip_from_lease_value(self, encoded_address, ip_address):


class TestGoalStateParsing(CiTestCase):

default_parameters = {
"incarnation": 1,
"container_id": "MyContainerId",
Expand Down Expand Up @@ -242,7 +243,6 @@ def test_missing_incarnation_in_goal_state_xml_raises_exc(self):


class TestAzureEndpointHttpClient(CiTestCase):

regular_headers = {
"x-ms-agent-name": "WALinuxAgent",
"x-ms-version": "2012-11-30",
Expand Down Expand Up @@ -556,7 +556,6 @@ def test_parse_certificates(self, mock_decrypt_certs):


class TestGoalStateHealthReporter(CiTestCase):

maxDiff = None

default_parameters = {
Expand Down Expand Up @@ -1648,3 +1647,33 @@ def test_invalid_xml(self, ovf, error):
azure_helper.OvfEnvXml.parse_text(ovf)

assert str(exc_info.value) == error


class TestReportDmesgToKvp:
@mock.patch.object(
azure_helper.subp, "subp", return_value=("dmesg test", "")
)
@mock.patch.object(azure_helper, "report_compressed_event")
def test_report_dmesg_to_kvp(
self, mock_report_compressed_event, mock_subp
):
azure_helper.report_dmesg_to_kvp()

assert mock_subp.mock_calls == [
mock.call(["dmesg"], decode=False, capture=True)
]
assert mock_report_compressed_event.mock_calls == [
mock.call("dmesg", "dmesg test")
]

@mock.patch.object(azure_helper.subp, "subp", side_effect=[Exception()])
@mock.patch.object(azure_helper, "report_compressed_event")
def test_report_dmesg_to_kvp_dmesg_error(
self, mock_report_compressed_event, mock_subp
):
azure_helper.report_dmesg_to_kvp()

assert mock_subp.mock_calls == [
mock.call(["dmesg"], decode=False, capture=True)
]
assert mock_report_compressed_event.mock_calls == []

0 comments on commit 3a96d8b

Please sign in to comment.