"""Integration test for the wireguard module.""" import pytest from pycloudlib.lxd.instance import LXDInstance from cloudinit.subp import subp from tests.integration_tests.instances import IntegrationInstance from tests.integration_tests.integration_settings import PLATFORM from tests.integration_tests.releases import IS_UBUNTU ASCII_TEXT = "ASCII text" USER_DATA = """\ #cloud-config wireguard: interfaces: - name: wg0 config_path: /etc/wireguard/wg0.conf content: | [Interface] Address = 192.168.254.1/32 ListenPort = 51820 PrivateKey = iNlmgtGo6yiFhD9TuVnx/qJSp+C5Cwg4wwPmOJwlZXI= [Peer] PublicKey = 6PewunPjxlUq/0xvbVxklN2p73YIytfjxpoIEohCukY= AllowedIPs = 192.168.254.2/32 - name: wg1 config_path: /etc/wireguard/wg1.conf content: | [Interface] PrivateKey = GGLU4+5vIcK9lGyfz4AJn9fR5/FN/6sf4Fd5chZ16Vc= Address = 192.168.254.2/24 [Peer] PublicKey = 2as8z3EDjSsfFEkvOQGVnJ1Hv+h1jRAh2BKJg+DHvGk= Endpoint = 127.0.0.1:51820 AllowedIPs = 0.0.0.0/0 readinessprobe: - ping -qc 5 192.168.254.1 2>&1 > /dev/null - echo $? > /tmp/ping # wg-quick configures the system interfaces and routes, but we need to ssh in # stop the service at the end of cloud-init runcmd: - [systemctl, stop, wg-quick@wg0.service] - [systemctl, stop, wg-quick@wg1.service] """ def load_wireguard_kernel_module_lxd(instance: LXDInstance): subp( "lxc config set {} linux.kernel_modules wireguard".format( instance.name ).split() ) @pytest.mark.ci @pytest.mark.user_data(USER_DATA) @pytest.mark.skipif( not IS_UBUNTU, reason="Hasn't been tested on other distros" ) @pytest.mark.skipif( PLATFORM not in ["lxd_vm", "gce", "ec2", "azure", "openstack", "oci"], reason=f"Test hasn't been tested on {PLATFORM}", ) class TestWireguard: @pytest.mark.parametrize( "cmd,expected_out", ( # check if wireguard module is loaded ("lsmod | grep '^wireguard' | awk '{print $1}'", "wireguard"), # test if file was written for wg0 ( "stat -c '%N' /etc/wireguard/wg0.conf", r"'/etc/wireguard/wg0.conf'", ), # check permissions for wg0 ("stat -c '%U %a' /etc/wireguard/wg0.conf", r"root 600"), # ASCII check wg1 ("file /etc/wireguard/wg1.conf", ASCII_TEXT), # md5sum check wg1 ( "md5sum