Skip to content

Commit

Permalink
fix(final_message): do not warn on datasourcenone when single ds
Browse files Browse the repository at this point in the history
Avoid warning level log "Used fallback datasource" when base
configuration defines `datasource_list: [ None ]`.

Using DataSourceNone in this case is desired behavior.

Also drop unused is_disconnected property from DataSource
which was only used by DataSourceNone in cc_final_message.

Instead compare based on dsname property.

Fixes: GH-5192
  • Loading branch information
blackboxsw committed Apr 25, 2024
1 parent d7e4f4d commit a71721c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
9 changes: 7 additions & 2 deletions cloudinit/config/cc_final_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,10 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
except Exception:
util.logexc(LOG, "Failed to write boot finished file %s", boot_fin_fn)

if cloud.datasource.is_disconnected:
LOG.warning("Used fallback datasource")
if cloud.datasource.dsname == "None":
if cloud.datasource.sys_cfg.get("datasource_list") == ["None"]:
# No warn on expected behavior when datasource_list: [ None ]
log_func = LOG.debug
else:
log_func = LOG.warning
log_func("Used fallback datasource")
4 changes: 0 additions & 4 deletions cloudinit/sources/DataSourceNone.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def _get_subplatform(self):
def get_instance_id(self):
return "iid-datasource-none"

@property
def is_disconnected(self):
return True


# Used to match classes to dependencies
datasources = [
Expand Down
4 changes: 0 additions & 4 deletions cloudinit/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,6 @@ def _filter_xdata(self, processed_ud):
new_ud = f.apply(new_ud)
return new_ud

@property
def is_disconnected(self):
return False

def get_userdata_raw(self):
return self.userdata_raw

Expand Down
48 changes: 41 additions & 7 deletions tests/unittests/config/test_cc_final_message.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# This file is part of cloud-init. See LICENSE file for license information.
from logging import DEBUG, WARNING
from pathlib import Path
from unittest import mock

import pytest

from cloudinit.config.cc_final_message import handle
from tests.unittests.util import get_cloud


class TestHandle:
Expand All @@ -24,17 +27,15 @@ def test_boot_finished_written(
file_is_written,
expected_log_substring,
caplog,
paths,
tmpdir,
):
instance_dir = tmpdir.join("var/lib/cloud/instance")
instance_dir = Path(paths.get_ipath_cur())
if instance_dir_exists:
instance_dir.ensure_dir()
boot_finished = instance_dir.join("boot-finished")

m_cloud = mock.Mock(
paths=mock.Mock(boot_finished=boot_finished.strpath)
)
instance_dir.mkdir()
boot_finished = instance_dir / "boot-finished"

m_cloud = get_cloud(paths=paths)
handle(None, {}, m_cloud, [])

# We should not change the status of the instance directory
Expand All @@ -43,3 +44,36 @@ def test_boot_finished_written(

if expected_log_substring:
assert expected_log_substring in caplog.text

@pytest.mark.parametrize(
"dsname,datasource_list,expected_log,log_level",
[
("None", ["None"], "Used fallback datasource", DEBUG),
("None", ["LXD", "None"], "Used fallback datasource", WARNING),
("LXD", ["LXD", "None"], None, DEBUG),
],
)
def test_only_warn_when_datasourcenone_is_fallback_in_datasource_list(
self,
dsname,
datasource_list,
expected_log,
log_level,
caplog,
paths,
):
"""Only warn when None is a fallback in multi-item datasource_list.
It is not a warning when datasource_list: [ None ] is configured.
"""
m_cloud = get_cloud(paths=paths)
m_cloud.datasource.dsname = dsname
Path(paths.get_ipath_cur()).mkdir()
with caplog.at_level(log_level):
handle(None, {}, m_cloud, [])

# We should not change the status of the instance directory
if expected_log:
assert expected_log in caplog.text
else:
assert "Used fallback datasource" not in caplog.text

0 comments on commit a71721c

Please sign in to comment.