Merge lp:~rvb/maas/bug-1065406 into lp:maas/trunk

Proposed by Raphaël Badin on 2012-10-11
Status: Merged
Approved by: Raphaël Badin on 2012-10-11
Approved revision: 1259
Merged at revision: 1257
Proposed branch: lp:~rvb/maas/bug-1065406
Merge into: lp:maas/trunk
Diff against target: 105 lines (+51/-0)
4 files modified
src/maasserver/dhcp.py (+4/-0)
src/maasserver/models/nodegroup.py (+13/-0)
src/maasserver/tests/test_dhcp.py (+10/-0)
src/maasserver/tests/test_nodegroup.py (+24/-0)
To merge this branch: bzr merge lp:~rvb/maas/bug-1065406
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) 2012-10-11 Approve on 2012-10-11
Review via email: mp+129131@code.launchpad.net

Commit Message

Generate a dhcp key (if nodegroup,dhcp_key is empty) before we write the dhcp_config.

Description of the Change

Make sure we have a valid dhcp_key before we write the dhcp_config.

= Pre-imp =

Discussed with Jeroen.

= Notes =

The trick in ensure_dhcp_key is a bit messy but I see no other way to do this: ensure_dhcp_key is called from configure_dhcp which is hooked-up to NodeGroup.post_save so it cannot trigger a pose_save signal itself.

To post a comment you must log in.
Jeroen T. Vermeulen (jtv) wrote :

Good stuff. Just this:

3 + def ensure_dhcp_key(self):
24 + """If self.dhcp_key is empty: create a valid dhcp key.
25 +
26 + This method persists the dhcp key without triggering the model
27 + signals (pre_save/post_save/etc)."""

Not just create: set it on the nodegroup. Maybe "ensure that this nodegroup has a dhcp key"?

28 + if self.dhcp_key == '':
29 + dhcp_key = generate_omapi_key()
30 + self.dhcp_key = dhcp_key
31 + # Persist the dhcp_key without triggering the signals.
32 + NodeGroup.objects.filter(id=self.id).update(dhcp_key=dhcp_key)

Don't forget to say why it's important that the signal doesn't fire!

Jeroen T. Vermeulen (jtv) wrote :

Hmmm that was meant to be an Approve. Thought I'd clicked that.

review: Approve
lp:~rvb/maas/bug-1065406 updated on 2012-10-11
1259. By Raphaël Badin on 2012-10-11

Fix docstring.

Raphaël Badin (rvb) wrote :

Thanks for the review.

