class TestConfigManager(unittest.TestCase): def setUp(self): self.workdir = tempfile.TemporaryDirectory() self.configmanager = ConfigManager(prefix=self.workdir.name, extra_files={}) os.makedirs(os.path.join(self.workdir.name, "etc/netplan")) os.makedirs(os.path.join(self.workdir.name, "run/systemd/network")) os.makedirs( os.path.join(self.workdir.name, "run/NetworkManager/system-connections")) with open(os.path.join(self.workdir.name, "newfile.yaml"), 'w') as fd: print('''network: version: 2 ethernets: ethtest: dhcp4: yes ''', file=fd) with open(os.path.join(self.workdir.name, "newfile_merging.yaml"), 'w') as fd: print('''network: version: 2 ethernets: eth0: dhcp6: on ethbr1: dhcp4: on ''', file=fd) with open(os.path.join(self.workdir.name, "newfile_emptydict.yaml"), 'w') as fd: print('''network: version: 2 ethernets: eth0: {} bridges: br666: {} ''', file=fd) with open(os.path.join(self.workdir.name, "ovs_merging.yaml"), 'w') as fd: print('''network: version: 2 openvswitch: ports: [[patchx, patcha], [patchy, patchb]] bridges: ovs0: {openvswitch: {}} ''', file=fd) with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"), 'w') as fd: print('''network: version: 2 renderer: networkd openvswitch: ports: [[patcha, patchb]] other-config: disable-in-band: true ethernets: eth0: dhcp4: false ethbr1: dhcp4: false ethbr2: dhcp4: false ethbond1: dhcp4: false ethbond2: dhcp4: false wifis: wlan1: access-points: testAP: {} modems: wwan0: apn: internet pin: 1234 dhcp4: yes addresses: [1.2.3.4/24, 5.6.7.8/24] vlans: vlan2: id: 2 link: eth99 bridges: br3: interfaces: [ ethbr1 ] br4: interfaces: [ ethbr2 ] parameters: stp: on bonds: bond5: interfaces: [ ethbond1 ] bond6: interfaces: [ ethbond2 ] parameters: mode: 802.3ad tunnels: he-ipv6: mode: sit remote: 2.2.2.2 local: 1.1.1.1 addresses: - "2001:dead:beef::2/64" gateway6: "2001:dead:beef::1" ''', file=fd) with open( os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'w') as fd: print("pretend .network", file=fd) with open( os.path.join(self.workdir.name, "run/NetworkManager/system-connections/pretend"), 'w') as fd: print("pretend NM config", file=fd) def test_parse(self): self.configmanager.parse() self.assertIn('eth0', self.configmanager.ethernets) self.assertIn('bond6', self.configmanager.bonds) self.assertIn('eth0', self.configmanager.physical_interfaces) self.assertNotIn('bond7', self.configmanager.interfaces) self.assertNotIn('bond6', self.configmanager.physical_interfaces) self.assertNotIn('parameters', self.configmanager.bonds.get('bond5')) self.assertIn('parameters', self.configmanager.bonds.get('bond6')) self.assertIn('wwan0', self.configmanager.modems) self.assertIn('wwan0', self.configmanager.physical_interfaces) self.assertIn('apn', self.configmanager.modems.get('wwan0')) self.assertIn('he-ipv6', self.configmanager.tunnels) self.assertNotIn('he-ipv6', self.configmanager.physical_interfaces) self.assertIn('remote', self.configmanager.tunnels.get('he-ipv6')) self.assertIn('other-config', self.configmanager.openvswitch) self.assertIn('ports', self.configmanager.openvswitch) self.assertEquals(2, self.configmanager.version) self.assertEquals('networkd', self.configmanager.renderer) def test_parse_merging(self): self.configmanager.parse(extra_config=[ os.path.join(self.workdir.name, "newfile_merging.yaml") ]) self.assertIn('eth0', self.configmanager.ethernets) self.assertIn('dhcp4', self.configmanager.ethernets['eth0']) self.assertEquals(True, self.configmanager.ethernets['eth0'].get('dhcp6')) self.assertEquals(True, self.configmanager.ethernets['ethbr1'].get('dhcp4')) def test_parse_merging_ovs(self): self.configmanager.parse( extra_config=[os.path.join(self.workdir.name, "ovs_merging.yaml")]) self.assertIn('eth0', self.configmanager.ethernets) self.assertIn('dhcp4', self.configmanager.ethernets['eth0']) self.assertIn('patchx', self.configmanager.ovs_ports) self.assertIn('patchy', self.configmanager.ovs_ports) self.assertIn('ovs0', self.configmanager.bridges) self.assertEqual( {}, self.configmanager.ovs_ports['patchx'].get('openvswitch')) self.assertEqual( {}, self.configmanager.ovs_ports['patchy'].get('openvswitch')) self.assertEqual({}, self.configmanager.bridges['ovs0'].get('openvswitch')) def test_parse_emptydict(self): self.configmanager.parse(extra_config=[ os.path.join(self.workdir.name, "newfile_emptydict.yaml") ]) self.assertIn('br666', self.configmanager.bridges) self.assertEquals(False, self.configmanager.ethernets['eth0'].get('dhcp4')) self.assertEquals(False, self.configmanager.ethernets['ethbr1'].get('dhcp4')) def test_parse_extra_config(self): self.configmanager.parse( extra_config=[os.path.join(self.workdir.name, "newfile.yaml")]) self.assertIn('ethtest', self.configmanager.ethernets) self.assertIn('bond6', self.configmanager.bonds) def test_add(self): self.configmanager.add({ os.path.join(self.workdir.name, "newfile.yaml"): os.path.join(self.workdir.name, "etc/netplan/newfile.yaml") }) self.assertIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertTrue( os.path.exists( os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) def test_backup_missing_dirs(self): backup_dir = self.configmanager.tempdir shutil.rmtree(os.path.join(self.workdir.name, "run/systemd/network")) self.configmanager.backup(backup_config_dir=False) self.assertTrue( os.path.exists( os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) # no source dir means no backup as well self.assertFalse( os.path.exists( os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertFalse( os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_backup_without_config_file(self): backup_dir = self.configmanager.tempdir self.configmanager.backup(backup_config_dir=False) self.assertTrue( os.path.exists( os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) self.assertTrue( os.path.exists( os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertFalse( os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_backup_with_config_file(self): backup_dir = self.configmanager.tempdir self.configmanager.backup(backup_config_dir=True) self.assertTrue( os.path.exists( os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) self.assertTrue( os.path.exists( os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertTrue( os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_revert(self): self.configmanager.backup() with open( os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'a+') as fd: print("CHANGED", file=fd) with open( os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'r') as fd: lines = fd.readlines() self.assertIn("CHANGED\n", lines) self.configmanager.revert() with open( os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'r') as fd: lines = fd.readlines() self.assertNotIn("CHANGED\n", lines) def test_revert_extra_files(self): self.configmanager.add({ os.path.join(self.workdir.name, "newfile.yaml"): os.path.join(self.workdir.name, "etc/netplan/newfile.yaml") }) self.assertIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertTrue( os.path.exists( os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) self.configmanager.revert() self.assertNotIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertFalse( os.path.exists( os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) def test_cleanup(self): backup_dir = self.configmanager.tempdir self.assertTrue(os.path.exists(backup_dir)) self.configmanager.cleanup() self.assertFalse(os.path.exists(backup_dir)) def test__copy_tree(self): self.configmanager._copy_tree(os.path.join(self.workdir.name, "etc"), os.path.join(self.workdir.name, "etc2")) self.assertTrue( os.path.exists( os.path.join(self.workdir.name, "etc2/netplan/test.yaml"))) def test__copy_tree_missing_source(self): with self.assertRaises(FileNotFoundError): self.configmanager._copy_tree(os.path.join(self.workdir.name, "nonexistent"), os.path.join(self.workdir.name, "nonexistent2"), missing_ok=False)
class NetplanTry(utils.NetplanCommand): def __init__(self): super().__init__(command_id='try', description='Try to apply a new netplan config to running ' 'system, with automatic rollback', leaf=True) self.configuration_changed = False self.new_interfaces = None self.config_manager = ConfigManager() def run(self): # pragma: nocover (requires user input) self.parser.add_argument('--config-file', help='Apply the config file in argument in addition to current configuration.') self.parser.add_argument('--timeout', type=int, default=DEFAULT_INPUT_TIMEOUT, help="Maximum number of seconds to wait for the user's confirmation") self.func = self.command_try self.parse_args() self.run_command() def command_try(self): # pragma: nocover (requires user input) if not self.is_revertable(): sys.exit(os.EX_CONFIG) try: fd = sys.stdin.fileno() t = netplan.terminal.Terminal(fd) # we really don't want to be interrupted while doing backup/revert operations signal.signal(signal.SIGINT, self._signal_handler) self.backup() self.setup() NetplanApply.command_apply(run_generate=True, sync=True, exit_on_error=False) t.get_confirmation_input(timeout=self.timeout) except netplan.terminal.InputRejected: print("\nReverting.") self.revert() except netplan.terminal.InputAccepted: print("\nConfiguration accepted.") except Exception as e: print("\nAn error occured: %s" % e) print("\nReverting.") self.revert() finally: self.cleanup() def backup(self): # pragma: nocover (requires user input) backup_config_dir = False if self.config_file: backup_config_dir = True self.config_manager.backup(backup_config_dir=backup_config_dir) def setup(self): # pragma: nocover (requires user input) if self.config_file: dest_dir = os.path.join("/", "etc", "netplan") dest_name = os.path.basename(self.config_file).rstrip('.yaml') dest_suffix = time.time() dest_path = os.path.join(dest_dir, "{}.{}.yaml".format(dest_name, dest_suffix)) self.config_manager.add({self.config_file: dest_path}) self.configuration_changed = True def revert(self): # pragma: nocover (requires user input) self.config_manager.revert() NetplanApply.command_apply(run_generate=False, sync=True, exit_on_error=False) for ifname in self.new_interfaces: if ifname not in self.config_manager.bonds and \ ifname not in self.config_manager.bridges and \ ifname not in self.config_manager.vlans: logging.debug("{} will not be removed: not a virtual interface".format(ifname)) continue try: cmd = ['ip', 'link', 'del', ifname] subprocess.check_call(cmd) except subprocess.CalledProcessError: logging.warn("Could not revert (remove) new interface '{}'".format(ifname)) def cleanup(self): # pragma: nocover (requires user input) self.config_manager.cleanup() def is_revertable(self): # pragma: nocover (requires user input) ''' Check if the configuration is revertable, if it doesn't contain bits that we know are likely to render the system unstable if we apply it, or if we revert. Returns True if the parsed config is "revertable", meaning that we can actually rely on backends to re-apply /all/ of the relevant configuration to interfaces when their config changes. Returns False if the parsed config contains options that are known to not cleanly revert via the backend. ''' # Parse; including any new config file passed on the command-line: # new config might include things we can't revert. extra_config = [] if self.config_file: extra_config.append(self.config_file) self.config_manager.parse(extra_config=extra_config) self.new_interfaces = self.config_manager.new_interfaces logging.debug("New interfaces: {}".format(self.new_interfaces)) revert_unsupported = [] # Bridges and bonds are special. They typically include (or could include) # more than one device in them, and they can be set with special parameters # to tweak their behavior, which are really hard to "revert", especially # as systemd-networkd doesn't necessarily touch them when config changes. multi_iface = {} multi_iface.update(self.config_manager.bridges) multi_iface.update(self.config_manager.bonds) for ifname, settings in multi_iface.items(): if settings and 'parameters' in settings: reason = "reverting custom parameters for bridges and bonds is not supported" revert_unsupported.append((ifname, reason)) if revert_unsupported: for ifname, reason in revert_unsupported: print("{}: {}".format(ifname, reason)) print("\nPlease carefully review the configuration and use 'netplan apply' directly.") return False return True def _signal_handler(self, signal, frame): # pragma: nocover (requires user input) if self.configuration_changed: raise netplan.terminal.InputRejected()
class TestConfigManager(unittest.TestCase): def setUp(self): self.workdir = tempfile.TemporaryDirectory() self.configmanager = ConfigManager(prefix=self.workdir.name, extra_files={}) os.makedirs(os.path.join(self.workdir.name, "etc/netplan")) os.makedirs(os.path.join(self.workdir.name, "run/systemd/network")) os.makedirs(os.path.join(self.workdir.name, "run/NetworkManager/system-connections")) with open(os.path.join(self.workdir.name, "newfile.yaml"), 'w') as fd: print('''network: version: 2 ethernets: ethtest: dhcp4: yes ''', file=fd) with open(os.path.join(self.workdir.name, "newfile_merging.yaml"), 'w') as fd: print('''network: version: 2 ethernets: eth0: dhcp6: on ethbr1: dhcp4: on ''', file=fd) with open(os.path.join(self.workdir.name, "newfile_emptydict.yaml"), 'w') as fd: print('''network: version: 2 ethernets: eth0: {} bridges: br666: {} ''', file=fd) with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"), 'w') as fd: print('''network: version: 2 renderer: networkd ethernets: eth0: dhcp4: false ethbr1: dhcp4: false ethbr2: dhcp4: false ethbond1: dhcp4: false ethbond2: dhcp4: false wifis: wlan1: access-points: testAP: {} vlans: vlan2: id: 2 link: eth99 bridges: br3: interfaces: [ ethbr1 ] br4: interfaces: [ ethbr2 ] parameters: stp: on bonds: bond5: interfaces: [ ethbond1 ] bond6: interfaces: [ ethbond2 ] parameters: mode: 802.3ad ''', file=fd) with open(os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'w') as fd: print("pretend .network", file=fd) with open(os.path.join(self.workdir.name, "run/NetworkManager/system-connections/pretend"), 'w') as fd: print("pretend NM config", file=fd) def test_parse(self): self.configmanager.parse() self.assertIn('eth0', self.configmanager.ethernets) self.assertIn('bond6', self.configmanager.bonds) self.assertIn('eth0', self.configmanager.physical_interfaces) self.assertNotIn('bond7', self.configmanager.interfaces) self.assertNotIn('bond6', self.configmanager.physical_interfaces) self.assertNotIn('parameters', self.configmanager.bonds.get('bond5')) self.assertIn('parameters', self.configmanager.bonds.get('bond6')) def test_parse_merging(self): self.configmanager.parse(extra_config=[os.path.join(self.workdir.name, "newfile_merging.yaml")]) self.assertIn('eth0', self.configmanager.ethernets) self.assertIn('dhcp4', self.configmanager.ethernets['eth0']) self.assertEquals(True, self.configmanager.ethernets['eth0'].get('dhcp6')) self.assertEquals(True, self.configmanager.ethernets['ethbr1'].get('dhcp4')) def test_parse_emptydict(self): self.configmanager.parse(extra_config=[os.path.join(self.workdir.name, "newfile_emptydict.yaml")]) self.assertIn('br666', self.configmanager.bridges) self.assertEquals(False, self.configmanager.ethernets['eth0'].get('dhcp4')) self.assertEquals(False, self.configmanager.ethernets['ethbr1'].get('dhcp4')) def test_parse_extra_config(self): self.configmanager.parse(extra_config=[os.path.join(self.workdir.name, "newfile.yaml")]) self.assertIn('ethtest', self.configmanager.ethernets) self.assertIn('bond6', self.configmanager.bonds) def test_add(self): self.configmanager.add({os.path.join(self.workdir.name, "newfile.yaml"): os.path.join(self.workdir.name, "etc/netplan/newfile.yaml")}) self.assertIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertTrue(os.path.exists(os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) def test_backup_missing_dirs(self): backup_dir = self.configmanager.tempdir shutil.rmtree(os.path.join(self.workdir.name, "run/systemd/network")) self.configmanager.backup(backup_config_dir=False) self.assertTrue(os.path.exists(os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) # no source dir means no backup as well self.assertFalse(os.path.exists(os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertFalse(os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_backup_without_config_file(self): backup_dir = self.configmanager.tempdir self.configmanager.backup(backup_config_dir=False) self.assertTrue(os.path.exists(os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) self.assertTrue(os.path.exists(os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertFalse(os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_backup_with_config_file(self): backup_dir = self.configmanager.tempdir self.configmanager.backup(backup_config_dir=True) self.assertTrue(os.path.exists(os.path.join(backup_dir, "run/NetworkManager/system-connections/pretend"))) self.assertTrue(os.path.exists(os.path.join(backup_dir, "run/systemd/network/01-pretend.network"))) self.assertTrue(os.path.exists(os.path.join(backup_dir, "etc/netplan/test.yaml"))) def test_revert(self): self.configmanager.backup() with open(os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'a+') as fd: print("CHANGED", file=fd) with open(os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'r') as fd: lines = fd.readlines() self.assertIn("CHANGED\n", lines) self.configmanager.revert() with open(os.path.join(self.workdir.name, "run/systemd/network/01-pretend.network"), 'r') as fd: lines = fd.readlines() self.assertNotIn("CHANGED\n", lines) def test_revert_extra_files(self): self.configmanager.add({os.path.join(self.workdir.name, "newfile.yaml"): os.path.join(self.workdir.name, "etc/netplan/newfile.yaml")}) self.assertIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertTrue(os.path.exists(os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) self.configmanager.revert() self.assertNotIn(os.path.join(self.workdir.name, "newfile.yaml"), self.configmanager.extra_files) self.assertFalse(os.path.exists(os.path.join(self.workdir.name, "etc/netplan/newfile.yaml"))) def test_cleanup(self): backup_dir = self.configmanager.tempdir self.assertTrue(os.path.exists(backup_dir)) self.configmanager.cleanup() self.assertFalse(os.path.exists(backup_dir)) def test__copy_tree(self): self.configmanager._copy_tree(os.path.join(self.workdir.name, "etc"), os.path.join(self.workdir.name, "etc2")) self.assertTrue(os.path.exists(os.path.join(self.workdir.name, "etc2/netplan/test.yaml"))) @unittest.expectedFailure def test__copy_tree_missing_source(self): self.configmanager._copy_tree(os.path.join(self.workdir.name, "nonexistent"), os.path.join(self.workdir.name, "nonexistent2"), missing_ok=False)