summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r--tests/test_utils.py96
1 files changed, 75 insertions, 21 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py
index b958d58..7cf1efc 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -26,6 +26,7 @@ import netifaces
from contextlib import redirect_stdout
from netplan.cli.core import Netplan
import netplan.cli.utils as utils
+import netplan.libnetplan as libnetplan
from unittest.mock import patch
@@ -70,11 +71,11 @@ printf '\\0' >> %(log)s
def set_output(self, output):
with open(self.path, "a") as fp:
- fp.write("cat << EOF\n%s\nEOF" % output)
+ fp.write("\ncat << EOF\n%s\nEOF" % output)
def touch(self, stamp_path):
with open(self.path, "a") as fp:
- fp.write("touch %s\n" % stamp_path)
+ fp.write("\ntouch %s\n" % stamp_path)
def set_timeout(self, timeout_dsec=10):
with open(self.path, "a") as fp:
@@ -94,7 +95,7 @@ fi
def set_returncode(self, returncode):
with open(self.path, "a") as fp:
- fp.write("exit %d" % returncode)
+ fp.write("\nexit %d" % returncode)
def call_cli(args):
@@ -113,10 +114,21 @@ class TestUtils(unittest.TestCase):
def setUp(self):
self.workdir = tempfile.TemporaryDirectory()
- os.makedirs(os.path.join(self.workdir.name, 'etc/netplan'))
+ self.confdir = os.path.join(self.workdir.name, 'etc/netplan')
+ self.default_conf = os.path.join(self.confdir, 'a.yaml')
+ os.makedirs(self.confdir)
os.makedirs(os.path.join(self.workdir.name,
'run/NetworkManager/system-connections'))
+ def load_conf(self, conf_txt):
+ with open(self.default_conf, 'w') as f:
+ f.write(conf_txt)
+ parser = libnetplan.Parser()
+ parser.load_yaml_hierarchy(rootdir=self.workdir.name)
+ state = libnetplan.State()
+ state.import_parser_results(parser)
+ return state
+
def _create_nm_keyfile(self, filename, ifname):
with open(os.path.join(self.workdir.name,
'run/NetworkManager/system-connections/', filename), 'w') as f:
@@ -155,36 +167,70 @@ class TestUtils(unittest.TestCase):
self.assertTrue('ens4' in ifaces)
self.assertTrue(len(ifaces) == 4)
- def test_find_matching_iface_too_many(self):
+ # For the matching tests, we mock out the functions querying extra data
+ @patch('netplan.cli.utils.get_interface_driver_name')
+ @patch('netplan.cli.utils.get_interface_macaddress')
+ def test_find_matching_iface_too_many(self, gim, gidn):
+ gidn.side_effect = lambda x: 'foo' if x == 'ens4' else 'bar'
+ gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'eth1' else '00:00:00:00:00:00'
+
+ state = self.load_conf('''network:
+ ethernets:
+ netplan-id:
+ match:
+ name: "e*"''')
# too many matches
- iface = utils.find_matching_iface(DEVICES, {'name': 'e*'})
+ iface = utils.find_matching_iface(DEVICES, state['netplan-id'])
self.assertEqual(iface, None)
+ @patch('netplan.cli.utils.get_interface_driver_name')
@patch('netplan.cli.utils.get_interface_macaddress')
- def test_find_matching_iface(self, gim):
+ def test_find_matching_iface(self, gim, gidn):
# we mock-out get_interface_macaddress to return useful values for the test
+ gidn.side_effect = lambda x: 'foo' if x == 'ens4' else 'bar'
gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'eth1' else '00:00:00:00:00:00'
- match = {'name': 'e*', 'macaddress': '00:01:02:03:04:05'}
- iface = utils.find_matching_iface(DEVICES, match)
+ state = self.load_conf('''network:
+ ethernets:
+ netplan-id:
+ match:
+ name: "e*"
+ macaddress: "00:01:02:03:04:05"''')
+
+ iface = utils.find_matching_iface(DEVICES, state['netplan-id'])
self.assertEqual(iface, 'eth1')
@patch('netplan.cli.utils.get_interface_driver_name')
- def test_find_matching_iface_name_and_driver(self, gidn):
- # we mock-out get_interface_driver_name to return useful values for the test
+ @patch('netplan.cli.utils.get_interface_macaddress')
+ def test_find_matching_iface_name_and_driver(self, gim, gidn):
gidn.side_effect = lambda x: 'foo' if x == 'ens4' else 'bar'
+ gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'eth1' else '00:00:00:00:00:00'
+
+ state = self.load_conf('''network:
+ ethernets:
+ netplan-id:
+ match:
+ name: "ens?"
+ driver: "f*"''')
- match = {'name': 'ens?', 'driver': 'f*'}
- iface = utils.find_matching_iface(DEVICES, match)
+ iface = utils.find_matching_iface(DEVICES, state['netplan-id'])
self.assertEqual(iface, 'ens4')
@patch('netplan.cli.utils.get_interface_driver_name')
- def test_find_matching_iface_name_and_drivers(self, gidn):
+ @patch('netplan.cli.utils.get_interface_macaddress')
+ def test_find_matching_iface_name_and_drivers(self, gim, gidn):
# we mock-out get_interface_driver_name to return useful values for the test
gidn.side_effect = lambda x: 'foo' if x == 'ens4' else 'bar'
+ gim.side_effect = lambda x: '00:01:02:03:04:05'
- match = {'name': 'ens?', 'driver': ['baz', 'f*', 'quux']}
- iface = utils.find_matching_iface(DEVICES, match)
+ state = self.load_conf('''network:
+ ethernets:
+ netplan-id:
+ match:
+ name: "ens?"
+ driver: ["baz", "f*", "quux"]''')
+
+ iface = utils.find_matching_iface(DEVICES, state['netplan-id'])
self.assertEqual(iface, 'ens4')
@patch('netifaces.ifaddresses')
@@ -215,17 +261,25 @@ class TestUtils(unittest.TestCase):
174 wwan0 wwan off linger''')
res = utils.networkd_interfaces()
self.assertEquals(self.mock_networkctl.calls(), [['networkctl', '--no-pager', '--no-legend']])
- self.assertIn('wlan0', res)
- self.assertIn('ens3', res)
+ self.assertIn('2', res)
+ self.assertIn('3', res)
+
+ def test_networkctl_reload(self):
+ self.mock_networkctl = MockCmd('networkctl')
+ path_env = os.environ['PATH']
+ os.environ['PATH'] = os.path.dirname(self.mock_networkctl.path) + os.pathsep + path_env
+ utils.networkctl_reload()
+ self.assertEquals(self.mock_networkctl.calls(), [
+ ['networkctl', 'reload']
+ ])
def test_networkctl_reconfigure(self):
self.mock_networkctl = MockCmd('networkctl')
path_env = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(self.mock_networkctl.path) + os.pathsep + path_env
- utils.networkctl_reconfigure(['eth0', 'eth1'])
+ utils.networkctl_reconfigure(['3', '5'])
self.assertEquals(self.mock_networkctl.calls(), [
- ['networkctl', 'reload'],
- ['networkctl', 'reconfigure', 'eth0', 'eth1']
+ ['networkctl', 'reconfigure', '3', '5']
])
def test_is_nm_snap_enabled(self):