diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 3b6405f54db..2cfd738a1f5 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -64,7 +64,7 @@ def update_event_enabled( datasource: sources.DataSource, cfg: dict, event_source_type: EventType, - scope: Optional[EventScope] = None, + scope: EventScope, ) -> bool: """Determine if a particular EventType is enabled. @@ -93,11 +93,7 @@ def update_event_enabled( ) LOG.debug("Allowed events: %s", allowed) - scopes: Iterable[EventScope] - if not scope: - scopes = allowed.keys() - else: - scopes = [scope] + scopes: Iterable[EventScope] = [scope] scope_values = [s.value for s in scopes] for evt_scope in scopes: @@ -122,7 +118,7 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None): else: self.ds_deps = [sources.DEP_FILESYSTEM, sources.DEP_NETWORK] # Created on first use - self._cfg: Optional[dict] = None + self._cfg: Dict = {} self._paths: Optional[helpers.Paths] = None self._distro: Optional[distros.Distro] = None # Changed only when a fetch occurs @@ -138,14 +134,11 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None): ) self.reporter = reporter - def _reset(self, reset_ds=False): + def _reset(self): # Recreated on access - self._cfg = None + self._cfg = {} self._paths = None self._distro = None - if reset_ds: - self.datasource = None - self.ds_restored = False @property def distro(self): @@ -179,8 +172,6 @@ def _extract_cfg(self, restriction): ocfg = util.get_cfg_by_path(ocfg, ("system_info",), {}) elif restriction == "paths": ocfg = util.get_cfg_by_path(ocfg, ("system_info", "paths"), {}) - if not isinstance(ocfg, (dict)): - ocfg = {} return ocfg @property @@ -262,10 +253,8 @@ def _initialize_filesystem(self): ) def read_cfg(self, extra_fns=None): - # None check so that we don't keep on re-loading if empty - if self._cfg is None: + if not self._cfg: self._cfg = self._read_cfg(extra_fns) - # LOG.debug("Loaded 'init' config %s", self._cfg) def _read_cfg(self, extra_fns): no_cfg_paths = helpers.Paths({}, self.datasource) diff --git a/tests/unittests/test_stages.py b/tests/unittests/test_stages.py index 773060892a8..4e2f8ba117e 100644 --- a/tests/unittests/test_stages.py +++ b/tests/unittests/test_stages.py @@ -608,8 +608,9 @@ def init(self, paths): yield init @mock.patch(M_PATH + "util.ensure_file") + @mock.patch(f"{M_PATH}Init._read_cfg") def test_ensure_file_not_called_if_no_log_file_configured( - self, m_ensure_file, init + self, m_read_cfg, m_ensure_file, init ): """If no log file is configured, we should not ensure its existence.""" init._cfg = {}