Merge ~ack/maas:1773150-smartctl-replace-invalid-utf8-3.1 into maas:3.1

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 985e9f3d18811101395f4ec3fcd545e805e6b745
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:1773150-smartctl-replace-invalid-utf8-3.1
Merge into: maas:3.1
Diff against target: 63 lines (+16/-4)
2 files modified
src/metadataserver/builtin_scripts/testing_scripts/smartctl.py (+8/-4)
src/metadataserver/builtin_scripts/testing_scripts/tests/test_smartctl.py (+8/-0)
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Review via email: mp+438395@code.launchpad.net

Commit message

LP:1773150 replace invalid UTF-8 chars in smartctl output

backport of 002f8c1c2d4a248b75fca33e3de29665464bf829

To post a comment you must log in.
Revision history for this message
Alberto Donato (ack) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/metadataserver/builtin_scripts/testing_scripts/smartctl.py b/src/metadataserver/builtin_scripts/testing_scripts/smartctl.py
2index 7fba550..61c9d93 100755
3--- a/src/metadataserver/builtin_scripts/testing_scripts/smartctl.py
4+++ b/src/metadataserver/builtin_scripts/testing_scripts/smartctl.py
5@@ -66,7 +66,9 @@ def run_smartctl(blockdevice, args, device=None, output=False, **kwargs):
6 cmd += [blockdevice]
7 if output:
8 print("INFO: Running command: %s" % " ".join(cmd))
9- return check_output(cmd, timeout=TIMEOUT, **kwargs).decode()
10+ return check_output(cmd, timeout=TIMEOUT, **kwargs).decode(
11+ errors="replace"
12+ )
13
14
15 def run_storcli(args, output=False, **kwargs):
16@@ -78,7 +80,9 @@ def run_storcli(args, output=False, **kwargs):
17 cmd = ["sudo", "-n", storcli] + args
18 if output:
19 print("INFO: Running command: %s" % " ".join(cmd))
20- return check_output(cmd, timeout=TIMEOUT, **kwargs).decode()
21+ return check_output(cmd, timeout=TIMEOUT, **kwargs).decode(
22+ errors="replace"
23+ )
24
25
26 def make_device_name(blockdevice, device=None):
27@@ -188,7 +192,7 @@ def check_SMART_support(blockdevice, device=None):
28 )
29 raise
30 else:
31- output = e.output.decode()
32+ output = e.output.decode(errors="replace")
33
34 if (
35 re.search(
36@@ -281,7 +285,7 @@ def check_smartctl(blockdevice, device=None):
37 )
38 raise
39 else:
40- output = e.output.decode()
41+ output = e.output.decode(errors="replace")
42
43 print("SUCCESS: SMART validation has PASSED for: %s" % device_name)
44 if output is not None:
45diff --git a/src/metadataserver/builtin_scripts/testing_scripts/tests/test_smartctl.py b/src/metadataserver/builtin_scripts/testing_scripts/tests/test_smartctl.py
46index 6443111..8fbee1a 100644
47--- a/src/metadataserver/builtin_scripts/testing_scripts/tests/test_smartctl.py
48+++ b/src/metadataserver/builtin_scripts/testing_scripts/tests/test_smartctl.py
49@@ -74,6 +74,14 @@ class TestRunSmartCTL(MAASTestCase):
50 )
51 self.assertThat(self.mock_print, MockCalledOnce())
52
53+ def test_output_invalid_utf8_replaced(self):
54+ # invalid UTF-8 input
55+ self.mock_check_output.return_value = b"foo\x99bar"
56+ self.assertEqual(
57+ "foo\ufffdbar",
58+ smartctl.run_smartctl(self.blockdevice, self.args, output=True),
59+ )
60+
61
62 class TestRunStorCLI(MAASTestCase):
63 def setUp(self):

Subscribers

People subscribed via source and target branches