I've updated the docstring of ensure_dhcp_key.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/dhcp.py'
2--- src/maasserver/dhcp.py 2012-09-25 14:45:59 +0000
3+++ src/maasserver/dhcp.py 2012-10-11 11:01:21 +0000
4@@ -48,6 +48,10 @@
5 if not is_dhcp_managed(nodegroup):
6 return
7
8+ # Make sure this nodegroup has a key to communicate with the dhcp
9+ # server.
10+ nodegroup.ensure_dhcp_key()
11+
12 # Use the server's address (which is where the central TFTP
13 # server is) for the next_server setting. We'll want to proxy
14 # it on the local worker later, and then we can use
15
16=== modified file 'src/maasserver/models/nodegroup.py'
17--- src/maasserver/models/nodegroup.py 2012-10-04 07:55:27 +0000
18+++ src/maasserver/models/nodegroup.py 2012-10-11 11:01:21 +0000
19@@ -201,6 +201,19 @@
20 nodegroup=self).exclude(
21 management=NODEGROUPINTERFACE_MANAGEMENT.UNMANAGED))
22
23+ def ensure_dhcp_key(self):
24+ """Ensure that this nodegroup has a dhcp key.
25+
26+ This method persists the dhcp key without triggering the model
27+ signals (pre_save/post_save/etc) because it's called from
28+ dhcp.configure_dhcp which, in turn, it called from the post_save
29+ signal of NodeGroup."""
30+ if self.dhcp_key == '':
31+ dhcp_key = generate_omapi_key()
32+ self.dhcp_key = dhcp_key
33+ # Persist the dhcp_key without triggering the signals.
34+ NodeGroup.objects.filter(id=self.id).update(dhcp_key=dhcp_key)
35+
36 @property
37 def work_queue(self):
38 """The name of the queue for tasks specific to this nodegroup."""
39
40=== modified file 'src/maasserver/tests/test_dhcp.py'
41--- src/maasserver/tests/test_dhcp.py 2012-09-30 21:19:08 +0000
42+++ src/maasserver/tests/test_dhcp.py 2012-10-11 11:01:21 +0000
43@@ -29,6 +29,7 @@
44 from netaddr import IPNetwork
45 from provisioningserver import tasks
46 from testresources import FixtureResource
47+from testtools.matchers import EndsWith
48
49
50 class TestDHCP(TestCase):
51@@ -107,6 +108,15 @@
52 mocked_check_call.call_args[0][0],
53 ['sudo', '-n', 'service', 'maas-dhcp-server', 'restart'])
54
55+ def test_configure_dhcp_is_called_with_valid_dhcp_key(self):
56+ self.patch(dhcp, 'write_dhcp_config')
57+ self.patch(settings, "DHCP_CONNECT", True)
58+ nodegroup = factory.make_node_group(
59+ status=NODEGROUP_STATUS.ACCEPTED, dhcp_key='')
60+ configure_dhcp(nodegroup)
61+ args, kwargs = dhcp.write_dhcp_config.apply_async.call_args
62+ self.assertThat(kwargs['kwargs']['omapi_key'], EndsWith('=='))
63+
64 def test_dhcp_config_gets_written_when_nodegroup_becomes_active(self):
65 nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.PENDING)
66 self.patch(settings, "DHCP_CONNECT", True)
67
68=== modified file 'src/maasserver/tests/test_nodegroup.py'
69--- src/maasserver/tests/test_nodegroup.py 2012-10-08 12:50:32 +0000
70+++ src/maasserver/tests/test_nodegroup.py 2012-10-11 11:01:21 +0000
71@@ -38,6 +38,7 @@
72 )
73 from testresources import FixtureResource
74 from testtools.matchers import (
75+ EndsWith,
76 GreaterThan,
77 MatchesStructure,
78 )
79@@ -324,3 +325,26 @@
80 status=factory.getRandomEnum(NODEGROUP_STATUS))
81 nodegroup.reject()
82 self.assertEqual(nodegroup.status, NODEGROUP_STATUS.REJECTED)
83+
84+ def test_ensure_dhcp_key_creates_key(self):
85+ nodegroup = factory.make_node_group(dhcp_key='')
86+ nodegroup.ensure_dhcp_key()
87+ # Check that the dhcp_key is not empty and looks
88+ # valid.
89+ self.assertThat(nodegroup.dhcp_key, EndsWith("=="))
90+ # The key is persisted.
91+ self.assertThat(
92+ reload_object(nodegroup).dhcp_key, EndsWith("=="))
93+
94+ def test_ensure_dhcp_key_preserves_existing_key(self):
95+ key = factory.make_name('dhcp-key')
96+ nodegroup = factory.make_node_group(dhcp_key=key)
97+ nodegroup.ensure_dhcp_key()
98+ self.assertEqual(key, nodegroup.dhcp_key)
99+
100+ def test_ensure_dhcp_key_creates_different_keys(self):
101+ nodegroup1 = factory.make_node_group(dhcp_key='')
102+ nodegroup2 = factory.make_node_group(dhcp_key='')
103+ nodegroup1.ensure_dhcp_key()
104+ nodegroup2.ensure_dhcp_key()
105+ self.assertNotEqual(nodegroup1.dhcp_key, nodegroup2.dhcp_key)