Merge ~smoser/cloud-init:bug/1705147-rename-support-mixed-case-macs into cloud-init:master

Proposed by Scott Moser
Status: Merged
Merged at revision: c0060fe4892197179a5cfbfd3239cf3b6c3e5029
Proposed branch: ~smoser/cloud-init:bug/1705147-rename-support-mixed-case-macs
Merge into: cloud-init:master
Diff against target: 77 lines (+33/-2)
2 files modified
cloudinit/net/__init__.py (+6/-2)
tests/unittests/test_net.py (+27/-0)
Reviewer Review Type Date Requested Status
Ryan Harper Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+327714@code.launchpad.net

Commit message

net: fix renaming of nics to support mac addresses written in upper case.

The network device renaming code previously required the case of
the mac address input to match that of the data read from the system.
For example, if user provided network config with mac address
in upper case, then cloud-init would not rename the device correctly
as /sys/class/net/address stores lower case values.

The fix here is to always compare lower case mac addresses.

LP: #1705147

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:c0060fe4892197179a5cfbfd3239cf3b6c3e5029
https://jenkins.ubuntu.com/server/job/cloud-init-ci/55/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: CentOS 6 & 7: Build & Test
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/55/rebuild

review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
2index cba991a..d1740e5 100644
3--- a/cloudinit/net/__init__.py
4+++ b/cloudinit/net/__init__.py
5@@ -302,7 +302,7 @@ def _get_current_rename_info(check_downable=True):
6 device has only automatically assigned ip addrs.
7 'device_id': Device id value (if it has one)
8 'driver': Device driver (if it has one)
9- 'mac': mac address
10+ 'mac': mac address (in lower case)
11 'name': name
12 'up': boolean: is_up(name)
13 }}
14@@ -313,7 +313,7 @@ def _get_current_rename_info(check_downable=True):
15 'downable': None,
16 'device_id': device_id,
17 'driver': driver,
18- 'mac': mac,
19+ 'mac': mac.lower(),
20 'name': name,
21 'up': is_up(name),
22 }
23@@ -348,6 +348,8 @@ def _rename_interfaces(renames, strict_present=True, strict_busy=True,
24 cur_info = {}
25 for name, data in current_info.items():
26 cur = data.copy()
27+ if cur.get('mac'):
28+ cur['mac'] = cur['mac'].lower()
29 cur['name'] = name
30 cur_info[name] = cur
31
32@@ -399,6 +401,8 @@ def _rename_interfaces(renames, strict_present=True, strict_busy=True,
33 return None
34
35 for mac, new_name, driver, device_id in renames:
36+ if mac:
37+ mac = mac.lower()
38 cur_ops = []
39 cur = find_entry(mac, driver, device_id)
40 if not cur:
41diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
42index 71c9c45..76721ba 100644
43--- a/tests/unittests/test_net.py
44+++ b/tests/unittests/test_net.py
45@@ -2176,5 +2176,32 @@ class TestRenameInterfaces(CiTestCase):
46 capture=True),
47 ])
48
49+ @mock.patch('cloudinit.util.subp')
50+ def test_rename_macs_case_insensitive(self, mock_subp):
51+ """_rename_interfaces must support upper or lower case macs."""
52+ renames = [
53+ ('aa:aa:aa:aa:aa:aa', 'en0', None, None),
54+ ('BB:BB:BB:BB:BB:BB', 'en1', None, None),
55+ ('cc:cc:cc:cc:cc:cc', 'en2', None, None),
56+ ('DD:DD:DD:DD:DD:DD', 'en3', None, None),
57+ ]
58+ current_info = {
59+ 'eth0': {'downable': True, 'mac': 'AA:AA:AA:AA:AA:AA',
60+ 'name': 'eth0', 'up': False},
61+ 'eth1': {'downable': True, 'mac': 'bb:bb:bb:bb:bb:bb',
62+ 'name': 'eth1', 'up': False},
63+ 'eth2': {'downable': True, 'mac': 'cc:cc:cc:cc:cc:cc',
64+ 'name': 'eth2', 'up': False},
65+ 'eth3': {'downable': True, 'mac': 'DD:DD:DD:DD:DD:DD',
66+ 'name': 'eth3', 'up': False},
67+ }
68+ net._rename_interfaces(renames, current_info=current_info)
69+
70+ expected = [
71+ mock.call(['ip', 'link', 'set', 'eth%d' % i, 'name', 'en%d' % i],
72+ capture=True)
73+ for i in range(len(renames))]
74+ mock_subp.assert_has_calls(expected)
75+
76
77 # vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches