summaryrefslogtreecommitdiff
path: root/tests/unittests/test_net.py
AgeCommit message (Collapse)Author
2022-02-08Fix IPv6 netmask format for sysconfig (#1215)Harald
This change converts the IPv6 netmask from the network_data.json[1] format to the CIDR style, <IPv6_addr>/<prefix>. Using an IPv6 address like ffff:ffff:ffff:ffff:: does not work with NetworkManager, nor networkscripts. NetworkManager will ignore the route, logging: ifcfg-rh: ignoring invalid route at \ "::/:: via fd00:fd00:fd00:2::fffe dev $DEV" \ (/etc/sysconfig/network-scripts/route6-$DEV:3): \ Argument for "::/::" is not ADDR/PREFIX format Similarly if using networkscripts, ip route fail with error: Error: inet6 prefix is expected rather than \ "fd00:fd00:fd00::/ffff:ffff:ffff:ffff::". Also a bit of refactoring ... cloudinit.net.sysconfig.Route.to_string: * Move a couple of lines around to reduce repeated code. * if "ADDRESS" not in key -> continute, so that the code block following it can be de-indented. cloudinit.net.network_state: * Refactors the ipv4_mask_to_net_prefix, ipv6_mask_to_net_prefix removes mask_to_net_prefix methods. Utilize ipaddress library to do some of the heavy lifting. LP: #1959148
2021-12-15Adopt Black and isort (SC-700) (#1157)James Falcon
Applied Black and isort, fixed any linting issues, updated tox.ini and CI.
2021-12-03Reorganize unit test locations under tests/unittests (#1126)Brett Holman
This attempts to standardize unit test file location under test/unittests/ such that any source file located at cloudinit/path/to/file.py may have a corresponding unit test file at test/unittests/path/to/test_file.py. Noteworthy Comments: ==================== Four different duplicate test files existed: test_{gpg,util,cc_mounts,cc_resolv_conf}.py Each of these duplicate file pairs has been merged together. This is a break in git history for these files. The test suite appears to have a dependency on test order. Changing test order causes some tests to fail. This should be rectified, but for now some tests have been modified in tests/unittests/config/test_set_passwords.py. A helper class name starts with "Test" which causes pytest to try executing it as a test case, which then throws warnings "due to Class having __init__()". Silence by changing the name of the class. # helpers.py is imported in many test files, import paths change cloudinit/tests/helpers.py -> tests/unittests/helpers.py # Move directories: cloudinit/distros/tests -> tests/unittests/distros cloudinit/cmd/devel/tests -> tests/unittests/cmd/devel cloudinit/cmd/tests -> tests/unittests/cmd/ cloudinit/sources/helpers/tests -> tests/unittests/sources/helpers cloudinit/sources/tests -> tests/unittests/sources cloudinit/net/tests -> tests/unittests/net cloudinit/config/tests -> tests/unittests/config cloudinit/analyze/tests/ -> tests/unittests/analyze/ # Standardize tests already in tests/unittests/ test_datasource -> sources test_distros -> distros test_vmware -> sources/vmware test_handler -> config # this contains cloudconfig module tests test_runs -> runs
2021-11-22testing: monkeypatch system_info call in unit tests (SC-533) (#1117)James Falcon
testing: monkeypatch system_info call in unit tests system_info can make calls that read or write from the filesystem, which should require special mocking. It is also decorated with 'lru_cache', which means test authors often don't realize they need to be mocking. Also, we don't actually want the results from the user's local machine, so monkeypatching it across all tests should be reasonable. Additionally, moved some of 'system_info` into a helper function to reduce the surface area of the monkeypatch, added tests for the new function (and fixed a bug as a result), and removed related mocks that should be no longer needed.
2021-08-30Add support to accept-ra in networkd renderer (#999)Shreenidhi Shedi
Also fix search path in networkd
2021-08-05Add support for EuroLinux 7 && EuroLinux 8 (#957)Aleksander Baranowski
2021-07-01Add new network activators to bring up interfaces (#919)James Falcon
Currently _bring_up_interfaces() is a no-op for any distro using renderers. We need to be able to support bringing up a single interfaces, a list of interfaces, and all interfaces. This should be independent of the renderers, as the network config is often generated independent of the mechanism used to apply it. Additionally, I included a refactor to remove "_supported_write_network_config". We had a confusing call chain of apply_network_config->_write_network_config->_supported_write_network_config. The last two have been combined.
2021-06-28Removed distro specific network code from Photon (#929)sshedi
Minor fixes in networkd renderer & fixed corresponding tests Removed datasource_list for Photon from cloud.cfg.tmpl & added a comment in cloud.cfg.tmpl about not to use multiline array for datasource_list. Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
2021-06-18Add support for VMware PhotonOS (#909)sshedi
Also added a new (currently experimental) systemd-networkd renderer, and includes a small refactor to cc_resolv_conf.py to support the resolved.conf used by systemd-resolved.
2021-04-09sysconfig: use BONDING_MODULE_OPTS on SUSE (#831)Jens Sandmann
Update sysconfig configuration to use BONDING_MODULES_OPTS instead of BONDING_OPTS when on a SUSE system. The sysconfig support requires use of BONDING_MODULE_OPTS whereas the initscript support that rhel uses requires BONDING_OPTS.
2021-03-29Fix mis-detecting network configuration in initramfs cmdline (#844)James Falcon
klibc initramfs in debian allows the 'iscsi_target_ip=' cmdline parameter to specify an iscsi device attachment. This can cause cloud-init to mis-detect the cmdline paramter as a networking config. LP: #1919188
2021-03-08net: exclude OVS internal interfaces in get_interfaces (#829)Daniel Watkins
`get_interfaces` is used to in two ways, broadly: firstly, to determine the available interfaces when converting cloud network configuration formats to cloud-init's network configuration formats; and, secondly, to ensure that any interfaces which are specified in network configuration are (a) available, and (b) named correctly. The first of these is unaffected by this commit, as no clouds support Open vSwitch configuration in their network configuration formats. For the second, we check that MAC addresses of physical devices are unique. In some OVS configurations, there are OVS-created devices which have duplicate MAC addresses, either with each other or with physical devices. As these interfaces are created by OVS, we can be confident that (a) they will be available when appropriate, and (b) that OVS will name them correctly. As such, this commit excludes any OVS-internal interfaces from the set of interfaces returned by `get_interfaces`. LP: #1912844
2021-01-13net: Fix static routes to host in eni renderer (#668)Pavel Abalikhin
Route '-net' parameter is incompatible with /32 IPv4 addresses so we have to use '-host' in that case.
2021-01-07Missing IPV6_AUTOCONF=no to render sysconfig dhcp6 stateful on RHEL (#753)Eduardo Otubo
IPV6_AUTOCONF needs to be set to 'no' on RHEL so NetworkManager can properly acquire ipv6 address. rhbz: #1859695 Signed-off-by: Eduardo Otubo <otubo@redhat.com>
2020-12-01Adding BOOTPROTO = dhcp to render sysconfig dhcp6 stateful on RHEL (#685)Eduardo Otubo
BOOTPROTO needs to be set to 'dhcp' on RHEL so NetworkManager can properly acquire ipv6 address. rhbz: #1859695 Signed-off-by: Eduardo Otubo <otubo@redhat.com> Co-authored-by: Daniel Watkins <oddbloke@ubuntu.com> Co-authored-by: Scott Moser <smoser@brickies.net>
2020-11-09Make wakeonlan Network Config v2 setting actually work (#626)dermotbradley
Add code so that specifying "wakeonlan: true" actually results in relevant configuration entry appearing in /etc/network/interfaces, Netplan, and sysconfig for RHEL and OpenSuse. Add testcases for the above.
2020-10-29Explicit set IPV6_AUTOCONF and IPV6_FORCE_ACCEPT_RA on static6 (#634)Eduardo Otubo
The static and static6 subnet types for network_data.json were being ignored by the Openstack handler, this would cause the code to break and not function properly. As of today, if a static6 configuration is chosen, the interface will still eventually be available to receive router advertisements or be set from NetworkManager to wait for them and cycle the interface in negative case. It is safe to assume that if the interface is manually configured to use static ipv6 address, there's no need to wait for router advertisements. This patch will set automatically IPV6_AUTOCONF and IPV6_FORCE_ACCEPT_RA both to "no" in this case. This patch fixes the specific behavior only for RHEL flavor and sysconfig renderer. It also introduces new unit tests for the specific case as well as adjusts some existent tests to be compatible with the new options. This patch also addresses this problem by assigning the appropriate subnet type for each case on the openstack handler. rhbz: #1889635 rhbz: #1889635 Signed-off-by: Eduardo Otubo otubo@redhat.com
2020-10-27Update network config docs to clarify MAC address quoting (#623)dermotbradley
Also update MAC addresses used in testcases to remove quotes where not required and add single quotes where quotes are required.
2020-09-15network: Fix type and respect name when rendering vlan in sysconfig. (#541)Eduardo Otubo
Prior to this change, vlans were rendered in sysconfig with 'TYPE=Ethernet', and incorrectly rendered the PHYSDEV based on the name of the vlan device rather than the 'link' provided in the network config. The change here fixes: * rendering of TYPE=Ethernet for a vlan * adds a warning if the configured device name is not supported per the RHEL 7 docs "11.5. Naming Scheme for VLAN Interfaces" LP: #1788915 LP: #1826608 RHBZ: #1861871
2020-06-09Fixes KeyError for bridge with no "parameters:" setting (#423)Brian Candler
Reason: commit ded1ec8 introduced a regression whereby a bridge with no "parameters:" setting caused a KeyError exception. LP: #1879673
2020-06-08Move subp into its own module. (#416)Scott Moser
This was painful, but it finishes a TODO from cloudinit/subp.py. It moves the following from util to subp: ProcessExecutionError subp which target_path I moved subp_blob_in_tempfile into cc_chef, which is its only caller. That saved us from having to deal with it using write_file and temp_utils from subp (which does not import any cloudinit things now). It is arguable that 'target_path' could be moved to a 'path_utils' or something, but in order to use it from subp and also from utils, we had to get it out of utils.
2020-06-02test: fix all flake8 E121 and E123 errors (#404)Joshua Powers
This fixes issues with closing brackets not matching the opening bracket's line and continuation line under-idented for hanging indent.
2020-06-02test: fix all flake8 E241 (#403)Joshua Powers
Remove extra spaces after a ','
2020-03-30net: ubuntu focal prioritize netplan over eni even if both present (#267)Chad Smith
On Focal and later, Ubuntu will prioritize netplan renderer over eni, even if ifupdown and netplan are both installed. ENI on Focal and later is considered an unsupported configuration so cloud-init should generally prefer netplan. On many cloud images, the /etc/network/interfaces config file does not include the dir /etc/network/interfaces.d thereby ignoring cloud-init's /etc/network/interfaces.d/50-cloud-init.cfg file. LP: #1867029
2020-03-04net: support network-config:disabled on the kernel commandline (#232)Chad Smith
Allow disabling cloud-init's network configuration via a plain-text kernel cmdline Cloud-init docs indicate that users can disable cloud-init networking via kernel command line parameter 'network-config=<YAML>'. This does not work unless the <YAML> payload base64 encoded. Document the base64 encoding requirement and add a plain-text value for disabling cloud-init network config: network-config=disabled Also: - Log an error and ignore any plain-text network-config payloads that are not specifically 'network-config=disabled'. - Log a warning if network-config kernel param is invalid yaml but do not raise an exception, allowing boot to continue and use fallback networking. LP: #1862702
2020-02-03sysconfig: distro-specific config rendering for BOOTPROTO option (#162)Robert Schweikert
- Introduce the "flavor" configuration option for the sysconfig renderer this is necessary to account for differences in the handling of the BOOTPROTO setting between distributions (lp#1858808) + Thanks to Petr Pavlu for the idea - Network config clean up for sysconfig renderer + The introduction of the "flavor" renderer configuration allows us to only write values that are pertinent for the given distro - Set the DHCPv6 client mode on SUSE (lp#1800854) Co-authored-by: Chad Smith <chad.smith@canonical.com> LP: #1800854
2020-01-30net/cmdline: correctly handle static ip= config (#201)Dimitri John Ledkov
It is proto 'none', not 'static' as was mistakenly implemented in initramfs-tools/cloud-init in the past, yet was never the case in the klibc ipconfig state file output. LP: #1861412
2019-11-25FreeBSD: fix for get_linux_distro() and lru_cache (#59)Igor Galić
Since `is_FreeBSD()` is used a lot, which uses `system_info()`, which uses `get_linux_distro()` we add caching, by decorating the following functions with `@lru_cache`: - get_architecture() - _lsb_release() - is_FreeBSD - get_linux_distro - system_info() - _get_cmdline() Since [functools](https://docs.python.org/3/library/functools.html) only exists in Python 3, only python 3 will benefit from this improvement. For python 2, our shim is just a pass-thru. Too bad, but, also… https://pythonclock.org/ The main motivation here was, at first, to cache more, following the style of _lsb_release. That is now consolidated under this very same roof. LP: #1815030
2019-11-20net: IPv6, accept_ra, slaac, stateless (#51)Harald
Router advertisements are required for the default route to be set up, thus accept_ra should be enabled for dhcpv6-stateful. sysconf: IPV6_FORCE_ACCEPT_RA controls accept_ra sysctl. eni: mode static and mode dhcp 'accept_ra' controls sysctl. Add 'accept-ra: true|false' parameter to config v1 and v2. When True: accept_ra is set to '1'. When False: accept_ra is set to '0'. When not defined in config the value is left to the operating system default. This change also extend the IPv6 support to distinguish between slaac and dhcpv6-stateless. SLAAC is autoconfig without any options from DHCP, while stateless auto-configures the address and the uses DHCP for other options. LP: #1806014 LP: #1808647
2019-11-04azure: support matching dhcp route-metrics for dual-stack ipv4 ipv6Chad Smith
Network v2 configuration for Azure will set both dhcp4 and dhcp6 to False by default. When IPv6 privateIpAddresses are present for an interface in Azure's Instance Metadata Service (IMDS), set dhcp6: True and provide a route-metric value that will match the corresponding dhcp4 route-metric. The route-metric value will increase by 100 for each additional interface present to ensure the primary interface has a route to IMDS. Also fix dhcp route-metric rendering for eni and sysconfig distros. LP: #1850308
2019-10-31net: fix subnet_is_ipv6() for stateless|statefulHarald Jensås
Function return false for ipv6_dhcpv6-stateless|stateful, the eni renderer does not add '6' to 'inet' which is incorrect. The subnet_is_ipv6() function is updated to also return true if startswith('ipv6'). LP: #1848690
2019-10-24net/netplan: use ipv6-mtu key for specifying ipv6 mtu valuesRyan Harper
netplan introduced an 'info' subcommand which emits yaml describing implemented features that indicate new or changed fields and values in the yaml that it accepts. Previously, cloud-init emitted the key 'mtu6' for ipv6 MTU values. This is not correct and netplan will fail to parse these values. Netplan as of 0.98 supports both the info subcommand and the ipv6-mtu key. This branch modifies the netplan renderer to collect the netplan info output into a 'features' property which is a list of available feature flags which the renderer can use to modify its output. If the command is not available, no feature flags are set and cloud-init will render IPv6 MTU values just as MTU for the subnet.
2019-10-21net: enable infiniband support in eni and sysconfig renderersDarren Birkett
Commit e7b0e5f72 added support for configuring infiniband devices by adding a new infiniband 'type'. This commit updates eni and sysconfig renderers to consume this new type and configure infiniband devices correctly. LP: #1847114
2019-10-16net: handle openstack dhcpv6-stateless configurationHarald Jensås
Openstack subnets can be configured to use SLAAC by setting ipv6_address_mode=dhcpv6-stateless. When this is the case the sysconfig interface configuration should use IPV6_AUTOCONF=yes and not set DHCPV6C=yes. This change sets the subnets type property to the full network['type'] from openstack metadata. cloudinit/net/sysconfig.py and cloudinit/net/eni.py are updated to support new subnet types: - 'ipv6_dhcpv6-stateless' => IPV6_AUTOCONF=yes - 'ipv6_dhcpv6-stateful' => DHCPV6C=yes Type 'dhcp6' in sysconfig is kept for backward compatibility with any implementations that set subnet_type == 'dhcp6'. LP: #1847517
2019-09-26sysconfig: only write resolv.conf if network_state has DNS valuesRyan Harper
If an OS image provided an /etc/resolv.conf file that was not empty cloud-init would read and re-write it with a cloud-init header even if no DNS network configuration was provided (e.g. DHCP only). This can cause problems for some network services which don't ignore cloud-init's header. LP: #1843634
2019-09-26sysconfig: use distro variant to check if availableRyan Harper
The sysconfig renderer used the distro name directly which mean some variants of distros were not considered supported. Fix this by using util.system_info()['variant'] instead. Fix the list of KNOWN_DISTROS value for redhat -> rhel. LP: #1843584
2019-08-22net/cmdline: refactor to allow multiple initramfs network config sourcesDaniel Watkins
This refactors read_initramfs_config to support multiple different types of initramfs network configuration. It introduces an InitramfsNetworkConfigSource abstract base class. There is currently a single sub-class, KlibcNetworkConfigSource, which contains the logic which previously was directly within read_initramfs_config.
2019-08-13azure/net: generate_fallback_nic emits network v2 config instead of v1Chad Smith
The function generate_fallback_config is used by Azure by default when not consuming IMDS configuration data. This function is also used by any datasource which does not implement it's own network config. This simple fallback configuration sets up dhcp on the most likely NIC. It will now emit network v2 instead of network v1. This is a step toward moving all components talking in v2 and allows us to avoid costly conversions between v1 and v2 for newer distributions which rely on netplan.
2019-07-26net/cmdline: split interfaces_by_mac and init network config determinationDaniel Watkins
Previously "cmdline" network configuration could be either user-specified network-config=... configuration data, or initramfs-provided configuration data. Before data sources could modify the order in which network config sources were considered, this conflation didn't matter (and, indeed, in the default data source configuration it will continue to not matter). However, it _is_ desirable for a data source to be able to specify that its network configuration should be preferred over the initramfs-provided network configuration but still allow explicit network-config=... configuration passed to the kernel cmdline to continue to override both of those sources. (This also modifies the Oracle data source to use read_initramfs_config directly, which is effectively what it was using read_kernel_cmdline_config for previously.)
2019-07-18Fix bug rendering MTU on bond or vlan when input was netplan.Scott Moser
If input to network_state.parse_net_config_data was netplan (v2 yaml) then the network state would lose the mtu information on bond or vlan. LP: #1836949
2019-07-17net: update net sequence, include wait on netdevs, opensuse netrules pathRyan Harper
On systems with many interfaces, processing udev events may take a while. Cloud-init expects devices included in a provided network-configuration to be present when attempting to configure them. This patch adds a step in net configuration where it will check for devices provided in the configuration and if not found, issue udevadm settle commands to wait for them to appear. Additionally, the default path for udev persistent network rules 70-persistent-net.rules may also be written to systems which include the 75-net-generator.rules. During boot, cloud-init and the generator may race and interleave values causing issues. OpenSUSE will now use a newer file, 85-persistent-net-cloud-init.rules which will take precedence over values created by 75-net-generator and avoid collisions on the same file. LP: #1817368
2019-06-21sysconfig: support more bonding optionsPenghui Liao
Currently, only a few bonding parameters can be configured on sysconfig systems. This patch aims to support more parameters documented on the docs site.
2019-05-29netplan: update netplan key mappings for gratuitous-arpRyan Harper
Previous versions of netplan included a misspelling for the bond parameter around gratuitous-arp. This has been fixed and released and cloud-init needs to accept both values. This branch fixes the key that will be rendered and transforms the previous misspelling when capturing network_state. LP: #1827238
2019-04-27git tests: no longer show warning about safe yaml.Scott Moser
Currently on 18.04, running tox -e py27 will spew errors like: .tests/unittests/test_net.py:2649: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. The change here just uses cloud-init's yaml, which does safeloading by default.
2019-04-22net/sysconfig: only indicate available on known sysconfig distrosRyan Harper
Restrict the sysconfig renderer availabily to known distros. Ubuntu/Debian systems may include network-manager but they do not have support for reading sysconfig network output; that is enabled via a Network-Manager plugin: ifcfg-rh which is not available in Ubuntu/Debian. LP: #1819994
2019-04-18test_azure: mock util.SeLinuxGuard where neededJason Zions (MSFT)
Mock util.SeLinuxGuard to do nothing within tests that mock functions used by the guard, when those mocks confuse the guard. This has no impact when executing unit tests on systems which do not enable selinux (e.g. Ubuntu). LP: #1825253
2019-03-25net/sysconfig: write out SUSE-compatible IPv6 configRobert Schweikert
For writing IPv6 addresses to ifcfg-* the name "IPV6ADDR" is used. For secondary IPs the value for "IPV6ADDR_SECONDARIES" is set. On SUSE based distributions the names "IPADDR6" and "IPADDR6_$SOMELABEL" need to be used.
2019-03-21net: Fix ipv6 static routes when using eni rendererRaphael Glon
When rendering ipv6 static routes in eni format the post-up/pre down commands were not correct for ipv6. LP: #1818669
2019-03-12net/sysconfig: Handle default route setup for dhcp configured NICsRobert Schweikert
When the network configuration has a default route configured and another network device that is configured with dhcp, SUSE sysconfig output should not accept the default route provided by the dhcp server. LP: #1812117
2019-03-04net: append type:dhcp[46] only if dhcp[46] is True in v2 netconfigKurt Stieger
When providing netplan configuration to cloud-init, the internal network state would enable DHCP if the 'dhcp' key was present in the source config. In netplan, dhcp[46] is a boolean and the value of the boolean should control whether DHCP is enabled rather than the presence of the key. This issue leaded to inconsistant sysconfig/network-scripts on fedora. 'BOOTPROTO' was always 'dhcp', even if the address config was static. After this change a dhcp subnet is added only if the 'dhcp' setting in source cfg dict is True. LP: #1818032