Skip to content

Commit 2b7d963

Browse files
fix: typing for rsyslog, ubuntu_pro, power_state_change (canonical#5985)
1 parent b860d70 commit 2b7d963

File tree

7 files changed

+25
-26
lines changed

7 files changed

+25
-26
lines changed

cloudinit/config/cc_power_state_change.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def givecmdline(pid):
4545
(output, _err) = subp.subp(["procstat", "-c", str(pid)])
4646
line = output.splitlines()[1]
4747
m = re.search(r"\d+ (\w|\.|-)+\s+(/\w.+)", line)
48-
return m.group(2)
48+
if m:
49+
return m.group(2)
50+
else:
51+
return None
4952
else:
5053
return util.load_text_file("/proc/%s/cmdline" % pid)
5154
except IOError:

cloudinit/config/cc_rsyslog.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ def __init__(
246246
self.proto = proto
247247

248248
self.addr = addr
249-
if port:
250-
self.port = int(port)
251-
else:
252-
self.port = None
249+
self.port = int(port) if port is not None else None
253250

254251
def validate(self):
255252
if self.port:

cloudinit/config/cc_ubuntu_pro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ def configure_pro(token, enable=None):
178178
# Allow `ua attach` to fail in already attached machines
179179
subp.subp(attach_cmd, rcs={0, 2}, logstring=redacted_cmd)
180180
except subp.ProcessExecutionError as e:
181-
err = str(e).replace(token, REDACTED)
182-
msg = f"Failure attaching Ubuntu Pro:\n{err}"
181+
er = str(e).replace(token, REDACTED)
182+
msg = f"Failure attaching Ubuntu Pro:\n{er}"
183183
util.logexc(LOG, msg)
184184
raise RuntimeError(msg) from e
185185

pyproject.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ module = [
5353
"cloudinit.config.cc_ca_certs",
5454
"cloudinit.config.cc_growpart",
5555
"cloudinit.config.cc_ntp",
56-
"cloudinit.config.cc_power_state_change",
57-
"cloudinit.config.cc_rsyslog",
58-
"cloudinit.config.cc_ubuntu_pro",
5956
"cloudinit.config.modules",
6057
"cloudinit.distros",
6158
"cloudinit.distros.alpine",
@@ -134,19 +131,16 @@ module = [
134131
"tests.unittests.config.test_cc_mcollective",
135132
"tests.unittests.config.test_cc_mounts",
136133
"tests.unittests.config.test_cc_phone_home",
137-
"tests.unittests.config.test_cc_power_state_change",
138134
"tests.unittests.config.test_cc_puppet",
139135
"tests.unittests.config.test_cc_resizefs",
140136
"tests.unittests.config.test_cc_resolv_conf",
141137
"tests.unittests.config.test_cc_rh_subscription",
142-
"tests.unittests.config.test_cc_rsyslog",
143138
"tests.unittests.config.test_cc_runcmd",
144139
"tests.unittests.config.test_cc_snap",
145140
"tests.unittests.config.test_cc_ssh",
146141
"tests.unittests.config.test_cc_timezone",
147142
"tests.unittests.config.test_cc_ubuntu_autoinstall",
148143
"tests.unittests.config.test_cc_ubuntu_drivers",
149-
"tests.unittests.config.test_cc_ubuntu_pro",
150144
"tests.unittests.config.test_cc_update_etc_hosts",
151145
"tests.unittests.config.test_cc_users_groups",
152146
"tests.unittests.config.test_cc_wireguard",

tests/unittests/config/test_cc_power_state_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_empty_mode(self):
4747
self.assertRaises(TypeError, psc.load_power_state, cfg, self.dist)
4848

4949
def test_valid_modes(self):
50-
cfg = {"power_state": {}}
50+
cfg: dict = {"power_state": {}}
5151
for mode in ("halt", "poweroff", "reboot"):
5252
cfg["power_state"]["mode"] = mode
5353
check_lps_ret(psc.load_power_state(cfg, self.dist), mode=mode)

tests/unittests/config/test_cc_rsyslog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def test_install_rsyslog_on_freebsd(self, m_which):
340340
with mock.patch.object(
341341
cloud.distro, "install_packages"
342342
) as m_install:
343-
handle("rsyslog", {"rsyslog": config}, cloud, None)
343+
handle("rsyslog", {"rsyslog": config}, cloud, [])
344344
m_which.assert_called_with(config["check_exe"])
345345
m_install.assert_called_with(config["packages"])
346346

@@ -356,6 +356,6 @@ def test_no_install_rsyslog_with_check_exe(self, m_which, m_isbsd):
356356
m_isbsd.return_value = False
357357
m_which.return_value = "/usr/sbin/rsyslogd"
358358
with mock.patch.object(cloud.distro, "install_packages") as m_install:
359-
handle("rsyslog", {"rsyslog": config}, cloud, None)
359+
handle("rsyslog", {"rsyslog": config}, cloud, [])
360360
m_which.assert_called_with(config["check_exe"])
361361
m_install.assert_not_called()

tests/unittests/config/test_cc_ubuntu_pro.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,24 @@ def fake_uaclient(mocker):
5757
m_uaclient = mock.Mock()
5858

5959
sys.modules["uaclient"] = m_uaclient
60+
mock_exceptions_module = mock.Mock()
6061

6162
# Exceptions
62-
_exceptions = namedtuple(
63-
"exceptions",
63+
Exceptions = namedtuple(
64+
"Exceptions",
6465
[
6566
"UserFacingError",
6667
"AlreadyAttachedError",
6768
],
68-
)(
69+
)
70+
mock_exceptions_module.UserFacingError = FakeUserFacingError
71+
mock_exceptions_module.AlreadyAttachedError = FakeAlreadyAttachedError
72+
sys.modules["uaclient.api.exceptions"] = mock_exceptions_module
73+
_exceptions = Exceptions(
6974
FakeUserFacingError,
7075
FakeAlreadyAttachedError,
7176
)
72-
sys.modules["uaclient.api.exceptions"] = _exceptions
77+
return _exceptions
7378

7479

7580
@pytest.mark.usefixtures("fake_uaclient")
@@ -834,7 +839,7 @@ def test_handle_attach(
834839
caplog,
835840
):
836841
"""Non-Pro schemas and instance."""
837-
handle("nomatter", cfg=cfg, cloud=cloud, args=None)
842+
handle("nomatter", cfg=cfg, cloud=cloud, args=[])
838843
for record_tuple in log_record_tuples:
839844
assert record_tuple in caplog.record_tuples
840845
if maybe_install_call_args_list is not None:
@@ -961,7 +966,7 @@ def test_handle_auto_attach_vs_attach(
961966
m_auto_attach.side_effect = auto_attach_side_effect
962967

963968
with expectation:
964-
handle("nomatter", cfg=cfg, cloud=cloud, args=None)
969+
handle("nomatter", cfg=cfg, cloud=cloud, args=[])
965970

966971
for record_tuple in log_record_tuples:
967972
assert record_tuple in caplog.record_tuples
@@ -1006,7 +1011,7 @@ def test_no_fallback_attach(
10061011
enable or disable pro auto-attach.
10071012
"""
10081013
m_should_auto_attach.return_value = is_pro
1009-
handle("nomatter", cfg=cfg, cloud=self.cloud, args=None)
1014+
handle("nomatter", cfg=cfg, cloud=self.cloud, args=[])
10101015
assert not m_attach.call_args_list
10111016

10121017
@pytest.mark.parametrize(
@@ -1061,7 +1066,7 @@ def test_handle_errors(self, cfg, match):
10611066
"nomatter",
10621067
cfg=cfg,
10631068
cloud=self.cloud,
1064-
args=None,
1069+
args=[],
10651070
)
10661071

10671072
@mock.patch(f"{MPATH}.subp.subp")
@@ -1087,7 +1092,7 @@ def test_pro_config_error_invalid_url(self, m_subp, caplog):
10871092
"nomatter",
10881093
cfg=cfg,
10891094
cloud=self.cloud,
1090-
args=None,
1095+
args=[],
10911096
)
10921097
assert not caplog.text
10931098

@@ -1107,7 +1112,7 @@ def test_fallback_to_attach_no_token(
11071112
"nomatter",
11081113
cfg=cfg,
11091114
cloud=self.cloud,
1110-
args=None,
1115+
args=[],
11111116
)
11121117
assert [] == m_subp.call_args_list
11131118
assert (

0 commit comments

Comments
 (0)