Merge lp:~julian-edwards/maas/nicer-mac-address-error into lp:~maas-committers/maas/trunk

Proposed by Julian Edwards
Status: Merged
Approved by: Julian Edwards
Approved revision: no longer in the source branch.
Merged at revision: 1204
Proposed branch: lp:~julian-edwards/maas/nicer-mac-address-error
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 59 lines (+30/-1)
2 files modified
src/maasserver/api.py (+10/-1)
src/maasserver/tests/test_api.py (+20/-0)
To merge this branch: bzr merge lp:~julian-edwards/maas/nicer-mac-address-error
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) Approve
John A Meinel (community) Approve
Review via email: mp+128421@code.launchpad.net

Commit message

Present a nice error rather than a stack trace when bad MAC addresses are detected in the API call to Nodes/list().

To post a comment you must log in.
Revision history for this message
John A Meinel (jameinel) wrote :

I thought Validation error took a dict of {'variable': 'message'} so the exception should be:
ValueError({'macs': "Invalid MAC address(es): %s" % ", ".join(invalid_macs)})

Otherwise, good.

review: Approve
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

I like how the test case reproduces the ":ZZ" error from the bug report.

review: Approve
Revision history for this message
Julian Edwards (julian-edwards) wrote :

<bigjools> jam: thanks for the review - not sure what difference your
suggestion makes as everything seems to work anyway. I think it's more a UI
validation thing isn't it?
<jam> bigjools: I think if you get a form with multiple errors, the 'dict'
method allows it to present both to users.
<jam> I could be 100% wrong, though.
<jam> I was just following along what other things did when I wrote a form
recently.
<bigjools> heh, I honestly have no idea. Given this was an API call I figured
a human readable string would be for the best but....
<jam> bigjools: so the big win is to just not traceback, the rest is gravy
<jam> If it works for you, that's enough for me for now.
<bigjools> righto, cheers

Revision history for this message
MAAS Lander (maas-lander) wrote :

The Jenkins job https://jenkins.qa.ubuntu.com/job/maas-merger-quantal/151/console reported an error when processing this lp:~julian-edwards/maas/nicer-mac-address-error branch.
Not merging it.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/api.py'
2--- src/maasserver/api.py 2012-10-05 08:04:58 +0000
3+++ src/maasserver/api.py 2012-10-08 07:06:22 +0000
4@@ -134,7 +134,10 @@
5 NodeStateViolation,
6 Unauthorized,
7 )
8-from maasserver.fields import validate_mac
9+from maasserver.fields import (
10+ mac_re,
11+ validate_mac,
12+ )
13 from maasserver.forms import (
14 get_node_create_form,
15 get_node_edit_form,
16@@ -762,6 +765,12 @@
17 # Get filters from request.
18 match_ids = get_optional_list(request.GET, 'id')
19 match_macs = get_optional_list(request.GET, 'mac_address')
20+ if match_macs is not None:
21+ invalid_macs = [
22+ mac for mac in match_macs if mac_re.match(mac) is None]
23+ if len(invalid_macs) != 0:
24+ raise ValidationError(
25+ "Invalid MAC address(es): %s" % ", ".join(invalid_macs))
26 # Fetch nodes and apply filters.
27 nodes = Node.objects.get_nodes(
28 request.user, NODE_PERMISSION.VIEW, ids=match_ids)
29
30=== modified file 'src/maasserver/tests/test_api.py'
31--- src/maasserver/tests/test_api.py 2012-10-05 08:04:58 +0000
32+++ src/maasserver/tests/test_api.py 2012-10-08 07:06:22 +0000
33@@ -1616,6 +1616,26 @@
34 self.assertItemsEqual(
35 [matching_system_id], extract_system_ids(parsed_result))
36
37+ def test_GET_list_with_invalid_macs_returns_sensible_error(self):
38+ # If specifying an invalid MAC, make sure the error that's
39+ # returned is not a crazy stack trace, but something nice to
40+ # humans.
41+ bad_mac1 = '00:E0:81:DD:D1:ZZ' # ZZ is bad.
42+ bad_mac2 = '00:E0:81:DD:D1:XX' # XX is bad.
43+ ok_mac = factory.make_mac_address()
44+ response = self.client.get(self.get_uri('nodes/'), {
45+ 'op': 'list',
46+ 'mac_address': [bad_mac1, bad_mac2, ok_mac],
47+ })
48+ observed = response.status_code, response.content
49+ expected = (
50+ Equals(httplib.BAD_REQUEST),
51+ Contains(
52+ "Invalid MAC address(es): 00:E0:81:DD:D1:ZZ, "
53+ "00:E0:81:DD:D1:XX"),
54+ )
55+ self.assertThat(observed, MatchesListwise(expected))
56+
57 def test_GET_list_allocated_returns_only_allocated_with_user_token(self):
58 # If the user's allocated nodes have different session tokens,
59 # list_allocated should only return the nodes that have the