diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-11-04 09:44:09 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-11-04 09:44:09 +0000 |
commit | f0c42a7cda2997c4ce81096ffc778f565e1c9d1d (patch) | |
tree | 9eedc939f88d78c643e250b11175524557748f60 /os_net_config | |
parent | 7f10579c4a75e3055adbbce4ae8c9557f545dc8d (diff) | |
parent | 455cabdee3158e7b2181c96e6d3b791f812c7fee (diff) |
Merge "Added natural sort for interfaces in utils.ordered_active_nics()"
Diffstat (limited to 'os_net_config')
-rw-r--r-- | os_net_config/tests/test_utils.py | 8 | ||||
-rw-r--r-- | os_net_config/utils.py | 13 |
2 files changed, 18 insertions, 3 deletions
diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py index e7326d3..b6531a6 100644 --- a/os_net_config/tests/test_utils.py +++ b/os_net_config/tests/test_utils.py @@ -33,7 +33,8 @@ class TestUtils(base.TestCase): return True self.stubs.Set(utils, '_is_active_nic', test_is_active_nic) - for nic in ['a1', 'em1', 'em2', 'eth2', 'z1']: + for nic in ['a1', 'em1', 'em2', 'eth2', 'z1', + 'enp8s0', 'enp10s0', 'enp1s0f0']: with open(os.path.join(tmpdir, nic), 'w') as f: f.write(nic) @@ -42,6 +43,9 @@ class TestUtils(base.TestCase): self.assertEqual('em2', nics[1]) self.assertEqual('eth2', nics[2]) self.assertEqual('a1', nics[3]) - self.assertEqual('z1', nics[4]) + self.assertEqual('enp1s0f0', nics[4]) + self.assertEqual('enp8s0', nics[5]) + self.assertEqual('enp10s0', nics[6]) + self.assertEqual('z1', nics[7]) shutil.rmtree(tmpdir) diff --git a/os_net_config/utils.py b/os_net_config/utils.py index f0ab307..ab7f3b3 100644 --- a/os_net_config/utils.py +++ b/os_net_config/utils.py @@ -17,6 +17,7 @@ import glob import logging import os +import re logger = logging.getLogger(__name__) @@ -75,6 +76,12 @@ def _is_active_nic(interface_name): return False +def _natural_sort_key(s): + nsre = re.compile('([0-9]+)') + return [int(text) if text.isdigit() else text + for text in re.split(nsre, s)] + + def ordered_active_nics(): embedded_nics = [] nics = [] @@ -86,7 +93,11 @@ def ordered_active_nics(): embedded_nics.append(nic) else: nics.append(nic) - return sorted(embedded_nics) + sorted(nics) + # NOTE: we could just natural sort all active devices, + # but this ensures em, eno, and eth are ordered first + # (more backwards compatible) + return (sorted(embedded_nics, key=_natural_sort_key) + + sorted(nics, key=_natural_sort_key)) def diff(filename, data): |