Merge lp:~smoser/cloud-init/trunk.dmidecode-null into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Scott Moser on 2016-03-10
Status: Merged
Merged at revision: 1180
Proposed branch: lp:~smoser/cloud-init/trunk.dmidecode-null
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 57 lines (+19/-3)
3 files modified
ChangeLog (+1/-0)
cloudinit/util.py (+9/-3)
tests/unittests/test_util.py (+9/-0)
To merge this branch: bzr merge lp:~smoser/cloud-init/trunk.dmidecode-null
Reviewer Review Type Date Requested Status
Ryan Harper 2016-03-10 Approve on 2016-03-10
Review via email: mp+288676@code.launchpad.net

Commit Message

dmi data: fix failure of reading dmi data for unset dmi values

it is not uncommon to find dmi data in /sys full of 'ff'. utf-8
decoding of those would fail, causing warning and stacktrace.

Return '.' instead of \xff. This maps to what dmidecode would return

$ dmidecode --string system-product-name

To post a comment you must log in.
1180. By Scott Moser on 2016-03-10

dmi data: fix failure of reading dmi data for unset dmi values

it is not uncommon to find dmi data in /sys full of 'ff'. utf-8
decoding of those would fail, causing warning and stacktrace.

Return '.' instead of \xff. This maps to what dmidecode would return

$ dmidecode --string system-product-name
.................................

Ryan Harper (raharper) :
Scott Moser (smoser) :
1181. By Scott Moser on 2016-03-10

improve comment

Ryan Harper (raharper) wrote :

We might want that as well in the test-case; but easily enough found in the code comments.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2016-03-10 01:47:27 +0000
3+++ ChangeLog 2016-03-10 17:48:09 +0000
4@@ -88,6 +88,7 @@
5 - No longer run pollinate in seed_random (LP: #1554152)
6 - groups: add defalt user to 'lxd' group. Create groups listed
7 for a user if they do not exist. (LP: #1539317)
8+ - dmi data: fix failure of reading dmi data for unset dmi values
9
10 0.7.6:
11 - open 0.7.6
12
13=== modified file 'cloudinit/util.py'
14--- cloudinit/util.py 2016-03-03 23:16:13 +0000
15+++ cloudinit/util.py 2016-03-10 17:48:09 +0000
16@@ -2140,13 +2140,19 @@
17 LOG.debug("did not find %s", dmi_key_path)
18 return None
19
20- key_data = load_file(dmi_key_path)
21+ key_data = load_file(dmi_key_path, decode=False)
22 if not key_data:
23 LOG.debug("%s did not return any data", dmi_key_path)
24 return None
25
26- LOG.debug("dmi data %s returned %s", dmi_key_path, key_data)
27- return key_data.strip()
28+ # uninitialized dmi values show as all \xff and /sys appends a '\n'.
29+ # in that event, return a string of '.' in the same length.
30+ if key_data == b'\xff' * (len(key_data) - 1) + b'\n':
31+ key_data = b'.' * (len(key_data) - 1) + b'\n'
32+
33+ str_data = key_data.decode('utf8').strip()
34+ LOG.debug("dmi data %s returned %s", dmi_key_path, str_data)
35+ return str_data
36
37 except Exception:
38 logexc(LOG, "failed read of %s", dmi_key_path)
39
40=== modified file 'tests/unittests/test_util.py'
41--- tests/unittests/test_util.py 2015-05-14 21:06:39 +0000
42+++ tests/unittests/test_util.py 2016-03-10 17:48:09 +0000
43@@ -385,6 +385,15 @@
44 self.patch_mapping({})
45 self.assertEqual(None, util.read_dmi_data('expect-fail'))
46
47+ def test_dots_returned_instead_of_foxfox(self):
48+ my_len = 32
49+ dmi_value = b'\xff' * my_len + b'\n'
50+ expected = '.' * my_len
51+ dmi_key = 'system-product-name'
52+ sysfs_key = 'product_name'
53+ self._create_sysfs_file(sysfs_key, dmi_value)
54+ self.assertEqual(expected, util.read_dmi_data(dmi_key))
55+
56
57 class TestMultiLog(helpers.FilesystemMockingTestCase):
58