Merge lp:~pwlars/lava-test/bug640251 into lp:lava-test/0.0

Proposed by Paul Larson
Status: Merged
Merged at revision: 30
Proposed branch: lp:~pwlars/lava-test/bug640251
Merge into: lp:lava-test/0.0
Diff against target: 277 lines (+131/-59)
4 files modified
abrek/hwprofile.py (+68/-53)
abrek/utils.py (+24/-4)
tests/test_hwprofile.py (+39/-1)
tests/test_main.py (+0/-1)
To merge this branch: bzr merge lp:~pwlars/lava-test/bug640251
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+35910@code.launchpad.net

Description of the change

Fix for bug 640251. James, this should fix the tracebacks you were getting in chroots.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

Looks good.

Thanks,

James

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'abrek/hwprofile.py'
2--- abrek/hwprofile.py 2010-08-18 15:33:32 +0000
3+++ abrek/hwprofile.py 2010-09-18 04:31:00 +0000
4@@ -16,6 +16,7 @@
5 import commands
6 import os
7 import re
8+import sys
9 from utils import read_file
10
11 INTEL_KEYMAP = {
12@@ -79,23 +80,26 @@
13 elif machine.startswith('arm'):
14 keymap, valmap = ARM_KEYMAP, ARM_VALMAP
15
16- cpuinfo = read_file("/proc/cpuinfo")
17- for line in cpuinfo.splitlines():
18- match = pattern.match(line)
19- if match:
20- key, value = match.groups()
21- key, value = _translate_cpuinfo(keymap, valmap,
22- key, value)
23- if cpudevs[cpunum].get(key):
24- cpunum += 1
25- cpudevs.append({})
26- cpudevs[cpunum][key] = value
27- for c in range(len(cpudevs)):
28- device = {}
29- device['device_type'] = 'device.cpu'
30- device['attributes'] = 'Processor #{0}'.format(c)
31- device['desc'] = cpudevs[c]
32- devices.append(device)
33+ try:
34+ cpuinfo = read_file("/proc/cpuinfo")
35+ for line in cpuinfo.splitlines():
36+ match = pattern.match(line)
37+ if match:
38+ key, value = match.groups()
39+ key, value = _translate_cpuinfo(keymap, valmap,
40+ key, value)
41+ if cpudevs[cpunum].get(key):
42+ cpunum += 1
43+ cpudevs.append({})
44+ cpudevs[cpunum][key] = value
45+ for c in range(len(cpudevs)):
46+ device = {}
47+ device['device_type'] = 'device.cpu'
48+ device['attributes'] = 'Processor #{0}'.format(c)
49+ device['desc'] = cpudevs[c]
50+ devices.append(device)
51+ except IOError:
52+ print >> sys.stderr, "WARNING: Could not read cpu information"
53 return devices
54
55
56@@ -108,24 +112,32 @@
57 device = {}
58 machine = os.uname()[-1]
59 if machine in ('i686', 'x86_64'):
60- name = read_file('/sys/class/dmi/id/board_name') or None
61- vendor = read_file('/sys/class/dmi/id/board_vendor') or None
62- version = read_file('/sys/class/dmi/id/board_version') or None
63- if name:
64- device['attributes'] = name.strip()
65- if vendor:
66- desc['vendor'] = vendor.strip()
67- if version:
68- desc['version'] = version.strip()
69+ try:
70+ name = read_file('/sys/class/dmi/id/board_name') or None
71+ vendor = read_file('/sys/class/dmi/id/board_vendor') or None
72+ version = read_file('/sys/class/dmi/id/board_version') or None
73+ if name:
74+ device['attributes'] = name.strip()
75+ if vendor:
76+ desc['vendor'] = vendor.strip()
77+ if version:
78+ desc['version'] = version.strip()
79+ except IOError:
80+ print >> sys.stderr, "WARNING: Could not read board information"
81+ return devices
82 elif machine.startswith('arm'):
83- cpuinfo = read_file("/proc/cpuinfo")
84- if cpuinfo is None:
85- return devices
86- pattern = re.compile("^Hardware\s*:\s*(?P<hardware>.+)$", re.M)
87- match = pattern.search(cpuinfo)
88- if match is None:
89- return devices
90- desc['hardware'] = match.group('hardware')
91+ try:
92+ cpuinfo = read_file("/proc/cpuinfo")
93+ if cpuinfo is None:
94+ return devices
95+ pattern = re.compile("^Hardware\s*:\s*(?P<hardware>.+)$", re.M)
96+ match = pattern.search(cpuinfo)
97+ if match is None:
98+ return devices
99+ desc['hardware'] = match.group('hardware')
100+ except IOError:
101+ print >> sys.stderr, "WARNING: Could not read board information"
102+ return devices
103 else:
104 return devices
105 device['desc'] = desc
106@@ -141,25 +153,28 @@
107 pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.+) kB$', re.M)
108
109 devices = []
110- meminfo = read_file("/proc/meminfo")
111- for match in pattern.finditer(meminfo):
112- key, value = match.groups()
113- if key not in ('MemTotal', 'SwapTotal'):
114- continue
115- capacity = int(value) << 10 #Kernel reports in 2^10 units
116- if capacity == 0:
117- continue
118- if key == 'MemTotal':
119- kind = 'RAM'
120- else:
121- kind = 'swap'
122- name = "{capacity}MiB of {kind}".format(
123- capacity=capacity >> 20, kind=kind)
124- device = {}
125- device['attributes'] = name
126- device['desc'] = {'capacity': capacity, 'kind': kind}
127- device['device_type'] = "device.mem"
128- devices.append(device)
129+ try:
130+ meminfo = read_file("/proc/meminfo")
131+ for match in pattern.finditer(meminfo):
132+ key, value = match.groups()
133+ if key not in ('MemTotal', 'SwapTotal'):
134+ continue
135+ capacity = int(value) << 10 #Kernel reports in 2^10 units
136+ if capacity == 0:
137+ continue
138+ if key == 'MemTotal':
139+ kind = 'RAM'
140+ else:
141+ kind = 'swap'
142+ name = "{capacity}MiB of {kind}".format(
143+ capacity=capacity >> 20, kind=kind)
144+ device = {}
145+ device['attributes'] = name
146+ device['desc'] = {'capacity': capacity, 'kind': kind}
147+ device['device_type'] = "device.mem"
148+ devices.append(device)
149+ except IOError:
150+ print >> sys.stderr, "WARNING: Could not read memory information"
151 return devices
152
153 def get_usb_devs():
154
155=== modified file 'abrek/utils.py'
156--- abrek/utils.py 2010-09-15 03:38:49 +0000
157+++ abrek/utils.py 2010-09-18 04:31:00 +0000
158@@ -19,6 +19,7 @@
159 import urlparse
160
161 _fake_files = None
162+_fake_paths = None
163
164 def geturl(url, path=""):
165 urlpath = urlparse.urlsplit(url).path
166@@ -42,18 +43,37 @@
167
168 def read_file(path):
169 global _fake_files
170+ global _fake_paths
171 if _fake_files is not None:
172 if path in _fake_files:
173 return _fake_files[path]
174+ if _fake_paths is not None:
175+ if path in _fake_paths:
176+ path = _fake_paths[path]
177 with open(path) as fd:
178 data = fd.read()
179 return data
180
181-def fake_file(path, data):
182+def fake_file(path, data=None, newpath=None):
183 """
184 Set up a fake file to be read with read_file() in testing
185+ If data is specified, the string passed as data will be returned instead
186+ if newpath is specified, the file attempted to be read will be replaced
187+ by newfile
188 """
189 global _fake_files
190- if _fake_files is None:
191- _fake_files = {}
192- _fake_files[path] = data
193+ global _fake_paths
194+ if data is not None:
195+ if _fake_files is None:
196+ _fake_files = {}
197+ _fake_files[path] = data
198+ if newpath is not None:
199+ if _fake_paths is None:
200+ _fake_paths = {}
201+ _fake_paths[path] = newpath
202+
203+def clear_fakes():
204+ global _fake_files
205+ global _fake_paths
206+ _fake_files = {}
207+ _fake_paths = {}
208
209=== modified file 'tests/test_hwprofile.py'
210--- tests/test_hwprofile.py 2010-09-10 17:09:14 +0000
211+++ tests/test_hwprofile.py 2010-09-18 04:31:00 +0000
212@@ -13,10 +13,13 @@
213 # You should have received a copy of the GNU General Public License
214 # along with this program. If not, see <http://www.gnu.org/licenses/>.
215
216+import os
217 import unittest
218
219 import abrek.hwprofile
220-from abrek.utils import fake_file
221+from abrek.utils import fake_file, clear_fakes
222+from imposters import OutputImposter
223+from fixtures import TestCaseWithFixtures
224
225
226 class AptCache:
227@@ -92,3 +95,38 @@
228 def test_get_usb_devs(self):
229 devs = abrek.hwprofile.get_usb_devs()
230 self.assertEqual('device.usb', devs[0]['device_type'])
231+
232+
233+class MissingFiles(TestCaseWithFixtures):
234+ """
235+ These are tests for situations where certain files used for gathering
236+ hardware profile information may be missing
237+ """
238+ def setUp(self):
239+ super(MissingFiles, self).setUp()
240+ clear_fakes()
241+ self.out = self.add_fixture(OutputImposter())
242+
243+ def test_bad_cpuinfo(self):
244+ errmsg = "WARNING: Could not read cpu information\n"
245+ fake_file('/proc/cpuinfo', newpath='/foo/bar')
246+ devs = abrek.hwprofile.get_cpu_devs()
247+ self.assertEqual([], devs)
248+ self.assertEqual(errmsg, self.out.getvalue())
249+
250+ def test_bad_boardinfo(self):
251+ machine = os.uname()[-1]
252+ errmsg = "WARNING: Could not read board information\n"
253+ fake_file('/sys/class/dmi/id/board_name', newpath='/foo/bar')
254+ devs = abrek.hwprofile.get_board_devs()
255+ self.assertEqual([], devs)
256+ if machine in ('i686', 'x86_64'):
257+ self.assertEqual(errmsg, self.out.getvalue())
258+
259+ def test_bad_meminfo(self):
260+ errmsg = "WARNING: Could not read memory information\n"
261+ fake_file('/proc/meminfo', newpath='/foo/bar')
262+ devs = abrek.hwprofile.get_mem_devs()
263+ self.assertEqual([], devs)
264+ self.assertEqual(errmsg, self.out.getvalue())
265+
266
267=== modified file 'tests/test_main.py'
268--- tests/test_main.py 2010-09-15 20:28:14 +0000
269+++ tests/test_main.py 2010-09-18 04:31:00 +0000
270@@ -22,7 +22,6 @@
271 def setUp(self):
272 super(testMain, self).setUp()
273 self.out = self.add_fixture(OutputImposter())
274- self._fixtures = []
275
276 def test_bad_subcmd(self):
277 errmsg = "Unknown usage './abrek results foo'\nUse 'abrek help [cmd]' for help\n"

Subscribers

People subscribed via source and target branches