Merge lp:~mpontillo/maas/fix-long-bridge-names--bug-1672327 into lp:~maas-committers/maas/trunk

Proposed by Mike Pontillo
Status: Merged
Approved by: Mike Pontillo
Approved revision: no longer in the source branch.
Merged at revision: 5999
Proposed branch: lp:~mpontillo/maas/fix-long-bridge-names--bug-1672327
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 116 lines (+48/-7)
2 files modified
src/maasserver/models/interface.py (+19/-3)
src/maasserver/models/tests/test_interface.py (+29/-4)
To merge this branch: bzr merge lp:~mpontillo/maas/fix-long-bridge-names--bug-1672327
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+322797@code.launchpad.net

Commit message

When creating a bridge name, keep the name under 16 characters.

Uses the same algorithm as proposed for Juju in https://github.com/juju/juju/pull/7204.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good.

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :
Download full text (33.1 KiB)

The attempt to merge lp:~mpontillo/maas/fix-long-bridge-names--bug-1672327 into lp:maas failed. Below is the output from the failed tests.

Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Hit:2 http://prodstack-zone-2.clouds.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://prodstack-zone-2.clouds.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:4 http://prodstack-zone-2.clouds.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Fetched 306 kB in 0s (393 kB/s)
Reading package lists...
sudo DEBIAN_FRONTEND=noninteractive apt-get -y \
    --no-install-recommends install apache2 archdetect-deb authbind avahi-utils bash bind9 bind9utils build-essential bzr bzr-builddeb chromium-browser chromium-chromedriver curl daemontools debhelper dh-apport dh-systemd distro-info dnsutils firefox freeipmi-tools git gjs ipython isc-dhcp-common isc-dhcp-server libjs-angularjs libjs-jquery libjs-jquery-hotkeys libjs-yui3-full libjs-yui3-min libnss-wrapper libpq-dev make nodejs-legacy npm postgresql psmisc pxelinux python3-all python3-apt python3-attr python3-bson python3-convoy python3-crochet python3-cssselect python3-curtin python3-dev python3-distro-info python3-django python3-django-nose python3-django-piston3 python3-dnspython python3-docutils python3-formencode python3-hivex python3-httplib2 python3-jinja2 python3-jsonschema python3-lxml python3-netaddr python3-netifaces python3-novaclient python3-oauth python3-oauthlib python3-openssl python3-paramiko python3-petname python3-pexpect python3-psycopg2 python3-pyinotify python3-pyparsing python3-pyvmomi python3-requests python3-seamicroclient python3-setuptools python3-simplestreams python3-sphinx python3-tempita python3-twisted python3-txtftp python3-tz python3-yaml python3-zope.interface python-bson python-crochet python-django python-django-piston python-djorm-ext-pgarray python-formencode python-lxml python-netaddr python-netifaces python-pocket-lint python-psycopg2 python-simplejson python-tempita python-twisted python-yaml socat syslinux-common tgt ubuntu-cloudimage-keyring wget xvfb
