Merge lp:~julian-edwards/maas/add-node-crash-bug-1305061 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: 2317
Proposed branch: lp:~julian-edwards/maas/add-node-crash-bug-1305061
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 64 lines (+29/-1)
2 files modified
src/maasserver/forms.py (+1/-1)
src/maasserver/tests/test_api_nodes.py (+28/-0)
To merge this branch: bzr merge lp:~julian-edwards/maas/add-node-crash-bug-1305061
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+219288@code.launchpad.net

Commit message

Ensure that validation errors are returned when adding a node over the API and its cluster controller is not contactable. Previously, the appserver thread crashed and returned a 500 to the client.

To post a comment you must log in.
Revision history for this message
Raphaël Badin (rvb) wrote :

Looks good. Just one remark:

Revision history for this message
Raphaël Badin (rvb) wrote :

(I just tried the new inline-comments feature, see my comment in the diff.)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/forms.py'
2--- src/maasserver/forms.py 2014-04-25 18:03:38 +0000
3+++ src/maasserver/forms.py 2014-05-14 07:17:47 +0000
4@@ -425,7 +425,7 @@
5 [CLUSTER_NOT_AVAILABLE + e.args[0]])
6 # If power_type is not set and power_parameters_skip_check is not
7 # on, reset power_parameters (set it to the empty string).
8- no_power_type = cleaned_data['power_type'] == ''
9+ no_power_type = cleaned_data.get('power_type', '') == ''
10 if no_power_type and not skip_check:
11 cleaned_data['power_parameters'] = ''
12 return cleaned_data
13
14=== modified file 'src/maasserver/tests/test_api_nodes.py'
15--- src/maasserver/tests/test_api_nodes.py 2014-03-06 05:33:46 +0000
16+++ src/maasserver/tests/test_api_nodes.py 2014-05-14 07:17:47 +0000
17@@ -19,12 +19,14 @@
18 import random
19
20 from django.core.urlresolvers import reverse
21+from maasserver import forms
22 from maasserver.enum import (
23 NODE_STATUS,
24 NODE_STATUS_CHOICES_DICT,
25 NODEGROUP_STATUS,
26 NODEGROUPINTERFACE_MANAGEMENT,
27 )
28+from maasserver.exceptions import ClusterUnavailable
29 from maasserver.fields import MAC
30 from maasserver.models import Node
31 from maasserver.models.user import (
32@@ -175,6 +177,32 @@
33 NODE_STATUS.DECLARED,
34 Node.objects.get(system_id=system_id).status)
35
36+ def test_POST_new_when_no_RPC_to_cluster_defaults_empty_power(self):
37+ # Test for bug 1305061, if there is no cluster RPC connection
38+ # then make sure that power_type is defaulted to the empty
39+ # string rather than being entirely absent, which results in a
40+ # crash.
41+ cluster_error = factory.make_name("cluster error")
42+ self.patch(forms, 'get_power_types').side_effect = (
43+ ClusterUnavailable(cluster_error))
44+ self.become_admin()
45+ # The patching behind the scenes to avoid *real* RPC is
46+ # complex and the available power types is actually a
47+ # valid set, so use an invalid type to trigger the bug here.
48+ power_type = factory.make_name("power_type")
49+ response = self.client.post(
50+ reverse('nodes_handler'),
51+ {
52+ 'op': 'new',
53+ 'autodetect_nodegroup': '1',
54+ 'architecture': make_usable_architecture(self),
55+ 'mac_addresses': ['aa:bb:cc:dd:ee:ff'],
56+ 'power_type': power_type,
57+ })
58+ self.assertEqual(httplib.BAD_REQUEST, response.status_code)
59+ validation_errors = json.loads(response.content)['power_type']
60+ self.assertIn(cluster_error, validation_errors[0])
61+
62 def test_GET_list_lists_nodes(self):
63 # The api allows for fetching the list of Nodes.
64 node1 = factory.make_node()