diff options
| author | Chad Smith <chad.smith@canonical.com> | 2022-05-31 14:13:22 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-31 14:13:22 -0600 |
| commit | 153a7ea25e0d15f707a37655c06cf7155114ea11 (patch) | |
| tree | 6e549496c5f858b203f958a52a247c668cf37201 /tests/unittests/cmd/devel/test_net_convert.py | |
| parent | de606405de2d016b7b82ae6d0e0595fd44094ab1 (diff) | |
| download | cloud-init-git-153a7ea25e0d15f707a37655c06cf7155114ea11.tar.gz | |
net-convert: use yaml.dump for debugging python NetworkState obj (#1484)
When debugging python's NetworkState intance we cannot use
safeyaml.dumps because that leverages the yaml.SafeDumper which
does not allow rendering python objects.
Use yamls.dump instead.
* Additional fix for networkd renderer to create /etc/systemd/network if
it does not exist
LP: #1975907
Diffstat (limited to 'tests/unittests/cmd/devel/test_net_convert.py')
| -rw-r--r-- | tests/unittests/cmd/devel/test_net_convert.py | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/tests/unittests/cmd/devel/test_net_convert.py b/tests/unittests/cmd/devel/test_net_convert.py new file mode 100644 index 00000000..60acb1a6 --- /dev/null +++ b/tests/unittests/cmd/devel/test_net_convert.py @@ -0,0 +1,187 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +import itertools + +import pytest + +from cloudinit.cmd.devel import net_convert +from cloudinit.distros.debian import NETWORK_FILE_HEADER +from tests.unittests.helpers import mock + +M_PATH = "cloudinit.cmd.devel.net_convert." + + +required_args = [ + "--directory", + "--network-data", + "--distro=ubuntu", + "--kind=eni", + "--output-kind=eni", +] + + +SAMPLE_NET_V1 = """\ +network: + version: 1 + config: + - type: physical + name: eth0 + subnets: + - type: dhcp +""" + + +SAMPLE_NETPLAN_CONTENT = f"""\ +{NETWORK_FILE_HEADER}network: + version: 2 + ethernets: + eth0: + dhcp4: true +""" + +SAMPLE_ENI_CONTENT = f"""\ +{NETWORK_FILE_HEADER}auto lo +iface lo inet loopback + +auto eth0 +iface eth0 inet dhcp +""" + +SAMPLE_NETWORKD_CONTENT = """\ +[Match] +Name=eth0 + +[Network] +DHCP=ipv4 + +""" + +SAMPLE_SYSCONFIG_CONTENT = """\ +# Created by cloud-init on instance boot automatically, do not edit. +# +BOOTPROTO=dhcp +DEVICE=eth0 +NM_CONTROLLED=no +ONBOOT=yes +TYPE=Ethernet +USERCTL=no +""" + +SAMPLE_NETWORK_MANAGER_CONTENT = """\ +# Generated by cloud-init. Changes will be lost. + +[connection] +id=cloud-init eth0 +uuid=1dd9a779-d327-56e1-8454-c65e2556c12c +type=ethernet +interface-name=eth0 + +[user] +org.freedesktop.NetworkManager.origin=cloud-init + +[ethernet] + +[ipv4] +method=auto +may-fail=false + +""" + + +class TestNetConvert: + + missing_required_args = itertools.combinations( + required_args, len(required_args) - 1 + ) + + def _replace_path_args(self, cmd, tmpdir): + """Inject tmpdir replacements for parameterize args.""" + updated_cmd = [] + for arg in cmd: + if arg == "--network-data": + net_file = tmpdir.join("net") + net_file.write("") + updated_cmd.append(f"--network-data={net_file}") + elif arg == "--directory": + updated_cmd.append(f"--directory={tmpdir.strpath}") + else: + updated_cmd.append(arg) + return updated_cmd + + @pytest.mark.parametrize("cmdargs", missing_required_args) + def test_argparse_error_on_missing_args(self, cmdargs, capsys, tmpdir): + """Log the appropriate error when required args are missing.""" + params = self._replace_path_args(cmdargs, tmpdir) + with mock.patch("sys.argv", ["net-convert"] + params): + with pytest.raises(SystemExit): + net_convert.get_parser().parse_args() + _out, err = capsys.readouterr() + assert "the following arguments are required" in err + + @pytest.mark.parametrize("debug", (False, True)) + @pytest.mark.parametrize( + "output_kind,outfile_content", + ( + ( + "netplan", + {"etc/netplan/50-cloud-init.yaml": SAMPLE_NETPLAN_CONTENT}, + ), + ( + "eni", + { + "etc/network/interfaces.d/50-cloud-init.cfg": SAMPLE_ENI_CONTENT # noqa: E501 + }, + ), + ( + "networkd", + { + "etc/systemd/network/10-cloud-init-eth0.network": SAMPLE_NETWORKD_CONTENT # noqa: E501 + }, + ), + ( + "sysconfig", + { + "etc/sysconfig/network-scripts/ifcfg-eth0": SAMPLE_SYSCONFIG_CONTENT # noqa: E501 + }, + ), + ( + "network-manager", + { + "etc/NetworkManager/system-connections/cloud-init-eth0.nmconnection": SAMPLE_NETWORK_MANAGER_CONTENT # noqa: E501 + }, + ), + ), + ) + def test_convert_output_kind_artifacts( + self, output_kind, outfile_content, debug, capsys, tmpdir + ): + """Assert proper output-kind artifacts are written.""" + network_data = tmpdir.join("network_data") + network_data.write(SAMPLE_NET_V1) + distro = "centos" if output_kind == "sysconfig" else "ubuntu" + args = [ + f"--directory={tmpdir.strpath}", + f"--network-data={network_data.strpath}", + f"--distro={distro}", + "--kind=yaml", + f"--output-kind={output_kind}", + ] + if debug: + args.append("--debug") + params = self._replace_path_args(args, tmpdir) + with mock.patch("sys.argv", ["net-convert"] + params): + args = net_convert.get_parser().parse_args() + with mock.patch("cloudinit.util.chownbyname") as chown: + net_convert.handle_args("somename", args) + for path in outfile_content: + outfile = tmpdir.join(path) + assert outfile_content[path] == outfile.read() + if output_kind == "networkd": + assert [ + mock.call( + outfile.strpath, "systemd-network", "systemd-network" + ) + ] == chown.call_args_list + + +# vi: ts=4 expandtab |
