Skip to content

Commit

Permalink
test: Refactor test_cc_set_hostname.py and test_cc_ntp.py
Browse files Browse the repository at this point in the history
* pytestify
* remove FilesystemMockingTestCase base class
* fix typing
* general cleanup

Any typing changes are to satisfy mypy.
  • Loading branch information
TheRealFalcon committed Sep 23, 2024
1 parent c93c259 commit 15e88e8
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 470 deletions.
13 changes: 7 additions & 6 deletions cloudinit/config/cc_ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import copy
import logging
import os
from typing import Dict, Mapping

from cloudinit import subp, temp_utils, templater, type_utils, util
from cloudinit.cloud import Cloud
Expand Down Expand Up @@ -98,7 +99,7 @@
}

# This is Distro-specific configuration overrides of the base config
DISTRO_CLIENT_CONFIG = {
DISTRO_CLIENT_CONFIG: Dict[str, Dict] = {
"alpine": {
"chrony": {
"confpath": "/etc/chrony/chrony.conf",
Expand Down Expand Up @@ -279,7 +280,7 @@ def distro_ntp_client_configs(distro):
return cfg


def select_ntp_client(ntp_client, distro):
def select_ntp_client(ntp_client, distro) -> Mapping:
"""Determine which ntp client is to be used, consulting the distro
for its preference.
Expand Down Expand Up @@ -318,7 +319,7 @@ def select_ntp_client(ntp_client, distro):
'Selected distro preferred NTP client "%s", not yet installed',
client,
)
clientcfg = distro_cfg.get(client)
clientcfg = distro_cfg.get(client, {})
else:
LOG.debug(
'Selected NTP client "%s" via distro system config',
Expand Down Expand Up @@ -565,7 +566,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:

template_fn = None
if not ntp_client_config.get("template"):
template_name = ntp_client_config.get("template_name").replace(
template_name = ntp_client_config["template_name"].replace(
"{distro}", cloud.distro.name
)
template_fn = cloud.get_template_filename(template_name)
Expand Down Expand Up @@ -611,14 +612,14 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:

try:
cloud.distro.manage_service(
"enable", ntp_client_config.get("service_name")
"enable", ntp_client_config["service_name"]
)
except subp.ProcessExecutionError as e:
LOG.exception("Failed to enable ntp service: %s", e)
raise
try:
cloud.distro.manage_service(
"reload", ntp_client_config.get("service_name")
"reload", ntp_client_config["service_name"]
)
except subp.ProcessExecutionError as e:
LOG.exception("Failed to reload/start ntp service: %s", e)
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def paths(tmpdir):
"cloud_dir": tmpdir.mkdir("cloud_dir").strpath,
"docs_dir": tmpdir.mkdir("docs_dir").strpath,
"run_dir": tmpdir.mkdir("run_dir").strpath,
"templates_dir": tmpdir.mkdir("templates_dir").strpath,
}
return helpers.Paths(dirs)

Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ module = [
"tests.unittests.config.test_cc_locale",
"tests.unittests.config.test_cc_mcollective",
"tests.unittests.config.test_cc_mounts",
"tests.unittests.config.test_cc_ntp",
"tests.unittests.config.test_cc_phone_home",
"tests.unittests.config.test_cc_power_state_change",
"tests.unittests.config.test_cc_puppet",
Expand All @@ -153,7 +152,6 @@ module = [
"tests.unittests.config.test_cc_rh_subscription",
"tests.unittests.config.test_cc_rsyslog",
"tests.unittests.config.test_cc_runcmd",
"tests.unittests.config.test_cc_set_hostname",
"tests.unittests.config.test_cc_snap",
"tests.unittests.config.test_cc_ssh",
"tests.unittests.config.test_cc_timezone",
Expand Down
Loading

0 comments on commit 15e88e8

Please sign in to comment.