Merge lp:~joetalbott/utah/add_product_uuid into lp:utah

Proposed by Joe Talbott
Status: Merged
Approved by: Javier Collado
Approved revision: 724
Merged at revision: 726
Proposed branch: lp:~joetalbott/utah/add_product_uuid
Merge into: lp:utah
Diff against target: 86 lines (+39/-2)
2 files modified
utah/client/common.py (+29/-2)
utah/client/tests/test_common.py (+10/-0)
To merge this branch: bzr merge lp:~joetalbott/utah/add_product_uuid
Reviewer Review Type Date Requested Status
Javier Collado (community) Approve
Joe Talbott (community) Needs Resubmitting
Review via email: mp+131407@code.launchpad.net

Description of the change

Add product_uuid for machine under test to the result packet.

To post a comment you must log in.
Revision history for this message
Javier Collado (javier.collado) wrote :

To read files I like to use a context manager to guarantee that the file is closed:

with open(filename) as f:
    product_uuid = f.read().strip()

Besides this, I'm not sure about the expected UUID format, but maybe using a regular
expression is a good idea.

Finally, when is the IOError expected to happen? If it's related to permissions,
then checking the permissions before opening the file might be clearer and explicit.

lp:~joetalbott/utah/add_product_uuid updated
723. By Joe Talbott

Check for read access of product_uuid file rather than try/except

* use 'with' wrapper for file reads
* note that some methods are best-effort.

724. By Joe Talbott

tests - Add test for get_product_uuid().

Revision history for this message
Joe Talbott (joetalbott) wrote :

Updated to address Javier's issues.

I don't think we need to worry about the contents of /sys/class/dmi/id/product_uuid. We do a best-effort attempt to read the file and return whatever is there.

review: Needs Resubmitting
Revision history for this message
Javier Collado (javier.collado) wrote :

Thanks for the update. Looks good to me now.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utah/client/common.py'
2--- utah/client/common.py 2012-10-22 08:15:08 +0000
3+++ utah/client/common.py 2012-10-25 19:42:18 +0000
4@@ -299,18 +299,43 @@
5 def get_media_info():
6 """
7 Get the contents of the media-info file if available.
8+
9+ NOTE: This is only a best-effort approach.
10+
11+ Returns the contents of the media-info file or None.
12 """
13
14 filename = '/var/log/installer/media-info'
15
16 media_info = None
17
18- if os.path.exists(filename):
19- media_info = open(filename, 'r').read()
20+ if os.path.exists(filename) and os.access(filename, os.R_OK):
21+ with open(filename, 'r') as f:
22+ media_info = f.read()
23
24 return media_info
25
26
27+def get_product_uuid():
28+ """
29+ Get the product_uuid of the machine under test.
30+
31+ NOTE: This is only a best-effort approach.
32+
33+ Returns the contents of the product_uuid file or None.
34+ """
35+
36+ filename = '/sys/class/dmi/id/product_uuid'
37+
38+ product_uuid = None
39+
40+ if os.path.exists(filename) and os.access(filename, os.R_OK):
41+ with open(filename) as f:
42+ product_uuid = f.read().strip()
43+
44+ return product_uuid
45+
46+
47 def get_host_info():
48 """
49 Get host info, useful for debugging.
50@@ -321,6 +346,8 @@
51 retval['uname'] = platform.uname()
52 retval['media-info'] = get_media_info()
53
54+ retval['product_uuid'] = get_product_uuid()
55+
56 return retval
57
58
59
60=== modified file 'utah/client/tests/test_common.py'
61--- utah/client/tests/test_common.py 2012-09-27 13:55:41 +0000
62+++ utah/client/tests/test_common.py 2012-10-25 19:42:18 +0000
63@@ -4,6 +4,7 @@
64 # REQUIRES that the top level utah package be in the Python path.
65 from utah.client.common import (
66 get_media_info,
67+ get_product_uuid,
68 get_host_info,
69 run_cmd,
70 debug_print,
71@@ -15,6 +16,15 @@
72 """
73 Tests for utah.client.common.
74 """
75+
76+ def test_get_product_uuid(self):
77+ """ Test that product_uuid is returned when possible. """
78+
79+ product_uuid = get_product_uuid()
80+
81+ if os.access('/sys/class/dmi/id/product_uuid', os.R_OK):
82+ self.assertIsNotNone(product_uuid)
83+
84 def test_get_media_info(self):
85 """
86 Test that if there is a media-info file that it is parsed.

Subscribers

People subscribed via source and target branches