Merge lp:~jtv/maas/register-mac-type into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 1575
Proposed branch: lp:~jtv/maas/register-mac-type
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~jtv/maas/router-macs
Diff against target: 118 lines (+23/-6)
4 files modified
src/maasserver/fields.py (+1/-5)
src/maasserver/start_up.py (+5/-0)
src/maasserver/testing/testcase.py (+6/-0)
src/maasserver/tests/test_fields.py (+11/-1)
To merge this branch: bzr merge lp:~jtv/maas/register-mac-type
Reviewer Review Type Date Requested Status
Julian Edwards (community) Approve
Review via email: mp+181723@code.launchpad.net

Commit message

Register MAC type with psycopg during program startup, not while importing modules.

Description of the change

It's not appropriate for module-level code to attempt to connect the database. It's an uncontrolled environment, and lint checkers may exercise the code by accident.

I did not try this out with a packaged install (although the tests should ensure that that will work) because we don't have a package with the requisite "array" extension yet.

Jeroen

To post a comment you must log in.
Revision history for this message
Julian Edwards (julian-edwards) wrote :

 review: approve

Looks good.

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

No proposals found for merge of lp:~jtv/maas/router-macs into lp:maas.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/fields.py'
2--- src/maasserver/fields.py 2013-08-23 07:03:07 +0000
3+++ src/maasserver/fields.py 2013-08-23 07:03:07 +0000
4@@ -14,6 +14,7 @@
5 "MAC",
6 "MACAddressField",
7 "MACAddressFormField",
8+ "register_mac_type",
9 ]
10
11 from copy import deepcopy
12@@ -25,7 +26,6 @@
13
14 from django.core.exceptions import ValidationError
15 from django.core.validators import RegexValidator
16-from django.db import connection
17 from django.db.models import (
18 Field,
19 SubfieldBase,
20@@ -234,10 +234,6 @@
21 (oid, ), b"macaddr", mac_caster))
22
23
24-# TODO bug=1215446: Don't do this at the module level!
25-register_mac_type(connection.cursor())
26-
27-
28 class JSONObjectField(Field):
29 """A field that will store any jsonizable python object."""
30
31
32=== modified file 'src/maasserver/start_up.py'
33--- src/maasserver/start_up.py 2013-05-28 16:06:35 +0000
34+++ src/maasserver/start_up.py 2013-08-23 07:03:07 +0000
35@@ -17,6 +17,7 @@
36
37 from textwrap import dedent
38
39+from django.db import connection
40 from lockfile import FileLock
41 from maasserver.components import (
42 get_persistent_error,
43@@ -24,6 +25,7 @@
44 )
45 from maasserver.dns import write_full_dns_config
46 from maasserver.enum import COMPONENT
47+from maasserver.fields import register_mac_type
48 from maasserver.maasavahi import setup_maas_avahi_service
49 from maasserver.models import (
50 BootImage,
51@@ -79,6 +81,9 @@
52
53 def inner_start_up():
54 """Startup jobs that must run serialized w.r.t. other starting servers."""
55+ # Register our MAC data type with psycopg.
56+ register_mac_type(connection.cursor())
57+
58 # Publish the MAAS server existence over Avahi.
59 setup_maas_avahi_service()
60
61
62=== modified file 'src/maasserver/testing/testcase.py'
63--- src/maasserver/testing/testcase.py 2013-06-14 16:03:35 +0000
64+++ src/maasserver/testing/testcase.py 2013-08-23 07:03:07 +0000
65@@ -24,8 +24,10 @@
66 import django
67 from django.core.cache import cache as django_cache
68 from django.core.urlresolvers import reverse
69+from django.db import connection
70 from django.test.client import encode_multipart
71 from fixtures import Fixture
72+from maasserver.fields import register_mac_type
73 from maasserver.testing.factory import factory
74 from maastesting.celery import CeleryFixture
75 import maastesting.djangotestcase
76@@ -43,6 +45,10 @@
77 class TestCase(maastesting.djangotestcase.DjangoTestCase):
78 """:class:`TestCase` variant with the basics for maasserver testing."""
79
80+ @classmethod
81+ def setUpClass(cls):
82+ register_mac_type(connection.cursor())
83+
84 def setUp(self):
85 super(TestCase, self).setUp()
86 self.useFixture(WorkerCacheFixture())
87
88=== modified file 'src/maasserver/tests/test_fields.py'
89--- src/maasserver/tests/test_fields.py 2013-08-23 07:03:07 +0000
90+++ src/maasserver/tests/test_fields.py 2013-08-23 07:03:07 +0000
91@@ -13,10 +13,14 @@
92 __all__ = []
93
94 from django.core.exceptions import ValidationError
95-from django.db import DatabaseError
96+from django.db import (
97+ connection,
98+ DatabaseError,
99+ )
100 from maasserver.fields import (
101 MAC,
102 NodeGroupFormField,
103+ register_mac_type,
104 validate_mac,
105 )
106 from maasserver.models import (
107@@ -160,6 +164,12 @@
108 set([MAC(addr), MAC(addr), MAC(MAC(addr)), addr]),
109 [addr])
110
111+ def test_register_mac_type_is_idempotent(self):
112+ register_mac_type(connection.cursor())
113+ register_mac_type(connection.cursor())
114+ # The test is that we get here without crashing.
115+ pass
116+
117
118 class TestMACAddressField(TestCase):
119