Reading package lists...
Building dependency tree...
Reading state information...
authbind is already the newest version (2.1.1+nmu1).
avahi-utils is already the newest version (0.6.32~rc+dfsg-1ubuntu2).
build-essential is already the newest version (12.1ubuntu2).
debhelper is already the newest version (9.20160115ubuntu3).
distro-info is already the newest version (0.14build1).
git is already the newest version (1:2.7.4-0ubuntu1).
libjs-angularjs is already the newest version (1.2.28-1ubuntu2).
libjs-jquery is already the newest version (1.11.3+dfsg-4).
libjs-yui3-full is already the newest version (3.5.1-1ubuntu3).
libjs-yui3-min is already the newest version (3.5.1-1ubuntu3).
make is already the newest version (4.1-6).
postgresql is already the newest version (9.5+173).
psmisc is already the newest version (22.21-2.1build1).
pxelinux is already the newest version (3:6.03+dfsg-11ubuntu1).
python-formencode is already the newest version (1.3.0-0ubuntu5).
python-lxml is already the newest version (3.5.0-1build1).
python-netaddr is already the newest version (0.7.18-1...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/interface.py'
2--- src/maasserver/models/interface.py 2017-04-17 14:11:28 +0000
3+++ src/maasserver/models/interface.py 2017-04-20 03:18:37 +0000
4@@ -13,6 +13,7 @@
5 ]
6
7 from collections import OrderedDict
8+from zlib import crc32
9
10 from django.contrib.postgres.fields import ArrayField
11 from django.core.exceptions import (
12@@ -1182,6 +1183,21 @@
13 auto_ip.save()
14 return subnet, auto_ip
15
16+ def get_default_bridge_name(self):
17+ """Returns the default name for a bridge created on this interface."""
18+ # This is a fix for bug #1672327, consistent with Juju's idea of what
19+ # the bridge name should be.
20+ ifname = self.get_name().encode('utf-8')
21+ name = b"br-%s" % ifname
22+ if len(name) > 15:
23+ name = b"b-%s" % ifname
24+ if ifname[:2] == b'en':
25+ name = b"b-%s" % ifname[2:]
26+ if len(name) > 15:
27+ ifname_hash = (b"%06x" % (crc32(ifname) & 0xffffffff))[-6:]
28+ name = b"b-%s-%s" % (ifname_hash, ifname[len(ifname) - 6:])
29+ return name.decode('utf-8')
30+
31 def create_acquired_bridge(self, bridge_stp=None, bridge_fd=None):
32 """Create an acquired bridge on top of this interface.
33
34@@ -1200,10 +1216,10 @@
35 }
36 if 'mtu' in self.params:
37 params['mtu'] = self.params['mtu']
38+ name = self.get_default_bridge_name()
39 bridge = BridgeInterface(
40- name="br-%s" % self.get_name(), node=self.node,
41- mac_address=self.mac_address, vlan=self.vlan,
42- enabled=True, acquired=True, params=params)
43+ name=name, node=self.node, mac_address=self.mac_address,
44+ vlan=self.vlan, enabled=True, acquired=True, params=params)
45 bridge.save()
46 # The order in which the creating and linkage between child and parent
47 # is important. The IP addresses must first be moved from this
48
49=== modified file 'src/maasserver/models/tests/test_interface.py'
50--- src/maasserver/models/tests/test_interface.py 2017-03-29 21:13:51 +0000
51+++ src/maasserver/models/tests/test_interface.py 2017-04-20 03:18:37 +0000
52@@ -1409,7 +1409,7 @@
53 parent.save()
54 bridge = parent.create_acquired_bridge()
55 self.assertThat(bridge, MatchesStructure(
56- name=Equals("br-%s" % parent.get_name()),
57+ name=Equals("%s" % parent.get_default_bridge_name()),
58 mac_address=Equals(parent.mac_address),
59 node=Equals(parent.node),
60 vlan=Equals(parent.vlan),
61@@ -2986,7 +2986,7 @@
62 parent = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
63 bridge = parent.create_acquired_bridge()
64 self.assertThat(bridge, MatchesStructure(
65- name=Equals("br-%s" % parent.get_name()),
66+ name=Equals("%s" % parent.get_default_bridge_name()),
67 mac_address=Equals(parent.mac_address),
68 node=Equals(parent.node),
69 vlan=Equals(parent.vlan),
70@@ -3007,7 +3007,7 @@
71 bridge = parent.create_acquired_bridge(
72 bridge_stp=bridge_stp, bridge_fd=bridge_fd)
73 self.assertThat(bridge, MatchesStructure(
74- name=Equals("br-%s" % parent.get_name()),
75+ name=Equals("%s" % parent.get_default_bridge_name()),
76 mac_address=Equals(parent.mac_address),
77 node=Equals(parent.node),
78 vlan=Equals(parent.vlan),
79@@ -3029,7 +3029,7 @@
80 alloc_type=IPADDRESS_TYPE.STICKY, interface=parent)
81 bridge = parent.create_acquired_bridge()
82 self.assertThat(bridge, MatchesStructure(
83- name=Equals("br-%s" % parent.get_name()),
84+ name=Equals("%s" % parent.get_default_bridge_name()),
85 mac_address=Equals(parent.mac_address),
86 node=Equals(parent.node),
87 vlan=Equals(parent.vlan),
88@@ -3179,3 +3179,28 @@
89 "Automatically created VLAN (observed by %s)." % (
90 iface.get_log_string()),
91 new_vlan.description)
92+
93+
94+class TestInterfaceGetDefaultBridgeName(MAASServerTestCase):
95+
96+ # Normally we would use scenarios for this, but this was copied and
97+ # pasted from Juju code in bridgepolicy_test.go.
98+ expected_bridge_names = {
99+ "eno0": "br-eno0",
100+ "twelvechars0": "br-twelvechars0",
101+ "thirteenchars": "b-thirteenchars",
102+ "enfourteenchar": "b-fourteenchar",
103+ "enfifteenchars0": "b-fifteenchars0",
104+ "fourteenchars1": "b-5590a4-chars1",
105+ "fifteenchars.12": "b-7e0acf-ars.12",
106+ "zeros0526193032": "b-000000-193032",
107+ "enx00e07cc81e1d": "b-x00e07cc81e1d",
108+ }
109+
110+ def test__returns_expected_bridge_names_consistent_with_juju(self):
111+ interface = factory.make_Interface()
112+ for ifname, expected_bridge_name in self.expected_bridge_names.items():
113+ interface.name = ifname
114+ self.assertThat(
115+ interface.get_default_bridge_name(),
116+ Equals(expected_bridge_name))