diff options
author | 2014-08-20 13:10:40 -0400 | |
---|---|---|
committer | 2014-08-20 13:10:40 -0400 | |
commit | b3e74611befb157dc4bf992f23aaba346e655a86 (patch) | |
tree | 30807f86f1e54e10576780611772ec790807f729 /os_net_config/tests | |
parent | 2d3af95651223ca142e99c9c2bbe284481e3f33d (diff) |
Add nic1, nic2 naming abstraction
Implements a new active NIC abstraction and naming convention
that allows nic1, nic2, etc. to be translated to actual (active)
network device names like em1, em2 (or eth0, eth1).
This includes some logic to map ordered active nics to the
nic1, nic2 naming scheme. Embedded nics are always listed
first (in sort order) followed by any other active Nics
on the system.
With the new code:
{"type": "interface", "name": "nic1" }
is automatically translated (internally) to:
{"type": "interface", "name": "em1" }
This works for all top level "interface" devices, vlans, bonds, and
bridges alike. For vlans the 'device' name is translated instead
of the device name per vlan object conventions.
Diffstat (limited to 'os_net_config/tests')
-rw-r--r-- | os_net_config/tests/base.py | 7 | ||||
-rw-r--r-- | os_net_config/tests/test_objects.py | 75 | ||||
-rw-r--r-- | os_net_config/tests/test_utils.py | 47 |
3 files changed, 129 insertions, 0 deletions
diff --git a/os_net_config/tests/base.py b/os_net_config/tests/base.py index c8ad7c4..1e87ba2 100644 --- a/os_net_config/tests/base.py +++ b/os_net_config/tests/base.py @@ -21,6 +21,8 @@ import fixtures import stubout import testtools +from os_net_config import objects + _TRUE_VALUES = ('True', 'true', '1', 'yes') @@ -33,6 +35,11 @@ class TestCase(testtools.TestCase): super(TestCase, self).setUp() self.stubs = stubout.StubOutForTesting() + + def test_numbered_nics(): + return {} + self.stubs.Set(objects, '_numbered_nics', test_numbered_nics) + test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) try: test_timeout = int(test_timeout) diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index 830a809..2f43997 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -92,6 +92,16 @@ class TestInterface(base.TestCase): self.assertEqual("em1", interface.name) self.assertEqual(True, interface.use_dhcp) + def test_from_json_dhcp_nic1(self): + def test_numbered_nics(): + return {"nic1": "em3"} + self.stubs.Set(objects, '_numbered_nics', test_numbered_nics) + + data = '{"type": "interface", "name": "nic1", "use_dhcp": true}' + interface = objects.object_from_json(json.loads(data)) + self.assertEqual("em3", interface.name) + self.assertEqual(True, interface.use_dhcp) + def test_from_json_with_addresses(self): data = """{ "type": "interface", @@ -130,6 +140,18 @@ class TestVlan(base.TestCase): self.assertEqual(16, vlan.vlan_id) self.assertEqual(True, vlan.use_dhcp) + def test_from_json_dhcp_nic1(self): + def test_numbered_nics(): + return {"nic1": "em4"} + self.stubs.Set(objects, '_numbered_nics', test_numbered_nics) + + data = '{"type": "vlan", "device": "nic1", "vlan_id": 16,' \ + '"use_dhcp": true}' + vlan = objects.object_from_json(json.loads(data)) + self.assertEqual("em4", vlan.device) + self.assertEqual(16, vlan.vlan_id) + self.assertEqual(True, vlan.use_dhcp) + class TestBridge(base.TestCase): @@ -152,6 +174,29 @@ class TestBridge(base.TestCase): self.assertEqual(True, interface1.ovs_port) self.assertEqual("br-foo", interface1.bridge_name) + def test_from_json_dhcp_with_nic1(self): + def test_numbered_nics(): + return {"nic1": "em5"} + self.stubs.Set(objects, '_numbered_nics', test_numbered_nics) + + data = """{ +"type": "ovs_bridge", +"name": "br-foo", +"use_dhcp": true, +"members": [{ + "type": "interface", + "name": "nic1" +}] +} +""" + bridge = objects.object_from_json(json.loads(data)) + self.assertEqual("br-foo", bridge.name) + self.assertEqual(True, bridge.use_dhcp) + interface1 = bridge.members[0] + self.assertEqual("em5", interface1.name) + self.assertEqual(True, interface1.ovs_port) + self.assertEqual("br-foo", interface1.bridge_name) + def test_from_json_primary_interface(self): data = """{ "type": "ovs_bridge", @@ -210,3 +255,33 @@ class TestBond(base.TestCase): self.assertEqual("em1", interface1.name) interface2 = bridge.members[1] self.assertEqual("em2", interface2.name) + + def test_from_json_dhcp_with_nic1_nic2(self): + + def test_numbered_nics(): + return {"nic1": "em1", "nic2": "em2"} + self.stubs.Set(objects, '_numbered_nics', test_numbered_nics) + + data = """{ +"type": "ovs_bond", +"name": "bond1", +"use_dhcp": true, +"members": [ + { + "type": "interface", + "name": "nic1" + }, + { + "type": "interface", + "name": "nic2" + } +] +} +""" + bridge = objects.object_from_json(json.loads(data)) + self.assertEqual("bond1", bridge.name) + self.assertEqual(True, bridge.use_dhcp) + interface1 = bridge.members[0] + self.assertEqual("em1", interface1.name) + interface2 = bridge.members[1] + self.assertEqual("em2", interface2.name) diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py new file mode 100644 index 0000000..e7326d3 --- /dev/null +++ b/os_net_config/tests/test_utils.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +# Copyright 2014 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os.path +import shutil +import tempfile + +from os_net_config.tests import base +from os_net_config import utils + + +class TestUtils(base.TestCase): + + def test_ordered_active_nics(self): + + tmpdir = tempfile.mkdtemp() + self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + + def test_is_active_nic(interface_name): + return True + self.stubs.Set(utils, '_is_active_nic', test_is_active_nic) + + for nic in ['a1', 'em1', 'em2', 'eth2', 'z1']: + with open(os.path.join(tmpdir, nic), 'w') as f: + f.write(nic) + + nics = utils.ordered_active_nics() + self.assertEqual('em1', nics[0]) + self.assertEqual('em2', nics[1]) + self.assertEqual('eth2', nics[2]) + self.assertEqual('a1', nics[3]) + self.assertEqual('z1', nics[4]) + + shutil.rmtree(tmpdir) |